| Conditions | 7 |
| Paths | 13 |
| Total Lines | 77 |
| Code Lines | 49 |
| 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 |
||
| 122 | function generateTemplate($path, $template) |
||
| 123 | { |
||
| 124 | $ignoreList = array( |
||
| 125 | 'index.json', 'bulk.json' |
||
| 126 | ); |
||
| 127 | |||
| 128 | if (array_search($path, $ignoreList) !== false) { |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | |||
| 132 | $path = '../vendor/elasticsearch/elasticsearch_src/rest-api-spec/api/'.$path; |
||
| 133 | $json = file_get_contents($path); |
||
| 134 | $data = json_decode($json, true); |
||
| 135 | |||
| 136 | reset($data); |
||
| 137 | $namespace = key($data); |
||
| 138 | $data = $data[$namespace]; |
||
| 139 | $namespace = explode(".", $namespace); |
||
| 140 | |||
| 141 | $underscoreNamespace = array( |
||
| 142 | 'get', |
||
| 143 | 'put', |
||
| 144 | 'post', |
||
| 145 | 'delete', |
||
| 146 | 'exists', |
||
| 147 | 'update', |
||
| 148 | 'create' |
||
| 149 | ); |
||
| 150 | |||
| 151 | $exceptions = array( |
||
| 152 | 'delete_by_query' |
||
| 153 | ); |
||
| 154 | |||
| 155 | if (strpos($namespace[count($namespace)-1], '_')) { |
||
| 156 | $temp = explode('_',$namespace[count($namespace)-1]); |
||
| 157 | |||
| 158 | if (array_search($temp[0], $underscoreNamespace) !== false && array_search($namespace[count($namespace)-1], $exceptions) === false) { |
||
| 159 | $namespace[count($namespace)-1] = $temp[1]; |
||
| 160 | $namespace[] = $temp[0]; |
||
| 161 | } else { |
||
| 162 | $namespace[count($namespace)-1] = str_replace('_', '', $namespace[count($namespace)-1]); |
||
| 163 | } |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | $data['url']['processed'] = processURLPaths($data); |
||
| 170 | $data['url']['default'] = getDefaultPath($data['url']['processed'][count($data['url']['processed'])-1]); |
||
| 171 | |||
| 172 | $renderVars = array( |
||
| 173 | 'json' => $json, |
||
| 174 | 'data' => $data, |
||
| 175 | 'namespace' => $namespace, |
||
| 176 | 'className' => ucfirst($namespace[count($namespace)-1]), |
||
| 177 | ); |
||
| 178 | |||
| 179 | $ret = $template->render($renderVars); |
||
| 180 | |||
| 181 | $dir = './output/'.implode('/', array_map("ucfirst", array_splice($namespace,0,count($namespace)-1))); |
||
| 182 | |||
| 183 | if (substr($dir,-1) !== '/') { |
||
| 184 | $dir .= '/'; |
||
| 185 | } |
||
| 186 | if (!file_exists($dir)) { |
||
| 187 | echo 'making dir: '.$dir."\n\n"; |
||
| 188 | $oldumask = umask(0); |
||
| 189 | mkdir($dir, 0777, true); |
||
| 190 | umask($oldumask); |
||
| 191 | } |
||
| 192 | |||
| 193 | echo $dir."\n\n"; |
||
| 194 | $path = $dir.$renderVars['className'].'.php'; |
||
| 195 | echo $path."\n\n";; |
||
| 196 | |||
| 197 | file_put_contents($path, $ret); |
||
| 198 | echo $ret; |
||
| 199 | } |