From daeec748b53de80a97498462513066834ec28f8b Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Thu, 19 Sep 2024 11:03:44 +0200 Subject: [PATCH] Check if treatPhpDocTypesAsCertain tip is enabled See https://github.com/phpstan/phpstan-src/pull/3452 --- rules.neon | 2 ++ src/Rules/Cast/UselessCastRule.php | 15 +++++++++++++-- src/Rules/Functions/ArrayFilterStrictRule.php | 11 ++++++++--- tests/Rules/Cast/UselessCastRuleTest.php | 5 ++++- .../Rules/Functions/ArrayFilterStrictRuleTest.php | 7 ++++++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/rules.neon b/rules.neon index e5194fdd..e9be35a2 100644 --- a/rules.neon +++ b/rules.neon @@ -170,6 +170,7 @@ services: class: PHPStan\Rules\Cast\UselessCastRule arguments: treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain% + treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain% - class: PHPStan\Rules\Classes\RequireParentConstructCallRule @@ -197,6 +198,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 adc20e14..99347cce 100644 --- a/src/Rules/Cast/UselessCastRule.php +++ b/src/Rules/Cast/UselessCastRule.php @@ -21,9 +21,16 @@ class UselessCastRule implements Rule /** @var bool */ private $treatPhpDocTypesAsCertain; - public function __construct(bool $treatPhpDocTypesAsCertain) + /** @var bool */ + private $treatPhpDocTypesAsCertainTip; + + public function __construct( + bool $treatPhpDocTypesAsCertain, + bool $treatPhpDocTypesAsCertainTip + ) { $this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain; + $this->treatPhpDocTypesAsCertainTip = $treatPhpDocTypesAsCertainTip; } public function getNodeType(): string @@ -55,7 +62,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 ff3bf631..59c05700 100644 --- a/src/Rules/Functions/ArrayFilterStrictRule.php +++ b/src/Rules/Functions/ArrayFilterStrictRule.php @@ -32,15 +32,20 @@ class ArrayFilterStrictRule implements Rule /** @var bool */ private $checkNullables; + /** @var bool */ + private $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 @@ -135,8 +140,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 21d82ae1..4671a3f6 100644 --- a/tests/Rules/Cast/UselessCastRuleTest.php +++ b/tests/Rules/Cast/UselessCastRuleTest.php @@ -16,7 +16,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 45ac87a7..4da28d21 100644 --- a/tests/Rules/Functions/ArrayFilterStrictRuleTest.php +++ b/tests/Rules/Functions/ArrayFilterStrictRuleTest.php @@ -19,7 +19,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