Skip to content

Commit cc75ed3

Browse files
committed
static analysis
1 parent 17a444b commit cc75ed3

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ parameters:
44
- '#Cannot access property \$args on PhpParser\\Node\|null#'
55
- '#Call to an undefined method PhpParser\\Node\\Expr\|PhpParser\\Node\\Name\:\:toString\(\)#'
66
- '#Parameter \#1 \$array of function array_flip expects array<int\|string>, array<int, int\|string>\|int\|string given.#'
7+
- '#Call to an undefined method Faker\\Generator\:\:userName\(\).#'
78

89
excludes_analyse:
910
- src/TestUtils/WorkshopExerciseTest.php

src/Exercise/TheAttributesOfSuccess.php

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

55
use Faker\Generator as FakerGenerator;
66
use PhpParser\Node;
7+
use PhpParser\Node\Expr\ClassConstFetch;
8+
use PhpParser\Node\Expr\Variable;
9+
use PhpParser\Node\Identifier;
710
use PhpParser\Node\Scalar\String_;
811
use PhpParser\Node\Stmt;
912
use PhpParser\Node\Stmt\Class_;
1013
use PhpParser\Node\Stmt\ClassMethod;
1114
use PhpParser\Node\Stmt\Property;
15+
use PhpParser\Node\Stmt\PropertyProperty;
16+
use PhpParser\Node\VarLikeIdentifier;
1217
use PhpParser\NodeFinder;
1318
use PhpParser\Parser;
1419
use PhpSchool\PhpWorkshop\Check\FileComparisonCheck;
@@ -68,7 +73,7 @@ public function getInitialCode(): SolutionInterface
6873
public function getSolution(): SolutionInterface
6974
{
7075
return DirectorySolution::fromDirectory(
71-
realpath(__DIR__ . '/../../exercises/the-attributes-of-success/solution'),
76+
(string) realpath(__DIR__ . '/../../exercises/the-attributes-of-success/solution'),
7277
);
7378
}
7479

@@ -80,16 +85,18 @@ public function getType(): ExerciseType
8085
public function getArgs(): array
8186
{
8287
return [
83-
json_encode(
84-
[
85-
'id' => random_int(0, 100),
86-
'comment' => $this->faker->sentence(4),
87-
'rating' => $this->faker->numberBetween(0, 5),
88-
'reviewer' => $this->faker->userName(),
89-
'date' => $this->faker->date('d-m-Y')
90-
],
91-
JSON_THROW_ON_ERROR
92-
)
88+
[
89+
json_encode(
90+
[
91+
'id' => random_int(0, 100),
92+
'comment' => $this->faker->sentence(4),
93+
'rating' => $this->faker->numberBetween(0, 5),
94+
'reviewer' => $this->faker->userName(),
95+
'date' => $this->faker->date('d-m-Y')
96+
],
97+
JSON_THROW_ON_ERROR
98+
)
99+
]
93100
];
94101
}
95102

@@ -157,10 +164,14 @@ public function check(Input $input): ResultInterface
157164
return new Failure($this->getName(), 'No flags were passed to Obfuscate Attribute definition');
158165
}
159166

160-
/** @var \PhpParser\Node\Expr\ClassConstFetch $value */
167+
/** @var ClassConstFetch $value */
161168
$value = $attribute->args[0]->value;
162169

163-
if ($value->class->toString() !== 'Attribute' || $value->name->name !== 'TARGET_METHOD') {
170+
if (
171+
$value->class->toString() !== 'Attribute'
172+
|| !$value->name instanceof Identifier
173+
|| $value->name->name !== 'TARGET_METHOD'
174+
) {
164175
return new Failure(
165176
$this->getName(),
166177
'The Obfuscate Attribute was not configured as Attribute::TARGET_METHOD'
@@ -170,10 +181,10 @@ public function check(Input $input): ResultInterface
170181
$prop = (new NodeFinder())->findFirst($attributeClass->getProperties(), function (Node $node) {
171182
return $node instanceof Property
172183
&& $node->isPublic()
173-
&& $node->type instanceof \PhpParser\Node\Identifier
184+
&& $node->type instanceof Identifier
174185
&& $node->type->name === 'string'
175-
&& $node->props[0] instanceof \PhpParser\Node\Stmt\PropertyProperty
176-
&& $node->props[0]->name instanceof \PhpParser\Node\VarLikeIdentifier
186+
&& $node->props[0] instanceof PropertyProperty
187+
&& $node->props[0]->name instanceof VarLikeIdentifier
177188
&& $node->props[0]->name->name === 'key';
178189
});
179190

@@ -182,8 +193,9 @@ public function check(Input $input): ResultInterface
182193
&& $node->name->name === '__construct'
183194
&& isset($node->params[0])
184195
&& $node->params[0]->flags === 1
196+
&& $node->params[0]->var instanceof Variable
185197
&& $node->params[0]->var->name === 'key'
186-
&& $node->params[0]->type instanceof \PhpParser\Node\Identifier
198+
&& $node->params[0]->type instanceof Identifier
187199
&& $node->params[0]->type->name === 'string';
188200
});
189201

0 commit comments

Comments
 (0)