|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection\Php; |
| 4 | + |
| 5 | +use PHPStan\PhpDoc\ResolvedPhpDocBlock; |
| 6 | +use PHPStan\Reflection\Assertions; |
| 7 | +use PHPStan\Reflection\ClassMemberReflection; |
| 8 | +use PHPStan\Reflection\ClassReflection; |
| 9 | +use PHPStan\Reflection\ExtendedFunctionVariant; |
| 10 | +use PHPStan\Reflection\ExtendedMethodReflection; |
| 11 | +use PHPStan\Reflection\ExtendedParameterReflection; |
| 12 | +use PHPStan\Reflection\ExtendedParametersAcceptor; |
| 13 | +use PHPStan\TrinaryLogic; |
| 14 | +use PHPStan\Turbo\ReferencedByTurboExtension; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use function array_map; |
| 17 | + |
| 18 | +/** |
| 19 | + * from() and tryFrom() of a backed enum. Reflection declares their parameter |
| 20 | + * as int|string, but PHP only accepts the enum's backing type. |
| 21 | + */ |
| 22 | +#[ReferencedByTurboExtension(key: 'backedEnumFromMethodReflection')] |
| 23 | +final class BackedEnumFromMethodReflection implements ExtendedMethodReflection |
| 24 | +{ |
| 25 | + |
| 26 | + /** @var list<ExtendedParametersAcceptor>|null */ |
| 27 | + private ?array $variants = null; |
| 28 | + |
| 29 | + public function __construct(private ExtendedMethodReflection $method, private Type $backingType) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + public function getDeclaringClass(): ClassReflection |
| 34 | + { |
| 35 | + return $this->method->getDeclaringClass(); |
| 36 | + } |
| 37 | + |
| 38 | + public function isStatic(): bool |
| 39 | + { |
| 40 | + return $this->method->isStatic(); |
| 41 | + } |
| 42 | + |
| 43 | + public function isPrivate(): bool |
| 44 | + { |
| 45 | + return $this->method->isPrivate(); |
| 46 | + } |
| 47 | + |
| 48 | + public function isPublic(): bool |
| 49 | + { |
| 50 | + return $this->method->isPublic(); |
| 51 | + } |
| 52 | + |
| 53 | + public function getDocComment(): ?string |
| 54 | + { |
| 55 | + return $this->method->getDocComment(); |
| 56 | + } |
| 57 | + |
| 58 | + public function getName(): string |
| 59 | + { |
| 60 | + return $this->method->getName(); |
| 61 | + } |
| 62 | + |
| 63 | + public function getPrototype(): ClassMemberReflection |
| 64 | + { |
| 65 | + return $this->method->getPrototype(); |
| 66 | + } |
| 67 | + |
| 68 | + public function getVariants(): array |
| 69 | + { |
| 70 | + return $this->variants ??= array_map(fn (ExtendedParametersAcceptor $variant): ExtendedParametersAcceptor => new ExtendedFunctionVariant( |
| 71 | + $variant->getTemplateTypeMap(), |
| 72 | + $variant->getResolvedTemplateTypeMap(), |
| 73 | + array_map(fn (ExtendedParameterReflection $parameter): ExtendedParameterReflection => new ExtendedDummyParameter( |
| 74 | + $parameter->getName(), |
| 75 | + $this->backingType, |
| 76 | + $parameter->isOptional(), |
| 77 | + $parameter->passedByReference(), |
| 78 | + $parameter->isVariadic(), |
| 79 | + $parameter->getDefaultValue(), |
| 80 | + $this->backingType, |
| 81 | + $this->backingType, |
| 82 | + $parameter->getOutType(), |
| 83 | + $parameter->isImmediatelyInvokedCallable(), |
| 84 | + $parameter->getClosureThisType(), |
| 85 | + $parameter->getAttributes(), |
| 86 | + $parameter->getAllowedConstants(), |
| 87 | + $parameter->isPureUnlessCallableIsImpureParameter(), |
| 88 | + $parameter->isPureUnlessParameterPassedParameter(), |
| 89 | + ), $variant->getParameters()), |
| 90 | + $variant->isVariadic(), |
| 91 | + $variant->getReturnType(), |
| 92 | + $variant->getPhpDocReturnType(), |
| 93 | + $variant->getNativeReturnType(), |
| 94 | + $variant->getCallSiteVarianceMap(), |
| 95 | + ), $this->method->getVariants()); |
| 96 | + } |
| 97 | + |
| 98 | + public function getOnlyVariant(): ExtendedParametersAcceptor |
| 99 | + { |
| 100 | + return $this->getVariants()[0]; |
| 101 | + } |
| 102 | + |
| 103 | + public function getNamedArgumentsVariants(): ?array |
| 104 | + { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + public function isDeprecated(): TrinaryLogic |
| 109 | + { |
| 110 | + return $this->method->isDeprecated(); |
| 111 | + } |
| 112 | + |
| 113 | + public function getDeprecatedDescription(): ?string |
| 114 | + { |
| 115 | + return $this->method->getDeprecatedDescription(); |
| 116 | + } |
| 117 | + |
| 118 | + public function isFinal(): TrinaryLogic |
| 119 | + { |
| 120 | + return $this->method->isFinal(); |
| 121 | + } |
| 122 | + |
| 123 | + public function isFinalByKeyword(): TrinaryLogic |
| 124 | + { |
| 125 | + return $this->method->isFinalByKeyword(); |
| 126 | + } |
| 127 | + |
| 128 | + public function isInternal(): TrinaryLogic |
| 129 | + { |
| 130 | + return $this->method->isInternal(); |
| 131 | + } |
| 132 | + |
| 133 | + public function isBuiltin(): TrinaryLogic|bool |
| 134 | + { |
| 135 | + return $this->method->isBuiltin(); |
| 136 | + } |
| 137 | + |
| 138 | + public function getThrowType(): ?Type |
| 139 | + { |
| 140 | + return $this->method->getThrowType(); |
| 141 | + } |
| 142 | + |
| 143 | + public function hasSideEffects(): TrinaryLogic |
| 144 | + { |
| 145 | + return $this->method->hasSideEffects(); |
| 146 | + } |
| 147 | + |
| 148 | + public function getAsserts(): Assertions |
| 149 | + { |
| 150 | + return $this->method->getAsserts(); |
| 151 | + } |
| 152 | + |
| 153 | + public function acceptsNamedArguments(): TrinaryLogic |
| 154 | + { |
| 155 | + return $this->method->acceptsNamedArguments(); |
| 156 | + } |
| 157 | + |
| 158 | + public function getSelfOutType(): ?Type |
| 159 | + { |
| 160 | + return $this->method->getSelfOutType(); |
| 161 | + } |
| 162 | + |
| 163 | + public function returnsByReference(): TrinaryLogic |
| 164 | + { |
| 165 | + return $this->method->returnsByReference(); |
| 166 | + } |
| 167 | + |
| 168 | + public function isAbstract(): TrinaryLogic|bool |
| 169 | + { |
| 170 | + return $this->method->isAbstract(); |
| 171 | + } |
| 172 | + |
| 173 | + public function isPure(): TrinaryLogic |
| 174 | + { |
| 175 | + return $this->method->isPure(); |
| 176 | + } |
| 177 | + |
| 178 | + public function getPureUnlessCallableIsImpureParameters(): array |
| 179 | + { |
| 180 | + return $this->method->getPureUnlessCallableIsImpureParameters(); |
| 181 | + } |
| 182 | + |
| 183 | + public function getPureUnlessParameterPassedParameters(): array |
| 184 | + { |
| 185 | + return $this->method->getPureUnlessParameterPassedParameters(); |
| 186 | + } |
| 187 | + |
| 188 | + public function getAttributes(): array |
| 189 | + { |
| 190 | + return $this->method->getAttributes(); |
| 191 | + } |
| 192 | + |
| 193 | + public function mustUseReturnValue(): TrinaryLogic |
| 194 | + { |
| 195 | + return $this->method->mustUseReturnValue(); |
| 196 | + } |
| 197 | + |
| 198 | + public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock |
| 199 | + { |
| 200 | + return $this->method->getResolvedPhpDoc(); |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments