| Conditions | 12 |
| Paths | 72 |
| Total Lines | 61 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 21 | private function getLogFileStatus(): array |
||
| 22 | { |
||
| 23 | $searchPath = '/var/log'; |
||
| 24 | $logrotateConfs = ['/etc/logrotate.conf', ...glob('/etc/logrotate.d/*')]; |
||
| 25 | |||
| 26 | // Step 1: Find all .log files under /var/log |
||
| 27 | $allLogs = []; |
||
| 28 | $iterator = new \RecursiveIteratorIterator( |
||
| 29 | new \RecursiveDirectoryIterator($searchPath, \FilesystemIterator::SKIP_DOTS) |
||
| 30 | ); |
||
| 31 | |||
| 32 | foreach ($iterator as $file) { |
||
| 33 | if (preg_match('/\.log$/', $file->getFilename())) { |
||
| 34 | $realPath = realpath($file->getPathname()); |
||
| 35 | if ($realPath !== false && is_file($realPath)) { |
||
| 36 | $allLogs[$realPath] = [ |
||
| 37 | 'size' => filesize($realPath), |
||
| 38 | 'last_modified' => filemtime($realPath) |
||
| 39 | ]; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | // Step 2: Extract managed log paths from logrotate config files |
||
| 45 | $managedLogs = []; |
||
| 46 | foreach ($logrotateConfs as $confFile) { |
||
| 47 | if (!is_readable($confFile)) continue; |
||
|
|
|||
| 48 | $lines = file($confFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
||
| 49 | foreach ($lines as $line) { |
||
| 50 | if (preg_match('#^\s*/[^\s{}]+\.log#', $line, $matches)) { |
||
| 51 | $path = realpath(trim($matches[0])); |
||
| 52 | if ($path !== false) { |
||
| 53 | $managedLogs[] = $path; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | $managedLogs = array_unique($managedLogs); |
||
| 60 | |||
| 61 | // Step 3: Build result arrays |
||
| 62 | $result = [ |
||
| 63 | 'managed' => [], |
||
| 64 | 'unmanaged' => [] |
||
| 65 | ]; |
||
| 66 | |||
| 67 | foreach ($allLogs as $path => $info) { |
||
| 68 | $entry = [ |
||
| 69 | 'path' => $path, |
||
| 70 | 'size' => $info['size'], |
||
| 71 | 'last_modified' => date('c', $info['last_modified']) // ISO 8601 format |
||
| 72 | ]; |
||
| 73 | |||
| 74 | if (in_array($path, $managedLogs)) { |
||
| 75 | $result['managed'][] = $entry; |
||
| 76 | } else { |
||
| 77 | $result['unmanaged'][] = $entry; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | return $result; |
||
| 82 | } |
||
| 84 |