Test Failed
Pull Request — master (#85)
by Gabriel
04:03 queued 01:11
created

ChangeWindows::fetchCurrentVersion()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 0
1
<?php
2
3
/**
4
 * Fetch versions from changewindows.org
5
 * © 2014-present CHANGEWINDOWS
6
 *
7
 * Disclaimer
8
 * All trademarks mentioned are the property of their respective owners. The content generated by this script
9
 * comes from changewindows.org and may not be accurate.
10
 */
11
class ChangeWindows
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    private static $errors = array(
14
        'could_not_fetch_version' => 'Could not fetch current version from ChangeWindows',
15
        'invalid_version' => 'Windows version is invalid',
16
        'could_not_fetch_page' => 'Could not fetch page from ChangeWindows'
17
    );
18
19
    public static function fetchVersions()
20
    {
21
        $windowsVersions = json_decode(file_get_contents(__DIR__ . '/windowsVersions.json'), true);
22
        if (!count($windowsVersions)) {
23
            $currentVersion = explode('.', self::fetchCurrentVersion(), 2);
24
            if (!isset($currentVersion[0])) {
25
                throw new Exception(self::$errors['invalid_version']);
26
            }
27
            $windowsVersions = self::fetchVersion($windowsVersions, $currentVersion[0]);
28
            self::writeWindowsVersions($windowsVersions);
29
        } else {
30
            reset($windowsVersions);
31
            $firstVersion = key($windowsVersions);
32
            end($windowsVersions);
33
            $lastVersion = key($windowsVersions);
34
35
            try {
36
                $result = self::fetchVersion($windowsVersions, $firstVersion);
37
                $windowsVersions = $result;
38
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
39
            }
40
41
            $windowsVersions = self::fetchVersion($windowsVersions, $lastVersion);
42
            self::writeWindowsVersions($windowsVersions);
43
        }
44
    }
45
46
    private static function fetchVersion($windowsVersions, $version)
47
    {
48
        $siblingVersions = self::fetchPage($version);
49
        $windowsVersions[$version] = true;
50
        self::writeWindowsVersions($windowsVersions);
51
52 View Code Duplication
        if (isset($siblingVersions[0]) && !isset($windowsVersions[$siblingVersions[0]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            $windowsVersions = self::fetchVersion($windowsVersions, $siblingVersions[0]);
54
        }
55
56 View Code Duplication
        if (isset($siblingVersions[1]) && !isset($windowsVersions[$siblingVersions[1]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            $windowsVersions = self::fetchVersion($windowsVersions, $siblingVersions[1]);
58
        }
59
60
        return $windowsVersions;
61
    }
62
63
    private static function writeWindowsVersions($windowsVersions)
64
    {
65
        ksort($windowsVersions);
66
        file_put_contents(__DIR__ . '/windowsVersions.json', json_encode($windowsVersions, JSON_PRETTY_PRINT));
67
    }
68
69
    private static function fetchCurrentVersion()
70
    {
71
        $content = file_get_contents('https://changewindows.org/filter/pc/all/current/month/true');
72
        if (!$content) {
73
            throw new Exception(self::$errors['could_not_fetch_version']);
74
        }
75
        $content = explode('class="timeline"', $content, 2);
76
        if (!isset($content[1])) {
77
            throw new Exception(self::$errors['could_not_fetch_version']);
78
        }
79
        $content = explode('build"', $content[1], 2);
80
        if (!isset($content[1])) {
81
            throw new Exception(self::$errors['could_not_fetch_version']);
82
        }
83
        preg_match("/(\d*\.\d*)<\/div>/", $content[1], $matches);
84
        if (!isset($matches[1])) {
85
            throw new Exception(self::$errors['could_not_fetch_version']);
86
        }
87
        return $matches[1];
88
    }
89
90
    private static function fetchPage($version)
91
    {
92
        $url = "https://changewindows.org/build/{$version}/pc";
93
        $content = file_get_contents($url);
94
        $siblingVersions = self::fetchSiblingVersions($content);
95
        self::fetchEdgeVersion($content);
96
        return $siblingVersions;
97
    }
98
99
    private static function fetchEdgeVersion($content)
100
    {
101
        preg_match('/<h4[^>]*> *Edge ([\d\.]*) *<\/h4>/', $content, $edge);
102
        preg_match('/<h4[^>]*>EdgeHTML ([\d\.]*)<\/h4>/', $content, $edgeHtml);
103
104
        if (isset($edge[1]) && isset($edgeHtml[1])) {
105
            self::writeEdgeVersion($edgeHtml[1], $edge[1]);
106
        }
107
        return null;
108
    }
109
110 View Code Duplication
    private static function writeEdgeVersion($edgeHtml, $edge)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $file = __DIR__ . '/../../src/edgeVersionMap.php';
113
        $currentVersions = require $file;
114
        if (!isset($currentVersions[$edgeHtml])) {
115
            $currentVersions[$edgeHtml] = $edge;
116
            ksort($currentVersions);
117
            $content = '';
118
            foreach ($currentVersions as $edgeHtml => $edge) {
119
                $content .= "    '{$edgeHtml}' => '{$edge}'," . PHP_EOL;
120
            }
121
            $data = <<<PHP
122
<?php
123
124
return array(
125
    %s
126
);
127
128
PHP;
129
            file_put_contents($file, sprintf($data, trim($content)));
130
        }
131
    }
132
133
    private static function fetchSiblingVersions($content)
134
    {
135
        if (!$content) {
136
            throw new Exception(self::$errors['could_not_fetch_page']);
137
        }
138
        $content = explode('build-sidebar', $content, 2);
139
        if (!isset($content[1])) {
140
            throw new Exception(self::$errors['could_not_fetch_page']);
141
        }
142
        $content = explode('fa-angle-left', $content[1]);
143
        if (!isset($content[1])) {
144
            throw new Exception(self::$errors['could_not_fetch_page']);
145
        }
146
        $content = explode('fa-angle-right', $content[1]);
147
        if (!isset($content[0])) {
148
            throw new Exception(self::$errors['could_not_fetch_page']);
149
        }
150
        preg_match_all("/> *(\d+) *</", $content[0], $matches);
151
        if (!isset($matches[1])) {
152
            throw new Exception(self::$errors['could_not_fetch_page']);
153
        }
154
        return $matches[1];
155
    }
156
}
157