Completed
Push — master ( 6bc3b4...34ca55 )
by Ralf
15s queued 11s
created

generateTemplate()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 77
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 49
nc 13
nop 2
dl 0
loc 77
rs 8.1793
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
require_once '../vendor/autoload.php';
4
5
$loader = new Twig_Loader_Filesystem('templates');
6
$twig = new Twig_Environment($loader);
7
$template = $twig->loadTemplate('endpoint.twig');
8
9
$counter = 0;
10
11
if ($handle = opendir('../vendor/elasticsearch/elasticsearch_src/rest-api-spec/api/')) {
12
    while (false !== ($entry = readdir($handle))) {
13
        if ($entry != "." && $entry != "..") {
14
            generateTemplate($entry, $template);
15
        }
16
17
    }
18
    closedir($handle);
19
}
20
21
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
}
78
79
function getDefaultPath($path) {
80
    if ($path['count'] === 0) {
81
        return $path['path'];
82
    } else {
83
        $final = str_replace('}','',$path['path']);
84
        $final = str_replace('{','$',$final);
85
        return $final;
86
    }
87
}
88
89
function forbid($key, $value)
90
{
91
    $forbid = array(
92
        'GET' => array(
93
            '/_nodes/hotthreads',
94
            '/_nodes/{node_id}/hotthreads',
95
            '/_nodes/{metric}'
96
        ),
97
        'HEAD' => array(),
98
        'PUT' => array(
99
            '/{index}/{type}/_mapping'
100
        ),
101
        'POST' => array(
102
            '/_all/{type}/_bulk',
103
            '/_all/{type}/_mget'
104
        ),
105
        'DELETE' => array(
106
            '/{index}/{type}',
107
            '/{index}/{type}/_mapping'
108
        ),
109
        'QS' => array(
110
            'operation_threading',
111
            'field_data'
112
        )
113
    );
114
115
    if (isset($forbid['key']) && $forbid['key'] === $value) {
116
        return true;
117
    } else {
118
        return false;
119
    }
120
}
121
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
}