|
4 | 4 |
|
5 | 5 | namespace Rector\Assert\Rector\ClassMethod; |
6 | 6 |
|
7 | | -use PhpParser\Node\Expr\Variable; |
8 | 7 | use PhpParser\Node; |
9 | 8 | use PhpParser\Node\Arg; |
10 | 9 | use PhpParser\Node\Expr; |
11 | 10 | use PhpParser\Node\Expr\FuncCall; |
12 | 11 | use PhpParser\Node\Expr\StaticCall; |
| 12 | +use PhpParser\Node\Expr\Variable; |
13 | 13 | use PhpParser\Node\Identifier; |
14 | 14 | use PhpParser\Node\Name; |
15 | 15 | use PhpParser\Node\Name\FullyQualified; |
16 | 16 | use PhpParser\Node\Stmt\ClassMethod; |
17 | 17 | use PhpParser\Node\Stmt\Expression; |
18 | 18 | use PhpParser\PrettyPrinter\Standard; |
19 | 19 | use PHPStan\Type\ArrayType; |
| 20 | +use PHPStan\Type\BooleanType; |
| 21 | +use PHPStan\Type\FloatType; |
20 | 22 | use PHPStan\Type\IntegerType; |
21 | 23 | use PHPStan\Type\StringType; |
| 24 | +use PHPStan\Type\Type; |
22 | 25 | use Rector\Assert\Enum\AssertClassName; |
23 | 26 | use Rector\Assert\NodeAnalyzer\ExistingAssertStaticCallResolver; |
24 | 27 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
@@ -133,19 +136,17 @@ public function refactor(Node $node): ?ClassMethod |
133 | 136 | continue; |
134 | 137 | } |
135 | 138 |
|
136 | | - // assert value |
137 | | - if ($paramDocType->getItemType() instanceof IntegerType) { |
138 | | - $assertStaticCallStmts[] = $this->createAssertExpression($param->var, 'allInteger'); |
139 | | - } elseif ($paramDocType->getItemType() instanceof StringType) { |
140 | | - $assertStaticCallStmts[] = $this->createAssertExpression($param->var, 'allString'); |
| 139 | + $valueAssertMethod = $this->matchTypeToAssertMethod($paramDocType->getItemType()); |
| 140 | + |
| 141 | + if (is_string($valueAssertMethod)) { |
| 142 | + $assertStaticCallStmts[] = $this->createAssertExpression($param->var, $valueAssertMethod); |
141 | 143 | } |
142 | 144 |
|
143 | | - // assert keys |
144 | | - $arrayKeys = new FuncCall(new Name('array_keys'), [new Arg($param->var)]); |
145 | | - if ($paramDocType->getKeyType() instanceof StringType) { |
146 | | - $assertStaticCallStmts[] = $this->createAssertExpression($arrayKeys, 'allString'); |
147 | | - } elseif ($paramDocType->getKeyType() instanceof IntegerType) { |
148 | | - $assertStaticCallStmts[] = $this->createAssertExpression($arrayKeys, 'allInteger'); |
| 145 | + $keyAssertMethod = $this->matchTypeToAssertMethod($paramDocType->getKeyType()); |
| 146 | + |
| 147 | + if (is_string($keyAssertMethod)) { |
| 148 | + $arrayKeys = new FuncCall(new Name('array_keys'), [new Arg($param->var)]); |
| 149 | + $assertStaticCallStmts[] = $this->createAssertExpression($arrayKeys, $keyAssertMethod); |
149 | 150 | } |
150 | 151 | } |
151 | 152 |
|
@@ -192,4 +193,25 @@ private function filterOutExistingStaticCall(array $assertStaticCallStmts, array |
192 | 193 | return ! in_array($currentStaticCallHash, $existingAssertCallHashes, true); |
193 | 194 | }); |
194 | 195 | } |
| 196 | + |
| 197 | + private function matchTypeToAssertMethod(Type $type): ?string |
| 198 | + { |
| 199 | + if ($type instanceof IntegerType) { |
| 200 | + return 'allInteger'; |
| 201 | + } |
| 202 | + |
| 203 | + if ($type instanceof StringType) { |
| 204 | + return 'allString'; |
| 205 | + } |
| 206 | + |
| 207 | + if ($type instanceof FloatType) { |
| 208 | + return 'allFloat'; |
| 209 | + } |
| 210 | + |
| 211 | + if ($type instanceof BooleanType) { |
| 212 | + return 'allBoolean'; |
| 213 | + } |
| 214 | + |
| 215 | + return null; |
| 216 | + } |
195 | 217 | } |
0 commit comments