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 dc786eb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 35 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
17 changes: 17 additions & 0 deletions src/Rule/Assertion/Declaration/ShouldNotExist/ExistsRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace PHPat\Rule\Assertion\Declaration\ShouldNotExist;

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

/**
* @implements Rule<InClassNode>
*/
final class ExistsRule extends ShouldNotExist implements Rule
{
use ExistsExtractor;
}
11 changes: 7 additions & 4 deletions src/Rule/Assertion/Declaration/ShouldNotExist/ShouldNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,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.

25 changes: 25 additions & 0 deletions src/Rule/Extractor/Declaration/ExistsExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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 @@ -4,7 +4,7 @@

use PHPat\Configuration;
use PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExist;
use PHPat\Rule\Assertion\Declaration\ShouldNotExist\ShouldNotExistRule;
use PHPat\Rule\Assertion\Declaration\ShouldNotExist\ExistsRule;
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 dc786eb

Please sign in to comment.