blob: d169939d82186860f7b9b486edafe55a9770edf8 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01003/**
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 Benkard7b2a3a12021-08-16 10:57:25 +020012namespace Carbon\PHPStan;
13
14use Carbon\CarbonInterface;
15use ReflectionClass;
16use ReflectionException;
17
18final 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}