Skip to content

Commit

Permalink
Rules use RuleErrorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed Nov 13, 2024
1 parent edb0ad3 commit 9dce5c1
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/ReflectionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
private readonly TypeAwareNodeFinder $typeAwareNodeFinder
) {
$parserFactory = new ParserFactory();
$this->parser = $parserFactory->create(ParserFactory::PREFER_PHP7);
$this->parser = $parserFactory->createForNewestSupportedVersion();
}

public function parseMethodReflection(ReflectionMethod|MethodReflection $reflectionMethod): ?ClassMethod
Expand Down
1 change: 0 additions & 1 deletion src/ReturnTypeExtension/NodeGetAttributeTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
Expand Down
8 changes: 3 additions & 5 deletions src/Rules/AnnotateRegexClassConstWithRegexLinkRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<ClassConst>
* @see \Symplify\PHPStanRules\Tests\Rules\AnnotateRegexClassConstWithRegexLinkRule\AnnotateRegexClassConstWithRegexLinkRuleTest
*/
final class AnnotateRegexClassConstWithRegexLinkRule implements Rule, DocumentedRuleInterface
Expand All @@ -39,10 +41,6 @@ public function getNodeType(): string
return ClassConst::class;
}

/**
* @param ClassConst $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (count($node->consts) !== 1) {
Expand All @@ -69,7 +67,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];
}

public function getRuleDefinition(): RuleDefinition
Expand Down
8 changes: 3 additions & 5 deletions src/Rules/CheckClassNamespaceFollowPsr4Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Composer\ClassNamespaceMatcher;
use Symplify\PHPStanRules\Composer\ComposerAutoloadResolver;
use Symplify\PHPStanRules\Composer\Psr4PathValidator;
Expand All @@ -17,6 +18,7 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<ClassLike>
* @see \Symplify\PHPStanRules\Tests\Rules\CheckClassNamespaceFollowPsr4Rule\CheckClassNamespaceFollowPsr4RuleTest
*/
final class CheckClassNamespaceFollowPsr4Rule implements Rule, DocumentedRuleInterface
Expand Down Expand Up @@ -47,10 +49,6 @@ public function getNodeType(): string
return ClassLike::class;
}

/**
* @param ClassLike $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->autoloadPsr4Paths === []) {
Expand Down Expand Up @@ -87,7 +85,7 @@ public function processNode(Node $node, Scope $scope): array
$namespacePart = substr($namespaceBeforeClass, 0, -1);
$errorMessage = sprintf(self::ERROR_MESSAGE, $namespacePart);

return [$errorMessage];
return [RuleErrorBuilder::message($errorMessage)->build()];
}

public function getRuleDefinition(): RuleDefinition
Expand Down
7 changes: 2 additions & 5 deletions src/Rules/CheckRequiredInterfaceInContractNamespaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -38,10 +39,6 @@ public function getNodeType(): string
return Interface_::class;
}

/**
* @param Interface_ $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
$namespace = $scope->getNamespace();
Expand All @@ -53,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];
}

public function getRuleDefinition(): RuleDefinition
Expand Down
7 changes: 4 additions & 3 deletions src/Rules/ClassNameRespectsParentSuffixRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\TestCase;
use Rector\Rector\AbstractRector;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand Down Expand Up @@ -72,7 +74,6 @@ public function getNodeType(): string

/**
* @param InClassNode $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -117,7 +118,7 @@ class SomeCommand extends Command
}

/**
* @return array<int, string>
* @return list<RuleError>
*/
private function processClassNameAndShort(ClassReflection $classReflection): array
{
Expand All @@ -132,7 +133,7 @@ private function processClassNameAndShort(ClassReflection $classReflection): arr
}

$errorMessage = sprintf(self::ERROR_MESSAGE, $expectedSuffix);
return [$errorMessage];
return [RuleErrorBuilder::message($errorMessage)->build()];
}

return [];
Expand Down
7 changes: 2 additions & 5 deletions src/Rules/ForbiddenExtendOfNonAbstractClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<InClassNode>
* @see \Symplify\PHPStanRules\Tests\Rules\ForbiddenExtendOfNonAbstractClassRule\ForbiddenExtendOfNonAbstractClassRuleTest
*/
final class ForbiddenExtendOfNonAbstractClassRule implements Rule, DocumentedRuleInterface
Expand All @@ -31,10 +32,6 @@ public function getNodeType(): string
return InClassNode::class;
}

/**
* @param InClassNode $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $node->getClassReflection();
Expand Down Expand Up @@ -63,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [self::ERROR_MESSAGE];
return [RuleNodeBuilder::message(self::ERROR_MESSAGE)->build()];
}

public function getRuleDefinition(): RuleDefinition
Expand Down
6 changes: 2 additions & 4 deletions src/Rules/NoGlobalConstRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\Const_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -28,12 +29,9 @@ public function getNodeType(): string
return Const_::class;
}

/**
* @param Const_ $node
*/
public function processNode(Node $node, Scope $scope): array
{
return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];
}

public function getRuleDefinition(): RuleDefinition
Expand Down

0 comments on commit 9dce5c1

Please sign in to comment.