AbstractMacro.php
2 years ago
Macro.php
2 years ago
MacroExtension.php
2 years ago
MacroScanner.php
2 years ago
AbstractMacro.php
239 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | /** |
| 5 | * This file is part of the Carbon package. |
| 6 | * |
| 7 | * (c) Brian Nesbitt <brian@nesbot.com> |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | namespace IAWPSCOPED\Carbon\PHPStan; |
| 13 | |
| 14 | use Closure; |
| 15 | use InvalidArgumentException; |
| 16 | use IAWPSCOPED\PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter as AdapterReflectionParameter; |
| 17 | use IAWPSCOPED\PHPStan\BetterReflection\Reflection\Adapter\ReflectionType as AdapterReflectionType; |
| 18 | use IAWPSCOPED\PHPStan\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass; |
| 19 | use IAWPSCOPED\PHPStan\BetterReflection\Reflection\ReflectionFunction as BetterReflectionFunction; |
| 20 | use IAWPSCOPED\PHPStan\BetterReflection\Reflection\ReflectionParameter as BetterReflectionParameter; |
| 21 | use IAWPSCOPED\PHPStan\Reflection\Php\BuiltinMethodReflection; |
| 22 | use IAWPSCOPED\PHPStan\TrinaryLogic; |
| 23 | use ReflectionClass; |
| 24 | use ReflectionFunction; |
| 25 | use ReflectionMethod; |
| 26 | use ReflectionParameter; |
| 27 | use ReflectionType; |
| 28 | use stdClass; |
| 29 | use Throwable; |
| 30 | /** @internal */ |
| 31 | abstract class AbstractMacro implements BuiltinMethodReflection |
| 32 | { |
| 33 | /** |
| 34 | * The reflection function/method. |
| 35 | * |
| 36 | * @var ReflectionFunction|ReflectionMethod |
| 37 | */ |
| 38 | protected $reflectionFunction; |
| 39 | /** |
| 40 | * The class name. |
| 41 | * |
| 42 | * @var class-string |
| 43 | */ |
| 44 | private $className; |
| 45 | /** |
| 46 | * The method name. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | private $methodName; |
| 51 | /** |
| 52 | * The parameters. |
| 53 | * |
| 54 | * @var ReflectionParameter[] |
| 55 | */ |
| 56 | private $parameters; |
| 57 | /** |
| 58 | * The is static. |
| 59 | * |
| 60 | * @var bool |
| 61 | */ |
| 62 | private $static = \false; |
| 63 | /** |
| 64 | * Macro constructor. |
| 65 | * |
| 66 | * @param class-string $className |
| 67 | * @param string $methodName |
| 68 | * @param callable $macro |
| 69 | */ |
| 70 | public function __construct(string $className, string $methodName, $macro) |
| 71 | { |
| 72 | $this->className = $className; |
| 73 | $this->methodName = $methodName; |
| 74 | $rawReflectionFunction = \is_array($macro) ? new ReflectionMethod($macro[0], $macro[1]) : new ReflectionFunction($macro); |
| 75 | $this->reflectionFunction = self::hasModernParser() ? $this->getReflectionFunction($macro) : $rawReflectionFunction; |
| 76 | // @codeCoverageIgnore |
| 77 | $this->parameters = \array_map(function ($parameter) { |
| 78 | if ($parameter instanceof BetterReflectionParameter) { |
| 79 | return new AdapterReflectionParameter($parameter); |
| 80 | } |
| 81 | return $parameter; |
| 82 | // @codeCoverageIgnore |
| 83 | }, $this->reflectionFunction->getParameters()); |
| 84 | if ($rawReflectionFunction->isClosure()) { |
| 85 | try { |
| 86 | $closure = $rawReflectionFunction->getClosure(); |
| 87 | $boundClosure = Closure::bind($closure, new stdClass()); |
| 88 | $this->static = !$boundClosure || (new ReflectionFunction($boundClosure))->getClosureThis() === null; |
| 89 | } catch (Throwable $e) { |
| 90 | $this->static = \true; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | private function getReflectionFunction($spec) |
| 95 | { |
| 96 | if (\is_array($spec) && \count($spec) === 2 && \is_string($spec[1])) { |
| 97 | \assert($spec[1] !== ''); |
| 98 | if (\is_object($spec[0])) { |
| 99 | return BetterReflectionClass::createFromInstance($spec[0])->getMethod($spec[1]); |
| 100 | } |
| 101 | return BetterReflectionClass::createFromName($spec[0])->getMethod($spec[1]); |
| 102 | } |
| 103 | if (\is_string($spec)) { |
| 104 | return BetterReflectionFunction::createFromName($spec); |
| 105 | } |
| 106 | if ($spec instanceof Closure) { |
| 107 | return BetterReflectionFunction::createFromClosure($spec); |
| 108 | } |
| 109 | throw new InvalidArgumentException('Could not create reflection from the spec given'); |
| 110 | // @codeCoverageIgnore |
| 111 | } |
| 112 | /** |
| 113 | * {@inheritdoc} |
| 114 | */ |
| 115 | public function getDeclaringClass() : ReflectionClass |
| 116 | { |
| 117 | return new ReflectionClass($this->className); |
| 118 | } |
| 119 | /** |
| 120 | * {@inheritdoc} |
| 121 | */ |
| 122 | public function isPrivate() : bool |
| 123 | { |
| 124 | return \false; |
| 125 | } |
| 126 | /** |
| 127 | * {@inheritdoc} |
| 128 | */ |
| 129 | public function isPublic() : bool |
| 130 | { |
| 131 | return \true; |
| 132 | } |
| 133 | /** |
| 134 | * {@inheritdoc} |
| 135 | */ |
| 136 | public function isFinal() : bool |
| 137 | { |
| 138 | return \false; |
| 139 | } |
| 140 | /** |
| 141 | * {@inheritdoc} |
| 142 | */ |
| 143 | public function isInternal() : bool |
| 144 | { |
| 145 | return \false; |
| 146 | } |
| 147 | /** |
| 148 | * {@inheritdoc} |
| 149 | */ |
| 150 | public function isAbstract() : bool |
| 151 | { |
| 152 | return \false; |
| 153 | } |
| 154 | /** |
| 155 | * {@inheritdoc} |
| 156 | */ |
| 157 | public function isStatic() : bool |
| 158 | { |
| 159 | return $this->static; |
| 160 | } |
| 161 | /** |
| 162 | * {@inheritdoc} |
| 163 | */ |
| 164 | public function getDocComment() : ?string |
| 165 | { |
| 166 | return $this->reflectionFunction->getDocComment() ?: null; |
| 167 | } |
| 168 | /** |
| 169 | * {@inheritdoc} |
| 170 | */ |
| 171 | public function getName() : string |
| 172 | { |
| 173 | return $this->methodName; |
| 174 | } |
| 175 | /** |
| 176 | * {@inheritdoc} |
| 177 | */ |
| 178 | public function getParameters() : array |
| 179 | { |
| 180 | return $this->parameters; |
| 181 | } |
| 182 | /** |
| 183 | * {@inheritdoc} |
| 184 | */ |
| 185 | public function getReturnType() : ?ReflectionType |
| 186 | { |
| 187 | $type = $this->reflectionFunction->getReturnType(); |
| 188 | if ($type instanceof ReflectionType) { |
| 189 | return $type; |
| 190 | // @codeCoverageIgnore |
| 191 | } |
| 192 | return self::adaptType($type); |
| 193 | } |
| 194 | /** |
| 195 | * {@inheritdoc} |
| 196 | */ |
| 197 | public function isDeprecated() : TrinaryLogic |
| 198 | { |
| 199 | return TrinaryLogic::createFromBoolean($this->reflectionFunction->isDeprecated() || \preg_match('/@deprecated/i', $this->getDocComment() ?: '')); |
| 200 | } |
| 201 | /** |
| 202 | * {@inheritdoc} |
| 203 | */ |
| 204 | public function isVariadic() : bool |
| 205 | { |
| 206 | return $this->reflectionFunction->isVariadic(); |
| 207 | } |
| 208 | /** |
| 209 | * {@inheritdoc} |
| 210 | */ |
| 211 | public function getPrototype() : BuiltinMethodReflection |
| 212 | { |
| 213 | return $this; |
| 214 | } |
| 215 | public function getTentativeReturnType() : ?ReflectionType |
| 216 | { |
| 217 | return null; |
| 218 | } |
| 219 | public function returnsByReference() : TrinaryLogic |
| 220 | { |
| 221 | return TrinaryLogic::createNo(); |
| 222 | } |
| 223 | private static function adaptType($type) |
| 224 | { |
| 225 | $method = \method_exists(AdapterReflectionType::class, 'fromTypeOrNull') ? 'fromTypeOrNull' : 'fromReturnTypeOrNull'; |
| 226 | // @codeCoverageIgnore |
| 227 | return AdapterReflectionType::$method($type); |
| 228 | } |
| 229 | private static function hasModernParser() : bool |
| 230 | { |
| 231 | static $modernParser = null; |
| 232 | if ($modernParser !== null) { |
| 233 | return $modernParser; |
| 234 | } |
| 235 | $modernParser = \method_exists(AdapterReflectionType::class, 'fromTypeOrNull'); |
| 236 | return $modernParser; |
| 237 | } |
| 238 | } |
| 239 |