Conditions | 27 |
Paths | 1520 |
Total Lines | 78 |
Code Lines | 38 |
Lines | 6 |
Ratio | 7.69 % |
Changes | 9 | ||
Bugs | 6 | 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 |
||
35 | public static function buildPathway(array $to = null, $encode = true) |
||
36 | { |
||
37 | |||
38 | // if empty passed - let show main page |
||
39 | if ($to === null) { |
||
40 | return null; |
||
41 | } |
||
42 | $response = Str::lowerCase(trim($to[0], '/')); // controller/action |
||
43 | list($controller, $action) = explode('/', $response); |
||
44 | |||
45 | $routing = App::$Properties->getAll('Routing'); |
||
46 | // sounds like dynamic callback |
||
47 | if (Str::startsWith('@', $controller)) { |
||
48 | $controller = trim($controller, '@'); |
||
49 | // search callback in properties |
||
50 | if (isset($routing['Callback'][env_name]) && Arr::in($controller, $routing['Callback'][env_name])) { |
||
51 | $pathInject = array_search($controller, $routing['Callback'][env_name]); |
||
52 | // if path is founded - lets set source |
||
53 | if ($pathInject !== false) { |
||
54 | $controller = Str::lowerCase($pathInject); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | // if controller still looks like path injection - define last entity like controller name |
||
59 | if (Str::contains('\\', $controller)) { |
||
60 | $controller = Str::lastIn($controller, '\\', true); |
||
61 | } |
||
62 | |||
63 | $response = $controller . '/' . $action; |
||
64 | } |
||
65 | |||
66 | // check if controller and action is defined |
||
67 | if (Str::likeEmpty($controller) || Str::likeEmpty($action)) { |
||
68 | return null; |
||
69 | } |
||
70 | |||
71 | // id is defined? |
||
72 | View Code Duplication | if (isset($to[1]) && !Str::likeEmpty($to[1])) { |
|
|
|||
73 | $response .= '/' . self::safeUri($to[1], $encode); |
||
74 | } |
||
75 | |||
76 | // add param is defined? |
||
77 | View Code Duplication | if (isset($to[2]) && !Str::likeEmpty($to[2])) { |
|
78 | $response .= '/' . self::safeUri($to[2], $encode); |
||
79 | } |
||
80 | |||
81 | // try to find static alias |
||
82 | if (isset($routing['Alias'][env_name]) && Arr::in('/' . $response, $routing['Alias'][env_name])) { |
||
83 | $pathAlias = array_search('/' . $response, $routing['Alias'][env_name]); |
||
84 | if ($pathAlias !== false) { |
||
85 | $response = Str::lowerCase(trim($pathAlias, '/')); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | // parse get attributes |
||
90 | if (isset($to[3]) && Obj::isArray($to[3]) && count($to[3]) > 0) { |
||
91 | // check if anchor bindig is exist |
||
92 | $anchor = false; |
||
93 | if (isset($to[3]['#']) && Obj::isString($to[3]['#']) && Str::startsWith('#', $to[3]['#'])) { |
||
94 | $anchor = $to[3]['#']; |
||
95 | unset($to[3]['#']); |
||
96 | } |
||
97 | $queryString = http_build_query($to[3]); |
||
98 | if (Str::length($queryString) > 0) { |
||
99 | $response .= '?' . http_build_query($to[3]); |
||
100 | } |
||
101 | if ($anchor !== false) { |
||
102 | $response .= $anchor; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // parse anchor link part #item-related-id-1 |
||
107 | if (isset($to[4]) && Obj::isString($to[4]) && Str::startsWith('#', $to[4])) { |
||
108 | $response .= $to[4]; |
||
109 | } |
||
110 | |||
111 | return $response; |
||
112 | } |
||
113 | |||
209 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.