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
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1587,12 +1587,6 @@ parameters:
count: 1
path: src/Type/Php/FilterFunctionReturnTypeHelper.php

-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php

-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.'
identifier: phpstanApi.instanceofType
Expand Down
1 change: 1 addition & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function findSpecifiedType(
'interface_exists',
'trait_exists',
'enum_exists',
'function_exists',
], true)) {
return null;
}
Expand Down
28 changes: 18 additions & 10 deletions src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\CallableType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use function count;
use function ltrim;

#[AutowiredService]
Expand All @@ -37,15 +37,23 @@ public function isFunctionSupported(
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$argType = $scope->getType($node->getArgs()[0]->value);
if ($argType instanceof ConstantStringType) {
return $this->typeSpecifier->create(
new FuncCall(new FullyQualified('function_exists'), [
new Arg(new String_(ltrim($argType->getValue(), '\\'))),
]),
new ConstantBooleanType(true),
$context,
$scope,
);

$constantStrings = $argType->getConstantStrings();
if (count($constantStrings) === 1) {
$specifiedTypes = new SpecifiedTypes();

foreach ($constantStrings as $constantString) {
$specifiedTypes = $specifiedTypes->unionWith($this->typeSpecifier->create(
new FuncCall(new FullyQualified('function_exists'), [
new Arg(new String_(ltrim($constantString->getValue(), '\\'))),
]),
new ConstantBooleanType(true),
$context,
$scope,
));
}

return $specifiedTypes;
}

return $this->typeSpecifier->create(
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Comparison/IfConstantConditionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,15 @@ public function testBug5020(): void
$this->analyse([__DIR__ . '/data/bug-5020.php'], []);
}

public function testBug8980(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8980.php'], [
[
'If condition is always true.',
23,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,12 @@ public function testBug8217(): void
$this->analyse([__DIR__ . '/data/bug-8217.php'], []);
}

public function testBug8980(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8980.php'], []);
}

public function testBug6211(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8980.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug8980;

use function function_exists;

// actual bug report snippet: function_exists inside array_filter callback
$undefined_curl_functions = array_filter(
[
'curl_multi_add_handle',
'curl_multi_exec',
'curl_multi_init',
],
static function( $function_name ) {
return ! function_exists( $function_name );
}
);

function testFunctionExistsGuardReturn(): void {
if (!function_exists('curl_init')) {
return;
}
if (function_exists('curl_init')) {
echo 'exists';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,20 @@ public function testBug14384(): void
$this->analyse([__DIR__ . '/data/bug-14384.php'], []);
}

public function testBug8980(): void
{
$this->analyse([__DIR__ . '/data/bug-8980.php'], [
[
'Function funcA not found.',
13,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'Function funcB not found.',
14,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
]);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-8980.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug8980;

function doFoo():void {
$func = rand(0,1) ? 'funcA' : 'funcB';

if (!function_exists($func)) {
throw new \Exception();
}

// the function_exists() will only assure one of the functions to exist.
funcA();
funcB();
Comment on lines +8 to +14
Copy link
Copy Markdown
Contributor Author

@staabm staabm May 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case, the function_exists() will only assure one of the functions to exist.
therefore - to be defensive - I am using only count($constantStrings) === 1 in the src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php so we still expectedly error about function exists

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding it as a coment in this file ?

$func();
}
Loading