Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShouldHaveAttribute assertion #244

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/documentation/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ It asserts that the selected classes **do not depend** on the target classes.
## shouldNotConstruct()
It asserts that the selected classes **do not use the constructor** of the target classes.

## shouldApplyAttribute()
It asserts that the selected classes **apply** the target attributes.

## canOnlyDependOn()
It asserts that the selected classes **do not depend** on anything else than the target classes.

Expand Down
6 changes: 6 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ services:
tags:
- phpstan.rules.rule

# ShouldApplyAttribute rules
-
class: PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ClassAttributeRule
tags:
- phpstan.rules.rule

parametersSchema:
phpat: structure([
ignore_doc_comments: bool(),
Expand Down
5 changes: 3 additions & 2 deletions src/Rule/Assertion/Relation/RelationAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPat\Configuration;
use PHPat\Rule\Assertion\Assertion;
use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ShouldApplyAttribute;
use PHPat\Rule\Assertion\Relation\ShouldExtend\ShouldExtend;
use PHPat\Rule\Assertion\Relation\ShouldImplement\ShouldImplement;
use PHPat\Selector\Classname;
Expand Down Expand Up @@ -97,8 +98,8 @@ protected function ruleApplies(Scope $scope, array $nodes): bool
return false;
}

// Can not skip if the rule is a ShouldExtend or ShouldImplement rule
if (is_a($this, ShouldExtend::class) || is_a($this, ShouldImplement::class)) {
// Can not skip if the rule is a ShouldExtend, ShouldImplement or ShouldApplyAttribute rule
if (is_a($this, ShouldExtend::class) || is_a($this, ShouldImplement::class) || is_a($this, ShouldApplyAttribute::class)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Relation\ShouldApplyAttribute;

use PHPat\Rule\Extractor\Relation\ClassAttributeExtractor;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;

/**
* @implements Rule<InClassNode>
*/
final class ClassAttributeRule extends ShouldApplyAttribute implements Rule
{
use ClassAttributeExtractor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Relation\ShouldApplyAttribute;

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 ShouldApplyAttribute 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 apply the attribute %s', $subject, $target),
);
}
}
8 changes: 8 additions & 0 deletions src/Test/Builder/AssertionStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPat\Rule\Assertion\Declaration\ShouldNotBeAbstract\ShouldNotBeAbstract;
use PHPat\Rule\Assertion\Declaration\ShouldNotBeFinal\ShouldNotBeFinal;
use PHPat\Rule\Assertion\Relation\CanOnlyDepend\CanOnlyDepend;
use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ShouldApplyAttribute;
use PHPat\Rule\Assertion\Relation\ShouldExtend\ShouldExtend;
use PHPat\Rule\Assertion\Relation\ShouldImplement\ShouldImplement;
use PHPat\Rule\Assertion\Relation\ShouldNotConstruct\ShouldNotConstruct;
Expand Down Expand Up @@ -108,4 +109,11 @@ public function shouldHaveOnlyOnePublicMethod(): Rule

return new BuildStep($this->rule);
}

public function shouldApplyAttribute(): TargetStep
{
$this->rule->assertion = ShouldApplyAttribute::class;

return new TargetStep($this->rule);
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/Simple/SimpleAttributeTwo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\fixtures\Simple;

#[\Attribute(\Attribute::TARGET_CLASS)]
class SimpleAttributeTwo {}
49 changes: 49 additions & 0 deletions tests/unit/rules/ShouldApplyAttribute/ClassAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldApplyAttribute;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ClassAttributeRule;
use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ShouldApplyAttribute;
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\SimpleAttributeTwo;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<ClassAttributeRule>
* @internal
* @coversNothing
*/
class ClassAttributeTest extends RuleTestCase
{
public const RULE_NAME = 'test_FixtureClassShouldApplySimpleAttributeTwo';

public function testRule(): void
{
$this->analyse(['tests/fixtures/FixtureClass.php'], [
[sprintf('%s should apply the attribute %s', FixtureClass::class, SimpleAttributeTwo::class), 29],
]);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ShouldApplyAttribute::class,
[new Classname(FixtureClass::class, false)],
[new Classname(SimpleAttributeTwo::class, false)]
);

return new ClassAttributeRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
$this->createReflectionProvider(),
self::getContainer()->getByType(FileTypeMapper::class)
);
}
}
49 changes: 49 additions & 0 deletions tests/unit/rules/ShouldApplyAttribute/SimpleClassAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldApplyAttribute;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ClassAttributeRule;
use PHPat\Rule\Assertion\Relation\ShouldApplyAttribute\ShouldApplyAttribute;
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\Simple\SimpleAttribute;
use Tests\PHPat\fixtures\Simple\SimpleClass;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<ClassAttributeRule>
* @internal
* @coversNothing
*/
class SimpleClassAttributeTest extends RuleTestCase
{
public const RULE_NAME = 'test_SimpleClassShouldApplySimpleAttribute';

public function testRule(): void
{
$this->analyse(['tests/fixtures/Simple/SimpleClass.php'], [
[sprintf('%s should apply the attribute %s', SimpleClass::class, SimpleAttribute::class), 5],
]);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ShouldApplyAttribute::class,
[new Classname(SimpleClass::class, false)],
[new Classname(SimpleAttribute::class, false)]
);

return new ClassAttributeRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
$this->createReflectionProvider(),
self::getContainer()->getByType(FileTypeMapper::class)
);
}
}
Loading