Skip to content

Commit f514e43

Browse files
committed
🔥 PHPStan!?
1 parent ca8806b commit f514e43

File tree

12 files changed

+76
-72
lines changed

12 files changed

+76
-72
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
22
# https://github.com/sebastianbergmann/phpunit/blob/master/.github/workflows/ci.yml
33

4+
name: "CI"
5+
46
on:
57
push:
68
branches:
@@ -10,7 +12,10 @@ on:
1012
- main
1113

1214

13-
name: "CI"
15+
env:
16+
PHP_EXTENSIONS: json
17+
PHP_INI_VALUES: memory_limit=-1, error_reporting=-1, display_errors=On
18+
1419

1520
jobs:
1621

@@ -39,15 +44,15 @@ jobs:
3944
uses: shivammathur/setup-php@v2
4045
with:
4146
php-version: ${{ matrix.php-version }}
42-
tools: pecl
47+
extensions: ${{ env.PHP_EXTENSIONS }}
48+
ini-values: ${{ env.PHP_INI_VALUES }}
4349
coverage: none
44-
extensions: ast, json
4550

4651
- name: "Update dependencies with composer"
4752
uses: ramsey/composer-install@v3
4853

49-
- name: "Run phan"
50-
run: php vendor/bin/phan --target-php-version=${{ matrix.php-version }}
54+
- name: "Run PHPStan"
55+
run: php vendor/bin/phpstan
5156

5257

5358
build-docs:
@@ -103,8 +108,9 @@ jobs:
103108
uses: shivammathur/setup-php@v2
104109
with:
105110
php-version: ${{ matrix.php-version }}
111+
extensions: ${{ env.PHP_EXTENSIONS }}
112+
ini-values: ${{ env.PHP_INI_VALUES }}
106113
coverage: pcov
107-
extensions: json
108114

109115
- name: "Install dependencies with composer"
110116
uses: ramsey/composer-install@v3

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ phpcs.xml
66
phpdoc.xml
77
phpmd.xml
88
phpunit.xml
9+
phpstan.neon

.phan/config.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@
2424
"ext-json": "*"
2525
},
2626
"require-dev": {
27-
"phan/phan": "^5.4",
2827
"phpmd/phpmd": "^2.15",
28+
"phpstan/phpstan": "^1.11",
2929
"phpunit/phpunit": "^10.5",
3030
"squizlabs/php_codesniffer": "^3.10"
3131
},
3232
"autoload": {
3333
"psr-4": {
34-
"chillerlan\\Settings\\": "src/"
34+
"chillerlan\\Settings\\": "src"
3535
}
3636
},
3737
"autoload-dev": {
3838
"psr-4": {
39-
"chillerlan\\SettingsTest\\": "tests/"
39+
"chillerlan\\SettingsTest\\": "tests"
4040
}
4141
},
4242
"scripts": {
4343
"phpunit": "@php vendor/bin/phpunit",
44-
"phan": "@php vendor/bin/phan"
44+
"phpstan": "@php vendor/bin/phpstan"
4545
},
4646
"config": {
4747
"lock": false,

examples/advanced.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class MySettings extends SettingsContainerAbstract{
6060
use SomeOptions, MoreOptions; // ...
6161
};
6262

63-
$container = new MySettings($commonOptions);
63+
$container = new MySettings($commonOptions); // wtf phpstorm???
6464
6565
var_dump($container->foo); // -> WHATEVER (constructor ran strtoupper on the value)
6666
var_dump($container->bar); // -> nothing

examples/simple.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@
1010

1111
require_once __DIR__.'/../vendor/autoload.php';
1212

13+
/**
14+
* @property string $foo
15+
* @property string $bar
16+
*/
1317
class MyContainer extends SettingsContainerAbstract{
1418
protected string $foo;
1519
protected string $bar;
1620
}
1721

18-
/** @var \chillerlan\Settings\SettingsContainerInterface $container */
1922
$container = new MyContainer(['foo' => 'what']);
2023
$container->bar = 'foo';
2124

2225
var_dump($container->toJSON()); // -> {"foo":"what","bar":"foo"}
2326

2427
// non-existing properties will be ignored:
28+
/** @phpstan-ignore-next-line */
2529
$container->nope = 'what';
2630

2731
var_dump($container->nope); // -> NULL

phpstan.dist.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://phpstan.org/config-reference
2+
3+
parameters:
4+
level: 9
5+
paths:
6+
- examples
7+
- src
8+
- tests
9+
tmpDir: .build/phpstan-cache
10+
11+
includes:
12+
- phar://phpstan.phar/conf/bleedingEdge.neon

src/SettingsContainerAbstract.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111

1212
namespace chillerlan\Settings;
1313

14-
use InvalidArgumentException, ReflectionClass, ReflectionProperty;
15-
use function array_keys, get_object_vars, is_object, json_decode,
16-
json_encode, method_exists, property_exists, serialize, unserialize;
14+
use InvalidArgumentException, JsonException, ReflectionClass, ReflectionProperty;
15+
use function array_keys, get_object_vars, is_object, json_decode, json_encode,
16+
json_last_error_msg, method_exists, property_exists, serialize, unserialize;
1717
use const JSON_THROW_ON_ERROR;
1818

1919
abstract class SettingsContainerAbstract implements SettingsContainerInterface{
2020

2121
/**
2222
* SettingsContainerAbstract constructor.
23+
*
24+
* @phpstan-param array<string, mixed> $properties
2325
*/
2426
public function __construct(iterable|null $properties = null){
2527

@@ -146,20 +148,28 @@ public function fromIterable(iterable $properties):static{
146148
* @inheritdoc
147149
*/
148150
public function toJSON(int|null $jsonOptions = null):string{
149-
return json_encode($this, ($jsonOptions ?? 0));
151+
$json = json_encode($this, ($jsonOptions ?? 0));
152+
153+
if($json === false){
154+
throw new JsonException(json_last_error_msg());
155+
}
156+
157+
return $json;
150158
}
151159

152160
/**
153161
* @inheritdoc
154162
*/
155163
public function fromJSON(string $json):static{
164+
/** @phpstan-var array<string, mixed> $data */
156165
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
157166

158167
return $this->fromIterable($data);
159168
}
160169

161170
/**
162171
* @inheritdoc
172+
* @return array<string, mixed>
163173
*/
164174
public function jsonSerialize():array{
165175
return $this->toArray();
@@ -228,6 +238,8 @@ public function __serialize():array{
228238
*
229239
* @inheritdoc
230240
* @see \chillerlan\Settings\SettingsContainerInterface::fromIterable()
241+
*
242+
* @param array<string, mixed> $data
231243
*/
232244
public function __unserialize(array $data):void{
233245

src/SettingsContainerInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,37 @@ public function __toString():string;
4949
* Returns an array representation of the settings object
5050
*
5151
* The values will be run through the magic __get(), which may also call custom getters.
52+
*
53+
* @return array<string, mixed>
5254
*/
5355
public function toArray():array;
5456

5557
/**
5658
* Sets properties from a given iterable
5759
*
5860
* The values will be run through the magic __set(), which may also call custom setters.
61+
*
62+
* @phpstan-param array<string, mixed> $properties
5963
*/
6064
public function fromIterable(iterable $properties):static;
6165

6266
/**
6367
* Returns a JSON representation of the settings object
68+
*
6469
* @see \json_encode()
6570
* @see \chillerlan\Settings\SettingsContainerInterface::toArray()
71+
*
72+
* @throws \JsonException
6673
*/
6774
public function toJSON(int|null $jsonOptions = null):string;
6875

6976
/**
7077
* Sets properties from a given JSON string
7178
*
79+
* @see \chillerlan\Settings\SettingsContainerInterface::fromIterable()
80+
*
7281
* @throws \Exception
7382
* @throws \JsonException
74-
* @see \chillerlan\Settings\SettingsContainerInterface::fromIterable()
7583
*/
7684
public function fromJSON(string $json):static;
7785

tests/ContainerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testConstruct():void{
2727

2828
$this::assertSame('test1', $container->test1);
2929
$this::assertSame(true, $container->test2);
30+
/** @phpstan-ignore-next-line */
3031
$this::assertNull($container->test3);
3132
$this::assertSame('test4', $container->test4);
3233

@@ -38,13 +39,16 @@ public function testGet():void{
3839

3940
$this::assertSame('foo', $container->test1);
4041
$this::assertNull($container->test2);
42+
/** @phpstan-ignore-next-line */
4143
$this::assertNull($container->test3);
4244
$this::assertNull($container->test4);
45+
/** @phpstan-ignore-next-line */
4346
$this::assertNull($container->foo);
4447

4548
// isset test
4649
$this::assertTrue(isset($container->test1));
4750
$this::assertFalse(isset($container->test2));
51+
/** @phpstan-ignore-next-line */
4852
$this::assertFalse(isset($container->test3));
4953
$this::assertFalse(isset($container->test4));
5054
$this::assertFalse(isset($container->foo));
@@ -62,10 +66,12 @@ public function testSet():void{
6266
$container = new TestContainer;
6367
$container->test1 = 'bar';
6468
$container->test2 = false;
69+
/** @phpstan-ignore-next-line */
6570
$container->test3 = 'nope';
6671

6772
$this::assertSame('bar', $container->test1);
6873
$this::assertSame(false, $container->test2);
74+
/** @phpstan-ignore-next-line */
6975
$this::assertNull($container->test3);
7076

7177
// unset
@@ -130,6 +136,7 @@ public function testSerializable():void{
130136
'testConstruct' => 'success',
131137
]);
132138

139+
/** @var \chillerlan\SettingsTest\TestContainer $container */
133140
$container = unserialize(serialize($container)); // object should remain in the same state
134141

135142
// serialize will return the object in its current state including private properties

tests/TestContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
class TestContainer extends SettingsContainerAbstract{
1717
use TestOptionsTrait;
1818

19+
/** @phpstan-ignore-next-line */
1920
private string $test3 = 'what';
2021
}

tests/TestOptionsTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
use function sha1;
1515

16+
/**
17+
* @property string $test1
18+
* @property bool|null $test2
19+
* @property string $testConstruct
20+
* @property string|null $test4
21+
* @property string $test5
22+
* @property string|null $test6
23+
*/
1624
trait TestOptionsTrait{
1725

1826
protected string $test1 = 'foo';

0 commit comments

Comments
 (0)