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) { |
|
| 68 | |||
| 69 | 4 | function __destruct() { |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | 6 | static function getDomainString() { |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 2 | static function getDirectoryString() { |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @return boolean |
||
| 105 | * |
||
| 106 | * @throws MutexException |
||
| 107 | */ |
||
| 108 | 1 | function is_free() { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param double|integer $time |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | * @throws MutexException |
||
| 135 | */ |
||
| 136 | 3 | function get_lock($time = -1) { |
|
| 189 | |||
| 190 | 4 | function release_lock() { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return double|null |
||
| 209 | */ |
||
| 210 | 1 | function get_acquired_time() { |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 1 | function get_mutex_name() { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return boolean |
||
| 223 | */ |
||
| 224 | 1 | function get_delete_on_release() { |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @return boolean |
||
| 230 | */ |
||
| 231 | 3 | function is_acquired() { |
|
| 234 | } |
||
| 235 | |||
| 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.