Skip to content

Commit daeec74

Browse files
ruudkondrejmirtes
authored andcommitted
Check if treatPhpDocTypesAsCertain tip is enabled
See phpstan/phpstan-src#3452
1 parent 876574c commit daeec74

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

rules.neon

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ services:
170170
class: PHPStan\Rules\Cast\UselessCastRule
171171
arguments:
172172
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
173+
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
173174

174175
-
175176
class: PHPStan\Rules\Classes\RequireParentConstructCallRule
@@ -197,6 +198,7 @@ services:
197198
arguments:
198199
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
199200
checkNullables: %checkNullables%
201+
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
200202

201203
-
202204
class: PHPStan\Rules\Functions\ClosureUsesThisRule

src/Rules/Cast/UselessCastRule.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ class UselessCastRule implements Rule
2121
/** @var bool */
2222
private $treatPhpDocTypesAsCertain;
2323

24-
public function __construct(bool $treatPhpDocTypesAsCertain)
24+
/** @var bool */
25+
private $treatPhpDocTypesAsCertainTip;
26+
27+
public function __construct(
28+
bool $treatPhpDocTypesAsCertain,
29+
bool $treatPhpDocTypesAsCertainTip
30+
)
2531
{
2632
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
33+
$this->treatPhpDocTypesAsCertainTip = $treatPhpDocTypesAsCertainTip;
2734
}
2835

2936
public function getNodeType(): string
@@ -55,7 +62,11 @@ public function processNode(Node $node, Scope $scope): array
5562
return $ruleErrorBuilder;
5663
}
5764

58-
return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
65+
if (!$this->treatPhpDocTypesAsCertainTip) {
66+
return $ruleErrorBuilder;
67+
}
68+
69+
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
5970
};
6071
return [
6172
$addTip(RuleErrorBuilder::message(sprintf(

src/Rules/Functions/ArrayFilterStrictRule.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ class ArrayFilterStrictRule implements Rule
3232
/** @var bool */
3333
private $checkNullables;
3434

35+
/** @var bool */
36+
private $treatPhpDocTypesAsCertainTip;
37+
3538
public function __construct(
3639
ReflectionProvider $reflectionProvider,
3740
bool $treatPhpDocTypesAsCertain,
38-
bool $checkNullables
41+
bool $checkNullables,
42+
bool $treatPhpDocTypesAsCertainTip
3943
)
4044
{
4145
$this->reflectionProvider = $reflectionProvider;
4246
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
4347
$this->checkNullables = $checkNullables;
48+
$this->treatPhpDocTypesAsCertainTip = $treatPhpDocTypesAsCertainTip;
4449
}
4550

4651
public function getNodeType(): string
@@ -135,8 +140,8 @@ public function processNode(Node $node, Scope $scope): array
135140
$callbackType->describe(VerbosityLevel::typeOnly())
136141
))->identifier('arrayFilter.strict');
137142

138-
if (!$this->isCallbackTypeNull($nativeCallbackType) && $this->treatPhpDocTypesAsCertain) {
139-
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
143+
if ($this->treatPhpDocTypesAsCertainTip && !$this->isCallbackTypeNull($nativeCallbackType) && $this->treatPhpDocTypesAsCertain) {
144+
$errorBuilder->treatPhpDocTypesAsCertainTip();
140145
}
141146

142147
return [$errorBuilder->build()];

tests/Rules/Cast/UselessCastRuleTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class UselessCastRuleTest extends RuleTestCase
1616

1717
protected function getRule(): Rule
1818
{
19-
return new UselessCastRule($this->treatPhpDocTypesAsCertain);
19+
return new UselessCastRule(
20+
$this->treatPhpDocTypesAsCertain,
21+
true
22+
);
2023
}
2124

2225
protected function shouldTreatPhpDocTypesAsCertain(): bool

tests/Rules/Functions/ArrayFilterStrictRuleTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ class ArrayFilterStrictRuleTest extends RuleTestCase
1919

2020
protected function getRule(): Rule
2121
{
22-
return new ArrayFilterStrictRule($this->createReflectionProvider(), $this->treatPhpDocTypesAsCertain, $this->checkNullables);
22+
return new ArrayFilterStrictRule(
23+
$this->createReflectionProvider(),
24+
$this->treatPhpDocTypesAsCertain,
25+
$this->checkNullables,
26+
true
27+
);
2328
}
2429

2530
protected function shouldTreatPhpDocTypesAsCertain(): bool

0 commit comments

Comments
 (0)