Conditions | 4 |
Paths | 4 |
Total Lines | 54 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
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 |
||
83 | private function buildRestRoutes($routeBaseName, $routeBaseData) |
||
84 | { |
||
85 | // If route has a parameters array defined, take the first defined |
||
86 | // argument as ":id" parameter, and use key as parameter name |
||
87 | // otherwise, default to id => [0-9]* |
||
88 | if ( |
||
89 | isset($routeBaseData['parameters']) && |
||
90 | is_array($routeBaseData['parameters']) |
||
91 | ) { |
||
92 | reset($routeBaseData['parameters']); |
||
93 | $primaryParameterName = key($routeBaseData['parameters']); |
||
94 | |||
95 | $routeParameters = dataGet($routeBaseData, 'parameters', []); |
||
96 | } else { |
||
97 | $primaryParameterName = 'id'; |
||
98 | $primaryParameterPattern = '[0-9]*'; |
||
99 | |||
100 | $routeParameters = array_merge( |
||
101 | [$primaryParameterName => $primaryParameterPattern], |
||
102 | dataGet($routeBaseData, 'parameters', []) |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | $resources = [ |
||
107 | 'index' => ['method' => ['GET'], 'append' => ''], |
||
108 | 'create' => ['method' => ['GET'], 'append' => '/create'], |
||
109 | 'store' => ['method' => ['POST', 'OPTIONS'], 'append' => ''], |
||
110 | 'show' => [ |
||
111 | 'method' => ['GET'], |
||
112 | 'append' => '/:' . $primaryParameterName |
||
113 | ], |
||
114 | 'edit' => [ |
||
115 | 'method' => ['GET'], |
||
116 | 'append' => '/:' . $primaryParameterName . '/edit' |
||
117 | ], |
||
118 | 'update' => [ |
||
119 | 'method' => ['PUT', 'OPTIONS'], |
||
120 | 'append' => '/:' . $primaryParameterName |
||
121 | ], |
||
122 | 'destroy' => [ |
||
123 | 'method' => ['DELETE', 'OPTIONS'], |
||
124 | 'append' => '/:' . $primaryParameterName |
||
125 | ] |
||
126 | ]; |
||
127 | |||
128 | foreach ($resources as $name => $definition) { |
||
129 | $routeName = $routeBaseName . '.' . $name; |
||
130 | $routeData = $routeBaseData; |
||
131 | $routeData['method'] = $definition['method']; |
||
132 | $routeData['path'] .= $definition['append']; |
||
133 | $routeData['target'] .= '::' . $name; |
||
134 | $routeData['parameters'] = $routeParameters; |
||
135 | |||
136 | $this->buildRoute($routeName, $routeData); |
||
137 | } |
||
228 |