| Conditions | 16 |
| Paths | 15 |
| Total Lines | 53 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 32 |
| CRAP Score | 17.0078 |
| Changes | 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 |
||
| 136 | 3 | function get_lock($time = -1) { |
|
|
1 ignored issue
–
show
|
|||
| 137 | 3 | $tmp_time = microtime(true); |
|
| 138 | 3 | if ($this->_lock_acquired) { |
|
| 139 | 2 | return true; |
|
| 140 | } |
||
| 141 | |||
| 142 | 3 | if (!file_exists(dirname($this->filename))) { |
|
| 143 | 1 | throw new MutexException('Folder "'.dirname($this->filename).'" does not exist', 1); |
|
| 144 | 3 | } elseif (!is_dir(dirname($this->filename))) { |
|
| 145 | 1 | throw new MutexException('Folder "'.dirname($this->filename).'" is not a folder', 4); |
|
| 146 | 3 | } elseif (!is_writable(dirname($this->filename))) { |
|
| 147 | 1 | throw new MutexException('Folder "'.dirname($this->filename).'" is not writable', 2); |
|
| 148 | 3 | } elseif (file_exists($this->filename) and !is_writable($this->filename)) { |
|
| 149 | 1 | throw new MutexException('File "'.$this->filename.'" is not writable', 3); |
|
| 150 | } |
||
| 151 | |||
| 152 | // Открываем файл |
||
| 153 | 3 | $this->_file_handler = fopen($this->filename, file_exists($this->filename) ? 'ab' : 'wb'); |
|
| 154 | 3 | while (($this->_file_handler === false) and ( |
|
| 155 | ($tmp_time + $time >= microtime(true)) or ($time == -1) |
||
| 156 | 3 | )) { |
|
| 157 | usleep(10000); |
||
| 158 | $this->_file_handler = fopen($this->filename, 'ab'); |
||
| 159 | } |
||
| 160 | 3 | if ($this->_file_handler === false) { |
|
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | // Блочим файл |
||
| 165 | 3 | if ($time >= 0) { |
|
| 166 | 3 | $result = flock($this->_file_handler, LOCK_EX | LOCK_NB); |
|
| 167 | 3 | while (!$result and ($tmp_time + $time >= microtime(true))) { |
|
| 168 | // U MAD? |
||
| 169 | 1 | usleep(10000); |
|
| 170 | 1 | $result = flock($this->_file_handler, LOCK_EX | LOCK_NB); |
|
| 171 | 1 | } |
|
| 172 | 3 | } else { |
|
| 173 | $result = flock($this->_file_handler, LOCK_EX); |
||
| 174 | } |
||
| 175 | |||
| 176 | 3 | if ($result) { |
|
| 177 | 3 | $this->_lock_acquired_time = microtime(true); |
|
| 178 | // @todo Не работает под Windows |
||
| 179 | 3 | fwrite($this->_file_handler, posix_getpid()."\n".microtime(true)."\n".posix_getuid()."\n\n"); |
|
| 180 | 3 | fflush($this->_file_handler); |
|
| 181 | 3 | $this->_lock_acquired = true; |
|
| 182 | 3 | } else { |
|
| 183 | 1 | fclose($this->_file_handler); |
|
| 184 | 1 | $this->_file_handler = false; |
|
| 185 | } |
||
| 186 | |||
| 187 | 3 | return $result; |
|
| 188 | } |
||
| 189 | |||
| 236 | ?> |
||
|
1 ignored issue
–
show
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.