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

Improve falsey type specification with constant string comparison #1015

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
use PHPStan\Type\SubtractableType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
Expand Down Expand Up @@ -234,6 +235,13 @@ public function specifyTypesInCondition(
}
}
}

if ($context->falsey() && $constantType instanceof ConstantStringType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I did not understand the change fully, but would this also make sense for other constant types here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tbh I'm also not a 100% sure if this is the right/best fix. But I thought the same, yeah. Will take a look later. Also thought of getting an opinion from Ondrej first maybe

$exprType = $scope->getType($exprNode);
if ($exprType instanceof SubtractableType) {
return $this->create($exprNode, $exprType->subtract($constantType), $context->negate(), false, $scope);
}
}
}

$rightType = $scope->getType($expr->right);
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 @@ -692,6 +692,7 @@ public function dataFileAsserts(): iterable
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6308.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6329.php');

if (PHP_VERSION_ID >= 70400) {
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-6473.php');
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,17 @@ public function dataCondition(): array
],
[],
],
[
new Expr\BinaryOp\BooleanOr(
new Expr\BinaryOp\BooleanAnd(
$this->createFunctionCall('is_string', 'a'),
new NotIdentical(new String_(''), new Variable('a')),
),
new Identical(new Expr\ConstFetch(new Name('null')), new Variable('a')),
),
['$a' => 'non-empty-string|null'],
['$a' => '~null'],
],
];
}

Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6329.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug6329;

use function PHPStan\Testing\assertType;

function foo($a): void
{
if (is_string($a) && '' !== $a) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need the same test, with switched arguments?

assertType('non-empty-string', $a);
}

if (is_string($a) && '' !== $a || null === $a) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same for is_string($a) && strlen($a) > 0 || null === $a ?

assertType('non-empty-string|null', $a);
}

if (is_string($a) || null === $a) {
assertType('string|null', $a);
if ('' !== $a) {
assertType('non-empty-string|null', $a);
}
}

if ((is_string($a) || null === $a) && '' !== $a) {
assertType('non-empty-string|null', $a);
}
}