-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ShouldInclude and ShouldNotInclude assertions
- Loading branch information
Showing
16 changed files
with
451 additions
and
2 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
15 changes: 15 additions & 0 deletions
15
src/Rule/Assertion/Relation/ShouldInclude/IncludedTraitsRule.php
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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Relation\ShouldInclude; | ||
|
||
use PHPat\Rule\Extractor\Relation\AllTraitsExtractor; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
final class IncludedTraitsRule extends ShouldInclude implements Rule | ||
{ | ||
use AllTraitsExtractor; | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Rule/Assertion/Relation/ShouldInclude/ShouldInclude.php
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,50 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Relation\ShouldInclude; | ||
|
||
use PHPat\Configuration; | ||
use PHPat\Rule\Assertion\Relation\RelationAssertion; | ||
use PHPat\Rule\Assertion\Relation\ValidationTrait; | ||
use PHPat\Statement\Builder\StatementBuilderFactory; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\FileTypeMapper; | ||
|
||
abstract class ShouldInclude extends RelationAssertion | ||
{ | ||
use ValidationTrait; | ||
|
||
public function __construct( | ||
StatementBuilderFactory $statementBuilderFactory, | ||
Configuration $configuration, | ||
ReflectionProvider $reflectionProvider, | ||
FileTypeMapper $fileTypeMapper | ||
) { | ||
parent::__construct( | ||
__CLASS__, | ||
$statementBuilderFactory, | ||
$configuration, | ||
$reflectionProvider, | ||
$fileTypeMapper | ||
); | ||
} | ||
|
||
protected function applyValidation( | ||
string $ruleName, | ||
ClassReflection $subject, | ||
array $targets, | ||
array $targetExcludes, | ||
array $nodes, | ||
array $tips | ||
): array { | ||
return $this->applyShould($ruleName, $subject, $targets, $targetExcludes, $nodes, $tips); | ||
} | ||
|
||
protected function getMessage(string $ruleName, string $subject, string $target): string | ||
{ | ||
return $this->prepareMessage( | ||
$ruleName, | ||
sprintf('%s should include %s', $subject, $target), | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Rule/Assertion/Relation/ShouldNotInclude/IncludedTraitsRule.php
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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Relation\ShouldNotInclude; | ||
|
||
use PHPat\Rule\Extractor\Relation\AllTraitsExtractor; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
final class IncludedTraitsRule extends ShouldNotInclude implements Rule | ||
{ | ||
use AllTraitsExtractor; | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Rule/Assertion/Relation/ShouldNotInclude/ShouldNotInclude.php
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,50 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Relation\ShouldNotInclude; | ||
|
||
use PHPat\Configuration; | ||
use PHPat\Rule\Assertion\Relation\RelationAssertion; | ||
use PHPat\Rule\Assertion\Relation\ValidationTrait; | ||
use PHPat\Statement\Builder\StatementBuilderFactory; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\FileTypeMapper; | ||
|
||
abstract class ShouldNotInclude extends RelationAssertion | ||
{ | ||
use ValidationTrait; | ||
|
||
public function __construct( | ||
StatementBuilderFactory $statementBuilderFactory, | ||
Configuration $configuration, | ||
ReflectionProvider $reflectionProvider, | ||
FileTypeMapper $fileTypeMapper | ||
) { | ||
parent::__construct( | ||
__CLASS__, | ||
$statementBuilderFactory, | ||
$configuration, | ||
$reflectionProvider, | ||
$fileTypeMapper | ||
); | ||
} | ||
|
||
protected function applyValidation( | ||
string $ruleName, | ||
ClassReflection $subject, | ||
array $targets, | ||
array $targetExcludes, | ||
array $nodes, | ||
array $tips | ||
): array { | ||
return $this->applyShouldNot($ruleName, $subject, $targets, $targetExcludes, $nodes, $tips); | ||
} | ||
|
||
protected function getMessage(string $ruleName, string $subject, string $target): string | ||
{ | ||
return $this->prepareMessage( | ||
$ruleName, | ||
sprintf('%s should not include %s', $subject, $target), | ||
); | ||
} | ||
} |
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,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Extractor\Relation; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Reflection\ClassReflection; | ||
|
||
trait AllTraitsExtractor | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
/** | ||
* @param InClassNode $node | ||
* @return array<class-string> | ||
*/ | ||
protected function extractNodeClassNames(Node $node, Scope $scope): array | ||
{ | ||
return array_map( | ||
static fn (ClassReflection $c) => $c->getName(), | ||
$node->getClassReflection()->getTraits() | ||
); | ||
} | ||
} |
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,48 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Selector; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
|
||
final class ClassIncludes implements SelectorInterface | ||
{ | ||
private string $traitname; | ||
private bool $isRegex; | ||
|
||
/** | ||
* @param class-string|string $traitname | ||
*/ | ||
public function __construct(string $traitname, bool $isRegex) | ||
{ | ||
$this->traitname = $traitname; | ||
$this->isRegex = $isRegex; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->traitname; | ||
} | ||
|
||
public function matches(ClassReflection $classReflection): bool | ||
{ | ||
if ($this->isRegex) { | ||
return $this->matchesRegex($classReflection->getTraits()); | ||
} | ||
|
||
return $classReflection->hasTraitUse($this->traitname); | ||
} | ||
|
||
/** | ||
* @param array<ClassReflection> $traits | ||
*/ | ||
private function matchesRegex(array $traits): bool | ||
{ | ||
foreach ($traits as $trait) { | ||
if (preg_match($this->traitname, $trait->getName()) === 1) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,5 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
trait SimpleTraitTwo {} |
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,49 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\PHPat\unit\rules\ShouldInclude; | ||
|
||
use PHPat\Configuration; | ||
use PHPat\Rule\Assertion\Relation\ShouldInclude\IncludedTraitsRule; | ||
use PHPat\Rule\Assertion\Relation\ShouldInclude\ShouldInclude; | ||
use PHPat\Selector\Classname; | ||
use PHPat\Statement\Builder\StatementBuilderFactory; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPStan\Type\FileTypeMapper; | ||
use Tests\PHPat\fixtures\FixtureClass; | ||
use Tests\PHPat\fixtures\Simple\SimpleTraitTwo; | ||
use Tests\PHPat\unit\FakeTestParser; | ||
|
||
/** | ||
* @extends RuleTestCase<IncludedTraitsRule> | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
class IncludedTraitsTest extends RuleTestCase | ||
{ | ||
public const RULE_NAME = 'test_FixtureClassShouldIncludeSimpleTraitTwo'; | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse(['tests/fixtures/FixtureClass.php'], [ | ||
[sprintf('%s should include %s', FixtureClass::class, SimpleTraitTwo::class), 29], | ||
]); | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$testParser = FakeTestParser::create( | ||
self::RULE_NAME, | ||
ShouldInclude::class, | ||
[new Classname(FixtureClass::class, false)], | ||
[new Classname(SimpleTraitTwo::class, false)] | ||
); | ||
|
||
return new IncludedTraitsRule( | ||
new StatementBuilderFactory($testParser), | ||
new Configuration(false, true, false), | ||
$this->createReflectionProvider(), | ||
self::getContainer()->getByType(FileTypeMapper::class) | ||
); | ||
} | ||
} |
Oops, something went wrong.