|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Carbon\PHPStan; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use stdClass; |
| 9 | +use ErrorException; |
| 10 | +use ReflectionClass; |
| 11 | +use ReflectionFunction; |
| 12 | +use PHPStan\Reflection\Php\BuiltinMethodReflection; |
| 13 | + |
| 14 | +final class Macro implements BuiltinMethodReflection |
| 15 | +{ |
| 16 | + /** |
| 17 | + * The class name. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + private $className; |
| 22 | + |
| 23 | + /** |
| 24 | + * The method name. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + private $methodName; |
| 29 | + |
| 30 | + /** |
| 31 | + * The reflection function. |
| 32 | + * |
| 33 | + * @var ReflectionFunction |
| 34 | + */ |
| 35 | + private $reflectionFunction; |
| 36 | + |
| 37 | + /** |
| 38 | + * The parameters. |
| 39 | + * |
| 40 | + * @var array |
| 41 | + */ |
| 42 | + private $parameters; |
| 43 | + |
| 44 | + /** |
| 45 | + * The is static. |
| 46 | + * |
| 47 | + * @var bool |
| 48 | + */ |
| 49 | + private $isStatic = false; |
| 50 | + |
| 51 | + /** |
| 52 | + * Macro constructor. |
| 53 | + * |
| 54 | + * @param string $className |
| 55 | + * @param string $methodName |
| 56 | + * @param ReflectionFunction $reflectionFunction |
| 57 | + */ |
| 58 | + public function __construct(string $className, string $methodName, ReflectionFunction $reflectionFunction) |
| 59 | + { |
| 60 | + $this->className = $className; |
| 61 | + $this->methodName = $methodName; |
| 62 | + $this->reflectionFunction = $reflectionFunction; |
| 63 | + $this->parameters = $this->reflectionFunction->getParameters(); |
| 64 | + $this->isStatic = false; |
| 65 | + |
| 66 | + if ($this->reflectionFunction->isClosure()) { |
| 67 | + try { |
| 68 | + /** @var Closure $closure */ |
| 69 | + $closure = $this->reflectionFunction->getClosure(); |
| 70 | + Closure::bind($closure, new stdClass); |
| 71 | + // The closure can be bound so it was not explicitly marked as static |
| 72 | + } catch (ErrorException $e) { |
| 73 | + // The closure was explicitly marked as static |
| 74 | + $this->isStatic = true; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritdoc} |
| 81 | + */ |
| 82 | + public function getDeclaringClass(): ReflectionClass |
| 83 | + { |
| 84 | + return new ReflectionClass($this->className); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function isPrivate(): bool |
| 91 | + { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritdoc} |
| 97 | + */ |
| 98 | + public function isPublic(): bool |
| 99 | + { |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + public function isFinal(): bool |
| 104 | + { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + public function isInternal(): bool |
| 109 | + { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + public function isAbstract(): bool |
| 114 | + { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * {@inheritdoc} |
| 120 | + */ |
| 121 | + public function isStatic(): bool |
| 122 | + { |
| 123 | + return $this->isStatic; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Set the is static value. |
| 128 | + * |
| 129 | + * @param bool $isStatic |
| 130 | + * |
| 131 | + * @return void |
| 132 | + */ |
| 133 | + public function setIsStatic(bool $isStatic): void |
| 134 | + { |
| 135 | + $this->isStatic = $isStatic; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * {@inheritdoc} |
| 140 | + */ |
| 141 | + public function getDocComment() |
| 142 | + { |
| 143 | + return $this->reflectionFunction->getDocComment(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * {@inheritdoc} |
| 148 | + */ |
| 149 | + public function getFileName() |
| 150 | + { |
| 151 | + return $this->reflectionFunction->getFileName(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * {@inheritdoc} |
| 156 | + */ |
| 157 | + public function getName(): string |
| 158 | + { |
| 159 | + return $this->methodName; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * {@inheritdoc} |
| 164 | + */ |
| 165 | + public function getParameters(): array |
| 166 | + { |
| 167 | + return $this->parameters; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Set the parameters value. |
| 172 | + * |
| 173 | + * @param array $parameters |
| 174 | + * |
| 175 | + * @return void |
| 176 | + */ |
| 177 | + public function setParameters(array $parameters): void |
| 178 | + { |
| 179 | + $this->parameters = $parameters; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * {@inheritdoc} |
| 184 | + */ |
| 185 | + public function getReturnType(): ?\ReflectionType |
| 186 | + { |
| 187 | + return $this->reflectionFunction->getReturnType(); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * {@inheritdoc} |
| 192 | + */ |
| 193 | + public function getStartLine() |
| 194 | + { |
| 195 | + return $this->reflectionFunction->getStartLine(); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * {@inheritdoc} |
| 200 | + */ |
| 201 | + public function getEndLine() |
| 202 | + { |
| 203 | + return $this->reflectionFunction->getEndLine(); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * {@inheritdoc} |
| 208 | + */ |
| 209 | + public function isDeprecated(): bool |
| 210 | + { |
| 211 | + return $this->reflectionFunction->isDeprecated(); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * {@inheritdoc} |
| 216 | + */ |
| 217 | + public function isVariadic(): bool |
| 218 | + { |
| 219 | + return $this->reflectionFunction->isVariadic(); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * {@inheritdoc} |
| 224 | + */ |
| 225 | + public function getPrototype(): BuiltinMethodReflection |
| 226 | + { |
| 227 | + return $this; |
| 228 | + } |
| 229 | +} |
0 commit comments