Completed
Push — master ( d0cfcf...43547c )
by Lorenzo
02:02
created

reflection.php ➔ getNamespaceFromFile()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 11
Ratio 57.89 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 4
nop 1
dl 11
loc 19
rs 8.2222
c 0
b 0
f 0
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
    for ($i = 0; $i < count($tokens); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
86
        if ($tokens[$i][0] === T_TRAIT || $tokens[$i][0] === T_INTERFACE) {
87
            return;
88
        }
89
        if ($tokens[$i][0] === T_CLASS) {
90
            for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
91
                if ($tokens[$j] === '{') {
92
                    return $tokens[$i + 2][1];
93
                }
94
            }
95
        }
96
    }
97
    return null;
98
}
99
100
/**
101
 * Get the namespace of the php file
102
 *
103
 * @param $filePath
104
 *
105
 * @return string|null
106
 * @see https://github.com/laradic/support/blob/master/src/Util.php
107
 */
108
function getNamespaceFromFile($filePath)
109
{
110
    $namespace = '';
111
    $tokens = token_get_all(file_get_contents($filePath));
112
    for ($i = 0; $i < count($tokens); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
113 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...
114
            for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
115
                if ($tokens[$j][0] === T_STRING) {
116
                    $namespace .= '\\' . $tokens[$j][1];
117
                } else {
118
                    if ($tokens[$j] === '{' || $tokens[$j] === ';') {
119
                        return $namespace;
120
                    }
121
                }
122
            }
123
        }
124
    }
125
    return null;
126
}
127
128
/**
129
 * Get the namespace, classes, interfaces and traits of the php file
130
 *
131
 * @param $filePath
132
 *
133
 * @return array
134
 * @see https://github.com/laradic/support/blob/master/src/Util.php
135
 */
136
function getPhpDefinitionsFromFile($filePath)
137
{
138
    $classes = [];
139
    $traits = [];
140
    $interfaces = [];
141
    $fp = fopen($filePath, 'r');
142
    $trait = $interface = $class = $namespace = $buffer = '';
0 ignored issues
show
Unused Code introduced by
$interface is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$trait is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
143
    $i = 0;
144
    while (!$class) {
145
        if (feof($fp)) {
146
            break;
147
        }
148
        $buffer .= fread($fp, 512);
149
        $tokens = token_get_all($buffer);
150
        if (strpos($buffer, '{') === false) {
151
            continue;
152
        }
153
        for (; $i < count($tokens); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
154 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...
155
                for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
156
                    if ($tokens[$j][0] === T_STRING) {
157
                        $namespace .= '\\' . $tokens[$j][1];
158
                    } else {
159
                        if ($tokens[$j] === '{' || $tokens[$j] === ';') {
160
                            break;
161
                        }
162
                    }
163
                }
164
            }
165 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...
166
                for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
167
                    if ($tokens[$j] === '{') {
168
                        $class = $tokens[$i + 2][1];
169
                        $classes[] = $class;
170
                    }
171
                }
172
            }
173 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...
174
                for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
175
                    if ($tokens[$j] === '{') {
176
                        $interface = $tokens[$i + 2][1];
177
                        $interfaces[] = $interface;
178
                    }
179
                }
180
            }
181 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...
182
                for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
183
                    if ($tokens[$j] === '{') {
184
                        $trait = $tokens[$i + 2][1];
185
                        $traits[] = $trait;
186
                    }
187
                }
188
            }
189
        }
190
    }
191
    return compact('namespace', 'classes', 'traits', 'interfaces');
192
}
193