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