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 |
||
5 | class FileMutex implements MutexInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var string Название файла, на котором будет работать мьютекс |
||
9 | */ |
||
10 | var $filename = ''; |
||
11 | protected $_mutex_type; |
||
12 | /** |
||
13 | * @var string|null Название мьютекса, должно состоять из валидных для имени файлов символов |
||
14 | */ |
||
15 | var $_mutex_name; |
||
16 | /** |
||
17 | * @var string|null Место, где расположен файл. |
||
18 | * Не используется в логике класса |
||
19 | */ |
||
20 | protected $_mutex_folder; |
||
21 | /** |
||
22 | * @var double|null Время, когда мьютекс был получен |
||
23 | */ |
||
24 | protected $_lock_acquired_time = null; |
||
25 | /** |
||
26 | * @var boolean Текущее состояние мьютекса |
||
27 | */ |
||
28 | protected $_lock_acquired = false; |
||
29 | /** |
||
30 | * @var resource|false Файл, открывающийся через fopen |
||
31 | */ |
||
32 | protected $_file_handler = false; |
||
33 | /** |
||
34 | * @var boolean Удалять файл при анлоке |
||
35 | * В данный момент это unsafe поведение, из-за немандаторного доступа к файлам из PHP |
||
36 | */ |
||
37 | protected $_delete_on_release = false; |
||
38 | |||
39 | /** |
||
40 | * @param MutexSettings|array $settings |
||
41 | 5 | */ |
|
42 | function __construct($settings) |
||
72 | 5 | ||
73 | 5 | function __destruct() |
|
77 | |||
78 | 3 | /** |
|
79 | 3 | * @return string |
|
80 | 1 | */ |
|
81 | 3 | static function getDomainString() |
|
93 | 2 | ||
94 | 2 | /** |
|
95 | 1 | * @return string |
|
96 | 2 | */ |
|
97 | 2 | static function getDirectoryString() |
|
109 | |||
110 | 1 | /** |
|
111 | 1 | * @return boolean |
|
112 | 1 | * |
|
113 | * @throws MutexException |
||
114 | 1 | */ |
|
115 | 1 | function is_free() |
|
137 | |||
138 | 4 | /** |
|
139 | 4 | * @param double|integer $time |
|
140 | 4 | * |
|
141 | 3 | * @return bool |
|
142 | * @throws MutexException |
||
143 | */ |
||
144 | 4 | function get_lock($time = -1) |
|
196 | 4 | ||
197 | 4 | function release_lock() |
|
214 | |||
215 | /** |
||
216 | * @return double|null |
||
217 | */ |
||
218 | 1 | function get_acquired_time() |
|
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | 2 | */ |
|
226 | 2 | function get_mutex_name() |
|
230 | |||
231 | /** |
||
232 | 4 | * @return boolean |
|
233 | 4 | */ |
|
234 | function get_delete_on_release() |
||
238 | |||
239 | 2 | /** |
|
240 | 2 | * @return boolean |
|
241 | 2 | */ |
|
242 | function is_acquired() |
||
246 | 2 | ||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | static function get_last_php_error_as_string() |
||
260 | |||
261 | 20 | /** |
|
262 | 20 | * @param string $path |
|
263 | 20 | * |
|
264 | * @return string |
||
265 | 20 | */ |
|
266 | static function sanify_path($path) |
||
280 | 11 | ||
281 | 1 | /** |
|
282 | * @param string $path |
||
283 | 1 | * |
|
284 | * @throws \NokitaKaze\Mutex\MutexException |
||
285 | 11 | */ |
|
286 | 2 | static function create_folders_in_path($path) |
|
308 | |||
309 | static function getpid() |
||
313 | |||
314 | static function getuid() |
||
318 | } |
||
319 | |||
320 | ?> |
If you suppress an error, we recommend checking for the error condition explicitly: