| Conditions | 13 |
| Paths | 11 |
| Total Lines | 44 |
| Code Lines | 23 |
| 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 |
||
| 85 | private function loadFile(string $class, callable $generator): void |
||
| 86 | { |
||
| 87 | $file = "$this->tempDirectory/$class.php"; |
||
| 88 | |||
| 89 | if (!$this->isExpired($file) && (@include $file) !== false) { // @ file may not exist |
||
| 90 | return; |
||
| 91 | } |
||
| 92 | |||
| 93 | Nette\Utils\FileSystem::createDir($this->tempDirectory); |
||
| 94 | |||
| 95 | $handle = @\fopen("$file.lock", 'c+'); // @ is escalated to exception |
||
| 96 | |||
| 97 | if (!$handle) { |
||
| 98 | throw new Nette\IOException("Unable to create file '$file.lock'. " . Nette\Utils\Helpers::getLastError()); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!@\flock($handle, \LOCK_EX)) { // @ is escalated to exception |
||
| 102 | throw new Nette\IOException("Unable to acquire exclusive lock on '$file.lock'. " . Nette\Utils\Helpers::getLastError()); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!\is_file($file) || $this->isExpired($file, $updatedMeta)) { |
||
| 106 | if (isset($updatedMeta)) { |
||
| 107 | $toWrite["$file.meta"] = $updatedMeta; |
||
| 108 | } else { |
||
| 109 | [$toWrite[$file], $toWrite["$file.meta"], $toWrite["$file.xml"]] = $this->generate($class, $generator); |
||
| 110 | } |
||
| 111 | |||
| 112 | foreach ($toWrite as $name => $content) { |
||
| 113 | if (\file_put_contents("$name.tmp", $content) !== \strlen($content) || !\rename("$name.tmp", $name)) { |
||
| 114 | @\unlink("$name.tmp"); // @ - file may not exist |
||
| 115 | |||
| 116 | throw new Nette\IOException("Unable to create file '$name'."); |
||
| 117 | } |
||
| 118 | |||
| 119 | if (\function_exists('opcache_invalidate')) { |
||
| 120 | @\opcache_invalidate($name, true); // @ can be restricted |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | if ((@include $file) === false) { // @ - error escalated to exception |
||
| 126 | throw new Nette\IOException("Unable to include '$file'."); |
||
| 127 | } |
||
| 128 | \flock($handle, \LOCK_UN); |
||
| 129 | } |
||
| 145 |