Skip to content
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

Narrow types based on non-strict equality with constant types #2889

Draft
wants to merge 1 commit into
base: 1.10.x
Choose a base branch
from
Draft
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
34 changes: 32 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ConditionalTypeForParameter;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -1469,15 +1470,24 @@ private function findTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\
$leftExpr = $leftExpr->getExpr();
}

$isNativeConstFetch = static function (Expr $expr): bool {
if (!$expr instanceof ConstFetch) {
return false;
}

$loweredConstName = strtolower($expr->name->toString());
return in_array($loweredConstName, ['true', 'false', 'null'], true);
};

if (
$leftType instanceof ConstantScalarType
&& !$rightExpr instanceof ConstFetch
&& !$isNativeConstFetch($rightExpr)
&& !$rightExpr instanceof ClassConstFetch
) {
return [$binaryOperation->right, $leftType];
} elseif (
$rightType instanceof ConstantScalarType
&& !$leftExpr instanceof ConstFetch
&& !$isNativeConstFetch($leftExpr)
&& !$leftExpr instanceof ClassConstFetch
) {
return [$binaryOperation->left, $rightType];
Expand Down Expand Up @@ -1809,6 +1819,26 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
);
}

$looseyType = new UnionType([
new NullType(),
new ConstantBooleanType(true),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantIntegerType(1),
new FloatType(), // equality on floats is not usefull
new ConstantStringType(''),
new ConstantStringType('0'),
new ConstantStringType('1'),
new ConstantArrayType([], []),
]);

if ($context->true()
&& ($exprNode instanceof Expr\Variable || $exprNode instanceof ConstFetch)
&& !$looseyType->isSuperTypeOf($constantType)->yes()
) {
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
}

if (
$exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5961.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10189.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10317.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-equal-consts.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10302-interface-extends.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10302-trait-extends.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10302-trait-implements.php');
Expand Down
92 changes: 92 additions & 0 deletions tests/PHPStan/Analyser/data/loose-equal-consts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php declare(strict_types=1);

namespace LooseEqualConsts;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function vars($mixed): void
{
if ($mixed === 'backend') {
assertType("'backend'", $mixed);
}

if ($mixed == 'backend') {
assertType("'backend'", $mixed);
}
if ($mixed == 500) {
assertType("500", $mixed);
}
if ('backend' == $mixed) {
assertType("'backend'", $mixed);
}
if (500 == $mixed) {
assertType("500", $mixed);
}
if ($mixed == 'backend' || $mixed == 'frontend') {
assertType("'backend'|'frontend'", $mixed);
}

if ($mixed == '1') {
assertType("mixed", $mixed);
}
if ($mixed == '0') {
assertType("mixed", $mixed);
}
if ($mixed == 1) {
assertType("mixed", $mixed);
}
if ($mixed == 0) {
assertType("mixed", $mixed);
}
Comment on lines +31 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 4 could be more precise

Suggested change
if ($mixed == '1') {
assertType("mixed", $mixed);
}
if ($mixed == '0') {
assertType("mixed", $mixed);
}
if ($mixed == 1) {
assertType("mixed", $mixed);
}
if ($mixed == 0) {
assertType("mixed", $mixed);
}
if ($mixed == '1') {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $mixed);
}
if ($mixed == '0') {
assertType("0|0.0|''|'0'|array{}|false|null", $mixed);
}
if ($mixed == 1) {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $mixed);
}
if ($mixed == 0) {
assertType("0|0.0|''|'0'|array{}|false|null", $mixed);
}

but I think its a unrelated problem for a followup?


// equality on floats is not usefull
if ($mixed == 1.0) {
assertType("mixed", $mixed);
}
if ($mixed == 0.0) {
assertType("mixed", $mixed);
}
if ($mixed == 123.0) {
assertType("mixed", $mixed);
}

if ($mixed == true) {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $mixed);
}
if ($mixed == false) {
assertType("0|0.0|''|'0'|array{}|false|null", $mixed);
}
if ($mixed == null) {
assertType("0|0.0|''|'0'|array{}|false|null", $mixed);
}

if ($mixed == []) {
assertType("mixed", $mixed);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, could be

Suggested change
assertType("mixed", $mixed);
assertType("0|0.0|''|'0'|array{}|false|null", $mixed);

}

if ($mixed !== 'backend') {
assertType("mixed~'backend'", $mixed);
}
if ($mixed != 'backend') {
assertType('mixed', $mixed);
}
}

public function constants(): void
{
if (APP_NAME === 'backend') {
assertType("'backend'", APP_NAME);
}
if (APP_NAME == 'backend') {
assertType("'backend'", APP_NAME);
}
if (APP_NAME !== 'backend') {
assertType('*ERROR*', APP_NAME);
}
if (APP_NAME != 'backend') {
assertType('*ERROR*', APP_NAME);
}
}
}
Loading