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