Skip to content

Commit 02a2d55

Browse files
committed
fixed PHPStan errors
1 parent fb27914 commit 02a2d55

10 files changed

Lines changed: 43 additions & 233 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"nette/tester": "^2.6",
2323
"nikic/php-parser": "^5.0",
2424
"tracy/tracy": "^2.8",
25-
"phpstan/phpstan-nette": "^2.0@stable",
25+
"phpstan/phpstan": "^2.0@stable",
2626
"jetbrains/phpstorm-attributes": "^1.2"
2727
},
2828
"suggest": {

phpstan-baseline.neon

Lines changed: 2 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,11 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
5-
identifier: expr.resultUnused
6-
count: 1
7-
path: src/PhpGenerator/ClassLike.php
8-
93
-
104
message: '#^Parameter \#1 \$class of method Nette\\PhpGenerator\\Printer\:\:printClass\(\) expects Nette\\PhpGenerator\\ClassType\|Nette\\PhpGenerator\\EnumType\|Nette\\PhpGenerator\\InterfaceType\|Nette\\PhpGenerator\\TraitType, \$this\(Nette\\PhpGenerator\\ClassLike\) given\.$#'
115
identifier: argument.type
126
count: 1
137
path: src/PhpGenerator/ClassLike.php
148

15-
-
16-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Constant \.\.\.\$consts\) \{…" on a separate line does not do anything\.$#'
17-
identifier: expr.resultUnused
18-
count: 1
19-
path: src/PhpGenerator/ClassType.php
20-
21-
-
22-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Method \.\.\.\$methods\) \{…" on a separate line does not do anything\.$#'
23-
identifier: expr.resultUnused
24-
count: 1
25-
path: src/PhpGenerator/ClassType.php
26-
27-
-
28-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Property \.\.\.\$props\) \{…" on a separate line does not do anything\.$#'
29-
identifier: expr.resultUnused
30-
count: 1
31-
path: src/PhpGenerator/ClassType.php
32-
33-
-
34-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\TraitUse \.\.\.\$traits\) \{…" on a separate line does not do anything\.$#'
35-
identifier: expr.resultUnused
36-
count: 1
37-
path: src/PhpGenerator/ClassType.php
38-
39-
-
40-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
41-
identifier: expr.resultUnused
42-
count: 1
43-
path: src/PhpGenerator/Closure.php
44-
45-
-
46-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Parameter \.\.\.\$uses\) \{…" on a separate line does not do anything\.$#'
47-
identifier: expr.resultUnused
48-
count: 1
49-
path: src/PhpGenerator/Closure.php
50-
51-
-
52-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Parameter \.\.\.\$val\) \{…" on a separate line does not do anything\.$#'
53-
identifier: expr.resultUnused
54-
count: 1
55-
path: src/PhpGenerator/Closure.php
56-
57-
-
58-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
59-
identifier: expr.resultUnused
60-
count: 1
61-
path: src/PhpGenerator/Constant.php
62-
63-
-
64-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
65-
identifier: expr.resultUnused
66-
count: 1
67-
path: src/PhpGenerator/EnumCase.php
68-
69-
-
70-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Constant \.\.\.\$consts\) \{…" on a separate line does not do anything\.$#'
71-
identifier: expr.resultUnused
72-
count: 1
73-
path: src/PhpGenerator/EnumType.php
74-
75-
-
76-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\EnumCase \.\.\.\$cases\) \{…" on a separate line does not do anything\.$#'
77-
identifier: expr.resultUnused
78-
count: 1
79-
path: src/PhpGenerator/EnumType.php
80-
81-
-
82-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Method \.\.\.\$methods\) \{…" on a separate line does not do anything\.$#'
83-
identifier: expr.resultUnused
84-
count: 1
85-
path: src/PhpGenerator/EnumType.php
86-
87-
-
88-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\TraitUse \.\.\.\$traits\) \{…" on a separate line does not do anything\.$#'
89-
identifier: expr.resultUnused
90-
count: 1
91-
path: src/PhpGenerator/EnumType.php
92-
939
-
9410
message: '#^Call to an undefined method Nette\\PhpGenerator\\ClassLike\:\:addConstant\(\)\.$#'
9511
identifier: method.notFound
@@ -127,8 +43,8 @@ parameters:
12743
path: src/PhpGenerator/Extractor.php
12844

12945
-
130-
message: '#^Method Nette\\PhpGenerator\\Factory\:\:getAttributes\(\) has parameter \$from with no type specified\.$#'
131-
identifier: missingType.parameter
46+
message: '#^Parameter &\$from by\-ref type of method Nette\\PhpGenerator\\Factory\:\:createClassObject\(\) expects ReflectionClass\<object\>, ReflectionEnum\<UnitEnum\> given\.$#'
47+
identifier: parameterByRef.type
13248
count: 1
13349
path: src/PhpGenerator/Factory.php
13450

@@ -144,128 +60,8 @@ parameters:
14460
count: 1
14561
path: src/PhpGenerator/Factory.php
14662

147-
-
148-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
149-
identifier: expr.resultUnused
150-
count: 1
151-
path: src/PhpGenerator/GlobalFunction.php
152-
153-
-
154-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Parameter \.\.\.\$val\) \{…" on a separate line does not do anything\.$#'
155-
identifier: expr.resultUnused
156-
count: 1
157-
path: src/PhpGenerator/GlobalFunction.php
158-
159-
-
160-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Constant \.\.\.\$consts\) \{…" on a separate line does not do anything\.$#'
161-
identifier: expr.resultUnused
162-
count: 1
163-
path: src/PhpGenerator/InterfaceType.php
164-
165-
-
166-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Method \.\.\.\$methods\) \{…" on a separate line does not do anything\.$#'
167-
identifier: expr.resultUnused
168-
count: 1
169-
path: src/PhpGenerator/InterfaceType.php
170-
171-
-
172-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Property \.\.\.\$props\) \{…" on a separate line does not do anything\.$#'
173-
identifier: expr.resultUnused
174-
count: 1
175-
path: src/PhpGenerator/InterfaceType.php
176-
177-
-
178-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
179-
identifier: expr.resultUnused
180-
count: 1
181-
path: src/PhpGenerator/Method.php
182-
183-
-
184-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Parameter \.\.\.\$val\) \{…" on a separate line does not do anything\.$#'
185-
identifier: expr.resultUnused
186-
count: 1
187-
path: src/PhpGenerator/Method.php
188-
189-
-
190-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
191-
identifier: expr.resultUnused
192-
count: 1
193-
path: src/PhpGenerator/Parameter.php
194-
195-
-
196-
message: '#^Instanceof between Nette\\PhpGenerator\\EnumType and Nette\\PhpGenerator\\EnumType will always evaluate to true\.$#'
197-
identifier: instanceof.alwaysTrue
198-
count: 1
199-
path: src/PhpGenerator/Printer.php
200-
20163
-
20264
message: '#^Method Nette\\PhpGenerator\\Printer\:\:printDocComment\(\) has parameter \$commentable with no type specified\.$#'
20365
identifier: missingType.parameter
20466
count: 1
20567
path: src/PhpGenerator/Printer.php
206-
207-
-
208-
message: '#^Parameter \#2 \$array of function implode expects array\<string\>, array\<string, Nette\\PhpGenerator\\PropertyHook\|string\> given\.$#'
209-
identifier: argument.type
210-
count: 2
211-
path: src/PhpGenerator/Printer.php
212-
213-
-
214-
message: '#^Result of \|\| is always true\.$#'
215-
identifier: booleanOr.alwaysTrue
216-
count: 1
217-
path: src/PhpGenerator/Printer.php
218-
219-
-
220-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\PropertyHook \.\.\.\$hooks\) \{…" on a separate line does not do anything\.$#'
221-
identifier: expr.resultUnused
222-
count: 1
223-
path: src/PhpGenerator/PromotedParameter.php
224-
225-
-
226-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
227-
identifier: expr.resultUnused
228-
count: 1
229-
path: src/PhpGenerator/Property.php
230-
231-
-
232-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\PropertyHook \.\.\.\$hooks\) \{…" on a separate line does not do anything\.$#'
233-
identifier: expr.resultUnused
234-
count: 1
235-
path: src/PhpGenerator/Property.php
236-
237-
-
238-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Attribute \.\.\.\$attrs\) \{…" on a separate line does not do anything\.$#'
239-
identifier: expr.resultUnused
240-
count: 1
241-
path: src/PhpGenerator/PropertyHook.php
242-
243-
-
244-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Parameter \.\.\.\$val\) \{…" on a separate line does not do anything\.$#'
245-
identifier: expr.resultUnused
246-
count: 1
247-
path: src/PhpGenerator/PropertyHook.php
248-
249-
-
250-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Constant \.\.\.\$consts\) \{…" on a separate line does not do anything\.$#'
251-
identifier: expr.resultUnused
252-
count: 1
253-
path: src/PhpGenerator/TraitType.php
254-
255-
-
256-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Method \.\.\.\$methods\) \{…" on a separate line does not do anything\.$#'
257-
identifier: expr.resultUnused
258-
count: 1
259-
path: src/PhpGenerator/TraitType.php
260-
261-
-
262-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\Property \.\.\.\$props\) \{…" on a separate line does not do anything\.$#'
263-
identifier: expr.resultUnused
264-
count: 1
265-
path: src/PhpGenerator/TraitType.php
266-
267-
-
268-
message: '#^Expression "\(function \(\\Nette\\PhpGenerator\\TraitUse \.\.\.\$traits\) \{…" on a separate line does not do anything\.$#'
269-
identifier: expr.resultUnused
270-
count: 1
271-
path: src/PhpGenerator/TraitType.php

phpstan.neon

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
includes:
2-
- phpstan-baseline.neon
3-
41
parameters:
52
level: 6
63

74
paths:
85
- src
6+
7+
checkMissingCallableSignature: true
8+
9+
ignoreErrors:
10+
# Intentional runtime type validation pattern: (function (Type ...$items) {})(...$items)
11+
- identifier: expr.resultUnused
12+
13+
includes:
14+
- phpstan-baseline.neon
15+

src/PhpGenerator/ClassType.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function addImplement(string $name): static
129129

130130
public function removeImplement(string $name): static
131131
{
132-
$this->implements = array_diff($this->implements, [$name]);
132+
$this->implements = array_values(array_diff($this->implements, [$name]));
133133
return $this;
134134
}
135135

@@ -185,10 +185,9 @@ public function validate(): void
185185
public function __clone(): void
186186
{
187187
parent::__clone();
188-
$clone = fn($item) => clone $item;
189-
$this->consts = array_map($clone, $this->consts);
190-
$this->methods = array_map($clone, $this->methods);
191-
$this->properties = array_map($clone, $this->properties);
192-
$this->traits = array_map($clone, $this->traits);
188+
$this->consts = array_map(fn(Constant $c) => clone $c, $this->consts);
189+
$this->methods = array_map(fn(Method $m) => clone $m, $this->methods);
190+
$this->properties = array_map(fn(Property $p) => clone $p, $this->properties);
191+
$this->traits = array_map(fn(TraitUse $t) => clone $t, $this->traits);
193192
}
194193
}

