Conditions | 25 |
Paths | 672 |
Total Lines | 90 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
33 | public function getPath($readonly = false) |
||
34 | { |
||
35 | /** |
||
36 | * Get the base system temporary directory |
||
37 | */ |
||
38 | $tmp_dir = rtrim(ini_get('upload_tmp_dir') ?: sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
||
39 | |||
40 | /** |
||
41 | * Calculate the security key |
||
42 | */ |
||
43 | { |
||
44 | $securityKey = array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : ''; |
||
|
|||
45 | if (!$securityKey || $securityKey === 'auto') { |
||
46 | if (isset($_SERVER[ 'HTTP_HOST' ])) { |
||
47 | $securityKey = preg_replace('/^www./', '', strtolower(str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ]))); |
||
48 | } else { |
||
49 | $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | if ($securityKey !== '') { |
||
54 | $securityKey .= '/'; |
||
55 | } |
||
56 | |||
57 | $securityKey = static::cleanFileName($securityKey); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Extends the temporary directory |
||
62 | * with the security key and the driver name |
||
63 | */ |
||
64 | $tmp_dir = rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
||
65 | |||
66 | if (empty($this->config[ 'path' ]) || !is_string($this->config[ 'path' ])) { |
||
67 | $path = $tmp_dir; |
||
68 | } else { |
||
69 | $path = rtrim($this->config[ 'path' ], '/') . DIRECTORY_SEPARATOR; |
||
70 | } |
||
71 | |||
72 | $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
||
73 | $full_path = $path . $path_suffix; |
||
74 | $full_path_tmp = $tmp_dir . $path_suffix; |
||
75 | $full_path_hash = md5($full_path); |
||
76 | |||
77 | /** |
||
78 | * In readonly mode we only attempt |
||
79 | * to verify if the directory exists |
||
80 | * or not, if it does not then we |
||
81 | * return the temp dir |
||
82 | */ |
||
83 | if ($readonly === true) { |
||
84 | if($this->config[ 'autoTmpFallback' ] && (!@file_exists($full_path) || !@is_writable($full_path))){ |
||
85 | return $full_path_tmp; |
||
86 | } |
||
87 | return $full_path; |
||
88 | }else{ |
||
89 | if (!isset($this->tmp[ $full_path_hash ]) || (!@file_exists($full_path) || !@is_writable($full_path))) { |
||
90 | if (!@file_exists($full_path)) { |
||
91 | @mkdir($full_path, $this->setChmodAuto(), true); |
||
92 | }else if (!@is_writable($full_path)) { |
||
93 | @chmod($full_path, $this->setChmodAuto()); |
||
94 | } |
||
95 | |||
96 | if ($this->config[ 'autoTmpFallback' ] && !@is_writable($full_path)) { |
||
97 | /** |
||
98 | * Switch back to tmp dir |
||
99 | * again if the path is not writable |
||
100 | */ |
||
101 | $full_path = $full_path_tmp; |
||
102 | if (!@file_exists($full_path)) { |
||
103 | @mkdir($full_path, $this->setChmodAuto(), true); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * In case there is no directory |
||
109 | * writable including tye temporary |
||
110 | * one, we must throw an exception |
||
111 | */ |
||
112 | if (!@file_exists($full_path) || !@is_writable($full_path)) { |
||
113 | throw new phpFastCacheIOException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
||
114 | } |
||
115 | |||
116 | $this->tmp[ $full_path_hash ] = $full_path; |
||
117 | $this->htaccessGen($full_path, array_key_exists('htaccess', $this->config) ? $this->config[ 'htaccess' ] : false); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | return realpath($full_path); |
||
122 | } |
||
123 | |||
311 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: