-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the TemplateExtends, TemplateImplements and TemplateUse attributes
- Loading branch information
1 parent
c8ab752
commit d0538e5
Showing
5 changed files
with
316 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\NodeVisitor; | ||
|
||
use Exception; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpStaticAnalysis\Attributes\TemplateExtends; | ||
|
||
class TemplateExtendsAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase | ||
{ | ||
public function testAddsTemplateExtendsPHPDoc(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addTemplateExtendsAttributeToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @template-extends TemplateClass<int>\n */", $docText); | ||
} | ||
|
||
private function addTemplateExtendsAttributeToNode(Node\Stmt\Class_ $node): void | ||
{ | ||
$args = [ | ||
new Node\Arg(new Node\Scalar\String_('TemplateClass<int>')) | ||
]; | ||
$attributeName = new FullyQualified(TemplateExtends::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$node->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpStaticAnalysis\Attributes\TemplateImplements; | ||
|
||
class TemplateImplementsAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase | ||
{ | ||
public function testAddsTemplateImplementsPHPDoc(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addTemplateImplementsAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @template-implements TemplateInterface<int>\n */", $docText); | ||
} | ||
|
||
public function testAddsSeveralTemplateImplementsPHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addTemplateImplementsAttributesToNode($node, 2); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @template-implements TemplateInterface<int>\n * @template-implements TemplateInterface<int>\n */", $docText); | ||
} | ||
|
||
public function testAddsMultipleTemplateImplementsPHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addTemplateImplementsAttributesToNode($node); | ||
$this->addTemplateImplementsAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @template-implements TemplateInterface<int>\n * @template-implements TemplateInterface<int>\n */", $docText); | ||
} | ||
|
||
private function addTemplateImplementsAttributesToNode(Node\Stmt\Class_ $node, int $num = 1): void | ||
{ | ||
$value = new Node\Scalar\String_('TemplateInterface<int>'); | ||
$args = []; | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value); | ||
} | ||
$attributeName = new FullyQualified(TemplateImplements::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$node->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
} | ||
} |
Oops, something went wrong.