Conditions | 8 |
Paths | 10 |
Total Lines | 66 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
8 | public function generateProviders() |
||
9 | { |
||
10 | $response = file_get_contents('https://oembed.com/providers.json'); |
||
11 | |||
12 | if (!$response) { |
||
13 | throw new Exception('Error reteriving providers list.'); |
||
14 | } |
||
15 | |||
16 | $providers = json_decode($response, true); |
||
17 | |||
18 | $providerFormat = <<<END |
||
19 | [ |
||
20 | 'provider_name' => '%s', |
||
21 | 'provider_url' => '%s', |
||
22 | 'endpoints' => [ |
||
23 | %s |
||
24 | ] |
||
25 | ], |
||
26 | |||
27 | END; |
||
28 | |||
29 | $endpointFormat = <<<END |
||
30 | [ |
||
31 | 'schemes' => [ |
||
32 | %s |
||
33 | ], |
||
34 | 'url' => '%s', |
||
35 | ], |
||
36 | END; |
||
37 | |||
38 | $schemeFormat = <<<END |
||
39 | '%s', |
||
40 | END; |
||
41 | |||
42 | $formattedProviders = ''; |
||
43 | foreach ($providers as $provider) { |
||
44 | $endpoints = ''; |
||
45 | foreach ($provider['endpoints'] as $ekey => $endpoint) { |
||
46 | if (!isset($endpoint['schemes'])) { |
||
47 | continue; |
||
48 | } |
||
49 | |||
50 | $schemes = ''; |
||
51 | $url = str_replace('.{format}', '.json', $endpoint['url']); |
||
52 | $url = str_replace('?format={format}', '?format=json', $url); |
||
|
|||
53 | foreach ($endpoint['schemes'] as $skey => $url) { |
||
54 | $scheme = '|^' . $url; |
||
55 | $scheme = preg_replace('/([.=?#-])/i', '\\\\\\\\${1}', $scheme); |
||
56 | $scheme = str_replace('http:', 'https?:', $scheme); |
||
57 | $scheme = str_replace('https:', 'https?:', $scheme); |
||
58 | $scheme = str_replace('*', '.*', $scheme); |
||
59 | $scheme .= '$|i'; |
||
60 | $schemes .= ($skey > 0 ? "\n " : "") . sprintf($schemeFormat, $scheme); |
||
61 | } |
||
62 | $endpoints .= ($ekey > 0 ? "\n " : "") . sprintf($endpointFormat, $schemes, $endpoint['url']); |
||
63 | } |
||
64 | |||
65 | $formattedProviders .= sprintf( |
||
66 | $providerFormat, |
||
67 | $provider['provider_name'], |
||
68 | $provider['provider_url'], |
||
69 | $endpoints |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | file_put_contents('providers.txt', $formattedProviders); |
||
74 | } |
||
133 |