Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2157,7 +2157,17 @@ private function expressionTypeIsUnchangeable(ExpressionTypeHolder $typeHolder):
return $expr instanceof FuncCall
&& !$expr->isFirstClassCallable()
&& $expr->name instanceof FullyQualified
&& $expr->name->toLowerString() === 'function_exists'
&& in_array(
$expr->name->toLowerString(),
[
'class_exists',
'interface_exists',
'trait_exists',
'enum_exists',
'function_exists',
],
true,
)
&& isset($expr->getArgs()[0])
&& count($this->getType($expr->getArgs()[0]->value)->getConstantStrings()) === 1
&& $type->isTrue()->yes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
}

public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{

Check failure on line 48 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 48 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.
$args = $node->getArgs();
$argType = $scope->getType($args[0]->value);
if ($argType instanceof ConstantStringType) {
$funcCall = new FuncCall(new FullyQualified('class_exists'), [
$funcCall = new FuncCall(new FullyQualified($functionReflection->getName()), [

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, windows-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.

Check failure on line 52 in src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #1 $name of class PhpParser\Node\Name\FullyQualified constructor expects non-empty-array<string>|PhpParser\Node\Name|non-empty-string, string given.
new Arg(new String_(ltrim($argType->getValue(), '\\'))),
]);
return $this->typeSpecifier->create(
Expand Down
64 changes: 63 additions & 1 deletion tests/PHPStan/Analyser/nsrt/closure-retain-expression-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace ClosureRetainExpressionTypes;

use function class_exists;
use function defined;
use function interface_exists;
use function enum_exists;
use function trait_exists;
use function function_exists;
use function PHPStan\Testing\assertType;

Expand All @@ -21,3 +23,63 @@ function () {
};
}
};

function () {
assertType('bool', class_exists('foo345'));
if (class_exists('foo345')) {
assertType('true', class_exists('foo345'));
function () {
assertType('true', class_exists('foo345'));
};
} else {
assertType('false', class_exists('foo345'));
function () {
assertType('bool', class_exists('foo345'));
};
}
};

function () {
assertType('bool', enum_exists('foo567'));
if (enum_exists('foo567')) {
assertType('true', enum_exists('foo567'));
function () {
assertType('true', enum_exists('foo567'));
};
} else {
assertType('false', enum_exists('foo567'));
function () {
assertType('bool', enum_exists('foo567'));
};
}
};

function () {
assertType('bool', interface_exists('foo890'));
if (interface_exists('foo890')) {
assertType('true', interface_exists('foo890'));
function () {
assertType('true', interface_exists('foo890'));
};
} else {
assertType('false', interface_exists('foo890'));
function () {
assertType('bool', interface_exists('foo890'));
};
}
};

function () {
assertType('bool', trait_exists('fooabc'));
if (trait_exists('fooabc')) {
assertType('true', trait_exists('fooabc'));
function () {
assertType('true', trait_exists('fooabc'));
};
} else {
assertType('false', trait_exists('fooabc'));
function () {
assertType('bool', trait_exists('fooabc'));
};
}
};
Loading