reflection.php ➔ getPhpDefinitionsFromFile()   D
last analyzed

Complexity

Conditions 19
Paths 2

Size

Total Lines 58

Duplication

Lines 35
Ratio 60.34 %

Importance

Changes 0
Metric Value
cc 19
nc 2
nop 1
dl 35
loc 58
rs 4.5166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if (!function_exists('short_class_name')) {
4
5
    /**
6
     * @param $object
7
     * @return string
8
     */
9
    function short_class_name($object): string
10
    {
11
        $objectProperties = new \ReflectionClass($object);
12
13
        return $objectProperties->getShortName();
14
    }
15
}
16
17
if (!function_exists('class_constants')) {
18
19
    /**
20
     * Get all class constants that starts with $startsWithFilter
21
     * or if empty get all class constants.
22
     * @param $object
23
     * @param string $startsWithFilter
24
     * @return array
25
     * @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php
26
     */
27
    function class_constants($object, string $startsWithFilter = ''): array
28
    {
29
        $objectProperties = new \ReflectionClass($object);
30
31
        $constants = $objectProperties->getConstants();
32
33
        if ($startsWithFilter == '') {
34
            return $constants;
35
        }
36
37
        return array_filter($constants, function ($key) use ($startsWithFilter) {
38
            return starts_with(strtolower($key), strtolower($startsWithFilter));
39
        }, ARRAY_FILTER_USE_KEY);
40
    }
41
}
42
43
if (!function_exists('class_uses_recursive')) {
44
    /**
45
     * Returns all traits used by a class, it's subclasses and trait of their traits
46
     *
47
     * @param  string $class
48
     * @return array
49
     */
50
    function class_uses_recursive($class)
51
    {
52
        $results = [];
53
        foreach (array_merge([$class => $class], class_parents($class)) as $class) {
54
            $results += trait_uses_recursive($class);
55
        }
56
        return array_unique($results);
57
    }
58
}
59
60
if (!function_exists('class_basename')) {
61
    /**
62
     * Get the class "basename" of the given object / class.
63
     *
64
     * @param  string|object $class
65
     * @return string
66
     */
67
    function class_basename($class)
68
    {
69
        $class = is_object($class) ? get_class($class) : $class;
70
        return basename(str_replace('\\', '/', $class));
71
    }
72
}
73
74
/**
75
 * Get the class name from a php file
76
 *
77
 * @param string $filePath
78
 *
79
 * @return string|null
80
 * @see https://github.com/laradic/support/blob/master/src/Util.php
81
 */
82
function getClassNameFromFile($filePath)
83
{
84
    $tokens = token_get_all(file_get_contents($filePath));
85
    $iMax = count($tokens);
86
    for ($i = 0; $i < $iMax; $i++) {
87
        if ($tokens[$i][0] === T_TRAIT || $tokens[$i][0] === T_INTERFACE) {
88
            return;
89
        }
90
        if ($tokens[$i][0] === T_CLASS) {
91
            for ($j = $i + 1; $j < $iMax; $j++) {
92
                if ($tokens[$j] === '{') {
93
                    return $tokens[$i + 2][1];
94
                }
95
            }
96
        }
97
    }
98
    return null;
99
}
100
101
/**
102
 * Get the namespace of the php file
103
 *
104
 * @param $filePath
105
 *
106
 * @return string|null
107
 * @see https://github.com/laradic/support/blob/master/src/Util.php
108
 */
109
function getNamespaceFromFile($filePath)
110
{
111
    $namespace = '';
112
    $tokens = token_get_all(file_get_contents($filePath));
113
    $iMax = count($tokens);
114
    for ($i = 0; $i < $iMax; $i++) {
115 View Code Duplication
        if ($tokens[$i][0] === T_NAMESPACE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
            for ($j = $i + 1; $j < $iMax; $j++) {
117
                if ($tokens[$j][0] === T_STRING) {
118
                    $namespace .= '\\' . $tokens[$j][1];
119
                } else {
120
                    if ($tokens[$j] === '{' || $tokens[$j] === ';') {
121
                        return $namespace;
122
                    }
123
                }
124
            }
125
        }
126
    }
127
    return null;
128
}
129
130
/**
131
 * Get the namespace, classes, interfaces and traits of the php file
132
 *
133
 * @param $filePath
134
 *
135
 * @return array
136
 * @see https://github.com/laradic/support/blob/master/src/Util.php
137
 */
138
function getPhpDefinitionsFromFile($filePath)
139
{
140
    $classes = [];
141
    $traits = [];
142
    $interfaces = [];
143
    $fp = fopen($filePath, 'r');
144
    $class = $namespace = $buffer = '';
145
    $i = 0;
146
    while (!$class) {
147
        if (feof($fp)) {
148
            break;
149
        }
150
        $buffer .= fread($fp, 512);
151
        $tokens = token_get_all($buffer);
152
        if (strpos($buffer, '{') === false) {
153
            continue;
154
        }
155
        $iMax = count($tokens);
156
        for (; $i < $iMax; $i++) {
157 View Code Duplication
            if ($tokens[$i][0] === T_NAMESPACE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
                for ($j = $i + 1; $j < $iMax; $j++) {
159
                    if ($tokens[$j][0] === T_STRING) {
160
                        $namespace .= '\\' . $tokens[$j][1];
161
                    } else {
162
                        if ($tokens[$j] === '{' || $tokens[$j] === ';') {
163
                            break;
164
                        }
165
                    }
166
                }
167
            }
168 View Code Duplication
            if ($tokens[$i][0] === T_CLASS) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
                for ($j = $i + 1; $j < $iMax; $j++) {
170
                    if ($tokens[$j] === '{') {
171
                        $class = $tokens[$i + 2][1];
172
                        $classes[] = $class;
173
                    }
174
                }
175
            }
176 View Code Duplication
            if ($tokens[$i][0] === T_INTERFACE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
                for ($j = $i + 1; $j < $iMax; $j++) {
178
                    if ($tokens[$j] === '{') {
179
                        $interface = $tokens[$i + 2][1];
180
                        $interfaces[] = $interface;
181
                    }
182
                }
183
            }
184 View Code Duplication
            if ($tokens[$i][0] === T_TRAIT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
                for ($j = $i + 1; $j < $iMax; $j++) {
186
                    if ($tokens[$j] === '{') {
187
                        $trait = $tokens[$i + 2][1];
188
                        $traits[] = $trait;
189
                    }
190
                }
191
            }
192
        }
193
    }
194
    return compact('namespace', 'classes', 'traits', 'interfaces');
195
}
196