Passed
Pull Request — master (#34)
by Evgeniy
01:51
created

UseStatementParser::normalizeUse()   D

Complexity

Conditions 18
Paths 11

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 18.0961

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 18
eloc 31
c 5
b 0
f 0
nc 11
nop 1
dl 0
loc 45
ccs 28
cts 30
cp 0.9333
crap 18.0961
rs 4.8666

How to fix   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
declare(strict_types=1);
4
5
namespace Yiisoft\VarDumper;
6
7
use function defined;
8
9
/**
10
 * UseStatementParser given a PHP file, returns a set of `use` statements from the code.
11
 */
12
final class UseStatementParser
13
{
14
    /**
15
     * @param string $file File to read.
16
     *
17
     * @return array Use statements data.
18
     */
19 44
    public function fromFile(string $file): array
20
    {
21 44
        $tokens = token_get_all(file_get_contents($file));
22 44
        array_shift($tokens);
23
24 44
        $uses = [];
25 44
        foreach ($tokens as $i => $token) {
26 44
            if (!isset($token[0])) {
27
                continue;
28
            }
29
30 44
            if ($token[0] === T_USE) {
31 44
                $uses = array_merge($uses, $this->normalizeUse(array_slice($tokens, $i + 1)));
32 44
                continue;
33
            }
34
        }
35
36 44
        return $uses;
37
    }
38
39
    /**
40
     * Normalizes raw tokens into uniform use statement data.
41
     *
42
     * @param array $tokens Raw tokens.
43
     *
44
     * @return array Normalized use statement data.
45
     */
46 44
    private function normalizeUse(array $tokens): array
47
    {
48 44
        $commonNamespace = '\\';
49 44
        $current = '';
50 44
        $alias = null;
51 44
        $uses = [];
52
53 44
        foreach ($tokens as $token) {
54 44
            if (!isset($token[0])) {
55
                continue;
56
            }
57
            if (
58 44
                $token[0] === T_STRING
59 44
                || $token[0] === T_NS_SEPARATOR
60 44
                || (defined('T_NAME_QUALIFIED') && $token[0] === T_NAME_QUALIFIED)
61 44
                || (defined('T_NAME_FULLY_QUALIFIED') && $token[0] === T_NAME_FULLY_QUALIFIED)
62 44
                || (defined('T_NAME_RELATIVE') && $token[0] === T_NAME_RELATIVE)
63
            ) {
64 44
                $current .= $token[1];
65 44
                continue;
66
            }
67 44
            if ($token === ',' || $token === ';') {
68 44
                if ($current !== '') {
69 44
                    if ($alias === null) {
70 44
                        $uses[] = $commonNamespace . $current;
71
                    } else {
72
                        $uses[$alias] = $commonNamespace . $current;
73
                    }
74 44
                    $current = '';
75
                }
76
            }
77 44
            if ($token === ';') {
78 44
                break;
79
            }
80 44
            if ($token === '{') {
81 1
                $commonNamespace .= $current;
82 1
                $current = '';
83 1
                continue;
84
            }
85 44
            if ($token[0] === T_AS) {
86 41
                $current .= '@';
87
            }
88
        }
89
90 44
        return $this->replaceAliases($uses);
91
    }
92
93 44
    private function replaceAliases(array $uses): array
94
    {
95 44
        $result = [];
96 44
        foreach ($uses as $use) {
97 44
            $delimiterPosition = strpos($use, '@');
98 44
            if ($delimiterPosition !== false) {
99 41
                $alias = mb_substr($use, $delimiterPosition + 1);
100 41
                $result[$alias] = mb_substr($use, 0, $delimiterPosition);
101
            } else {
102 42
                $result[substr(strrchr($use, '\\'), 1)] = $use;
103
            }
104
        }
105
106 44
        return $result;
107
    }
108
}
109