Conditions | 4 |
Paths | 4 |
Total Lines | 73 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 2 |
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 | #!/usr/bin/env php |
||
153 | function semanticProfileAndAnalyze(array $args): array |
||
154 | { |
||
155 | $script = $args['script'] ?? ''; |
||
156 | $xdebugMode = $args['xdebug_mode'] ?? 'trace'; |
||
157 | |||
158 | if (! $script) { |
||
159 | return [ |
||
160 | 'content' => [ |
||
161 | [ |
||
162 | 'type' => 'text', |
||
163 | 'text' => 'Error: script parameter is required', |
||
164 | ], |
||
165 | ], |
||
166 | 'isError' => true, |
||
167 | ]; |
||
168 | } |
||
169 | |||
170 | if (! file_exists($script)) { |
||
171 | return [ |
||
172 | 'content' => [ |
||
173 | [ |
||
174 | 'type' => 'text', |
||
175 | 'text' => "Error: Script not found: $script", |
||
176 | ], |
||
177 | ], |
||
178 | 'isError' => true, |
||
179 | ]; |
||
180 | } |
||
181 | |||
182 | // Record time before execution to find newly created log files |
||
183 | $beforeExecution = time(); |
||
184 | |||
185 | // Execute the PHP script with profiling using php-dev.ini |
||
186 | $env = "XDEBUG_MODE=$xdebugMode XDEBUG_CONFIG='compression_level=0'"; |
||
187 | $phpDevIni = __DIR__ . '/php-dev.ini'; |
||
188 | $command = "$env php -c " . escapeshellarg($phpDevIni) . " " . escapeshellarg($script) . ' 2>&1'; |
||
189 | |||
190 | $output = shell_exec($command); |
||
191 | |||
192 | // Find semantic log files created during script execution |
||
193 | $logDirectory = $GLOBALS['logDirectory']; |
||
194 | $pattern = rtrim($logDirectory, '/') . '/semantic-dev-*.json'; |
||
195 | |||
196 | $files = glob($pattern); |
||
197 | $newLogFiles = array_filter($files, static fn ($file) => filemtime($file) >= $beforeExecution); |
||
198 | |||
199 | if (empty($newLogFiles)) { |
||
200 | return [ |
||
201 | 'content' => [ |
||
202 | [ |
||
203 | 'type' => 'text', |
||
204 | 'text' => "Script executed but no new semantic log generated.\nOutput:\n$output", |
||
205 | ], |
||
206 | ], |
||
207 | 'isError' => false, |
||
208 | ]; |
||
209 | } |
||
210 | |||
211 | // Get the newest log file from this execution |
||
212 | usort($newLogFiles, static fn ($a, $b) => filemtime($b) <=> filemtime($a)); |
||
213 | $executionLog = $newLogFiles[0]; |
||
214 | |||
215 | // Load and return the log data |
||
216 | $logData = getLog($executionLog); |
||
217 | |||
218 | return [ |
||
219 | 'content' => [ |
||
220 | [ |
||
221 | 'type' => 'text', |
||
222 | 'text' => "Script executed successfully.\nLog file: $executionLog\n\nSemantic Log Data:\n" . json_encode($logData, JSON_PRETTY_PRINT), |
||
223 | ], |
||
224 | ], |
||
225 | 'isError' => false, |
||
226 | ]; |
||
228 |