Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.6.x' into 2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 20, 2024
2 parents 90006f2 + daeec74 commit e227ffd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ services:
class: PHPStan\Rules\Cast\UselessCastRule
arguments:
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%

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

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

return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};
return [
$addTip(RuleErrorBuilder::message(sprintf(
Expand Down
10 changes: 7 additions & 3 deletions src/Rules/Functions/ArrayFilterStrictRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <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 @@ -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
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 @@ -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
Expand Down

0 comments on commit e227ffd

Please sign in to comment.