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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ parameters:
-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
count: 7
count: 8
path: src/Type/TypeCombinator.php

-
Expand Down
5 changes: 4 additions & 1 deletion src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,10 @@ public static function doUnion(Type ...$types): Type
$enumCaseTypes = [];
$integerRangeTypes = [];
for ($i = 0; $i < $typesCount; $i++) {
if ($types[$i]->isConstantScalarValue()->yes()) {
// a constant scalar refined by an accessory type ('Foo'&hasMethod(bar))
// stays with the other types so it is compared against the plain
// constant scalars and other refinements of the same value
if (!$types[$i] instanceof IntersectionType && $types[$i]->isConstantScalarValue()->yes()) {
$type = $types[$i];
$scalarTypes[get_class($type)][$type->describe(VerbosityLevel::cache())] = $type;
unset($types[$i]);
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15591.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug15591;

use function PHPStan\Testing\assertType;

class Foo
{
}

function test(): void
{
$class = Foo::class;
foreach (['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10', 'm11', 'm12'] as $method) {
if (method_exists($class, $method)) {
}
}
assertType("'Bug15591\\\\Foo'", $class);
}

function testStraightLine(): void
{
$class = Foo::class;
if (method_exists($class, 'm1')) {
assertType("'Bug15591\\\\Foo'&hasMethod(m1)", $class);
}
assertType("'Bug15591\\\\Foo'", $class);
if (method_exists($class, 'm2')) {
assertType("'Bug15591\\\\Foo'&hasMethod(m2)", $class);
}
assertType("'Bug15591\\\\Foo'", $class);
}

function testProperty(): void
{
$class = Foo::class;
if (property_exists($class, 'p1')) {
}
if (property_exists($class, 'p2')) {
}
assertType("'Bug15591\\\\Foo'", $class);
}
16 changes: 16 additions & 0 deletions tests/bench/data/bug-15591.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace MethodExistsLoopRepro;

class Foo
{
}

function test(): void
{
$class = Foo::class;
foreach (['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10', 'm11', 'm12'] as $method) {
if (method_exists($class, $method)) {
}
}
}
7 changes: 5 additions & 2 deletions turbo-ext/src/TypeCombinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,11 @@ class TypeCombinator
Level cacheLevel(PT_LC("cache"));
for (size_t i = 0; i < typesCount; i++) {
zval *type = types[i].raw();
zend_long isConstantScalarValue = callTrinary(type, PT_LC("isconstantscalarvalue"));
PT_FAIL_IF_NEG(isConstantScalarValue);
zend_long isConstantScalarValue = PT_TRI_NO;
if (!isInstance(type, pt_ce_intersection_type)) {
isConstantScalarValue = callTrinary(type, PT_LC("isconstantscalarvalue"));
PT_FAIL_IF_NEG(isConstantScalarValue);
}
if (isConstantScalarValue == PT_TRI_YES) {
zv::Val description = describe(type, cacheLevel);
zend_string *key = describedKey(description);
Expand Down
Loading