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
8 changes: 5 additions & 3 deletions src/voku/PHPStan/Rules/IfConditionBooleanAndRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$cond = $node->getOriginalNode();
$rightScope = $node->getRightScope();

$errors = IfConditionHelper::processNode($cond->left, $scope, $this->classesNotInIfConditions);
$errors = array_merge($errors, IfConditionHelper::processNode($cond->right, $rightScope, $this->classesNotInIfConditions));
$left = $scope->getType($cond->left);
$right = $scope->getType($cond->right);
$errors = [];
$errors = IfConditionHelper::processNodeHelper($left, $right, $node, $errors, $this->classesNotInIfConditions);
$errors = IfConditionHelper::processNodeHelper($right, $left, $node, $errors, $this->classesNotInIfConditions);

return $errors;
}
Expand Down
2 changes: 1 addition & 1 deletion src/voku/PHPStan/Rules/IfConditionBooleanNotRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public function processNode(Node $node, Scope $scope): array
{
$cond = $node->expr;

return IfConditionHelper::processNode($cond, $scope, $this->classesNotInIfConditions);
return IfConditionHelper::processNodeHelper($scope->getType($cond), null, $node, [], $this->classesNotInIfConditions);
}
}
8 changes: 5 additions & 3 deletions src/voku/PHPStan/Rules/IfConditionBooleanOrRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$cond = $node->getOriginalNode();
$rightScope = $node->getRightScope();

$errors = IfConditionHelper::processNode($cond->left, $scope, $this->classesNotInIfConditions);
$errors = array_merge($errors, IfConditionHelper::processNode($cond->right, $rightScope, $this->classesNotInIfConditions));
$left = $scope->getType($cond->left);
$right = $scope->getType($cond->right);
$errors = [];
$errors = IfConditionHelper::processNodeHelper($left, $right, $node, $errors, $this->classesNotInIfConditions);
$errors = IfConditionHelper::processNodeHelper($right, $left, $node, $errors, $this->classesNotInIfConditions);

return $errors;
}
Expand Down
56 changes: 2 additions & 54 deletions src/voku/PHPStan/Rules/IfConditionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,14 @@

final class IfConditionHelper
{

/**
* @param \PhpParser\Node\Expr $cond
* @param array<int, class-string> $classesNotInIfConditions
*
* @return array<int, \PHPStan\Rules\RuleError>
*/
public static function processNode(
Node $cond,
Scope $scope,
array $classesNotInIfConditions
): array
{
// init
$errors = [];

// ignore mixed types
$condType = $scope->getType($cond);
if ($condType instanceof \PHPStan\Type\MixedType) {
return [];
}

if (
!property_exists($cond, 'left')
&&
!property_exists($cond, 'right')
) {
$errors = self::prcessNodeHelper($condType, null, $cond, $errors, $classesNotInIfConditions);

return $errors;
}

if (property_exists($cond, 'left')) {
$leftType = $scope->getType($cond->left);
} else {
$leftType = null;
}

if (property_exists($cond, 'right')) {
$rightType = $scope->getType($cond->right);
} else {
$rightType = null;
}

// left <-> right
$errors = self::prcessNodeHelper($leftType, $rightType, $cond, $errors, $classesNotInIfConditions);
// right <-> left
$errors = self::prcessNodeHelper($rightType, $leftType, $cond, $errors, $classesNotInIfConditions);

return $errors;
}

/**
* @param \PhpParser\Node\Expr $cond
* @param \PhpParser\Node $cond
* @param array<int, \PHPStan\Rules\RuleError> $errors
* @param array<int, class-string> $classesNotInIfConditions
*
* @return array<int, \PHPStan\Rules\RuleError>
*/
private static function prcessNodeHelper(
public static function processNodeHelper(
?\PHPStan\Type\Type $type_1,
?\PHPStan\Type\Type $type_2,
Node $cond,
Expand Down
17 changes: 9 additions & 8 deletions src/voku/PHPStan/Rules/IfConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PHPStan\Rules\Rule;

/**
* @implements Rule<\PhpParser\Node\Stmt>
* @implements Rule<\PhpParser\Node\Expr\BinaryOp>
*/
final class IfConditionRule implements Rule
{
Expand All @@ -29,22 +29,23 @@ public function __construct(array $classesNotInIfConditions = [])

public function getNodeType(): string
{
return \PhpParser\Node\Stmt::class;
return \PhpParser\Node\Expr\BinaryOp::class;
}

/**
* @param \PhpParser\Node\Stmt $node
* @param \PhpParser\Node\Expr\BinaryOp $node
*
* @return array<int, \PHPStan\Rules\RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
if (!($node instanceof \PhpParser\Node\Stmt\If_) && !($node instanceof \PhpParser\Node\Stmt\ElseIf_)) {
return [];
}
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

$cond = $node->cond;
$errors = [];
$errors = IfConditionHelper::processNodeHelper($leftType, $rightType, $node, $errors, $this->classesNotInIfConditions);
$errors = IfConditionHelper::processNodeHelper($rightType, $leftType, $node, $errors, $this->classesNotInIfConditions);

return IfConditionHelper::processNode($cond, $scope, $this->classesNotInIfConditions);
return $errors;
}
}
4 changes: 1 addition & 3 deletions src/voku/PHPStan/Rules/IfConditionTernaryOperatorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public function processNode(Node $node, Scope $scope): array
return []; // elvis ?:
}

$cond = $node->cond;

return IfConditionHelper::processNode($cond, $scope, $this->classesNotInIfConditions);
return IfConditionHelper::processNodeHelper($scope->getType($node->cond), null, $node, [], $this->classesNotInIfConditions);
}
}
4 changes: 4 additions & 0 deletions tests/IfConditionBooleanAndRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function testIfConditions(): void
'Use a method to check the condition e.g. `$foo->value()` instead of `$foo`.',
8,
],
[
'Do not compare objects directly.',
8,
],
]
);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/IfConditionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function testIfConditions(): void
'Do not compare objects directly.',
55
],
[
'Do not compare objects directly.',
72
],
]
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/IfConditionsFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ public function foo(self $bar)
}
}
}

// Intercept binary op wherever they are
$var = function(): bool {
return '2032-03-04' <= new \DateTimeImmutable();
};