Skip to content
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
30 changes: 24 additions & 6 deletions src/Rules/Comparison/BooleanAndConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Comparison;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Analyser\CollectedDataEmitter;
use PHPStan\Analyser\NodeCallbackInvoker;
use PHPStan\Analyser\Scope;
Expand All @@ -27,6 +28,7 @@ public function __construct(
private ConstantConditionRuleHelper $helper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
private FunctionCallConstantConditionHelper $functionCallConstantConditionHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
Expand Down Expand Up @@ -87,16 +89,18 @@ public function processNode(
}
$ruleError = $errorBuilder->build();
$hasLeftOrRightError = true;
if ($isInTrait) {
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($originalNode->left)) {
$this->functionCallConstantConditionHelper->emitFunctionCallError(self::class, $scope, $originalNode->left, $leftType->getValue(), $ruleError);
} elseif ($isInTrait) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $originalNode->left, $leftType->getValue(), $ruleError);
} else {
$errors[] = $ruleError;
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->left);
$this->emitNoError($scope, $originalNode->left);
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->left);
$this->emitNoError($scope, $originalNode->left);
}

$rightScope = $node->getRightScope();
Expand Down Expand Up @@ -140,16 +144,18 @@ public function processNode(
}
$ruleError = $errorBuilder->build();
$hasLeftOrRightError = true;
if ($isInTrait) {
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($originalNode->right)) {
$this->functionCallConstantConditionHelper->emitFunctionCallError(self::class, $scope, $originalNode->right, $rightType->getValue(), $ruleError);
} elseif ($isInTrait) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $originalNode->right, $rightType->getValue(), $ruleError);
} else {
$errors[] = $ruleError;
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->right);
$this->emitNoError($scope, $originalNode->right);
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->right);
$this->emitNoError($scope, $originalNode->right);
}

if (count($errors) === 0 && !$hasLeftOrRightError && !$scope->isInFirstLevelStatement()) {
Expand Down Expand Up @@ -203,4 +209,16 @@ public function processNode(
return $errors;
}

private function emitNoError(
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Expr $expr,
): void
{
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($expr)) {
$this->functionCallConstantConditionHelper->emitFunctionCallNoError(self::class, $scope, $expr);
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $expr);
}
}

}
11 changes: 10 additions & 1 deletion src/Rules/Comparison/BooleanNotConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
private ConstantConditionRuleHelper $helper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
private FunctionCallConstantConditionHelper $functionCallConstantConditionHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
Expand Down Expand Up @@ -78,6 +79,10 @@ public function processNode(
$errorBuilder->identifier(sprintf('booleanNot.always%s', $exprType->getValue() ? 'False' : 'True'));

$ruleError = $errorBuilder->build();
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($node->expr)) {
$this->functionCallConstantConditionHelper->emitFunctionCallError(self::class, $scope, $node->expr, !$exprType->getValue(), $ruleError);
return [];
}
if ($scope->isInTrait()) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node->expr, !$exprType->getValue(), $ruleError);
return [];
Expand All @@ -87,7 +92,11 @@ public function processNode(
}
}

$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node->expr);
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($node->expr)) {
$this->functionCallConstantConditionHelper->emitFunctionCallNoError(self::class, $scope, $node->expr);
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node->expr);
}
return [];
}

Expand Down
30 changes: 24 additions & 6 deletions src/Rules/Comparison/BooleanOrConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Comparison;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Analyser\CollectedDataEmitter;
use PHPStan\Analyser\NodeCallbackInvoker;
use PHPStan\Analyser\Scope;
Expand All @@ -27,6 +28,7 @@ public function __construct(
private ConstantConditionRuleHelper $helper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
private FunctionCallConstantConditionHelper $functionCallConstantConditionHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
Expand Down Expand Up @@ -87,16 +89,18 @@ public function processNode(
}
$ruleError = $errorBuilder->build();
$hasLeftOrRightError = true;
if ($isInTrait) {
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($originalNode->left)) {
$this->functionCallConstantConditionHelper->emitFunctionCallError(self::class, $scope, $originalNode->left, $leftType->getValue(), $ruleError);
} elseif ($isInTrait) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $originalNode->left, $leftType->getValue(), $ruleError);
} else {
$messages[] = $ruleError;
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->left);
$this->emitNoError($scope, $originalNode->left);
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->left);
$this->emitNoError($scope, $originalNode->left);
}

$rightScope = $node->getRightScope();
Expand Down Expand Up @@ -140,16 +144,18 @@ public function processNode(
}
$ruleError = $errorBuilder->build();
$hasLeftOrRightError = true;
if ($isInTrait) {
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($originalNode->right)) {
$this->functionCallConstantConditionHelper->emitFunctionCallError(self::class, $scope, $originalNode->right, $rightType->getValue(), $ruleError);
} elseif ($isInTrait) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $originalNode->right, $rightType->getValue(), $ruleError);
} else {
$messages[] = $ruleError;
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->right);
$this->emitNoError($scope, $originalNode->right);
}
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $originalNode->right);
$this->emitNoError($scope, $originalNode->right);
}

