AbstractMacro.php
2 years ago
Macro.php
2 years ago
MacroExtension.php
2 years ago
MacroScanner.php
2 years ago
MacroScanner.php
68 lines
| 1 | <?php |
| 2 | |
| 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 IAWPSCOPED\Carbon\PHPStan; |
| 12 | |
| 13 | use IAWPSCOPED\Carbon\CarbonInterface; |
| 14 | use IAWPSCOPED\PHPStan\Reflection\ReflectionProvider; |
| 15 | use ReflectionClass; |
| 16 | use ReflectionException; |
| 17 | /** @internal */ |
| 18 | final class MacroScanner |
| 19 | { |
| 20 | /** |
| 21 | * @var \PHPStan\Reflection\ReflectionProvider |
| 22 | */ |
| 23 | private $reflectionProvider; |
| 24 | /** |
| 25 | * MacroScanner constructor. |
| 26 | * |
| 27 | * @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider |
| 28 | */ |
| 29 | public function __construct(ReflectionProvider $reflectionProvider) |
| 30 | { |
| 31 | $this->reflectionProvider = $reflectionProvider; |
| 32 | } |
| 33 | /** |
| 34 | * Return true if the given pair class-method is a Carbon macro. |
| 35 | * |
| 36 | * @param class-string $className |
| 37 | * @param string $methodName |
| 38 | * |
| 39 | * @return bool |
| 40 | */ |
| 41 | public function hasMethod(string $className, string $methodName) : bool |
| 42 | { |
| 43 | $classReflection = $this->reflectionProvider->getClass($className); |
| 44 | if ($classReflection->getName() !== CarbonInterface::class && !$classReflection->isSubclassOf(CarbonInterface::class)) { |
| 45 | return \false; |
| 46 | } |
| 47 | return \is_callable([$className, 'hasMacro']) && $className::hasMacro($methodName); |
| 48 | } |
| 49 | /** |
| 50 | * Return the Macro for a given pair class-method. |
| 51 | * |
| 52 | * @param class-string $className |
| 53 | * @param string $methodName |
| 54 | * |
| 55 | * @throws ReflectionException |
| 56 | * |
| 57 | * @return Macro |
| 58 | */ |
| 59 | public function getMethod(string $className, string $methodName) : Macro |
| 60 | { |
| 61 | $reflectionClass = new ReflectionClass($className); |
| 62 | $property = $reflectionClass->getProperty('globalMacros'); |
| 63 | $property->setAccessible(\true); |
| 64 | $macro = $property->getValue()[$methodName]; |
| 65 | return new Macro($className, $methodName, $macro); |
| 66 | } |
| 67 | } |
| 68 |