| Conditions | 9 |
| Paths | 25 |
| Total Lines | 55 |
| Code Lines | 29 |
| 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 |
||
| 22 | function processURLPaths($data) |
||
| 23 | { |
||
| 24 | $final = array(); |
||
| 25 | |||
| 26 | $containsType = false; |
||
| 27 | $containsIndex = false; |
||
| 28 | foreach ($data['url']['paths'] as $path) { |
||
| 29 | $params = array(); |
||
| 30 | preg_match_all('/{(.*?)}/', $path, $params); |
||
| 31 | $params = $params[1]; |
||
| 32 | $count = count($params); |
||
| 33 | $parsedPath = str_replace('}','',$path); |
||
| 34 | $parsedPath = str_replace('{','$',$parsedPath); |
||
| 35 | |||
| 36 | if (array_search('index', $params) !== false) { |
||
| 37 | $containsIndex = true; |
||
| 38 | } |
||
| 39 | |||
| 40 | if (array_search('type', $params) !== false) { |
||
| 41 | $containsType = true; |
||
| 42 | } |
||
| 43 | |||
| 44 | $duplicate = false; |
||
| 45 | foreach ($final as $existing) { |
||
| 46 | if ($existing['params'] === $params) { |
||
| 47 | $duplicate = true; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($duplicate !== true) { |
||
| 52 | $final[] = array( |
||
| 53 | 'path' => $path, |
||
| 54 | 'parsedPath' => $parsedPath, |
||
| 55 | 'params' => $params, |
||
| 56 | 'count' => $count |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /* |
||
| 62 | foreach ($final as &$existing) { |
||
| 63 | if ($containsIndex === true && array_search('index', $existing['params']) === false && array_search('type', $existing['params']) !== false) { |
||
| 64 | $existing['parsedPath'] = '/_all'.$existing['parsedPath']; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | */ |
||
| 68 | |||
| 69 | usort($final, function($a, $b) { |
||
| 70 | if ($a['count'] == $b['count']) { |
||
| 71 | return 0; |
||
| 72 | } |
||
| 73 | return ($a['count'] > $b['count']) ? -1 : 1; |
||
| 74 | }); |
||
| 75 | |||
| 76 | return $final; |
||
| 77 | } |
||
| 199 | } |