diff --git a/rules.neon b/rules.neon index 3332a30..49b1a1f 100644 --- a/rules.neon +++ b/rules.neon @@ -171,6 +171,7 @@ services: class: PHPStan\Rules\Cast\UselessCastRule arguments: treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain% + treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain% - class: PHPStan\Rules\Classes\RequireParentConstructCallRule @@ -198,6 +199,7 @@ services: arguments: treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain% checkNullables: %checkNullables% + treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain% - class: PHPStan\Rules\Functions\ClosureUsesThisRule diff --git a/src/Rules/Cast/UselessCastRule.php b/src/Rules/Cast/UselessCastRule.php index e2ea72b..ca8f226 100644 --- a/src/Rules/Cast/UselessCastRule.php +++ b/src/Rules/Cast/UselessCastRule.php @@ -20,9 +20,15 @@ class UselessCastRule implements Rule private bool $treatPhpDocTypesAsCertain; - public function __construct(bool $treatPhpDocTypesAsCertain) + private bool $treatPhpDocTypesAsCertainTip; + + public function __construct( + bool $treatPhpDocTypesAsCertain, + bool $treatPhpDocTypesAsCertainTip + ) { $this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain; + $this->treatPhpDocTypesAsCertainTip = $treatPhpDocTypesAsCertainTip; } public function getNodeType(): string @@ -54,7 +60,11 @@ public function processNode(Node $node, Scope $scope): array return $ruleErrorBuilder; } - return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.'); + if (!$this->treatPhpDocTypesAsCertainTip) { + return $ruleErrorBuilder; + } + + return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip(); }; return [ $addTip(RuleErrorBuilder::message(sprintf( diff --git a/src/Rules/Functions/ArrayFilterStrictRule.php b/src/Rules/Functions/ArrayFilterStrictRule.php index 65e1fad..6760c7d 100644 --- a/src/Rules/Functions/ArrayFilterStrictRule.php +++ b/src/Rules/Functions/ArrayFilterStrictRule.php @@ -29,15 +29,19 @@ class ArrayFilterStrictRule implements Rule private bool $checkNullables; + private bool $treatPhpDocTypesAsCertainTip; + public function __construct( ReflectionProvider $reflectionProvider, bool $treatPhpDocTypesAsCertain, - bool $checkNullables + bool $checkNullables, + bool $treatPhpDocTypesAsCertainTip ) { $this->reflectionProvider = $reflectionProvider; $this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain; $this->checkNullables = $checkNullables; + $this->treatPhpDocTypesAsCertainTip = $treatPhpDocTypesAsCertainTip; } public function getNodeType(): string @@ -132,8 +136,8 @@ public function processNode(Node $node, Scope $scope): array $callbackType->describe(VerbosityLevel::typeOnly()), ))->identifier('arrayFilter.strict'); - if (!$this->isCallbackTypeNull($nativeCallbackType) && $this->treatPhpDocTypesAsCertain) { - $errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.'); + if ($this->treatPhpDocTypesAsCertainTip && !$this->isCallbackTypeNull($nativeCallbackType) && $this->treatPhpDocTypesAsCertain) { + $errorBuilder->treatPhpDocTypesAsCertainTip(); } return [$errorBuilder->build()]; diff --git a/tests/Rules/Cast/UselessCastRuleTest.php b/tests/Rules/Cast/UselessCastRuleTest.php index ec8a02a..2a56ca6 100644 --- a/tests/Rules/Cast/UselessCastRuleTest.php +++ b/tests/Rules/Cast/UselessCastRuleTest.php @@ -15,7 +15,10 @@ class UselessCastRuleTest extends RuleTestCase protected function getRule(): Rule { - return new UselessCastRule($this->treatPhpDocTypesAsCertain); + return new UselessCastRule( + $this->treatPhpDocTypesAsCertain, + true, + ); } protected function shouldTreatPhpDocTypesAsCertain(): bool diff --git a/tests/Rules/Functions/ArrayFilterStrictRuleTest.php b/tests/Rules/Functions/ArrayFilterStrictRuleTest.php index b241219..4ef5448 100644 --- a/tests/Rules/Functions/ArrayFilterStrictRuleTest.php +++ b/tests/Rules/Functions/ArrayFilterStrictRuleTest.php @@ -17,7 +17,12 @@ class ArrayFilterStrictRuleTest extends RuleTestCase protected function getRule(): Rule { - return new ArrayFilterStrictRule($this->createReflectionProvider(), $this->treatPhpDocTypesAsCertain, $this->checkNullables); + return new ArrayFilterStrictRule( + $this->createReflectionProvider(), + $this->treatPhpDocTypesAsCertain, + $this->checkNullables, + true, + ); } protected function shouldTreatPhpDocTypesAsCertain(): bool