Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not report non-existent function after function_exists() check
- Loading branch information
1 parent
6bb0fd6
commit 28e8c11
Showing
7 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Scalar\String_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\SpecifiedTypes; | ||
use PHPStan\Analyser\TypeSpecifier; | ||
use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
|
||
class FunctionExistsFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
private TypeSpecifier $typeSpecifier; | ||
|
||
public function isFunctionSupported( | ||
FunctionReflection $functionReflection, | ||
FuncCall $node, | ||
TypeSpecifierContext $context | ||
): bool | ||
{ | ||
return $functionReflection->getName() === 'function_exists' && isset($node->getArgs()[0]) && $context->truthy(); | ||
} | ||
|
||
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, | ||
false, | ||
$scope | ||
); | ||
} | ||
|
||
return new SpecifiedTypes(); | ||
} | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Bug3576; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
if (\function_exists('bug3576')) { | ||
bug3576(); | ||
\bug3576(); | ||
} else { | ||
bug3576(); | ||
} | ||
|
||
bug3576(); | ||
} | ||
|
||
public function doBar(): void | ||
{ | ||
if (function_exists('bug3576')) { | ||
bug3576(); | ||
\bug3576(); | ||
} else { | ||
bug3576(); | ||
} | ||
|
||
bug3576(); | ||
} | ||
|
||
public function doBaz(): void | ||
{ | ||
if (function_exists('\bug3576')) { | ||
bug3576(); | ||
\bug3576(); | ||
} else { | ||
bug3576(); | ||
} | ||
|
||
bug3576(); | ||
} | ||
|
||
} | ||
|