|
21 | 21 | use PhpParser\Node\Stmt\Interface_;
|
22 | 22 | use PhpParser\Node\Stmt\Trait_;
|
23 | 23 | use PhpParser\Node\Stmt\TraitUse;
|
| 24 | +use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagMethodValueNode; |
| 25 | +use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagPropertyValueNode; |
| 26 | +use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode; |
24 | 27 | use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
|
25 | 28 | use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
|
26 | 29 | use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
|
|
42 | 45 | use PHPStan\PhpDocParser\Ast\PhpDoc\UsesTagValueNode;
|
43 | 46 | use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
44 | 47 | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
| 48 | +use PhpStaticAnalysis\Attributes\Assert; |
| 49 | +use PhpStaticAnalysis\Attributes\AssertIfFalse; |
| 50 | +use PhpStaticAnalysis\Attributes\AssertIfTrue; |
45 | 51 | use PhpStaticAnalysis\Attributes\Param;
|
46 | 52 | use PhpStaticAnalysis\Attributes\ParamOut;
|
47 | 53 | use PhpStaticAnalysis\Attributes\Property;
|
@@ -69,6 +75,8 @@ final class AnnotationsToAttributesRector extends AbstractRector implements Conf
|
69 | 75 |
|
70 | 76 | private bool $addParamAttributeOnParameters = false;
|
71 | 77 |
|
| 78 | + private bool $addAssertAttributeOnParameters = false; |
| 79 | + |
72 | 80 | private bool $useTypeAttributeForReturnAnnotation = false;
|
73 | 81 |
|
74 | 82 | private bool $usePropertyAttributeForVarAnnotation = false;
|
@@ -148,6 +156,8 @@ public function configure(array $configuration): void
|
148 | 156 | $this->annotationsToAttributes[$tag] = $value;
|
149 | 157 | } elseif (is_bool($value) && $key == 'addParamAttributeOnParameters') {
|
150 | 158 | $this->addParamAttributeOnParameters = $value;
|
| 159 | + } elseif (is_bool($value) && $key == 'addAssertAttributeOnParameters') { |
| 160 | + $this->addAssertAttributeOnParameters = $value; |
151 | 161 | } elseif (is_bool($value) && $key == 'useTypeAttributeForReturnAnnotation') {
|
152 | 162 | $this->useTypeAttributeForReturnAnnotation = $value;
|
153 | 163 | } elseif (is_bool($value) && $key == 'usePropertyAttributeForVarAnnotation') {
|
@@ -210,12 +220,17 @@ public function refactor(Node $node): ?Node
|
210 | 220 | $this->attributeGroupNamedArgumentManipulator->decorate($attributeGroups);
|
211 | 221 | }
|
212 | 222 |
|
213 |
| - if ($this->addParamAttributeOnParameters && |
| 223 | + if (($this->addParamAttributeOnParameters || $this->addAssertAttributeOnParameters) && |
214 | 224 | ($node instanceof ClassMethod || $node instanceof Function_)) {
|
215 | 225 | foreach ($attributeGroups as $attrKey => $attributeGroup) {
|
216 | 226 | foreach ($attributeGroup->attrs as $key => $attribute) {
|
217 | 227 | $attributeName = (string)$attribute->name;
|
218 |
| - if ($attributeName === Param::class || $attributeName == ParamOut::class) { |
| 228 | + if ( |
| 229 | + (($attributeName === Param::class || $attributeName === ParamOut::class) |
| 230 | + && $this->addParamAttributeOnParameters) || |
| 231 | + (($attributeName === Assert::class || $attributeName === AssertIfFalse::class || $attributeName === AssertIfTrue::class) |
| 232 | + && $this->addAssertAttributeOnParameters) |
| 233 | + ) { |
219 | 234 | $args = $attribute->args;
|
220 | 235 | if (isset($args[0])) {
|
221 | 236 | $arg = $args[0];
|
@@ -373,6 +388,33 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
|
373 | 388 | )
|
374 | 389 | ];
|
375 | 390 | break;
|
| 391 | + case $tagValueNode instanceof AssertTagValueNode: |
| 392 | + case $tagValueNode instanceof AssertTagPropertyValueNode: |
| 393 | + case $tagValueNode instanceof AssertTagMethodValueNode: |
| 394 | + $type = (string)($tagValueNode->type); |
| 395 | + if ($tagValueNode->isNegated) { |
| 396 | + $type = '!' . $type; |
| 397 | + } |
| 398 | + if ($tagValueNode->isEquality) { |
| 399 | + $type = '=' . $type; |
| 400 | + } |
| 401 | + if ($tagValueNode instanceof AssertTagValueNode) { |
| 402 | + $args = [ |
| 403 | + new Arg( |
| 404 | + value: new String_($type), |
| 405 | + name: new Identifier(substr($tagValueNode->parameter, 1)) |
| 406 | + ) |
| 407 | + ]; |
| 408 | + } else { |
| 409 | + if ($tagValueNode instanceof AssertTagPropertyValueNode) { |
| 410 | + $type .= ' ' . $tagValueNode->parameter . '->' . $tagValueNode->property; |
| 411 | + } else { |
| 412 | + $type .= ' ' . $tagValueNode->parameter . '->' . $tagValueNode->method . '()'; |
| 413 | + } |
| 414 | + $args = [new Arg(new String_($type))]; |
| 415 | + } |
| 416 | + $attributeComment = $tagValueNode->description; |
| 417 | + break; |
376 | 418 | default:
|
377 | 419 | continue 2;
|
378 | 420 | }
|
|
0 commit comments