-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDefaultParameterValueResolver.php
More file actions
65 lines (53 loc) · 2.08 KB
/
Copy pathDefaultParameterValueResolver.php
File metadata and controls
65 lines (53 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Reflection;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use Rector\Exception\ShouldNotHappenException;
final class DefaultParameterValueResolver
{
public function resolveFromParameterReflection(ParameterReflection $parameterReflection): Expr|null
{
$defaultValueType = $parameterReflection->getDefaultValue();
if (! $defaultValueType instanceof Type) {
return null;
}
if (! $defaultValueType->isConstantValue()->yes()) {
throw new ShouldNotHappenException();
}
return $this->resolveValueFromType($defaultValueType);
}
private function resolveValueFromType(Type $constantType): ConstFetch|Expr
{
if ($constantType instanceof ConstantBooleanType) {
return $this->resolveConstantBooleanType($constantType);
}
if ($constantType instanceof ConstantArrayType) {
$values = [];
foreach ($constantType->getValueTypes() as $valueType) {
if (! $valueType->isConstantValue()->yes()) {
throw new ShouldNotHappenException();
}
$values[] = $this->resolveValueFromType($valueType);
}
return BuilderHelpers::normalizeValue($values);
}
/** @var ConstantStringType|ConstantIntegerType|NullType $constantType */
return BuilderHelpers::normalizeValue($constantType->getValue());
}
private function resolveConstantBooleanType(ConstantBooleanType $constantBooleanType): ConstFetch
{
$value = $constantBooleanType->describe(VerbosityLevel::value());
return new ConstFetch(new Name($value));
}
}