Complex classes like PathSeekerTrait 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 PathSeekerTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait PathSeekerTrait |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | public $tmp = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param bool $skip_create_path |
||
|
|
|||
| 29 | * @param $config |
||
| 30 | * @return string |
||
| 31 | * @throws \Exception |
||
| 32 | */ |
||
| 33 | public function getPath() |
||
| 34 | { |
||
| 35 | $tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir(); |
||
| 36 | |||
| 37 | if (!isset($this->config[ 'path' ]) || $this->config[ 'path' ] == '') { |
||
| 38 | if (self::isPHPModule()) { |
||
| 39 | $path = $tmp_dir; |
||
| 40 | } else { |
||
| 41 | $document_root_path = rtrim($_SERVER[ 'DOCUMENT_ROOT' ], '/') . '/../'; |
||
| 42 | $path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) && is_writable($document_root_path) ? $document_root_path : rtrim(__DIR__, '/') . '/'; |
||
| 43 | } |
||
| 44 | |||
| 45 | if ($this->config[ 'path' ] != '') { |
||
| 46 | $path = $this->config[ 'path' ]; |
||
|
1 ignored issue
–
show
|
|||
| 47 | } |
||
| 48 | |||
| 49 | } else { |
||
| 50 | $path = $this->config[ 'path' ]; |
||
| 51 | } |
||
| 52 | |||
| 53 | $securityKey = array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : ''; |
||
| 54 | if ($securityKey == "" || $securityKey == 'auto') { |
||
| 55 | $securityKey = $this->config[ 'securityKey' ]; |
||
| 56 | if ($securityKey == 'auto' || $securityKey == '') { |
||
| 57 | $securityKey = isset($_SERVER[ 'HTTP_HOST' ]) ? preg_replace('/^www./', '', strtolower($_SERVER[ 'HTTP_HOST' ])) : "default"; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | if ($securityKey != '') { |
||
| 61 | $securityKey .= '/'; |
||
| 62 | } |
||
| 63 | |||
| 64 | $securityKey = $this->cleanFileName($securityKey); |
||
| 65 | |||
| 66 | $full_path = rtrim($path, '/') . '/' . $securityKey; |
||
| 67 | $full_pathx = md5($full_path); |
||
| 68 | |||
| 69 | |||
| 70 | if (!isset($this->tmp[ $full_pathx ])) { |
||
| 71 | |||
| 72 | if (!@file_exists($full_path) || !@is_writable($full_path)) { |
||
| 73 | if (!@file_exists($full_path)) { |
||
| 74 | @mkdir($full_path, $this->setChmodAuto(), true); |
||
|
1 ignored issue
–
show
|
|||
| 75 | } |
||
| 76 | if (!@is_writable($full_path)) { |
||
| 77 | @chmod($full_path, $this->setChmodAuto()); |
||
|
1 ignored issue
–
show
|
|||
| 78 | } |
||
| 79 | if (!@is_writable($full_path)) { |
||
| 80 | // switch back to tmp dir again if the path is not writeable |
||
| 81 | $full_path = rtrim($tmp_dir, '/') . '/' . $securityKey; |
||
| 82 | if (!@file_exists($full_path)) { |
||
| 83 | @mkdir($full_path, $this->setChmodAuto(), true); |
||
|
1 ignored issue
–
show
|
|||
| 84 | } |
||
| 85 | if (!@is_writable($full_path)) { |
||
| 86 | @chmod($full_path, $this->setChmodAuto()); |
||
|
1 ignored issue
–
show
|
|||
| 87 | } |
||
| 88 | } |
||
| 89 | if (!@file_exists($full_path) || !@is_writable($full_path)) { |
||
| 90 | throw new phpFastCacheCoreException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!', 92); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->tmp[ $full_pathx ] = true; |
||
| 95 | $this->htaccessGen($full_path, array_key_exists('htaccess', $this->config) ? $this->config[ 'htaccess' ] : false); |
||
| 96 | } |
||
| 97 | |||
| 98 | return realpath($full_path); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $keyword |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | protected function encodeFilename($keyword) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function isExpired() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param $keyword |
||
| 122 | * @param bool $skip |
||
| 123 | * @return string |
||
| 124 | * @throws phpFastCacheDriverException |
||
| 125 | */ |
||
| 126 | private function getFilePath($keyword, $skip = false) |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * @param $this ->config |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | public function setChmodAuto() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param $filename |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | protected static function cleanFileName($filename) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param $path |
||
| 184 | * @param bool $create |
||
| 185 | * @throws \Exception |
||
| 186 | */ |
||
| 187 | protected function htaccessGen($path, $create = true) |
||
| 214 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.