Complex classes like FileMutex often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileMutex, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 5 | class FileMutex implements MutexInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var string Название файла, на котором будет работать мьютекс |
||
| 9 | */ |
||
| 10 | public $filename = ''; |
||
| 11 | protected $_mutex_type; |
||
| 12 | /** |
||
| 13 | * @var string|null Название мьютекса, должно состоять из валидных для имени файлов символов |
||
| 14 | */ |
||
| 15 | public $_mutex_name; |
||
| 16 | /** |
||
| 17 | * @var string|null Место, где расположен файл. |
||
| 18 | * |
||
| 19 | * Не используется в логике класса |
||
| 20 | */ |
||
| 21 | protected $_mutex_folder; |
||
| 22 | /** |
||
| 23 | * @var double|null Время, когда мьютекс был получен |
||
| 24 | */ |
||
| 25 | protected $_lock_acquired_time = null; |
||
| 26 | /** |
||
| 27 | * @var boolean Текущее состояние мьютекса |
||
| 28 | */ |
||
| 29 | protected $_lock_acquired = false; |
||
| 30 | /** |
||
| 31 | * @var resource|false Файл, открывающийся через fopen |
||
| 32 | */ |
||
| 33 | protected $_file_handler = false; |
||
| 34 | /** |
||
| 35 | * @var boolean Удалять файл при анлоке |
||
| 36 | * |
||
| 37 | * В данный момент это unsafe поведение, из-за немандаторного доступа к файлам из PHP |
||
| 38 | */ |
||
| 39 | protected $_delete_on_release = false; |
||
| 40 | |||
| 41 | 5 | /** |
|
| 42 | * @param MutexSettings|array $settings |
||
| 43 | */ |
||
| 44 | public function __construct($settings) |
||
| 74 | |||
| 75 | public function __destruct() |
||
| 79 | 3 | ||
| 80 | 1 | /** |
|
| 81 | 3 | * @return string |
|
| 82 | 3 | */ |
|
| 83 | public static function getDomainString(): string |
||
| 95 | 1 | ||
| 96 | 2 | /** |
|
| 97 | 2 | * @return string |
|
| 98 | 1 | */ |
|
| 99 | 1 | public static function getDirectoryString(): string |
|
| 111 | 1 | ||
| 112 | 1 | /** |
|
| 113 | * @return boolean |
||
| 114 | 1 | * |
|
| 115 | 1 | * @throws MutexException |
|
| 116 | 1 | */ |
|
| 117 | 1 | public function is_free(): bool |
|
| 139 | 4 | ||
| 140 | 4 | /** |
|
| 141 | 3 | * @param double|integer $time |
|
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | 4 | * @throws MutexException |
|
| 145 | 4 | */ |
|
| 146 | public function get_lock(float $time = -1): bool |
||
| 198 | 4 | ||
| 199 | 4 | public function release_lock() |
|
| 216 | |||
| 217 | /** |
||
| 218 | 1 | * @return double|null |
|
| 219 | 1 | */ |
|
| 220 | public function get_acquired_time(): ?float |
||
| 224 | |||
| 225 | 2 | /** |
|
| 226 | 2 | * @return string |
|
| 227 | */ |
||
| 228 | public function get_mutex_name(): string |
||
| 232 | 4 | ||
| 233 | 4 | /** |
|
| 234 | * @return boolean |
||
| 235 | */ |
||
| 236 | public function get_delete_on_release(): bool |
||
| 240 | 2 | ||
| 241 | 2 | /** |
|
| 242 | * @return boolean |
||
| 243 | */ |
||
| 244 | public function is_acquired(): bool |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public static function get_last_php_error_as_string(): string |
||
| 265 | 20 | ||
| 266 | /** |
||
| 267 | * @param string $path |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public static function sanify_path(string $path): string |
||
| 285 | 11 | ||
| 286 | 2 | /** |
|
| 287 | * @param string $path |
||
| 288 | 11 | * @param int $permissions |
|
| 289 | 9 | * |
|
| 290 | * @throws \NokitaKaze\Mutex\MutexException |
||
| 291 | */ |
||
| 292 | public static function create_folders_in_path(string $path, $permissions = 7 << 6) |
||
| 306 | |||
| 307 | public static function getpid(): int |
||
| 311 | |||
| 312 | public static function getuid(): int |
||
| 316 | } |
||
| 317 |
If you suppress an error, we recommend checking for the error condition explicitly: