Skip to content
Open
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
31 changes: 29 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2242,13 +2242,40 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif

public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
// Normalize to: fn() === expr
$leftExpr = $expr->left;
$rightExpr = $expr->right;

// Normalize to: fn() === expr
if ($rightExpr instanceof FuncCall && !$leftExpr instanceof FuncCall) {
[$leftExpr, $rightExpr] = [$rightExpr, $leftExpr];
$specifiedTypes = $this->resolveNormalizedIdentical(new Expr\BinaryOp\Identical(
$rightExpr,
$leftExpr,
), $scope, $context);
} else {
$specifiedTypes = $this->resolveNormalizedIdentical(new Expr\BinaryOp\Identical(
$leftExpr,
$rightExpr,
), $scope, $context);
}

// merge result of fn1() === fn2() and fn2() === fn1()
if ($rightExpr instanceof FuncCall && $leftExpr instanceof FuncCall) {
return $specifiedTypes->unionWith(
$this->resolveNormalizedIdentical(new Expr\BinaryOp\Identical(
$rightExpr,
$leftExpr,
), $scope, $context),
);
}

return $specifiedTypes;
}

private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$leftExpr = $expr->left;
$rightExpr = $expr->right;

$unwrappedLeftExpr = $leftExpr;
if ($leftExpr instanceof AlwaysRememberedExpr) {
$unwrappedLeftExpr = $leftExpr->getExpr();
Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13749.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types = 1);

namespace Bug13749;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param non-empty-string $nonES
*/
public function sayNonEmpty(string $s, string $nonES): void
{
if (strlen($nonES) === strlen($s)) {
assertType('non-empty-string', $s);
}
if (strlen($nonES) >= strlen($s)) {
assertType('string', $s); // could be non-empty-string
}

if (strlen($s) === strlen($nonES)) {
assertType('non-empty-string', $s);
}
if (strlen($s) >= strlen($nonES)) {
assertType('non-empty-string', $s);
}
}

/**
* @param non-falsy-string $nonFalsy
*/
public function sayNonFalsy(string $s, string $nonFalsy): void
{
if (strlen($nonFalsy) === strlen($s)) {
assertType('non-empty-string', $s);
}
if (strlen($nonFalsy) >= strlen($s)) {
assertType('string', $s); // could be non-empty-string
}

if (strlen($s) === strlen($nonFalsy)) {
assertType('non-empty-string', $s);
}
if (strlen($s) >= strlen($nonFalsy)) {
assertType('non-empty-string', $s);
}
}

/**
* @param non-empty-array $nonEmptyArr
*/
public function sayCount(array $arr, array $nonEmptyArr): void
{
if (count($arr) === count($nonEmptyArr)) {
assertType('non-empty-array', $arr);
assertType('non-empty-array', $nonEmptyArr);
}
assertType('array', $arr);
assertType('non-empty-array', $nonEmptyArr);

if (count($nonEmptyArr) === count($arr)) {
assertType('non-empty-array', $arr);
assertType('non-empty-array', $nonEmptyArr);
}
assertType('array', $arr);
assertType('non-empty-array', $nonEmptyArr);
}

}
Loading