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
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
28 changes: 24 additions & 4 deletions src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand Down Expand Up @@ -49,14 +50,33 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$args = $node->getArgs();
$argType = $scope->getType($args[0]->value);
if ($argType instanceof ConstantStringType) {
$funcCall = new FuncCall(new FullyQualified('class_exists'), [
new Arg(new String_(ltrim($argType->getValue(), '\\'))),
]);
if ($functionReflection->getName() === '') {
throw new ShouldNotHappenException();
}
return $this->typeSpecifier->create(
new AlwaysRememberedExpr($funcCall, new BooleanType(), new BooleanType()),
new AlwaysRememberedExpr(
new FuncCall(new FullyQualified($functionReflection->getName()), [
new Arg(new String_(ltrim($argType->getValue(), '\\'))),
]),
new BooleanType(),
new BooleanType(),
),
new ConstantBooleanType(true),
$context,
$scope,
)->unionWith(
$this->typeSpecifier->create(
new AlwaysRememberedExpr(
new FuncCall(new FullyQualified('class_exists'), [
new Arg(new String_(ltrim($argType->getValue(), '\\'))),
]),
new BooleanType(),
new BooleanType(),
),
new ConstantBooleanType(true),
$context,
$scope,
),
);
}

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