Skip to content

Commit f6e53dd

Browse files
committed
fixed PHPStan errors
1 parent 5dda133 commit f6e53dd

25 files changed

+292
-80
lines changed

.github/workflows/coding-style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
coverage: none
1515

1616
- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
17-
- run: php temp/code-checker/code-checker --strict-types --no-progress --ignore "tests/*/fixtures" --ignore expected
17+
- run: php temp/code-checker/code-checker --strict-types --no-progress --ignore "tests/*/fixtures" --ignore expected --ignore src/Bridges/DITracy/*.latte
1818

1919

2020
nette_cs:

phpstan.neon

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,122 @@
11
parameters:
2-
level: 5
2+
level: 8
33

44
paths:
55
- src
6+
7+
excludePaths:
8+
- src/compatibility.php
9+
10+
fileExtensions:
11+
- php
12+
- phtml
13+
14+
ignoreErrors:
15+
-
16+
identifier: return.type
17+
path: src/DI/Definitions/FactoryDefinition.php
18+
-
19+
identifier: instanceof.alwaysTrue
20+
path: src/DI/Definitions/ServiceDefinition.php
21+
-
22+
identifier: function.alreadyNarrowedType
23+
path: src/DI/Definitions/Statement.php
24+
-
25+
identifier: function.impossibleType
26+
path: src/DI/ContainerBuilder.php
27+
-
28+
identifier: argument.type
29+
path: src/DI/Extensions/ServicesExtension.php
30+
-
31+
identifier: identical.alwaysFalse
32+
path: src/DI/Helpers.php
33+
-
34+
identifier: property.notFound
35+
path: src/Bridges/DITracy/ContainerPanel.php
36+
-
37+
identifier: closure.unusedUse
38+
path: src/Bridges/DITracy/ContainerPanel.php
39+
-
40+
identifier: argument.type
41+
path: src/DI/Config/Adapters/NeonAdapter.php
42+
-
43+
identifier: instanceof.alwaysTrue
44+
path: src/DI/Extensions/ParametersExtension.php
45+
-
46+
identifier: identical.alwaysFalse
47+
path: src/DI/Autowiring.php
48+
-
49+
identifier: isset.offset
50+
path: src/DI/Autowiring.php
51+
-
52+
identifier: booleanAnd.alwaysFalse
53+
path: src/DI/Autowiring.php
54+
-
55+
identifier: nullCoalesce.offset
56+
path: src/DI/Autowiring.php
57+
-
58+
identifier: function.impossibleType
59+
path: src/Bridges/DITracy/dist/panel.phtml
60+
- # getByType() generic template limitation
61+
identifier: return.type
62+
path: src/DI/Container.php
63+
- # closure passes string to class-string params
64+
identifier: argument.type
65+
path: src/DI/Container.php
66+
- # callInjects passes array as callable
67+
identifier: argument.type
68+
path: src/DI/Extensions/InjectExtension.php
69+
- # string as class-string in various places
70+
identifier: argument.type
71+
path: src/DI/Resolver.php
72+
- # class_parents/class_implements key type
73+
identifier: offsetAccess.invalidOffset
74+
path: src/DI/Autowiring.php
75+
- # $this->$list assignment
76+
identifier: assign.propertyType
77+
path: src/DI/Autowiring.php
78+
- # string from array key used as class-string
79+
identifier: argument.type
80+
path: src/DI/Extensions/SearchExtension.php
81+
- # string from array key used as class-string
82+
identifier: argument.type
83+
path: src/DI/Extensions/DefinitionSchema.php
84+
- # template compiled with precise types, is_string always true
85+
identifier: function.alreadyNarrowedType
86+
path: src/Bridges/DITracy/dist/panel.phtml
87+
- # constructor entity param deliberately broad (native array, no value type)
88+
identifier: missingType.iterableValue
89+
path: src/DI/Definitions/Statement.php
90+
- # promoted property stores broad array, getEntity() declares precise tuple
91+
identifier: return.type
92+
path: src/DI/Definitions/Statement.php
93+
- # exhaustive type check after precise getEntity() return type
94+
identifier: elseif.alwaysTrue
95+
path: src/DI/Config/Adapters/NeonAdapter.php
96+
- # array_values after unset not recognized as list
97+
identifier: assign.propertyType
98+
path: src/DI/Config/Adapters/NeonAdapter.php
99+
- # getByType template type unresolvable in closure
100+
identifier: argument.templateType
101+
path: src/DI/Container.php
102+
- # normalizeClass: ReflectionClass->name is string not class-string
103+
identifier: return.type
104+
path: src/DI/Helpers.php
105+
- # normalizeEntity: reference tracking defeats type narrowing
106+
identifier: return.type
107+
path: src/DI/Resolver.php
108+
- # string as class-string for ReflectionClass in canBeLazy
109+
identifier: argument.type
110+
path: src/DI/Definitions/ServiceDefinition.php
111+
- # string as class-string for ReflectionClass in calculateHash
112+
identifier: argument.type
113+
path: src/DI/DependencyChecker.php
114+
- # exportMeta complex return shape with optional keys
115+
identifier: return.type
116+
path: src/DI/ContainerBuilder.php
117+
- # addDefinition template type unresolvable
118+
identifier: argument.templateType
119+
path: src/DI/Extensions/ServicesExtension.php
120+
121+
includes:
122+
- phpstan-baseline.neon

src/Bridges/DITracy/dist/panel.phtml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php declare(strict_types=1); ?>
2-
<?php use Tracy\Dumper ?>
2+
<?php use Tracy\Dumper ?><?php /** @var array<string, string> $services */ ?><?php /** @var array<string, array<int, list<string>>> $wiring */ ?><?php /** @var array<string, object> $instances */ ?><?php /** @var array<string, array<string, mixed>> $tags */ ?><?php /** @var array<string, mixed>|null $parameters */ ?><?php /** @var string $file */ ?>
33
<style class="tracy-debug">
44
#tracy-debug .nette-ContainerPanel table {
55
width: 100%;
@@ -41,17 +41,16 @@
4141
</thead>
4242
<tbody>
4343
<?php foreach ($services as $name => $type): ?><?php $name = (string) $name ?><?php $autowired = in_array($name, array_merge($wiring[$type][0] ?? [], $wiring[$type][1] ?? []), strict: true) ?> <tr>
44-
<td class="<?= Tracy\Helpers::escapeHtml(isset($instances[$name]) ? 'created' : '') ?>
45-
">
44+
<td class=<?= Tracy\Helpers::escapeHtml(isset($instances[$name]) ? 'created' : '') ?>
45+
>
4646
<?php if (is_numeric($name)): ?>
4747
<small><?= Tracy\Helpers::escapeHtml($name) ?>
4848
</small><?php else: ?>
4949
<?= Tracy\Helpers::escapeHtml($name) ?>
5050

