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
9 changes: 9 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,15 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType
return $this->resolveIdenticalType($leftType, $rightType);
}

if ($leftType instanceof ConstantArrayType && $leftType->isEmpty() && $rightType instanceof ConstantScalarType) {
// @phpstan-ignore-next-line
return new ConstantBooleanType($rightType->getValue() == []); // phpcs:ignore
}
if ($rightType instanceof ConstantArrayType && $rightType->isEmpty() && $leftType instanceof ConstantScalarType) {
// @phpstan-ignore-next-line
return new ConstantBooleanType($leftType->getValue() == []); // phpcs:ignore
}

if ($leftType instanceof ConstantScalarType && $rightType instanceof ConstantScalarType) {
// @phpstan-ignore-next-line
return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,14 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/composer-non-empty-array-after-unset.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-6000.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/prestashop-breakdowns-empty-array.php');

if (PHP_VERSION_ID < 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php7.php');
}
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php8.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons.php');
}

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/PHPStan/Analyser/data/loose-comparisons-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace LooseSemanticsPhp7;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param 0 $zero
* @param 'php' $phpStr
* @param '' $emptyStr
*/
public function sayZero(
$zero,
$phpStr,
$emptyStr
): void
{
assertType('true', $zero == $phpStr);
assertType('true', $zero == $emptyStr);
}

/**
* @param 0 $zero
* @param 'php' $phpStr
*/
public function sayPhpStr(
$zero,
$phpStr,
): void
{
assertType('true', $phpStr == $zero);
}

/**
* @param 0 $zero
* @param '' $emptyStr
*/
public function sayEmptyStr(
$zero,
$emptyStr
): void
{
assertType('true', $emptyStr == $zero);
}
}
49 changes: 49 additions & 0 deletions tests/PHPStan/Analyser/data/loose-comparisons-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace LooseSemanticsPhp8;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param 0 $zero
* @param 'php' $phpStr
* @param '' $emptyStr
*/
public function sayZero(
$zero,
$phpStr,
$emptyStr
): void
{
assertType('false', $zero == $phpStr); // PHP8+ only
assertType('false', $zero == $emptyStr); // PHP8+ only
}

/**
* @param 0 $zero
* @param 'php' $phpStr
*/
public function sayPhpStr(
$zero,
$phpStr,
): void
{
assertType('false', $phpStr == $zero); // PHP8+ only
}

/**
* @param 0 $zero
* @param '' $emptyStr
*/
public function sayEmptyStr(
$zero,
$emptyStr
): void
{
assertType('false', $emptyStr == $zero); // PHP8+ only
}
}
Loading