Skip to content

Commit

Permalink
[DowngradePhp80] Apply PHPStan 1.7.x-dev compatible for PhpParameterR…
Browse files Browse the repository at this point in the history
…eflection (#2336)
  • Loading branch information
samsonasik committed May 20, 2022
1 parent 1a56ec1 commit f0a1b68
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/target-repository/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2|^8.0",
"phpstan/phpstan": "^1.6.8 || <1.7"
"phpstan/phpstan": "^1.6.8"
},
"autoload": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions config/services-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
__DIR__ . '/../rules/*/Contract/*',
__DIR__ . '/../rules/*/Exception/*',
__DIR__ . '/../rules/*/Enum/*',
__DIR__ . '/../rules/DowngradePhp80/Reflection/SimplePhpParameterReflection.php',
]);
};
10 changes: 3 additions & 7 deletions rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
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;

final class NamedToUnnamedArgs
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly DefaultParameterValueResolver $defaultParameterValueResolver,
private readonly DefaultParameterValueResolver $defaultParameterValueResolver
) {
}

Expand Down Expand Up @@ -92,12 +93,7 @@ public function fillFromJumpedNamedArgs(

/** @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];
}
Expand Down
69 changes: 69 additions & 0 deletions rules/DowngradePhp80/Reflection/SimplePhpParameterReflection.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit f0a1b68

Please sign in to comment.