| Conditions | 15 |
| Paths | 21 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 90 | public function purge(int $olderThanMinutes = 0, bool $dryRun = false, bool $strict = false): array |
||
| 91 | { |
||
| 92 | $dir = $this->getTempDir(); |
||
| 93 | $this->assertBaseDir($dir); |
||
| 94 | |||
| 95 | $deleted = 0; $bytes = 0; |
||
| 96 | |||
| 97 | if (!is_dir($dir) || !is_readable($dir)) { |
||
| 98 | return ['files' => 0, 'bytes' => 0]; |
||
| 99 | } |
||
| 100 | |||
| 101 | $cutoff = $olderThanMinutes > 0 ? (time() - $olderThanMinutes * 60) : null; |
||
| 102 | |||
| 103 | $rii = new \RecursiveIteratorIterator( |
||
| 104 | new \RecursiveDirectoryIterator( |
||
| 105 | $dir, |
||
| 106 | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS |
||
| 107 | ), |
||
| 108 | \RecursiveIteratorIterator::CHILD_FIRST |
||
| 109 | ); |
||
| 110 | |||
| 111 | foreach ($rii as $f) { |
||
| 112 | if (!$strict && $this->isExcluded($dir, $f)) { |
||
| 113 | // Skip excluded subtree |
||
| 114 | if ($f->isDir()) { |
||
| 115 | $rii->next(); |
||
| 116 | } |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | $bn = $f->getBasename(); |
||
| 121 | if ($this->isProtected($bn)) { |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($f->isFile()) { |
||
| 126 | if (null !== $cutoff && $f->getMTime() > $cutoff) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | $bytes += (int) $f->getSize(); |
||
| 130 | if (!$dryRun) { |
||
| 131 | @unlink($f->getPathname()); |
||
|
|
|||
| 132 | } |
||
| 133 | $deleted++; |
||
| 134 | } elseif ($f->isDir()) { |
||
| 135 | if (!$dryRun) { |
||
| 136 | @rmdir($f->getPathname()); // best-effort (only if empty) |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | return ['files' => $deleted, 'bytes' => $bytes]; |
||
| 142 | } |
||
| 187 |
If you suppress an error, we recommend checking for the error condition explicitly: