Conditions | 7 |
Paths | 7 |
Total Lines | 52 |
Code Lines | 36 |
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 |
||
76 | public function generateSimpleProviders() |
||
77 | { |
||
78 | $response = file_get_contents('https://oembed.com/providers.json'); |
||
79 | |||
80 | if (!$response) { |
||
81 | throw new Exception('Error reteriving providers list.'); |
||
82 | } |
||
83 | |||
84 | $providers = json_decode($response, true); |
||
85 | |||
86 | $providerFormat = <<<END |
||
87 | '%s' => [ |
||
88 | 'schemes' => [ |
||
89 | %s |
||
90 | ] |
||
91 | ], |
||
92 | |||
93 | END; |
||
94 | |||
95 | $schemeFormat = <<<END |
||
96 | '%s', |
||
97 | END; |
||
98 | |||
99 | $formattedProviders = ''; |
||
100 | foreach ($providers as $provider) { |
||
101 | foreach ($provider['endpoints'] as $endpoint) { |
||
102 | if (!isset($endpoint['schemes'])) { |
||
103 | continue; |
||
104 | } |
||
105 | |||
106 | $schemes = ''; |
||
107 | $url = str_replace('.{format}', '.json', $endpoint['url']); |
||
108 | $url = str_replace('?format={format}', '?format=json', $url); |
||
109 | foreach ($endpoint['schemes'] as $skey => $url) { |
||
110 | $scheme = '|^' . $url; |
||
111 | $scheme = preg_replace('/([.=?#-])/i', '\\\\\\\\${1}', $scheme); |
||
112 | $scheme = str_replace('http:', 'https?:', $scheme); |
||
113 | $scheme = str_replace('https:', 'https?:', $scheme); |
||
114 | $scheme = str_replace('*', '.*', $scheme); |
||
115 | $scheme .= '$|i'; |
||
116 | $schemes .= ($skey > 0 ? "\n " : "") . sprintf($schemeFormat, $scheme); |
||
117 | } |
||
118 | |||
119 | $formattedProviders .= sprintf( |
||
120 | $providerFormat, |
||
121 | $endpoint['url'], |
||
122 | $schemes |
||
123 | ); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | file_put_contents('simple_providers.txt', $formattedProviders); |
||
128 | } |
||
133 |