5151
<?php endif ?>
5252
</td>
53-
<td class="<?= Tracy\Helpers::escapeHtml($autowired ? 'yes' : '') ?>
54-
">
53+
<td<?php if ($ʟ_tmp = $autowired ? 'yes' : '') echo ' class', is_bool($ʟ_tmp) ? '' : '="' . Tracy\Helpers::escapeHtml($ʟ_tmp) . '"' ?>>
5554
<?= Tracy\Helpers::escapeHtml($autowired ? 'yes' : (isset($wiring[$type]) ? 'no' : '?')) ?>
5655

5756
</td>

src/Bridges/DITracy/dist/tab.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php declare(strict_types=1); ?>
2-
<span title="Nette DI Container">
2+
<?php /** @var ?float $elapsedTime */ ?><span title="Nette DI Container">
33
<svg viewBox="0 0 2048 2048">
44
<path fill="#ae7c21" d="m1675 690.55v-125c0-36-4-60-13-71-9-12-26-18-52-18s-43 6-52 17c-8 11-12 36-12 72v910.62c0 37 4 61 12 72 9 11 26 17 52 17s44-6 52-17c9-12 13-36 13-72v-207h243v165c0 86-4 148-12 187-8 38-21 70-39 96-20 28-45 48-77 59-31 11-87 16-170 16-96 0-161-5-196-16-34-11-63-31-86-59-21-26-36-59-45-97-9-39-14-101-14-186v-826.62c0-85 4-147 13-185s25-71 46-97c23-29 54-49 92-60s102-17 189-17c75 0 130 6 164 17s61 30 81 59c19 26 33 59 41 99 8 39 12 101 12 185v84zm-524 1086.6h-254v-1514.6h254zm-1025 0v-1514.6h316c127 0 212 27 257 81 44 53 66 159 66 316v721.62c0 157-22 263-66 317-44 53-130 80-257 80zm388-314v-884.62c0-35-4-58-13-70-9-11-26-17-53-17h-66v1058.6h66c26 0 44-6 53-17 9-12 14-35 14-70z"/>
55
</svg

