|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace test\PhpStaticAnalysis\NodeVisitor; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Attribute; |
| 7 | +use PhpParser\Node\AttributeGroup; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\Name\FullyQualified; |
| 10 | +use PhpStaticAnalysis\Attributes\ParamOut; |
| 11 | + |
| 12 | +class ParamOutAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase |
| 13 | +{ |
| 14 | + public function testAddsParamOutPHPDoc(): void |
| 15 | + { |
| 16 | + $node = new Node\Stmt\ClassMethod('Test'); |
| 17 | + $this->addParamOutAttributesToNode($node); |
| 18 | + $this->nodeVisitor->enterNode($node); |
| 19 | + $docText = $this->getDocText($node); |
| 20 | + $this->assertEquals("/**\n * @param-out string \$param\n */", $docText); |
| 21 | + } |
| 22 | + |
| 23 | + public function testAddsSeveralParamOutPHPDocs(): void |
| 24 | + { |
| 25 | + $node = new Node\Stmt\ClassMethod('Test'); |
| 26 | + $this->addParamOutAttributesToNode($node, 2); |
| 27 | + $this->nodeVisitor->enterNode($node); |
| 28 | + $docText = $this->getDocText($node); |
| 29 | + $this->assertEquals("/**\n * @param-out string \$param\n * @param-out string \$param\n */", $docText); |
| 30 | + } |
| 31 | + |
| 32 | + public function testAddsMultipleParamOutPHPDocs(): void |
| 33 | + { |
| 34 | + $node = new Node\Stmt\ClassMethod('Test'); |
| 35 | + $this->addParamOutAttributesToNode($node); |
| 36 | + $this->addParamOutAttributesToNode($node); |
| 37 | + $this->nodeVisitor->enterNode($node); |
| 38 | + $docText = $this->getDocText($node); |
| 39 | + $this->assertEquals("/**\n * @param-out string \$param\n * @param-out string \$param\n */", $docText); |
| 40 | + } |
| 41 | + |
| 42 | + public function testAddsParamOutPHPDocToParam(): void |
| 43 | + { |
| 44 | + $node = new Node\Stmt\ClassMethod('Test'); |
| 45 | + $this->addParamOutAttributeToParamNode($node); |
| 46 | + $this->nodeVisitor->enterNode($node); |
| 47 | + $docText = $this->getDocText($node); |
| 48 | + $this->assertEquals("/**\n * @param-out string \$param\n */", $docText); |
| 49 | + } |
| 50 | + |
| 51 | + private function addParamOutAttributesToNode(Node\Stmt\ClassMethod $node, int $num = 1): void |
| 52 | + { |
| 53 | + $name = new Identifier('param'); |
| 54 | + $value = new Node\Scalar\String_('string'); |
| 55 | + $args = []; |
| 56 | + for ($i = 0; $i < $num; $i++) { |
| 57 | + $args[] = new Node\Arg($value, name: $name); |
| 58 | + } |
| 59 | + $attributeName = new FullyQualified(ParamOut::class); |
| 60 | + $attribute = new Attribute($attributeName, $args); |
| 61 | + $node->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); |
| 62 | + } |
| 63 | + |
| 64 | + private function addParamOutAttributeToParamNode(Node\Stmt\ClassMethod $node): void |
| 65 | + { |
| 66 | + $var = new Node\Expr\Variable('param'); |
| 67 | + $parameter = new Node\Param($var); |
| 68 | + $value = new Node\Scalar\String_('string'); |
| 69 | + $args = [new Node\Arg($value)]; |
| 70 | + $attributeName = new FullyQualified(ParamOut::class); |
| 71 | + $attribute = new Attribute($attributeName, $args); |
| 72 | + $parameter->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); |
| 73 | + $node->params = [$parameter]; |
| 74 | + } |
| 75 | +} |
0 commit comments