-
-
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 DefineType and ImportType attributes
- Loading branch information
1 parent
176cc97
commit b25264b
Showing
5 changed files
with
197 additions
and
3 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,70 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpStaticAnalysis\Attributes\DefineType; | ||
|
||
class DefineTypeAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase | ||
{ | ||
public function testAddsDefineTypePHPDoc(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addDefineTypeAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @type StringArray string[]\n */", $docText); | ||
} | ||
|
||
public function testAddsDefineTypePHPDocWithoutArgumentName(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addDefineTypeAttributesToNode($node, useArgumentName: false); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @type StringArray string[]\n */", $docText); | ||
} | ||
|
||
public function testAddsSeveralDefineTypePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addDefineTypeAttributesToNode($node, 2); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @type StringArray string[]\n * @type StringArray string[]\n */", $docText); | ||
} | ||
|
||
public function testAddsMultipleDefineTypePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addDefineTypeAttributesToNode($node); | ||
$this->addDefineTypeAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @type StringArray string[]\n * @type StringArray string[]\n */", $docText); | ||
} | ||
|
||
private function addDefineTypeAttributesToNode(Node\Stmt\Class_ $node, int $num = 1, bool $useArgumentName = true): void | ||
{ | ||
$args = []; | ||
if ($useArgumentName) { | ||
$name = new Identifier('StringArray'); | ||
$value = new Node\Scalar\String_('string[]'); | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value, name: $name); | ||
} | ||
} else { | ||
$value = new Node\Scalar\String_('StringArray string[]'); | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value); | ||
} | ||
} | ||
$attributeName = new FullyQualified(DefineType::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,70 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpStaticAnalysis\Attributes\ImportType; | ||
|
||
class ImportTypeAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase | ||
{ | ||
public function testAddsImportTypePHPDoc(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addImportTypeAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @import-type StringArray from StringClass\n */", $docText); | ||
} | ||
|
||
public function testAddsImportTypePHPDocWithoutArgumentName(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addImportTypeAttributesToNode($node, useArgumentName: false); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @import-type StringArray from StringClass\n */", $docText); | ||
} | ||
|
||
public function testAddsSeveralImportTypePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addImportTypeAttributesToNode($node, 2); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @import-type StringArray from StringClass\n * @import-type StringArray from StringClass\n */", $docText); | ||
} | ||
|
||
public function testAddsMultipleImportTypePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\Class_('Test'); | ||
$this->addImportTypeAttributesToNode($node); | ||
$this->addImportTypeAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @import-type StringArray from StringClass\n * @import-type StringArray from StringClass\n */", $docText); | ||
} | ||
|
||
private function addImportTypeAttributesToNode(Node\Stmt\Class_ $node, int $num = 1, bool $useArgumentName = true): void | ||
{ | ||
$args = []; | ||
if ($useArgumentName) { | ||
$name = new Identifier('StringArray'); | ||
$value = new Node\Scalar\String_('StringClass'); | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value, name: $name); | ||
} | ||
} else { | ||
$value = new Node\Scalar\String_('StringArray from StringClass'); | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value); | ||
} | ||
} | ||
$attributeName = new FullyQualified(ImportType::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