if (count($messages) === 0 && !$hasLeftOrRightError && !$scope->isInFirstLevelStatement()) {
Expand Down Expand Up @@ -203,4 +209,16 @@ public function processNode(
return $messages;
}

private function emitNoError(
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Expr $expr,
): void
{
if ($this->functionCallConstantConditionHelper->isTypeCheckCandidate($expr)) {
$this->functionCallConstantConditionHelper->emitFunctionCallNoError(self::class, $scope, $expr);
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $expr);
}
}

}
11 changes: 7 additions & 4 deletions src/Rules/Comparison/ConstantConditionInTraitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function __construct(
{
}

private function exprString(Expr $expr): string
{
return sprintf('%s:%d', $this->exprPrinter->printExpr($expr), $expr->getStartLine());
}

/**
* @param class-string<Rule<covariant Node>> $ruleName
*/
Expand All @@ -40,11 +45,10 @@ public function emitNoError(
return;
}

$exprString = sprintf('%s:%d', $this->exprPrinter->printExpr($expr), $expr->getStartLine());
$scope->emitCollectedData(ConstantConditionInTraitCollector::class, [
$ruleName,
$scope->getTraitReflection()->getName(),
$exprString,
$this->exprString($expr),
null,
]);
}
Expand All @@ -68,11 +72,10 @@ public function emitError(
return;
}

$exprString = sprintf('%s:%d', $this->exprPrinter->printExpr($expr), $expr->getStartLine());
$scope->emitCollectedData(ConstantConditionInTraitCollector::class, [
$ruleName,
$scope->getTraitReflection()->getName(),
$exprString,
$this->exprString($expr),
$value,
$this->ruleErrorTransformer->transform($ruleError, $scope, [], $expr),
]);
Expand Down
22 changes: 3 additions & 19 deletions src/Rules/Comparison/ConstantConditionRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace PHPStan\Rules\Comparison;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
Expand All @@ -15,14 +13,13 @@ final class ConstantConditionRuleHelper
{

public function __construct(
private ImpossibleCheckTypeHelper $impossibleCheckTypeHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
)
{
}

private function shouldSkip(Scope $scope, Expr $expr): bool
private function shouldSkip(Expr $expr): bool
{
if (
$expr instanceof Expr\BinaryOp\Equal
Expand Down Expand Up @@ -50,25 +47,12 @@ private function shouldSkip(Scope $scope, Expr $expr): bool
return true;
}

if (
(
$expr instanceof FuncCall
|| $expr instanceof MethodCall
|| $expr instanceof Expr\StaticCall
) && !$expr->isFirstClassCallable()
) {
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $expr);
if ($isAlways !== null) {
return true;
}
}

return false;
}

public function getBooleanType(Scope $scope, Expr $expr): BooleanType
{
if ($this->shouldSkip($scope, $expr)) {
if ($this->shouldSkip($expr)) {
return new BooleanType();
}

Expand All @@ -81,7 +65,7 @@ public function getBooleanType(Scope $scope, Expr $expr): BooleanType

public function getNativeBooleanType(Scope $scope, Expr $expr): BooleanType
{
if ($this->shouldSkip($scope, $expr)) {
if ($this->shouldSkip($expr)) {
return new BooleanType();
}

Expand Down
30 changes: 25 additions & 5 deletions src/Rules/Comparison/DoWhileLoopConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Comparison;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
Expand All @@ -28,6 +29,7 @@ public function __construct(
private ConstantConditionRuleHelper $helper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
private FunctionCallConstantConditionHelper $functionCallConstantConditionHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')]
Expand All @@ -44,31 +46,32 @@ public function getNodeType(): string
public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array
{
$exprType = $this->helper->getBooleanType($scope, $node->getCond());
$isTypeCheckCandidate = $this->functionCallConstantConditionHelper->isTypeCheckCandidate($node->getCond());
if ($exprType instanceof ConstantBooleanType) {
if ($exprType->getValue()) {
if ($node->hasYield()) {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node->getCond());
$this->emitNoError($scope, $node->getCond(), $isTypeCheckCandidate);
return [];
}
foreach ($node->getExitPoints() as $exitPoint) {
$statement = $exitPoint->getStatement();
if (!$statement instanceof Continue_) {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node->getCond());
$this->emitNoError($scope, $node->getCond(), $isTypeCheckCandidate);
return [];
}
if (!$statement->num instanceof Int_) {
continue;
}
if ($statement->num->value > 1) {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node->getCond());
$this->emitNoError($scope, $node->getCond(), $isTypeCheckCandidate);
return [];
}
}
} else {
foreach ($node->getExitPoints() as $exitPoint) {
$statement = $exitPoint->getStatement();
if ($statement instanceof Break_) {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node->getCond());
$this->emitNoError($scope, $node->getCond(), $isTypeCheckCandidate);
return [];
}
}
Expand Down Expand Up @@ -99,6 +102,10 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
->line($node->getCond()->getStartLine())
->identifier(sprintf('doWhile.always%s', $exprType->getValue() ? 'True' : 'False'))
->build();
if ($isTypeCheckCandidate) {
$this->functionCallConstantConditionHelper->emitFunctionCallError(self::class, $scope, $node->getCond(), $exprType->getValue(), $ruleError);
return [];
}
if ($scope->isInTrait()) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node->getCond(), $exprType->getValue(), $ruleError);
return [];
Expand All @@ -107,8 +114,21 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
return [$ruleError];
}

$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node->getCond());
$this->emitNoError($scope, $node->getCond(), $isTypeCheckCandidate);
return [];
}

private function emitNoError(
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Expr $cond,
bool $isTypeCheckCandidate,
): void
{
if ($isTypeCheckCandidate) {
$this->functionCallConstantConditionHelper->emitFunctionCallNoError(self::class, $scope, $cond);
} else {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $cond);
}
}

}
Loading
Loading