AbstractMacro.php 6.6 KB

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