Skip to content

Commit

Permalink
Remove checkAlwaysTrue* options
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 24, 2024
1 parent 59fd06a commit d22c481
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 167 deletions.
9 changes: 9 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ parameters:
identifier: missingType.generics
```

### Removed `checkAlwaysTrue*` options

These options have been removed because PHPStan now always behaves as if these were set to `true`:

* `checkAlwaysTrueCheckTypeFunctionCall`
* `checkAlwaysTrueInstanceof`
* `checkAlwaysTrueStrictComparison`
* `checkAlwaysTrueLooseComparison`

### Removed option `excludes_analyse`

It has been replaced with [`excludePaths`](https://phpstan.org/user-guide/ignoring-errors#excluding-whole-files).
Expand Down
7 changes: 0 additions & 7 deletions conf/config.level4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ services:
-
class: PHPStan\Rules\Classes\ImpossibleInstanceOfRule
arguments:
checkAlwaysTrueInstanceof: %checkAlwaysTrueInstanceof%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
Expand Down Expand Up @@ -157,7 +156,6 @@ services:
-
class: PHPStan\Rules\Comparison\ImpossibleCheckTypeFunctionCallRule
arguments:
checkAlwaysTrueCheckTypeFunctionCall: %checkAlwaysTrueCheckTypeFunctionCall%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
Expand All @@ -167,7 +165,6 @@ services:
-
class: PHPStan\Rules\Comparison\ImpossibleCheckTypeMethodCallRule
arguments:
checkAlwaysTrueCheckTypeFunctionCall: %checkAlwaysTrueCheckTypeFunctionCall%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
Expand All @@ -177,7 +174,6 @@ services:
-
class: PHPStan\Rules\Comparison\ImpossibleCheckTypeStaticMethodCallRule
arguments:
checkAlwaysTrueCheckTypeFunctionCall: %checkAlwaysTrueCheckTypeFunctionCall%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
Expand All @@ -196,7 +192,6 @@ services:
-
class: PHPStan\Rules\Comparison\MatchExpressionRule
arguments:
checkAlwaysTrueStrictComparison: %checkAlwaysTrueStrictComparison%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
tags:
Expand All @@ -213,7 +208,6 @@ services:
-
class: PHPStan\Rules\Comparison\StrictComparisonOfDifferentTypesRule
arguments:
checkAlwaysTrueStrictComparison: %checkAlwaysTrueStrictComparison%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
Expand All @@ -223,7 +217,6 @@ services:
-
class: PHPStan\Rules\Comparison\ConstantLooseComparisonRule
arguments:
checkAlwaysTrueLooseComparison: %checkAlwaysTrueLooseComparison%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
treatPhpDocTypesAsCertainTip: %tips.treatPhpDocTypesAsCertain%
Expand Down
4 changes: 0 additions & 4 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ parameters:
fileExtensions:
- php
checkAdvancedIsset: false
checkAlwaysTrueCheckTypeFunctionCall: true
checkAlwaysTrueInstanceof: true
checkAlwaysTrueStrictComparison: true
checkAlwaysTrueLooseComparison: true
reportAlwaysTrueInLastCondition: false
checkClassCaseSensitivity: false
checkExplicitMixed: false
Expand Down
4 changes: 0 additions & 4 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ parametersSchema:
])
fileExtensions: listOf(string())
checkAdvancedIsset: bool()
checkAlwaysTrueCheckTypeFunctionCall: bool()
checkAlwaysTrueInstanceof: bool()
checkAlwaysTrueStrictComparison: bool()
checkAlwaysTrueLooseComparison: bool()
reportAlwaysTrueInLastCondition: bool()
checkClassCaseSensitivity: bool()
checkExplicitMixed: bool()
Expand Down
35 changes: 16 additions & 19 deletions src/Rules/Classes/ImpossibleInstanceOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ final class ImpossibleInstanceOfRule implements Rule
{

public function __construct(
private bool $checkAlwaysTrueInstanceof,
private bool $treatPhpDocTypesAsCertain,
private bool $reportAlwaysTrueInLastCondition,
private bool $treatPhpDocTypesAsCertainTip,
Expand Down Expand Up @@ -89,28 +88,26 @@ public function processNode(Node $node, Scope $scope): array
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
)))->identifier('instanceof.alwaysFalse')->build(),
];
} elseif ($this->checkAlwaysTrueInstanceof) {
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s will always evaluate to true.',
$exprType->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
}

$errorBuilder->identifier('instanceof.alwaysTrue');
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

return [$errorBuilder->build()];
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s will always evaluate to true.',
$exprType->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}

return [];
$errorBuilder->identifier('instanceof.alwaysTrue');

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

}
35 changes: 16 additions & 19 deletions src/Rules/Comparison/ConstantLooseComparisonRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class ConstantLooseComparisonRule implements Rule
{

public function __construct(
private bool $checkAlwaysTrueLooseComparison,
private bool $treatPhpDocTypesAsCertain,
private bool $reportAlwaysTrueInLastCondition,
private bool $treatPhpDocTypesAsCertainTip,
Expand Down Expand Up @@ -67,28 +66,26 @@ public function processNode(Node $node, Scope $scope): array
$scope->getType($node->right)->describe(VerbosityLevel::value()),
)))->identifier(sprintf('%s.alwaysFalse', $node instanceof Node\Expr\BinaryOp\Equal ? 'equal' : 'notEqual'))->build(),
];
} elseif ($this->checkAlwaysTrueLooseComparison) {
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to true.',
$node->getOperatorSigil(),
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
}

$errorBuilder->identifier(sprintf('%s.alwaysTrue', $node instanceof Node\Expr\BinaryOp\Equal ? 'equal' : 'notEqual'));
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

return [$errorBuilder->build()];
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to true.',
$node->getOperatorSigil(),
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}

return [];
$errorBuilder->identifier(sprintf('%s.alwaysTrue', $node instanceof Node\Expr\BinaryOp\Equal ? 'equal' : 'notEqual'));

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

}
33 changes: 15 additions & 18 deletions src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class ImpossibleCheckTypeFunctionCallRule implements Rule

public function __construct(
private ImpossibleCheckTypeHelper $impossibleCheckTypeHelper,
private bool $checkAlwaysTrueCheckTypeFunctionCall,
private bool $treatPhpDocTypesAsCertain,
private bool $reportAlwaysTrueInLastCondition,
private bool $treatPhpDocTypesAsCertainTip,
Expand Down Expand Up @@ -70,27 +69,25 @@ public function processNode(Node $node, Scope $scope): array
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)))->identifier('function.impossibleType')->build(),
];
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) {
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Call to function %s()%s will always evaluate to true.',
$functionName,
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
}

$errorBuilder->identifier('function.alreadyNarrowedType');
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

return [$errorBuilder->build()];
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Call to function %s()%s will always evaluate to true.',
$functionName,
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}

return [];
$errorBuilder->identifier('function.alreadyNarrowedType');

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

}
37 changes: 17 additions & 20 deletions src/Rules/Comparison/ImpossibleCheckTypeMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class ImpossibleCheckTypeMethodCallRule implements Rule

public function __construct(
private ImpossibleCheckTypeHelper $impossibleCheckTypeHelper,
private bool $checkAlwaysTrueCheckTypeFunctionCall,
private bool $treatPhpDocTypesAsCertain,
private bool $reportAlwaysTrueInLastCondition,
private bool $treatPhpDocTypesAsCertainTip,
Expand Down Expand Up @@ -70,29 +69,27 @@ public function processNode(Node $node, Scope $scope): array
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)))->identifier('method.impossibleType')->build(),
];
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) {
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

$method = $this->getMethod($node->var, $node->name->name, $scope);
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Call to method %s::%s()%s will always evaluate to true.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
}

$errorBuilder->identifier('method.alreadyNarrowedType');
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

return [$errorBuilder->build()];
$method = $this->getMethod($node->var, $node->name->name, $scope);
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Call to method %s::%s()%s will always evaluate to true.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}

return [];
$errorBuilder->identifier('method.alreadyNarrowedType');

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

private function getMethod(
Expand Down
37 changes: 17 additions & 20 deletions src/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class ImpossibleCheckTypeStaticMethodCallRule implements Rule

public function __construct(
private ImpossibleCheckTypeHelper $impossibleCheckTypeHelper,
private bool $checkAlwaysTrueCheckTypeFunctionCall,
private bool $treatPhpDocTypesAsCertain,
private bool $reportAlwaysTrueInLastCondition,
private bool $treatPhpDocTypesAsCertainTip,
Expand Down Expand Up @@ -71,29 +70,27 @@ public function processNode(Node $node, Scope $scope): array
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)))->identifier('staticMethod.impossibleType')->build(),
];
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) {
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

$method = $this->getMethod($node->class, $node->name->name, $scope);
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Call to static method %s::%s()%s will always evaluate to true.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
}

$errorBuilder->identifier('staticMethod.alreadyNarrowedType');
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

return [$errorBuilder->build()];
$method = $this->getMethod($node->class, $node->name->name, $scope);
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Call to static method %s::%s()%s will always evaluate to true.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}

return [];
$errorBuilder->identifier('staticMethod.alreadyNarrowedType');

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

/**
Expand Down
Loading

0 comments on commit d22c481

Please sign in to comment.