-
-
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 Assert, AssertIfTrue and AssertIfFalse attributes
- Loading branch information
1 parent
b012631
commit e8ee463
Showing
5 changed files
with
271 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,75 @@ | ||
<?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\Assert; | ||
|
||
class AssertAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase | ||
{ | ||
public function testAddsAssertPHPDoc(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsSeveralAssertPHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertAttributesToNode($node, 2); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert string \$param\n * @assert string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsMultipleAssertPHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertAttributesToNode($node); | ||
$this->addAssertAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert string \$param\n * @assert string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsAssertPHPDocToParam(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertAttributeToParamNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert string \$param\n */", $docText); | ||
} | ||
|
||
private function addAssertAttributesToNode(Node\Stmt\ClassMethod $node, int $num = 1): void | ||
{ | ||
$name = new Identifier('param'); | ||
$value = new Node\Scalar\String_('string'); | ||
$args = []; | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value, name: $name); | ||
} | ||
$attributeName = new FullyQualified(Assert::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$node->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
} | ||
|
||
private function addAssertAttributeToParamNode(Node\Stmt\ClassMethod $node): void | ||
{ | ||
$var = new Node\Expr\Variable('param'); | ||
$parameter = new Node\Param($var); | ||
$value = new Node\Scalar\String_('string'); | ||
$args = [new Node\Arg($value)]; | ||
$attributeName = new FullyQualified(Assert::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$parameter->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
$node->params = [$parameter]; | ||
} | ||
} |
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,75 @@ | ||
<?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\AssertIfFalse; | ||
|
||
class AssertIfFalseAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase | ||
{ | ||
public function testAddsAssertIfFalsePHPDoc(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfFalseAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-false string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsSeveralAssertIfFalsePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfFalseAttributesToNode($node, 2); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-false string \$param\n * @assert-if-false string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsMultipleAssertIfFalsePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfFalseAttributesToNode($node); | ||
$this->addAssertIfFalseAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-false string \$param\n * @assert-if-false string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsAssertIfFalsePHPDocToParam(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfFalseAttributeToParamNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-false string \$param\n */", $docText); | ||
} | ||
|
||
private function addAssertIfFalseAttributesToNode(Node\Stmt\ClassMethod $node, int $num = 1): void | ||
{ | ||
$name = new Identifier('param'); | ||
$value = new Node\Scalar\String_('string'); | ||
$args = []; | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value, name: $name); | ||
} | ||
$attributeName = new FullyQualified(AssertIfFalse::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$node->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
} | ||
|
||
private function addAssertIfFalseAttributeToParamNode(Node\Stmt\ClassMethod $node): void | ||
{ | ||
$var = new Node\Expr\Variable('param'); | ||
$parameter = new Node\Param($var); | ||
$value = new Node\Scalar\String_('string'); | ||
$args = [new Node\Arg($value)]; | ||
$attributeName = new FullyQualified(AssertIfFalse::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$parameter->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
$node->params = [$parameter]; | ||
} | ||
} |
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,75 @@ | ||
<?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\AssertIfTrue; | ||
|
||
class AssertIfTrueAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase | ||
{ | ||
public function testAddsAssertIfTruePHPDoc(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfTrueAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-true string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsSeveralAssertIfTruePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfTrueAttributesToNode($node, 2); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-true string \$param\n * @assert-if-true string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsMultipleAssertIfTruePHPDocs(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfTrueAttributesToNode($node); | ||
$this->addAssertIfTrueAttributesToNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-true string \$param\n * @assert-if-true string \$param\n */", $docText); | ||
} | ||
|
||
public function testAddsAssertIfTruePHPDocToParam(): void | ||
{ | ||
$node = new Node\Stmt\ClassMethod('Test'); | ||
$this->addAssertIfTrueAttributeToParamNode($node); | ||
$this->nodeVisitor->enterNode($node); | ||
$docText = $this->getDocText($node); | ||
$this->assertEquals("/**\n * @assert-if-true string \$param\n */", $docText); | ||
} | ||
|
||
private function addAssertIfTrueAttributesToNode(Node\Stmt\ClassMethod $node, int $num = 1): void | ||
{ | ||
$name = new Identifier('param'); | ||
$value = new Node\Scalar\String_('string'); | ||
$args = []; | ||
for ($i = 0; $i < $num; $i++) { | ||
$args[] = new Node\Arg($value, name: $name); | ||
} | ||
$attributeName = new FullyQualified(AssertIfTrue::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$node->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
} | ||
|
||
private function addAssertIfTrueAttributeToParamNode(Node\Stmt\ClassMethod $node): void | ||
{ | ||
$var = new Node\Expr\Variable('param'); | ||
$parameter = new Node\Param($var); | ||
$value = new Node\Scalar\String_('string'); | ||
$args = [new Node\Arg($value)]; | ||
$attributeName = new FullyQualified(AssertIfTrue::class); | ||
$attribute = new Attribute($attributeName, $args); | ||
$parameter->attrGroups = array_merge($node->attrGroups, [new AttributeGroup([$attribute])]); | ||
$node->params = [$parameter]; | ||
} | ||
} |