Conditions | 26 |
Paths | 1924 |
Total Lines | 128 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
281 | protected function parseACL($acl) |
||
282 | { |
||
283 | // Take off the first and last characters (the braces) |
||
284 | $acl = \mb_substr($acl, 1, \mb_strlen($acl) - 2); |
||
285 | |||
286 | // Pick out individual ACE's by carefully parsing. This is necessary in order |
||
287 | // to cope with usernames and stuff that contain commas |
||
288 | $aces = []; |
||
289 | $i = $j = 0; |
||
290 | $in_quotes = false; |
||
291 | |||
292 | while (\mb_strlen($acl) > $i) { |
||
293 | // If current char is a double quote and it's not escaped, then |
||
294 | // enter quoted bit |
||
295 | $char = \mb_substr($acl, $i, 1); |
||
296 | |||
297 | if ('"' === $char && (0 === $i || '\\' !== \mb_substr($acl, $i - 1, 1))) { |
||
298 | $in_quotes = !$in_quotes; |
||
299 | } elseif (',' === $char && !$in_quotes) { |
||
300 | // Add text so far to the array |
||
301 | $aces[] = \mb_substr($acl, $j, $i - $j); |
||
302 | $j = $i + 1; |
||
303 | } |
||
304 | ++$i; |
||
305 | } |
||
306 | // Add final text to the array |
||
307 | $aces[] = \mb_substr($acl, $j); |
||
308 | |||
309 | // Create the array to be returned |
||
310 | $temp = []; |
||
311 | |||
312 | // For each ACE, generate an entry in $temp |
||
313 | foreach ($aces as $v) { |
||
314 | // If the ACE begins with a double quote, strip them off both ends |
||
315 | // and unescape backslashes and double quotes |
||
316 | // $unquote = false; |
||
317 | if (0 === \mb_strpos($v, '"')) { |
||
318 | $v = \mb_substr($v, 1, \mb_strlen($v) - 2); |
||
319 | $v = \str_replace('\\"', '"', $v); |
||
320 | $v = \str_replace('\\\\', '\\', $v); |
||
321 | } |
||
322 | |||
323 | // Figure out type of ACE (public, user or group) |
||
324 | if (0 === \mb_strpos($v, '=')) { |
||
325 | $atype = 'public'; |
||
326 | } else { |
||
327 | if ($this->hasRoles()) { |
||
328 | $atype = 'role'; |
||
329 | } else { |
||
330 | if (0 === \mb_strpos($v, 'group ')) { |
||
331 | $atype = 'group'; |
||
332 | // Tear off 'group' prefix |
||
333 | $v = \mb_substr($v, 6); |
||
334 | } else { |
||
335 | $atype = 'user'; |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | // Break on unquoted equals sign... |
||
341 | $i = 0; |
||
342 | $in_quotes = false; |
||
343 | $entity = null; |
||
344 | $chars = null; |
||
345 | |||
346 | while (\mb_strlen($v) > $i) { |
||
347 | // If current char is a double quote and it's not escaped, then |
||
348 | // enter quoted bit |
||
349 | $char = \mb_substr($v, $i, 1); |
||
350 | $next_char = \mb_substr($v, $i + 1, 1); |
||
351 | |||
352 | if ('"' === $char && (0 === $i || '"' !== $next_char)) { |
||
353 | $in_quotes = !$in_quotes; |
||
354 | } elseif ('"' === $char && '"' === $next_char) { |
||
355 | // Skip over escaped double quotes |
||
356 | ++$i; |
||
357 | } elseif ('=' === $char && !$in_quotes) { |
||
358 | // Split on current equals sign |
||
359 | $entity = \mb_substr($v, 0, $i); |
||
360 | $chars = \mb_substr($v, $i + 1); |
||
361 | |||
362 | break; |
||
363 | } |
||
364 | ++$i; |
||
365 | } |
||
366 | |||
367 | // Check for quoting on entity name, and unescape if necessary |
||
368 | if (0 === \mb_strpos($entity, '"')) { |
||
369 | $entity = \mb_substr($entity, 1, \mb_strlen($entity) - 2); |
||
370 | $entity = \str_replace('""', '"', $entity); |
||
371 | } |
||
372 | |||
373 | // New row to be added to $temp |
||
374 | // (type, grantee, privileges, grantor, grant option? |
||
375 | $row = [$atype, $entity, [], '', []]; |
||
376 | |||
377 | // Loop over chars and add privs to $row |
||
378 | for ($i = 0; \mb_strlen($chars) > $i; ++$i) { |
||
379 | // Append to row's privs list the string representing |
||
380 | // the privilege |
||
381 | $char = \mb_substr($chars, $i, 1); |
||
382 | |||
383 | if ('*' === $char) { |
||
384 | $row[4][] = $this->privmap[\mb_substr($chars, $i - 1, 1)]; |
||
385 | } elseif ('/' === $char) { |
||
386 | $grantor = \mb_substr($chars, $i + 1); |
||
387 | // Check for quoting |
||
388 | if (0 === \mb_strpos($grantor, '"')) { |
||
389 | $grantor = \mb_substr($grantor, 1, \mb_strlen($grantor) - 2); |
||
390 | $grantor = \str_replace('""', '"', $grantor); |
||
391 | } |
||
392 | $row[3] = $grantor; |
||
393 | |||
394 | break; |
||
395 | } else { |
||
396 | if (!isset($this->privmap[$char])) { |
||
397 | return -3; |
||
398 | } |
||
399 | |||
400 | $row[2][] = $this->privmap[$char]; |
||
401 | } |
||
402 | } |
||
403 | |||
404 | // Append row to temp |
||
405 | $temp[] = $row; |
||
406 | } |
||
407 | |||
408 | return $temp; |
||
409 | } |
||
411 |