Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 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 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 12 | namespace Carbon\PHPStan; |
| 13 | |
| 14 | use Carbon\CarbonInterface; |
| 15 | use ReflectionClass; |
| 16 | use ReflectionException; |
| 17 | |
| 18 | final class MacroScanner |
| 19 | { |
| 20 | /** |
| 21 | * Return true if the given pair class-method is a Carbon macro. |
| 22 | * |
| 23 | * @param string $className |
| 24 | * @phpstan-param class-string $className |
| 25 | * |
| 26 | * @param string $methodName |
| 27 | * |
| 28 | * @return bool |
| 29 | */ |
| 30 | public function hasMethod(string $className, string $methodName): bool |
| 31 | { |
| 32 | return is_a($className, CarbonInterface::class, true) && |
| 33 | \is_callable([$className, 'hasMacro']) && |
| 34 | $className::hasMacro($methodName); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Return the Macro for a given pair class-method. |
| 39 | * |
| 40 | * @param string $className |
| 41 | * @phpstan-param class-string $className |
| 42 | * |
| 43 | * @param string $methodName |
| 44 | * |
| 45 | * @throws ReflectionException |
| 46 | * |
| 47 | * @return Macro |
| 48 | */ |
| 49 | public function getMethod(string $className, string $methodName): Macro |
| 50 | { |
| 51 | $reflectionClass = new ReflectionClass($className); |
| 52 | $property = $reflectionClass->getProperty('globalMacros'); |
| 53 | |
| 54 | $property->setAccessible(true); |
| 55 | $macro = $property->getValue()[$methodName]; |
| 56 | |
| 57 | return new Macro( |
| 58 | $className, |
| 59 | $methodName, |
| 60 | $macro |
| 61 | ); |
| 62 | } |
| 63 | } |