-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DowngradePhp80] Apply PHPStan 1.7.x-dev compatible for PhpParameterR…
…eflection (#2336)
- Loading branch information
1 parent
1a56ec1
commit f0a1b68
Showing
5 changed files
with
75 additions
and
9 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
69 changes: 69 additions & 0 deletions
69
rules/DowngradePhp80/Reflection/SimplePhpParameterReflection.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,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; | ||
} | ||
} |