Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Carbon\PHPStan;
13
 
14
use Carbon\CarbonInterface;
15
use PHPStan\Reflection\ReflectionProvider;
16
use ReflectionClass;
17
use ReflectionException;
18
 
19
final class MacroScanner
20
{
21
    /**
22
     * @var \PHPStan\Reflection\ReflectionProvider
23
     */
24
    private $reflectionProvider;
25
 
26
    /**
27
     * MacroScanner constructor.
28
     *
29
     * @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
30
     */
31
    public function __construct(ReflectionProvider $reflectionProvider)
32
    {
33
        $this->reflectionProvider = $reflectionProvider;
34
    }
35
 
36
    /**
37
     * Return true if the given pair class-method is a Carbon macro.
38
     *
39
     * @param string $className
40
     * @phpstan-param class-string $className
41
     *
42
     * @param string $methodName
43
     *
44
     * @return bool
45
     */
46
    public function hasMethod(string $className, string $methodName): bool
47
    {
48
        $classReflection = $this->reflectionProvider->getClass($className);
49
 
50
        if (
51
            $classReflection->getName() !== CarbonInterface::class &&
52
            !$classReflection->isSubclassOf(CarbonInterface::class)
53
        ) {
54
            return false;
55
        }
56
 
57
        return \is_callable([$className, 'hasMacro']) &&
58
            $className::hasMacro($methodName);
59
    }
60
 
61
    /**
62
     * Return the Macro for a given pair class-method.
63
     *
64
     * @param string $className
65
     * @phpstan-param class-string $className
66
     *
67
     * @param string $methodName
68
     *
69
     * @throws ReflectionException
70
     *
71
     * @return Macro
72
     */
73
    public function getMethod(string $className, string $methodName): Macro
74
    {
75
        $reflectionClass = new ReflectionClass($className);
76
        $property = $reflectionClass->getProperty('globalMacros');
77
 
78
        $property->setAccessible(true);
79
        $macro = $property->getValue()[$methodName];
80
 
81
        return new Macro(
82
            $className,
83
            $methodName,
84
            $macro
85
        );
86
    }
87
}