|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Nette\PHPStan\Utils; |
| 4 | + |
| 5 | +use PHPStan\Reflection\ClassMemberReflection; |
| 6 | +use PHPStan\Reflection\ClassReflection; |
| 7 | +use PHPStan\Reflection\FunctionVariant; |
| 8 | +use PHPStan\Reflection\MethodReflection; |
| 9 | +use PHPStan\Reflection\MethodsClassReflectionExtension; |
| 10 | +use PHPStan\TrinaryLogic; |
| 11 | +use PHPStan\Type\Generic\TemplateTypeMap; |
| 12 | +use PHPStan\Type\MixedType; |
| 13 | +use PHPStan\Type\StaticType; |
| 14 | +use PHPStan\Type\Type; |
| 15 | +use function in_array, strlen, substr; |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * Resolves getXxx(), setXxx(), addXxx() magic methods on Nette\Utils\Html |
| 20 | + * that are handled by __call() but not declared via @method annotations. |
| 21 | + */ |
| 22 | +final class HtmlMethodsClassReflectionExtension implements MethodsClassReflectionExtension |
| 23 | +{ |
| 24 | + public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
| 25 | + { |
| 26 | + if (!$classReflection->is('Nette\Utils\Html')) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + return strlen($methodName) > 3 |
| 31 | + && in_array(substr($methodName, 0, 3), ['get', 'set', 'add'], true); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
| 36 | + { |
| 37 | + $prefix = substr($methodName, 0, 3); |
| 38 | + return new HtmlCallMethodReflection($classReflection, $methodName, $prefix); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + * @internal |
| 45 | + */ |
| 46 | +final class HtmlCallMethodReflection implements MethodReflection |
| 47 | +{ |
| 48 | + public function __construct( |
| 49 | + private ClassReflection $declaringClass, |
| 50 | + private string $name, |
| 51 | + private string $prefix, |
| 52 | + ) { |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + public function getDeclaringClass(): ClassReflection |
| 57 | + { |
| 58 | + return $this->declaringClass; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + public function isStatic(): bool |
| 63 | + { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public function isPrivate(): bool |
| 69 | + { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public function isPublic(): bool |
| 75 | + { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + public function getDocComment(): ?string |
| 81 | + { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + public function getName(): string |
| 87 | + { |
| 88 | + return $this->name; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + public function getPrototype(): ClassMemberReflection |
| 93 | + { |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + public function getVariants(): array |
| 99 | + { |
| 100 | + if ($this->prefix === 'get') { |
| 101 | + return [ |
| 102 | + new FunctionVariant( |
| 103 | + TemplateTypeMap::createEmpty(), |
| 104 | + TemplateTypeMap::createEmpty(), |
| 105 | + [], |
| 106 | + false, |
| 107 | + new MixedType, |
| 108 | + ), |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + return [ |
| 113 | + new FunctionVariant( |
| 114 | + TemplateTypeMap::createEmpty(), |
| 115 | + TemplateTypeMap::createEmpty(), |
| 116 | + [], |
| 117 | + true, |
| 118 | + new StaticType($this->declaringClass), |
| 119 | + ), |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + public function isDeprecated(): TrinaryLogic |
| 125 | + { |
| 126 | + return TrinaryLogic::createNo(); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + public function getDeprecatedDescription(): ?string |
| 131 | + { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + public function isFinal(): TrinaryLogic |
| 137 | + { |
| 138 | + return TrinaryLogic::createYes(); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + public function isInternal(): TrinaryLogic |
| 143 | + { |
| 144 | + return TrinaryLogic::createNo(); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + public function getThrowType(): ?Type |
| 149 | + { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + public function hasSideEffects(): TrinaryLogic |
| 155 | + { |
| 156 | + return $this->prefix === 'get' |
| 157 | + ? TrinaryLogic::createNo() |
| 158 | + : TrinaryLogic::createYes(); |
| 159 | + } |
| 160 | +} |
0 commit comments