src/PhpGenerator/Dumper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private function dumpObject(object $var, array $parents, int $level, int $column
148148
return '(object) ' . $this->dumpArray($var, $parents, $level, $column + 10);
149149

150150
} elseif ($class === \DateTime::class || $class === \DateTimeImmutable::class) {
151+
assert($var instanceof \DateTimeInterface);
151152
return $this->format(
152153
"new \\$class(?, new \\DateTimeZone(?))",
153154
$var->format('Y-m-d H:i:s.u'),

src/PhpGenerator/EnumType.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function addImplement(string $name): static
6868

6969
public function removeImplement(string $name): static
7070
{
71-
$this->implements = array_diff($this->implements, [$name]);
71+
$this->implements = array_values(array_diff($this->implements, [$name]));
7272
return $this;
7373
}
7474

@@ -137,10 +137,9 @@ public function addMember(Method|Constant|EnumCase|TraitUse $member, bool $overw
137137
public function __clone(): void
138138
{
139139
parent::__clone();
140-
$clone = fn($item) => clone $item;
141-
$this->consts = array_map($clone, $this->consts);
142-
$this->methods = array_map($clone, $this->methods);
143-
$this->traits = array_map($clone, $this->traits);
144-
$this->cases = array_map($clone, $this->cases);
140+
$this->consts = array_map(fn(Constant $c) => clone $c, $this->consts);
141+
$this->methods = array_map(fn(Method $m) => clone $m, $this->methods);
142+
$this->traits = array_map(fn(TraitUse $t) => clone $t, $this->traits);
143+
$this->cases = array_map(fn(EnumCase $c) => clone $c, $this->cases);
145144
}
146145
}

src/PhpGenerator/Extractor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PhpParser\Node;
1616
use PhpParser\NodeFinder;
1717
use PhpParser\ParserFactory;
18-
use function addcslashes, array_map, assert, class_exists, end, in_array, is_array, rtrim, str_contains, str_repeat, str_replace, str_starts_with, strlen, substr, substr_replace, usort;
18+
use function addcslashes, array_map, assert, class_exists, end, in_array, is_array, is_string, rtrim, str_contains, str_repeat, str_replace, str_starts_with, strlen, substr, substr_replace, usort;
1919

2020

2121
/**
@@ -475,10 +475,12 @@ private function setupFunction(GlobalFunction|Method|PropertyHook $function, Nod
475475
}
476476

477477
foreach ($node->getParams() as $item) {
478+
assert($item->var instanceof Node\Expr\Variable && is_string($item->var->name));
478479
$getVisibility = $this->toVisibility($item->flags);
479480
$setVisibility = $this->toSetterVisibility($item->flags);
480481
$final = (bool) ($item->flags & Modifiers::FINAL);
481482
if ($getVisibility || $setVisibility || $final) {
483+
assert($function instanceof Method);
482484
$param = $function->addPromotedParameter($item->var->name)
483485
->setVisibility($getVisibility, $setVisibility)
484486
->setReadonly($item->isReadonly())

0 commit comments

Comments
 (0)