Skip to content

Check if treatPhpDocTypesAsCertain tip is enabled #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading