| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\PHPStan; |
| 15 |
|
| 16 |
use Carbon\CarbonInterface; |
| 17 |
use Carbon\FactoryImmutable; |
| 18 |
use Closure; |
| 19 |
use InvalidArgumentException; |
| 20 |
use PHPStan\Reflection\ClassReflection; |
| 21 |
use PHPStan\Reflection\MethodReflection; |
| 22 |
use PHPStan\Reflection\MethodsClassReflectionExtension; |
| 23 |
use PHPStan\Reflection\ReflectionProvider; |
| 24 |
use PHPStan\Type\ClosureTypeFactory; |
| 25 |
use ReflectionFunction; |
| 26 |
use ReflectionMethod; |
| 27 |
use stdClass; |
| 28 |
use Throwable; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
final class MacroExtension implements MethodsClassReflectionExtension |
| 36 |
{ |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
protected $reflectionProvider; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected $closureTypeFactory; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( |
| 54 |
ReflectionProvider $reflectionProvider, |
| 55 |
ClosureTypeFactory $closureTypeFactory |
| 56 |
) { |
| 57 |
$this->reflectionProvider = $reflectionProvider; |
| 58 |
$this->closureTypeFactory = $closureTypeFactory; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
| 65 |
{ |
| 66 |
if ( |
| 67 |
$classReflection->getName() !== CarbonInterface::class && |
| 68 |
!$classReflection->isSubclassOf(CarbonInterface::class) |
| 69 |
) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
$className = $classReflection->getName(); |
| 74 |
|
| 75 |
return \is_callable([$className, 'hasMacro']) && |
| 76 |
$className::hasMacro($methodName); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
| 83 |
{ |
| 84 |
$macros = FactoryImmutable::getDefaultInstance()->getSettings()['macros'] ?? []; |
| 85 |
$macro = $macros[$methodName] ?? throw new InvalidArgumentException("Macro '$methodName' not found"); |
| 86 |
$static = false; |
| 87 |
$final = false; |
| 88 |
$deprecated = false; |
| 89 |
$docComment = null; |
| 90 |
|
| 91 |
if (\is_array($macro) && \count($macro) === 2 && \is_string($macro[1])) { |
| 92 |
\assert($macro[1] !== ''); |
| 93 |
|
| 94 |
$reflection = new ReflectionMethod($macro[0], $macro[1]); |
| 95 |
$closure = \is_object($macro[0]) ? $reflection->getClosure($macro[0]) : $reflection->getClosure(); |
| 96 |
|
| 97 |
$static = $reflection->isStatic(); |
| 98 |
$final = $reflection->isFinal(); |
| 99 |
$deprecated = $reflection->isDeprecated(); |
| 100 |
$docComment = $reflection->getDocComment() ?: null; |
| 101 |
} elseif (\is_string($macro)) { |
| 102 |
$reflection = new ReflectionFunction($macro); |
| 103 |
$closure = $reflection->getClosure(); |
| 104 |
$deprecated = $reflection->isDeprecated(); |
| 105 |
$docComment = $reflection->getDocComment() ?: null; |
| 106 |
} elseif ($macro instanceof Closure) { |
| 107 |
$closure = $macro; |
| 108 |
|
| 109 |
try { |
| 110 |
$boundClosure = Closure::bind($closure, new stdClass()); |
| 111 |
$static = (!$boundClosure || (new ReflectionFunction($boundClosure))->getClosureThis() === null); |
| 112 |
} catch (Throwable) { |
| 113 |
$static = true; |
| 114 |
} |
| 115 |
|
| 116 |
$reflection = new ReflectionFunction($macro); |
| 117 |
$deprecated = $reflection->isDeprecated(); |
| 118 |
$docComment = $reflection->getDocComment() ?: null; |
| 119 |
} |
| 120 |
|
| 121 |
if (!isset($closure)) { |
| 122 |
throw new InvalidArgumentException('Could not create reflection from the spec given'); |
| 123 |
} |
| 124 |
|
| 125 |
$closureType = $this->closureTypeFactory->fromClosureObject($closure); |
| 126 |
|
| 127 |
return new MacroMethodReflection( |
| 128 |
$classReflection, |
| 129 |
$methodName, |
| 130 |
$closureType, |
| 131 |
$static, |
| 132 |
$final, |
| 133 |
$deprecated, |
| 134 |
$docComment, |
| 135 |
); |
| 136 |
} |
| 137 |
} |