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
18 changes: 14 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,13 @@ private function resolveType(Expr $node): Type
}

if ($node instanceof Expr\BinaryOp\Identical) {
$leftType = $this->getType($node->left);
$rightType = $this->getType($node->right);
if ($this->treatPhpDocTypesAsCertain) {
$leftType = $this->getType($node->left);
$rightType = $this->getType($node->right);
} else {
$leftType = $this->getNativeType($node->left);
$rightType = $this->getNativeType($node->right);
}

if (
(
Expand Down Expand Up @@ -851,8 +856,13 @@ private function resolveType(Expr $node): Type
}

if ($node instanceof Expr\BinaryOp\NotIdentical) {
$leftType = $this->getType($node->left);
$rightType = $this->getType($node->right);
if ($this->treatPhpDocTypesAsCertain) {
$leftType = $this->getType($node->left);
$rightType = $this->getType($node->right);
} else {
$leftType = $this->getNativeType($node->left);
$rightType = $this->getNativeType($node->right);
}

if (
(
Expand Down
5 changes: 3 additions & 2 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ public function gatherAssertTypes(string $file): array
$actualType = $scope->getType($node->args[1]->value);
$assert = ['type', $file, $expectedType, $actualType, $node->getLine()];
} elseif ($functionName === 'PHPStan\\Testing\\assertNativeType') {
$expectedType = $scope->getNativeType($node->args[0]->value);
$actualType = $scope->getNativeType($node->args[1]->value);
$nativeScope = $scope->doNotTreatPhpDocTypesAsCertain();
$expectedType = $nativeScope->getNativeType($node->args[0]->value);
$actualType = $nativeScope->getNativeType($node->args[1]->value);
$assert = ['type', $file, $expectedType, $actualType, $node->getLine()];
} elseif ($functionName === 'PHPStan\\Testing\\assertVariableCertainty') {
$certainty = $node->args[0]->value;
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 @@ -379,6 +379,7 @@ public function dataFileAsserts(): iterable
require_once __DIR__ . '/data/type-aliases.php';

yield from $this->gatherAssertTypes(__DIR__ . '/data/type-aliases.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4650.php');
}

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

namespace Bug4650;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

class Foo
{

/**
* @phpstan-param non-empty-array<string|int> $idx
*/
function doFoo(array $idx): void {
assertType('array<int|string>&nonEmpty', $idx);
assertNativeType('array', $idx);

assertType('array()', []);
assertNativeType('array()', []);

assertType('false', $idx === []);
assertNativeType('bool', $idx === []);
assertType('true', $idx !== []);
assertNativeType('bool', $idx !== []);
}

}