[DowngradePhp80] Apply PHPStan 1.7.x-dev compatible for PhpParameterReflection (#2336)

This commit is contained in:
Abdul Malik Ikhsan 2022-05-20 17:16:54 +07:00 committed by GitHub
parent 1a56ec1736
commit f0a1b688f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 9 deletions

View File

@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2|^8.0",
"phpstan/phpstan": "^1.6.8 || <1.7"
"phpstan/phpstan": "^1.6.8"
},
"autoload": {
"files": [

View File

@ -22,7 +22,7 @@
"nikic/php-parser": "^4.13.2",
"ondram/ci-detector": "^4.1",
"phpstan/phpdoc-parser": "^1.5.1",
"phpstan/phpstan": "^1.6.8 || <1.7",
"phpstan/phpstan": "^1.6.8",
"phpstan/phpstan-phpunit": "^1.0",
"psr/log": "^2.0",
"react/child-process": "^0.6.4",

View File

@ -24,5 +24,6 @@ return static function (RectorConfig $rectorConfig): void {
__DIR__ . '/../rules/*/Contract/*',
__DIR__ . '/../rules/*/Exception/*',
__DIR__ . '/../rules/*/Enum/*',
__DIR__ . '/../rules/DowngradePhp80/Reflection/SimplePhpParameterReflection.php',
]);
};

View File

@ -12,6 +12,7 @@ use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\Php\PhpParameterReflection;
use Rector\DowngradePhp80\Reflection\DefaultParameterValueResolver;
use Rector\DowngradePhp80\Reflection\SimplePhpParameterReflection;
use Rector\NodeNameResolver\NodeNameResolver;
use ReflectionFunction;
@ -19,7 +20,7 @@ final class NamedToUnnamedArgs
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly DefaultParameterValueResolver $defaultParameterValueResolver,
private readonly DefaultParameterValueResolver $defaultParameterValueResolver
) {
}
@ -92,12 +93,7 @@ final class NamedToUnnamedArgs
/** @var ParameterReflection|PhpParameterReflection $parameterReflection */
if ($functionLikeReflection instanceof ReflectionFunction) {
// @todo since PHPStan 1.7.* add new InitializerExprTypeResolver() service as 1st arg - https://github.com/phpstan/phpstan-src/commit/c8b3926f005d008178d6d8c62aaca0200a6359a2#diff-ce65c81a2653b1f53bc416082582e248f629d65c066440d9c4edc5005d16af32
$parameterReflection = new PhpParameterReflection(
$functionLikeReflection->getParameters()[$i],
null,
null
);
$parameterReflection = new SimplePhpParameterReflection($functionLikeReflection, $i);
} else {
$parameterReflection = $parameters[$i];
}

View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Reflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Type\ConstantTypeHelper;
use PHPStan\Type\Type;
use Rector\Core\Exception\NotImplementedYetException;
use ReflectionFunction;
use ReflectionParameter;
use Throwable;
final class SimplePhpParameterReflection implements ParameterReflection
{
private readonly ReflectionParameter $parameter;
public function __construct(ReflectionFunction $reflectionFunction, int $position)
{
$this->parameter = $reflectionFunction->getParameters()[$position];
}
public function getName(): string
{
return $this->parameter->getName();
}
public function isOptional(): bool
{
return $this->parameter->isOptional();
}
/**
* getType() is never used yet on manual object creation, and the implementation require PHPStan $phpDocType services injection
* @see https://github.com/phpstan/phpstan-src/blob/92420cd4b190b57d1ba8bf9e800eb97c8c0ee2f2/src/Reflection/Php/PhpParameterReflection.php#L24
*/
public function getType(): Type
{
throw new NotImplementedYetException();
}
public function passedByReference(): PassedByReference
{
return $this->parameter->isPassedByReference()
? PassedByReference::createCreatesNewVariable()
: PassedByReference::createNo();
}
public function isVariadic(): bool
{
return $this->parameter->isVariadic();
}
public function getDefaultValue(): ?Type
{
try {
if ($this->parameter->isDefaultValueAvailable()) {
$defaultValue = $this->parameter->getDefaultValue();
return ConstantTypeHelper::getTypeFromValue($defaultValue);
}
} catch (Throwable) {
return null;
}
return null;
}
}