Skip to content

Commit

Permalink
[should-not-exist-rule] rename class and move functions to extractor …
Browse files Browse the repository at this point in the history
…trait
  • Loading branch information
annadamm-check24 committed Jul 1, 2024
1 parent 11dc221 commit c231866
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docs/documentation/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ It asserts that the selected classes **do not depend** on the target classes.
It asserts that the selected classes **do not use the constructor** of the target classes.

## shouldNotExist()
It asserts that the selected classes **should not exist**.
It asserts that the selected classes **do not exist**.

## shouldApplyAttribute()
It asserts that the selected classes **apply** the target attributes.
Expand Down
2 changes: 1 addition & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ services:

# ShouldNotExist rules
-
class: PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExistRule
class: PHPat\Rule\Assertion\Declaration\ShouldNotExist\ExistsRule
tags:
- phpstan.rules.rule

Expand Down
15 changes: 15 additions & 0 deletions src/Rule/Assertion/Declaration/ShouldNotExist/ExistsRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldNotExist;

use PHPat\Rule\Extractor\Declaration\ExistsExtractor;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;

/**
* @implements Rule<InClassNode>
*/
final class ExistsRule extends ShouldNotExist implements Rule
{
use ExistsExtractor;
}
12 changes: 7 additions & 5 deletions src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPat\Rule\Assertion\Declaration\DeclarationAssertion;
use PHPat\Rule\Assertion\Declaration\ValidationTrait;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\FileTypeMapper;
Expand All @@ -30,13 +29,16 @@ public function __construct(
);
}

public function getNodeType(): string
protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips, array $params = []): array
{
return InClassNode::class;
return $this->applyShouldNot($ruleName, $subject, $meetsDeclaration, $tips, $params);
}

protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips, array $params = []): array
protected function getMessage(string $ruleName, string $subject, array $params = []): string
{
return $this->applyShouldNot($ruleName, $subject, $meetsDeclaration, $tips, $params);
return $this->prepareMessage(
$ruleName,
sprintf('%s should not exist', $subject)
);
}
}

This file was deleted.

23 changes: 23 additions & 0 deletions src/Rule/Extractor/Declaration/ExistsExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace PHPat\Rule\Extractor\Declaration;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;

trait ExistsExtractor
{
public function getNodeType(): string
{
return InClassNode::class;
}

/**
* @param InClassNode $node
*/
protected function meetsDeclaration(Node $node, Scope $scope, array $params = []): bool
{
return true;
}
}
4 changes: 2 additions & 2 deletions tests/unit/rules/ShouldNotExist/ShouldNotExistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Tests\PHPat\unit\rules\ShouldNotExist;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Declaration\ShouldNotExist\ExistsRule;
use PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExist;
use PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExistRule;
use PHPat\Selector\Classname;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -38,7 +38,7 @@ protected function getRule(): Rule
[]
);

return new ShouldNotExistRule(
return new ExistsRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
$this->createReflectionProvider(),
Expand Down

0 comments on commit c231866

Please sign in to comment.