Skip to content

Commit

Permalink
Check if treatPhpDocTypesAsCertain tip is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk authored and ondrejmirtes committed Sep 20, 2024
1 parent 876574c commit daeec74
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ services:
class: PHPStan\Rules\Cast\UselessCastRule
arguments:
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%

-
class: PHPStan\Rules\Classes\RequireParentConstructCallRule
Expand Down Expand Up @@ -197,6 +198,7 @@ services:
arguments:
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
checkNullables: %checkNullables%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%

-
class: PHPStan\Rules\Functions\ClosureUsesThisRule
Expand Down
15 changes: 13 additions & 2 deletions src/Rules/Cast/UselessCastRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
if (!$this->treatPhpDocTypesAsCertainTip) {
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};
return [
$addTip(RuleErrorBuilder::message(sprintf(
Expand Down
11 changes: 8 additions & 3 deletions src/Rules/Functions/ArrayFilterStrictRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
if ($this->treatPhpDocTypesAsCertainTip && !$this->isCallbackTypeNull($nativeCallbackType) && $this->treatPhpDocTypesAsCertain) {
$errorBuilder->treatPhpDocTypesAsCertainTip();
}

return [$errorBuilder->build()];
Expand Down
5 changes: 4 additions & 1 deletion tests/Rules/Cast/UselessCastRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tests/Rules/Functions/ArrayFilterStrictRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit daeec74

Please sign in to comment.