src/Bridges/DITracy/panel.latte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{use Tracy\Dumper}
2+
{varType array<string, string> $services}
3+
{varType array<string, array<int, list<string>>> $wiring}
4+
{varType array<string, object> $instances}
5+
{varType array<string, array<string, mixed>> $tags}
6+
{varType array<string, mixed>|null $parameters}
7+
{varType string $file}
28

39
<style class="tracy-debug">
410
#tracy-debug .nette-ContainerPanel table {
@@ -43,10 +49,10 @@
4349
{do $name = (string) $name}
4450
{do $autowired = in_array($name, array_merge($wiring[$type][0] ?? [], $wiring[$type][1] ?? []), strict: true)}
4551
<tr>
46-
<td class="{isset($instances[$name]) ? created : ''}">
52+
<td class={isset($instances[$name]) ? created : ''}>
4753
{if is_numeric($name)}<small>{$name}</small>{else}{$name}{/if}
4854
</td>
49-
<td class="{$autowired ? yes : ''}">
55+
<td class={$autowired ? yes : ''}>
5056
{$autowired ? yes : (isset($wiring[$type]) ? no : '?')}
5157
</td>
5258
<td>

src/Bridges/DITracy/tab.latte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{varType ?float $elapsedTime}
12
<span title="Nette DI Container">
23
<svg viewBox="0 0 2048 2048">
34
<path fill="#ae7c21" d="m1675 690.55v-125c0-36-4-60-13-71-9-12-26-18-52-18s-43 6-52 17c-8 11-12 36-12 72v910.62c0 37 4 61 12 72 9 11 26 17 52 17s44-6 52-17c9-12 13-36 13-72v-207h243v165c0 86-4 148-12 187-8 38-21 70-39 96-20 28-45 48-77 59-31 11-87 16-170 16-96 0-161-5-196-16-34-11-63-31-86-59-21-26-36-59-45-97-9-39-14-101-14-186v-826.62c0-85 4-147 13-185s25-71 46-97c23-29 54-49 92-60s102-17 189-17c75 0 130 6 164 17s61 30 81 59c19 26 33 59 41 99 8 39 12 101 12 185v84zm-524 1086.6h-254v-1514.6h254zm-1025 0v-1514.6h316c127 0 212 27 257 81 44 53 66 159 66 316v721.62c0 157-22 263-66 317-44 53-130 80-257 80zm388-314v-884.62c0-35-4-58-13-70-9-11-26-17-53-17h-66v1058.6h66c26 0 44-6 53-17 9-12 14-35 14-70z"/>

src/DI/Compiler.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Nette;
1111
use Nette\Schema;
12-
use function array_diff_key, array_filter, array_keys, array_merge, assert, count, implode, key, sprintf, strtolower;
12+
use function array_diff_key, array_filter, array_keys, array_merge, array_values, assert, count, implode, key, sprintf, strtolower;
1313

