Conditions | 61 |
Paths | > 20000 |
Total Lines | 225 |
Code Lines | 123 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 2 | Features | 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 |
||
53 | function normalize_lines($filename): void |
||
54 | { |
||
55 | $stream = file_get_contents($filename); |
||
56 | |||
57 | $text = $stream; |
||
58 | $text = str_replace("\r\n", "\n", $text); |
||
59 | $text = str_replace("\t", ' ', $text); |
||
60 | |||
61 | if (empty(trim($text))) { |
||
62 | // Warn |
||
63 | global $warnings; |
||
64 | $warnings[] = "File $filename is empty"; |
||
65 | |||
66 | return; |
||
67 | } |
||
68 | |||
69 | $lines = explode("\n", $text); |
||
70 | $new_lines = []; |
||
71 | |||
72 | $last_line = ''; |
||
73 | $was_last_line_heading = false; |
||
74 | $is_inside_fenced_code_block = false; |
||
75 | $is_inside_fenced_fenced_code_block = false; |
||
76 | $firstHeadingLevel = null; |
||
77 | foreach ($lines as $index => $line) { |
||
78 | global $linesCounted; |
||
79 | $linesCounted++; |
||
80 | |||
81 | /** Normalization */ |
||
82 | |||
83 | // Remove multiple empty lines |
||
84 | if (trim($line) == '' && trim($last_line) == '') { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | // Make sure there is a space after headings |
||
89 | if ($was_last_line_heading && trim($line) != '') { |
||
90 | $new_lines[] = ''; |
||
91 | } |
||
92 | |||
93 | // Make sure there are two empty lines before level 2 headings (but not if it's the first l2 heading) |
||
94 | if ($is_inside_fenced_code_block !== true && str_starts_with($line, '## ') && $index > $firstHeadingLevel + 3) { |
||
95 | $new_lines[] = ''; |
||
96 | } |
||
97 | |||
98 | if ($firstHeadingLevel === null && str_starts_with($line, '# ')) { |
||
99 | $firstHeadingLevel = $index; |
||
100 | } |
||
101 | |||
102 | // Check if line is a heading |
||
103 | if (str_starts_with($line, '##')) { |
||
104 | $was_last_line_heading = true; |
||
105 | global $headings; |
||
106 | $headings[$filename][$index + 1] = $line; |
||
107 | } else { |
||
108 | $was_last_line_heading = false; |
||
109 | } |
||
110 | |||
111 | // Make sure there is a space before opening a fenced code block (search for ```language) |
||
112 | if (str_starts_with($line, '```') && $line !== '```' && trim($last_line) != '') { |
||
113 | if (! $is_inside_fenced_fenced_code_block) { |
||
114 | $new_lines[] = ''; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // Check if line is a fenced code block |
||
119 | if (str_starts_with($line, '``')) { |
||
120 | $is_inside_fenced_code_block = ! $is_inside_fenced_code_block; |
||
121 | } |
||
122 | |||
123 | // Check if line is an escaped fenced code block |
||
124 | if (str_starts_with($line, '````')) { |
||
125 | $is_inside_fenced_fenced_code_block = ! $is_inside_fenced_fenced_code_block; |
||
126 | } |
||
127 | |||
128 | // Remove trailing spaces |
||
129 | $line = rtrim($line); |
||
130 | |||
131 | $new_lines[] = $line; |
||
132 | $last_line = $line; |
||
133 | |||
134 | /** Linting */ |
||
135 | |||
136 | // if not inside fenced code block |
||
137 | if (! $is_inside_fenced_code_block) { |
||
138 | // Add any links to buffer, so we can check them later |
||
139 | preg_match_all('/\[([^\[]+)]\((.*)\)/', $line, $matches); |
||
140 | if (count($matches) > 0) { |
||
141 | foreach ($matches[2] as $match) { |
||
142 | // If link is for an anchor, prefix the filename |
||
143 | if (str_starts_with($match, '#')) { |
||
144 | $match = 'ANCHOR_'.basename($filename).$match; |
||
145 | } |
||
146 | |||
147 | global $links; |
||
148 | $links[] = [ |
||
149 | 'filename' => $filename, |
||
150 | 'line' => $index + 1, |
||
151 | 'link' => $match, |
||
152 | ]; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // Check for un-backtick-ed inline code |
||
157 | // If line contains $ |
||
158 | if (str_contains($line, '$') && ! str_contains($line, '[Blade]:') && ! str_contains($line, '$ php')) { |
||
159 | // Check character before the $ is not a backtick |
||
160 | $pos = strpos($line, '$'); |
||
161 | if ($pos > 0) { |
||
162 | $charBefore = substr($line, $pos - 1, 1); |
||
163 | if ($charBefore !== '`') { |
||
164 | global $warnings; |
||
165 | $warnings['Inline code'][] = sprintf('Unformatted inline code found in %s:%s', $filename, $index + 1); |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | // If line contains command |
||
170 | if (str_contains($line, 'php hyde') && ! str_contains($line, '[Blade]:') && ! str_contains($line, '$ php')) { |
||
171 | // Check character before the php hyde is not a backtick |
||
172 | $pos = strpos($line, 'php hyde'); |
||
173 | if ($pos > 0) { |
||
174 | $charBefore = substr($line, $pos - 1, 1); |
||
175 | if ($charBefore !== '`') { |
||
176 | global $warnings; |
||
177 | $warnings['Inline code'][] = sprintf('Unformatted inline command found in %s:%s', $filename, $index + 1); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | // If word ends in .php |
||
182 | if (str_contains($line, '.php') && ! str_contains($line, '[Blade]:') && ! str_contains($line, '$ php') && ! str_contains($line, 'http') && ! str_contains(strtolower($line), 'filepath')) { |
||
183 | // Check character after the .php is not a backtick |
||
184 | $pos = strpos($line, '.php'); |
||
185 | if ($pos > 0) { |
||
186 | $charAfter = substr($line, $pos + 4, 1); |
||
187 | if ($charAfter !== '`') { |
||
188 | global $warnings; |
||
189 | $warnings['Inline code'][] = sprintf('Unformatted inline filename found in %s:%s', $filename, $index + 1); |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | // If word ends in .json |
||
195 | if (str_contains($line, '.json') && ! str_contains($line, '[Blade]:') && ! str_contains($line, '$ json') && ! str_contains($line, 'http') && ! str_contains(strtolower($line), 'filepath')) { |
||
196 | // Check character after the .json is not a backtick |
||
197 | $pos = strpos($line, '.json'); |
||
198 | if ($pos > 0) { |
||
199 | $charAfter = substr($line, $pos + 5, 1); |
||
200 | if ($charAfter !== '`') { |
||
201 | global $warnings; |
||
202 | $warnings['Inline code'][] = sprintf('Unformatted inline filename found in %s:%s', $filename, $index + 1); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | // if word ends with () |
||
207 | if (str_contains($line, '()') && ! str_contains($line, '[Blade]:')) { |
||
208 | // Check character after the () is not a backtick |
||
209 | $pos = strpos($line, '()'); |
||
210 | if ($pos > 0) { |
||
211 | $charAfter = substr($line, $pos + 2, 1); |
||
212 | if ($charAfter !== '`') { |
||
213 | global $warnings; |
||
214 | $warnings['Inline code'][] = sprintf('Unformatted inline function found in %s:%s', $filename, $index + 1); |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | |||
219 | // Check for invalid command signatures |
||
220 | if (str_contains($line, 'php hyde')) { |
||
221 | // Extract signature from line |
||
222 | $start = strpos($line, 'php hyde'); |
||
223 | $substr = substr($line, $start); |
||
224 | $explode = explode(' ', $substr, 3); |
||
225 | $signature = $explode[0].' '.$explode[1].' '.$explode[2]; |
||
226 | $end = strpos($signature, '`'); |
||
227 | if ($end === false) { |
||
228 | $end = strpos($signature, '<'); |
||
229 | if ($end === false) { |
||
230 | $end = strlen($signature); |
||
231 | } |
||
232 | } |
||
233 | $signature = substr($signature, 0, $end); |
||
234 | $signatures = getSignatures(); |
||
235 | if (! in_array($signature, $signatures)) { |
||
236 | global $warnings; |
||
237 | $warnings['Invalid command signatures'][] = sprintf('Invalid command signature \'%s\' found in %s:%s', $signature, $filename, $index + 1); |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | |||
242 | // Check if line is too long |
||
243 | if (strlen($line) > 120) { |
||
244 | global $warnings; |
||
245 | // $warnings[] = 'Line '.$linesCounted.' in file '.$filename.' is too long'; |
||
246 | } |
||
247 | |||
248 | // Warn if documentation contains legacy markers (experimental, beta, etc) |
||
249 | $markers = ['experimental', 'beta', 'alpha', 'v0.']; |
||
250 | foreach ($markers as $marker) { |
||
251 | if (str_contains($line, $marker)) { |
||
252 | global $warnings; |
||
253 | $warnings['Legacy markers'][] = sprintf('Legacy marker found in %s:%s Found "%s"', $filename, $index + 1, $marker); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | // Warn when legacy terms are used (for example slug instead of identifier/route key) |
||
258 | $legacyTerms = [ |
||
259 | 'slug' => '"identifier" or "route key"', |
||
260 | 'slugs' => '"identifiers" or "route keys"', |
||
261 | ]; |
||
262 | |||
263 | foreach ($legacyTerms as $legacyTerm => $newTerm) { |
||
264 | if (str_contains(strtolower($line), $legacyTerm)) { |
||
265 | global $warnings; |
||
266 | $warnings['Legacy terms'][] = sprintf('Legacy term found in %s:%s Found "%s", should be %s', $filename, $index + 1, $legacyTerm, $newTerm); |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
271 | $new_content = implode("\n", $new_lines); |
||
272 | $new_content = trim($new_content)."\n"; |
||
273 | file_put_contents($filename, $new_content); |
||
274 | |||
275 | if ($new_content !== $stream) { |
||
276 | global $filesChanged; |
||
277 | $filesChanged++; |
||
278 | } |
||
472 |