Skip to content

Commit 545ca23

Browse files
committed
Bump up phpstan to level 8
1 parent 40a6208 commit 545ca23

19 files changed

+132
-71
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ indent_size = 4
1111
insert_final_newline = true
1212
trim_trailing_whitespace = true
1313

14+
[*.neon]
15+
indent_style = tab
16+
17+
[*.neon.dist]
18+
indent_style = tab
19+
1420
[*.yml]
1521
indent_size = 2
1622

phpstan.neon

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 6
6-
paths:
7-
- src/
8-
bootstrapFiles:
9-
- tests/bootstrap.php
10-
ignoreErrors:
11-
- identifier: missingType.iterableValue
12-
- identifier: missingType.generics
13-
- identifier: method.childReturnType
5+
level: 8
6+
paths:
7+
- src/
8+
bootstrapFiles:
9+
- tests/bootstrap.php
10+
ignoreErrors:
11+
- identifier: missingType.iterableValue

src/BakePlugin.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Cake\Http\BaseApplication;
2727
use DirectoryIterator;
2828
use ReflectionClass;
29-
use ReflectionException;
3029

3130
/**
3231
* Plugin class for bake
@@ -140,11 +139,11 @@ protected function findInPath(string $namespace, string $path): array
140139
$class = $namespace . $item->getBasename('.php');
141140

142141
if (!$hasSubfolder) {
143-
try {
144-
$reflection = new ReflectionClass($class);
145-
} catch (ReflectionException) {
142+
if (!class_exists($class)) {
146143
continue;
147144
}
145+
146+
$reflection = new ReflectionClass($class);
148147
if (!$reflection->isInstantiable() || !$reflection->isSubclassOf(BakeCommand::class)) {
149148
continue;
150149
}

src/CodeGen/CodeParser.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public function parseFile(string $code): ?ParsedFile
8181
{
8282
$this->fileText = $code;
8383
try {
84-
$this->traverser->traverse($this->parser->parse($code));
84+
$ast = $this->parser->parse($code);
85+
if ($ast === null) {
86+
return null;
87+
}
88+
$this->traverser->traverse($ast);
8589
} catch (Error $e) {
8690
throw new ParseException($e->getMessage(), null, $e);
8791
}
@@ -172,7 +176,11 @@ public function enterNode(Node $node)
172176
throw new ParseException('Multiple constants per line are not supported, update your file');
173177
}
174178

175-
$name = (string)current($constant->consts)->name;
179+
$const = current($constant->consts);
180+
if ($const === false) {
181+
continue;
182+
}
183+
$name = (string)$const->name;
176184
$constants[$name] = $this->getNodeCode($constant);
177185
}
178186

@@ -182,7 +190,11 @@ public function enterNode(Node $node)
182190
throw new ParseException('Multiple properties per line are not supported, update your file');
183191
}
184192

185-
$name = (string)current($property->props)->name;
193+
$prop = current($property->props);
194+
if ($prop === false) {
195+
continue;
196+
}
197+
$name = (string)$prop->name;
186198
$properties[$name] = $this->getNodeCode($property);
187199
}
188200

src/CodeGen/ColumnTypeExtractor.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function extract(string $code): array
7373
// Wrap code in a dummy class if needed for parsing
7474
$wrappedCode = "<?php\nclass Dummy {\n" . $code . "\n}";
7575
$ast = $this->parser->parse($wrappedCode);
76+
if ($ast === null) {
77+
return [];
78+
}
7679

7780
$traverser = new NodeTraverser();
7881
$traverser->addVisitor($this);
@@ -144,8 +147,13 @@ protected function processMethodCall(MethodCall $methodCall): void
144147
) {
145148
// Extract the column name and type expression
146149
if (count($methodCall->args) >= 2) {
147-
$columnArg = $methodCall->args[0]->value;
148-
$typeArg = $methodCall->args[1]->value;
150+
$columnArgNode = $methodCall->args[0];
151+
$typeArgNode = $methodCall->args[1];
152+
if (!$columnArgNode instanceof Node\Arg || !$typeArgNode instanceof Node\Arg) {
153+
return;
154+
}
155+
$columnArg = $columnArgNode->value;
156+
$typeArg = $typeArgNode->value;
149157

150158
// Get column name
151159
$columnName = $this->getStringValue($columnArg);
@@ -199,7 +207,11 @@ protected function getTypeExpression(Node $node): ?string
199207
if ($className === 'EnumType' || str_ends_with($className, '\\EnumType')) {
200208
if ($methodName === 'from' && count($node->args) > 0) {
201209
// Extract the enum class name
202-
$arg = $node->args[0]->value;
210+
$argNode = $node->args[0];
211+
if (!$argNode instanceof Node\Arg) {
212+
return null;
213+
}
214+
$arg = $argNode->value;
203215
if ($arg instanceof Node\Expr\ClassConstFetch) {
204216
if (
205217
$arg->class instanceof Node\Name &&

src/Command/BakeCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ protected function _getName(string $name): string
112112
*/
113113
protected function getPrefix(Arguments $args): string
114114
{
115+
/** @var string|null $prefix */
115116
$prefix = $args->getOption('prefix');
116117
if (!$prefix) {
117118
return '';
@@ -225,7 +226,12 @@ protected function isValidColumnName(string $name): bool
225226
protected function parseFile(string $path): ?ParsedFile
226227
{
227228
if (file_exists($path)) {
228-
return (new CodeParser())->parseFile(file_get_contents($path));
229+
$contents = file_get_contents($path);
230+
if ($contents === false) {
231+
return null;
232+
}
233+
234+
return (new CodeParser())->parseFile($contents);
229235
}
230236

231237
return null;

src/Command/CommandCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function templateData(Arguments $arguments): array
6969
$data['command_name'] = Inflector::underscore(str_replace(
7070
'.',
7171
' ',
72-
$arguments->getArgument('name'),
72+
$arguments->getArgument('name') ?? '',
7373
));
7474

7575
return $data;

src/Command/ControllerCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo
8484
$actions = ['index', 'view', 'add', 'edit', 'delete'];
8585
}
8686
if ($args->getOption('actions')) {
87-
$actions = array_map('trim', explode(',', $args->getOption('actions')));
87+
$actions = array_map('trim', explode(',', (string)$args->getOption('actions')));
8888
$actions = array_filter($actions);
8989
}
9090
if (!$args->getOption('actions') && Plugin::isLoaded('Authentication') && $controllerName === 'Users') {
@@ -221,7 +221,7 @@ public function getComponents(Arguments $args): array
221221
{
222222
$components = [];
223223
if ($args->getOption('components')) {
224-
$components = explode(',', $args->getOption('components'));
224+
$components = explode(',', (string)$args->getOption('components'));
225225
$components = array_values(array_filter(array_map('trim', $components)));
226226
} else {
227227
if (Plugin::isLoaded('Authorization')) {
@@ -242,7 +242,7 @@ public function getHelpers(Arguments $args): array
242242
{
243243
$helpers = [];
244244
if ($args->getOption('helpers')) {
245-
$helpers = explode(',', $args->getOption('helpers'));
245+
$helpers = explode(',', (string)$args->getOption('helpers'));
246246
$helpers = array_values(array_filter(array_map('trim', $helpers)));
247247
}
248248

src/Command/FixtureAllCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
8383
$this->extractCommonProperties($args);
8484

8585
/** @var \Cake\Database\Connection $connection */
86-
$connection = ConnectionManager::get($args->getOption('connection') ?? 'default');
86+
$connection = ConnectionManager::get((string)($args->getOption('connection') ?: 'default'));
8787
$scanner = new TableScanner($connection);
8888
$fixture = new FixtureCommand();
8989

src/Command/FixtureCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
122122
return static::CODE_SUCCESS;
123123
}
124124

125-
$table = $args->getOption('table') ?? '';
125+
$table = (string)$args->getOption('table');
126126
$model = $this->_camelize($name);
127127
$this->bake($model, $table, $args, $io);
128128

@@ -285,16 +285,19 @@ protected function _generateSchema(TableSchemaInterface $table): string
285285
{
286286
$cols = $indexes = $constraints = [];
287287
foreach ($table->columns() as $field) {
288+
/** @var array $fieldData */
288289
$fieldData = $table->getColumn($field);
289290
$properties = implode(', ', $this->_values($fieldData));
290291
$cols[] = " '$field' => [$properties],";
291292
}
292293
foreach ($table->indexes() as $index) {
294+
/** @var array $fieldData */
293295
$fieldData = $table->getIndex($index);
294296
$properties = implode(', ', $this->_values($fieldData));
295297
$indexes[] = " '$index' => [$properties],";
296298
}
297299
foreach ($table->constraints() as $index) {
300+
/** @var array $fieldData */
298301
$fieldData = $table->getConstraint($index);
299302
$properties = implode(', ', $this->_values($fieldData));
300303
$constraints[] = " '$index' => [$properties],";
@@ -360,6 +363,7 @@ protected function _generateRecords(TableSchemaInterface $table, int $recordCoun
360363
for ($i = 0; $i < $recordCount; $i++) {
361364
$record = [];
362365
foreach ($table->columns() as $field) {
366+
/** @var array $fieldInfo */
363367
$fieldInfo = $table->getColumn($field);
364368
$insert = '';
365369
switch ($fieldInfo['type']) {

0 commit comments

Comments
 (0)