-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of Broker extracted into NativeFunctionReflectionProvider
- Loading branch information
1 parent
00d8c88
commit b6183bb
Showing
6 changed files
with
131 additions
and
95 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
105 changes: 105 additions & 0 deletions
105
src/Reflection/SignatureMap/NativeFunctionReflectionProvider.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,105 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Reflection\SignatureMap; | ||
|
||
use PHPStan\Reflection\FunctionVariant; | ||
use PHPStan\Reflection\Native\NativeFunctionReflection; | ||
use PHPStan\Reflection\Native\NativeParameterReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\FloatType; | ||
use PHPStan\Type\Generic\TemplateTypeMap; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType; | ||
use PHPStan\Type\TypeUtils; | ||
use PHPStan\Type\UnionType; | ||
|
||
class NativeFunctionReflectionProvider | ||
{ | ||
|
||
/** @var NativeFunctionReflection[] */ | ||
private static $functionMap = []; | ||
|
||
/** @var \PHPStan\Reflection\SignatureMap\SignatureMapProvider */ | ||
private $signatureMapProvider; | ||
|
||
public function __construct(SignatureMapProvider $signatureMapProvider) | ||
{ | ||
$this->signatureMapProvider = $signatureMapProvider; | ||
} | ||
|
||
public function findFunctionReflection(string $functionName): ?NativeFunctionReflection | ||
{ | ||
$lowerCasedFunctionName = strtolower($functionName); | ||
if (isset(self::$functionMap[$lowerCasedFunctionName])) { | ||
return self::$functionMap[$lowerCasedFunctionName]; | ||
} | ||
|
||
if (!$this->signatureMapProvider->hasFunctionSignature($lowerCasedFunctionName)) { | ||
return null; | ||
} | ||
|
||
$variantName = $lowerCasedFunctionName; | ||
$variants = []; | ||
$i = 0; | ||
while ($this->signatureMapProvider->hasFunctionSignature($variantName)) { | ||
$functionSignature = $this->signatureMapProvider->getFunctionSignature($variantName, null); | ||
$returnType = $functionSignature->getReturnType(); | ||
if ($lowerCasedFunctionName === 'pow') { | ||
$returnType = TypeUtils::toBenevolentUnion($returnType); | ||
} | ||
$variants[] = new FunctionVariant( | ||
TemplateTypeMap::createEmpty(), | ||
null, | ||
array_map(static function (ParameterSignature $parameterSignature) use ($lowerCasedFunctionName): NativeParameterReflection { | ||
$type = $parameterSignature->getType(); | ||
if ( | ||
$parameterSignature->getName() === 'args' | ||
&& ( | ||
$lowerCasedFunctionName === 'printf' | ||
|| $lowerCasedFunctionName === 'sprintf' | ||
) | ||
) { | ||
$type = new UnionType([ | ||
new StringAlwaysAcceptingObjectWithToStringType(), | ||
new IntegerType(), | ||
new FloatType(), | ||
new NullType(), | ||
new BooleanType(), | ||
]); | ||
} | ||
return new NativeParameterReflection( | ||
$parameterSignature->getName(), | ||
$parameterSignature->isOptional(), | ||
$type, | ||
$parameterSignature->passedByReference(), | ||
$parameterSignature->isVariadic(), | ||
null | ||
); | ||
}, $functionSignature->getParameters()), | ||
$functionSignature->isVariadic(), | ||
$returnType | ||
); | ||
|
||
$i++; | ||
$variantName = sprintf($lowerCasedFunctionName . '\'' . $i); | ||
} | ||
|
||
if ($this->signatureMapProvider->hasFunctionMetadata($lowerCasedFunctionName)) { | ||
$hasSideEffects = TrinaryLogic::createFromBoolean($this->signatureMapProvider->getFunctionMetadata($lowerCasedFunctionName)['hasSideEffects']); | ||
} else { | ||
$hasSideEffects = TrinaryLogic::createMaybe(); | ||
} | ||
$functionReflection = new NativeFunctionReflection( | ||
$lowerCasedFunctionName, | ||
$variants, | ||
null, | ||
$hasSideEffects | ||
); | ||
self::$functionMap[$lowerCasedFunctionName] = $functionReflection; | ||
|
||
return $functionReflection; | ||
} | ||
|
||
} |
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