Conditions | 26 |
Paths | 1924 |
Total Lines | 124 |
Code Lines | 68 |
Lines | 24 |
Ratio | 19.35 % |
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 |
||
104 | protected function parseACL($acl) |
||
105 | { |
||
106 | // Take off the first and last characters (the braces) |
||
107 | $acl = substr($acl, 1, strlen($acl) - 2); |
||
108 | |||
109 | // Pick out individual ACE's by carefully parsing. This is necessary in order |
||
110 | // to cope with usernames and stuff that contain commas |
||
111 | $aces = []; |
||
112 | $i = $j = 0; |
||
113 | $in_quotes = false; |
||
114 | View Code Duplication | while ($i < strlen($acl)) { |
|
115 | // If current char is a double quote and it's not escaped, then |
||
116 | // enter quoted bit |
||
117 | $char = substr($acl, $i, 1); |
||
118 | if ($char == '"' && ($i == 0 || substr($acl, $i - 1, 1) != '\\')) { |
||
119 | $in_quotes = !$in_quotes; |
||
120 | } elseif ($char == ',' && !$in_quotes) { |
||
121 | // Add text so far to the array |
||
122 | $aces[] = substr($acl, $j, $i - $j); |
||
123 | $j = $i + 1; |
||
124 | } |
||
125 | ++$i; |
||
126 | } |
||
127 | // Add final text to the array |
||
128 | $aces[] = substr($acl, $j); |
||
129 | |||
130 | // Create the array to be returned |
||
131 | $temp = []; |
||
132 | |||
133 | // For each ACE, generate an entry in $temp |
||
134 | foreach ($aces as $v) { |
||
135 | // If the ACE begins with a double quote, strip them off both ends |
||
136 | // and unescape backslashes and double quotes |
||
137 | // $unquote = false; |
||
138 | if (strpos($v, '"') === 0) { |
||
139 | $v = substr($v, 1, strlen($v) - 2); |
||
140 | $v = str_replace('\\"', '"', $v); |
||
141 | $v = str_replace('\\\\', '\\', $v); |
||
142 | } |
||
143 | |||
144 | // Figure out type of ACE (public, user or group) |
||
145 | if (strpos($v, '=') === 0) { |
||
146 | $atype = 'public'; |
||
147 | } else { |
||
148 | if ($this->hasRoles()) { |
||
149 | $atype = 'role'; |
||
150 | } else { |
||
151 | if (strpos($v, 'group ') === 0) { |
||
152 | $atype = 'group'; |
||
153 | // Tear off 'group' prefix |
||
154 | $v = substr($v, 6); |
||
155 | } else { |
||
156 | $atype = 'user'; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | // Break on unquoted equals sign... |
||
162 | $i = 0; |
||
163 | $in_quotes = false; |
||
164 | $entity = null; |
||
165 | $chars = null; |
||
166 | while ($i < strlen($v)) { |
||
167 | // If current char is a double quote and it's not escaped, then |
||
168 | // enter quoted bit |
||
169 | $char = substr($v, $i, 1); |
||
170 | $next_char = substr($v, $i + 1, 1); |
||
171 | if ($char == '"' && ($i == 0 || $next_char != '"')) { |
||
172 | $in_quotes = !$in_quotes; |
||
173 | } elseif ($char == '"' && $next_char == '"') { |
||
174 | // Skip over escaped double quotes |
||
175 | ++$i; |
||
176 | } elseif ($char == '=' && !$in_quotes) { |
||
177 | // Split on current equals sign |
||
178 | $entity = substr($v, 0, $i); |
||
179 | $chars = substr($v, $i + 1); |
||
180 | |||
181 | break; |
||
182 | } |
||
183 | ++$i; |
||
184 | } |
||
185 | |||
186 | // Check for quoting on entity name, and unescape if necessary |
||
187 | if (strpos($entity, '"') === 0) { |
||
188 | $entity = substr($entity, 1, strlen($entity) - 2); |
||
189 | $entity = str_replace('""', '"', $entity); |
||
190 | } |
||
191 | |||
192 | // New row to be added to $temp |
||
193 | // (type, grantee, privileges, grantor, grant option? |
||
194 | $row = [$atype, $entity, [], '', []]; |
||
195 | |||
196 | // Loop over chars and add privs to $row |
||
197 | for ($i = 0; $i < strlen($chars); ++$i) { |
||
198 | // Append to row's privs list the string representing |
||
199 | // the privilege |
||
200 | $char = substr($chars, $i, 1); |
||
201 | if ($char == '*') { |
||
202 | $row[4][] = $this->privmap[substr($chars, $i - 1, 1)]; |
||
203 | View Code Duplication | } elseif ($char == '/') { |
|
204 | $grantor = substr($chars, $i + 1); |
||
205 | // Check for quoting |
||
206 | if (strpos($grantor, '"') === 0) { |
||
207 | $grantor = substr($grantor, 1, strlen($grantor) - 2); |
||
208 | $grantor = str_replace('""', '"', $grantor); |
||
209 | } |
||
210 | $row[3] = $grantor; |
||
211 | |||
212 | break; |
||
213 | } else { |
||
214 | if (!isset($this->privmap[$char])) { |
||
215 | return -3; |
||
216 | } |
||
217 | |||
218 | $row[2][] = $this->privmap[$char]; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | // Append row to temp |
||
223 | $temp[] = $row; |
||
224 | } |
||
225 | |||
226 | return $temp; |
||
227 | } |
||
228 | |||
402 |
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: