Completed
Push — master ( 37a2ca...e45344 )
by Maarten
01:19
created

ClassReader::isClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Services;
4
5
use Illuminate\Support\Collection;
6
use ReflectionException;
7
use ReflectionMethod;
8
use ReflectionClass;
9
10
class ClassReader
11
{
12
    /**
13
     * @var bool
14
     */
15
    private static $hasNamespace;
16
17
    /**
18
     * @var bool
19
     */
20
    private static $hasClass;
21
22
    /**
23
     * @var string
24
     */
25
    private static $namespace;
26
27
    /**
28
     * @var string
29
     */
30
    private static $class;
31
32
33
    /**
34
     * @param string $path
35
     * @return string
36
     */
37
    public static function getClassName(string $path): string
38
    {
39
        self::$namespace = self::$class = '';
40
        self::$hasNamespace = self::$hasClass = false;
41
42
        foreach (token_get_all(file_get_contents($path)) as $token) {
43
            $tokenArray = is_array($token);
44
45
            self::isNameSpace($tokenArray, $token);
46
            self::isClass($tokenArray, $token);
47
            self::setNamespace($tokenArray, $token);
48
            self::setClassName($tokenArray, $token);
49
50
            if (self::$class) {
51
                break;
52
            }
53
        }
54
55
        return self::$namespace ? self::$namespace . '\\' . self::$class : self::$class;
56
    }
57
58
    /**
59
     * @param bool $tokenArray
60
     * @param $token
61
     * @return bool
62
     */
63
    private static function isClass(bool $tokenArray, $token)
64
    {
65
        self::$hasClass = self::$hasClass || ($tokenArray && $token[0] === T_CLASS);
66
    }
67
68
    /**
69
     * @param bool $tokenArray
70
     * @param $token
71
     * @return bool
72
     */
73
    private static function isNameSpace(bool $tokenArray, $token)
74
    {
75
        self::$hasNamespace = self::$hasNamespace || ($tokenArray && $token[0] === T_NAMESPACE);
76
    }
77
78
    private static function setNamespace(bool $tokenArray, $token)
79
    {
80
        if (self::$hasNamespace) {
81
            if ($tokenArray && in_array($token[0], [T_STRING, T_NS_SEPARATOR], true)) {
82
                self::$namespace .= $token[1];
83
            } else if ($token === ';') {
84
                self::$hasNamespace = false;
85
            }
86
        }
87
    }
88
89
    /**
90
     * @param bool $tokenArray
91
     * @param $token
92
     * @return mixed
93
     */
94
    private static function setClassName(bool $tokenArray, $token)
95
    {
96
        if (self::$hasClass && $tokenArray && $token[0] === T_STRING) {
97
            self::$class = $token[1];
98
        }
99
    }
100
101
    /**
102
     * @param string $className
103
     * @return Collection
104
     * @throws ReflectionException
105
     */
106
    public static function getMethods(string $className): Collection
107
    {
108
        $class = new ReflectionClass($className);
109
110
        return Collection::make($class->getMethods(ReflectionMethod::IS_PUBLIC))
111
            ->merge(Collection::make($class->getTraits())
112
                ->map(static function (ReflectionClass $trait) {
113
                    return Collection::make(
114
                        $trait->getMethods(ReflectionMethod::IS_PUBLIC)
115
                    );
116
                })->flatten()
117
            )
118
            ->reject(static function (ReflectionMethod $method) use ($className) {
119
                return $method->class !== $className || $method->getNumberOfParameters() > 0;
120
            });
121
    }
122
}
123