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

Wikipedia::extractVersion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 3
nop 1
1
<?php
2
3
class Wikipedia
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...
4
{
5
    const URL = 'https://en.wikipedia.org/w/api.php?action=query&titles=Microsoft_Edge&prop=revisions&rvprop=content&rvsection=4&format=xml';
6
7
    private static $errors = array(
8
        'fetch_error' => 'Unable to fetch content',
9
        'parse_error' => 'Unable to parse content',
10
    );
11
12
    public static function fetch()
13
    {
14
        $content = file_get_contents(self::URL);
15
        if (!$content) {
16
            throw new Exception(self::$errors['fetch_error']);
17
        }
18
        $content = explode('===Release history===', $content);
19
        if (!isset($content[1])) {
20
            throw new Exception(self::$errors['parse_error']);
21
        }
22
        $table = explode('|-', $content[1]);
23
        if (!isset($table[1])) {
24
            throw new Exception(self::$errors['parse_error']);
25
        }
26
        $table = array_slice($table, 1);
27
        $versions = array_map(array('Wikipedia', 'extractVersion'), $table);
28
        self::writeEdgeVersions($versions);
29
    }
30
31
    private static function extractVersion($content)
32
    {
33
        $lines = array_slice(array_filter(
34
            explode(PHP_EOL, $content),
35
            function ($val) {
36
                return trim($val) && strpos($val, '|') === 0;
37
            }
38
        ), 0, 2);
39
40
        preg_match("/{[^}{]*Version[^}{]*\| ?([\d\.]+)}/", $lines[0], $edgeVersion);
41
        preg_match("/\| *(\d*\.\d*)/", $lines[1], $edgeHtmlVersion);
42
43
        if (!isset($edgeVersion[1])) {
44
            throw new Exception(self::$errors['parse_error']);
45
        }
46
        if (!isset($edgeHtmlVersion[1])) {
47
            throw new Exception(self::$errors['parse_error']);
48
        }
49
50
        return array($edgeHtmlVersion[1], $edgeVersion[1]);
51
    }
52
53 View Code Duplication
    private static function writeEdgeVersions($versions)
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...
54
    {
55
        $file = __DIR__ . '/../../src/edgeVersionMap.php';
56
        $currentVersions = require $file;
57
58
        foreach ($versions as $version) {
59
            $currentVersions[$version[0]] = $version[1];
60
        }
61
        ksort($currentVersions);
62
63
        $content = '';
64
        foreach ($currentVersions as $edgeHtml => $edge) {
65
            $content .= "    '{$edgeHtml}' => '{$edge}'," . PHP_EOL;
66
        }
67
        $data = <<<PHP
68
<?php
69
70
return array(
71
    %s
72
);
73
74
PHP;
75
        file_put_contents($file, sprintf($data, trim($content)));
76
    }
77
}
78