ImportProviders::generateProviders()   B
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 66
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 66
rs 7.8682
cc 8
nc 10
nop 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
namespace Cohensive\OEmbed;
4
5
use \Exception;
0 ignored issues
show
Bug introduced by
The type \Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class ImportProviders
8
{
9
    public function generateProviders()
10
    {
11
        $response = file_get_contents('https://oembed.com/providers.json');
12
13
        if (!$response) {
14
            throw new Exception('Error reteriving providers list.');
15
        }
16
17
        $providers = json_decode($response, true);
18
19
        $providerFormat = <<<END
20
        [
21
            'provider_name' => '%s',
22
            'provider_url' => '%s',
23
            'endpoints' => [
24
                %s
25
            ]
26
        ],
27
28
        END;
29
30
        $endpointFormat = <<<END
31
        [
32
                    'schemes' => [
33
                %s
34
                    ],
35
                    'url' => '%s',
36
                ],
37
        END;
38
39
        $schemeFormat = <<<END
40
                '%s',
41
        END;
42
43
        $formattedProviders = '';
44
        foreach ($providers as $provider) {
45
            $endpoints = '';
46
            foreach ($provider['endpoints'] as $ekey => $endpoint) {
47
                if (!isset($endpoint['schemes'])) {
48
                    continue;
49
                }
50
51
                $schemes = '';
52
                $url = str_replace('.{format}', '.json', $endpoint['url']);
53
                $url = str_replace('?format={format}', '?format=json', $url);
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
54
                foreach ($endpoint['schemes'] as $skey => $url) {
55
                    $scheme = '|^' . $url;
56
                    $scheme = preg_replace('/([.=?#-])/i', '\\\\\\\\${1}', $scheme);
57
                    $scheme = str_replace('http:', 'https?:', $scheme);
58
                    $scheme = str_replace('https:', 'https?:', $scheme);
59
                    $scheme = str_replace('*', '.*', $scheme);
60
                    $scheme .= '$|i';
61
                    $schemes .= ($skey > 0 ? "\n        " : "") . sprintf($schemeFormat, $scheme);
62
                }
63
                $endpoints .= ($ekey > 0 ? "\n        " : "") . sprintf($endpointFormat, $schemes, $endpoint['url']);
64
            }
65
66
            $formattedProviders .= sprintf(
67
                $providerFormat,
68
                $provider['provider_name'],
69
                $provider['provider_url'],
70
                $endpoints
71
            );
72
        }
73
74
        file_put_contents('providers.txt', $formattedProviders);
75
    }
76
77
    public function generateSimpleProviders()
78
    {
79
        $response = file_get_contents('https://oembed.com/providers.json');
80
81
        if (!$response) {
82
            throw new Exception('Error reteriving providers list.');
83
        }
84
85
        $providers = json_decode($response, true);
86
87
        $providerFormat = <<<END
88
        '%s' => [
89
            'schemes' => [
90
                %s
91
            ]
92
        ],
93
94
        END;
95
96
        $schemeFormat = <<<END
97
        '%s',
98
        END;
99
100
        $formattedProviders = '';
101
        foreach ($providers as $provider) {
102
            foreach ($provider['endpoints'] as $endpoint) {
103
                if (!isset($endpoint['schemes'])) {
104
                    continue;
105
                }
106
107
                $schemes = '';
108
                $url = str_replace('.{format}', '.json', $endpoint['url']);
109
                $url = str_replace('?format={format}', '?format=json', $url);
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
110
                foreach ($endpoint['schemes'] as $skey => $url) {
111
                    $scheme = '|^' . $url;
112
                    $scheme = preg_replace('/([.=?#-])/i', '\\\\\\\\${1}', $scheme);
113
                    $scheme = str_replace('http:', 'https?:', $scheme);
114
                    $scheme = str_replace('https:', 'https?:', $scheme);
115
                    $scheme = str_replace('*', '.*', $scheme);
116
                    $scheme .= '$|i';
117
                    $schemes .= ($skey > 0 ? "\n        " : "") . sprintf($schemeFormat, $scheme);
118
                }
119
120
                $formattedProviders .= sprintf(
121
                    $providerFormat,
122
                    $endpoint['url'],
123
                    $schemes
124
                );
125
            }
126
        }
127
128
        file_put_contents('simple_providers.txt', $formattedProviders);
129
    }
130
}
131
132
(new ImportProviders)->generateProviders();
133
(new ImportProviders)->generateSimpleProviders();
134