Conditions | 11 |
Paths | 5 |
Total Lines | 65 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
80 | public function generateQuery(string $namespaceAlias, array $path): string |
||
81 | { |
||
82 | $query = ''; |
||
83 | |||
84 | while (\count($path)) { |
||
85 | $p = \array_shift($path); |
||
86 | $next = $path[0] ?? null; |
||
87 | |||
88 | // Reduce to only the upper/lower of the active letters |
||
89 | // ≈30-35% faster than the full alphabet |
||
90 | $pLet = \count_chars($p, 3); |
||
91 | $pLow = \mb_strtolower($pLet); |
||
92 | $pUp = \mb_strtoupper($pLet); |
||
93 | $pAttr = ('@' === \mb_substr($p, 0, 1)); |
||
94 | |||
95 | if (\is_int($next)) { |
||
96 | if ($this->caseSensitive || $pAttr) { |
||
97 | // case; next |
||
98 | $query .= \sprintf( |
||
99 | '/%s%s[position() = %d]', |
||
100 | (!$pAttr |
||
101 | ? $namespaceAlias . ':' |
||
102 | : ''), |
||
103 | $p, |
||
104 | \array_shift($path) + 1 |
||
105 | ); |
||
106 | } else { |
||
107 | // icase; next |
||
108 | $query .= \sprintf( |
||
109 | '/%s*[translate(name(), \'%s\', \'%s\') = \'%s\'][position() = %d]', |
||
110 | (!$pAttr |
||
111 | ? $namespaceAlias . ':' |
||
112 | : ''), |
||
113 | $pUp, |
||
114 | $pLow, |
||
115 | $p, |
||
116 | \array_shift($path) + 1 |
||
117 | ); |
||
118 | } |
||
119 | } else { |
||
120 | if ($this->caseSensitive || $pAttr) { |
||
121 | // case; no-next |
||
122 | $query .= \sprintf( |
||
123 | '/%s%s', |
||
124 | (!$pAttr |
||
125 | ? $namespaceAlias . ':' |
||
126 | : ''), |
||
127 | $p |
||
128 | ); |
||
129 | } else { |
||
130 | // icase; no-next |
||
131 | $query .= \sprintf( |
||
132 | '/%s*[translate(name(), \'%s\', \'%s\') = \'%s\']', |
||
133 | (!$pAttr |
||
134 | ? $namespaceAlias . ':' |
||
135 | : ''), |
||
136 | $pUp, |
||
137 | $pLow, |
||
138 | $p |
||
139 | ); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return $query; |
||
145 | } |
||
147 |