1414

1515
/**
@@ -268,7 +268,9 @@ private function processBeforeCompile(): void
268268

269269
foreach ($this->extensions as $extension) {
270270
$extension->beforeCompile();
271-
$this->dependencies->add([(new \ReflectionClass($extension))->getFileName()]);
271+
if ($file = (new \ReflectionClass($extension))->getFileName()) {
272+
$this->dependencies->add([$file]);
273+
}
272274
}
273275

274276
$this->builder->complete();
@@ -288,7 +290,7 @@ private function processSchema(Schema\Schema $schema, array $configs, ?string $n
288290
$context->dynamics = &$this->extensions[self::Parameters]->dynamicValidators;
289291
};
290292
try {
291-
$res = $processor->processMultiple($schema, $configs);
293+
$res = $processor->processMultiple($schema, array_values($configs));
292294
} catch (Schema\ValidationException $e) {
293295
throw new Nette\DI\InvalidConfigurationException($e->getMessage());
294296
}
@@ -326,7 +328,7 @@ public function loadDefinitionsFromConfig(array $configList): void
326328
$configList = Helpers::expand($configList, $this->builder->parameters);
327329
$extension = $this->extensions[self::Services];
328330
assert($extension instanceof Extensions\ServicesExtension);
329-
$extension->loadDefinitions($this->processSchema($extension->getConfigSchema(), [$configList]));
331+
$extension->loadDefinitions((array) $this->processSchema($extension->getConfigSchema(), [$configList]));
330332
}
331333

332334

src/DI/CompilerExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getConfigSchema(): Nette\Schema\Schema
7474
public function validateConfig(array $expected, ?array $config = null, ?string $name = null): array
7575
{
7676
if (func_num_args() === 1) {
77-
return $this->config = $this->validateConfig($expected, $this->config);
77+
return $this->config = $this->validateConfig($expected, (array) $this->config);
7878
}
7979

8080
if ($extra = array_diff_key((array) $config, $expected)) {

src/DI/Config/Adapters/NeonAdapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Nette\DI\Definitions\Statement;
1414
use Nette\Neon;
1515
use Nette\Neon\Node;
16-
use function array_walk_recursive, constant, count, defined, implode, is_array, is_string, ltrim, preg_match, preg_replace, sprintf, str_contains, str_ends_with, str_starts_with, substr;
16+
use function array_values, array_walk_recursive, constant, count, defined, implode, is_array, is_string, ltrim, preg_match, preg_replace, sprintf, str_contains, str_ends_with, str_starts_with, substr;
1717

1818

1919
/**
@@ -44,7 +44,9 @@ public function load(string $file): array
4444
$node = $traverser->traverse($node, $this->convertAtSignVisitor(...));
4545
$node = $traverser->traverse($node, $this->deprecatedParametersVisitor(...));
4646
$node = $traverser->traverse($node, $this->resolveConstantsVisitor(...));
47-
return $this->process((array) $node->toValue());
47+
/** @var array<string, mixed> $result */
48+
$result = $this->process((array) $node->toValue());
49+
return $result;
4850
}
4951

5052

@@ -192,6 +194,8 @@ private function removeUnderscoreVisitor(Node $node): void
192194
$index = true;
193195
}
194196
}
197+
198+
$node->attributes = array_values($node->attributes);
195199
}
196200

197201

src/DI/Config/Loader.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,12 @@ private function getAdapter(string $file): Adapter
116116
throw new Nette\InvalidArgumentException(sprintf("Unknown file extension '%s'.", $file));
117117
}
118118

119-
return is_object($this->adapters[$extension])
120-
? $this->adapters[$extension]
121-
: new $this->adapters[$extension];
119+
$adapter = $this->adapters[$extension];
120+
if ($adapter instanceof Adapter) {
121+
return $adapter;
122+
}
123+
124+
return new $adapter;
122125
}
123126

124127

0 commit comments

Comments
 (0)