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 | * @var string Название файла, на котором будет работать мьютекс |
||
8 | */ |
||
9 | var $filename = ''; |
||
10 | protected $_mutex_type; |
||
11 | /** |
||
12 | * @var string|null Название мьютекса, должно состоять из валидных для имени файлов символов |
||
13 | */ |
||
14 | var $_mutex_name; |
||
15 | /** |
||
16 | * @var string|null Место, где расположен файл. |
||
17 | * Не используется в логике класса |
||
18 | */ |
||
19 | protected $_mutex_folder; |
||
20 | /** |
||
21 | * @var double|null Время, когда мьютекс был получен |
||
22 | */ |
||
23 | protected $_lock_acquired_time = null; |
||
24 | /** |
||
25 | * @var boolean Текущее состояние мьютекса |
||
26 | */ |
||
27 | protected $_lock_acquired = false; |
||
28 | /** |
||
29 | * @var resource|false Файл, открывающийся через fopen |
||
30 | */ |
||
31 | protected $_file_handler = false; |
||
32 | /** |
||
33 | * @var boolean Удалять файл при анлоке |
||
34 | * В данный момент это unsafe поведение, из-за немандаторного доступа к файлам из PHP |
||
35 | */ |
||
36 | protected $_delete_on_release = false; |
||
37 | |||
38 | /** |
||
39 | * @param MutexSettings|array $settings |
||
40 | */ |
||
41 | 4 | function __construct($settings) { |
|
70 | |||
71 | 4 | function __destruct() { |
|
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | 3 | static function getDomainString() { |
|
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | 2 | static function getDirectoryString() { |
|
104 | |||
105 | /** |
||
106 | * @return boolean |
||
107 | * |
||
108 | * @throws MutexException |
||
109 | */ |
||
110 | 1 | function is_free() { |
|
131 | |||
132 | /** |
||
133 | * @param double|integer $time |
||
134 | * |
||
135 | * @return bool |
||
136 | * @throws MutexException |
||
137 | */ |
||
138 | 3 | function get_lock($time = -1) { |
|
190 | |||
191 | 4 | function release_lock() { |
|
207 | |||
208 | /** |
||
209 | * @return double|null |
||
210 | */ |
||
211 | function get_acquired_time() { |
||
214 | |||
215 | /** |
||
216 | * @return string |
||
217 | */ |
||
218 | 1 | function get_mutex_name() { |
|
221 | |||
222 | /** |
||
223 | * @return boolean |
||
224 | */ |
||
225 | 1 | function get_delete_on_release() { |
|
228 | |||
229 | /** |
||
230 | * @return boolean |
||
231 | */ |
||
232 | 2 | function is_acquired() { |
|
235 | |||
236 | /** |
||
237 | * @param string $path |
||
238 | * |
||
239 | * @throws \NokitaKaze\Mutex\MutexException |
||
240 | */ |
||
241 | 10 | static function create_folders_in_path($path) { |
|
265 | } |
||
266 | |||
267 | ?> |
If you suppress an error, we recommend checking for the error condition explicitly: