Test Failed
Pull Request — master (#85)
by Gabriel
02:58
created

Wikipedia::extractVersion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
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) throw new Exception(self::$errors['fetch_error']);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
16
        $content = explode('===Release history===', $content);
17
        if (!isset($content[1])) throw new Exception(self::$errors['parse_error']);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
18
        $table = explode('|-', $content[1]);
19
        if (!isset($table[1])) throw new Exception(self::$errors['parse_error']);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
20
        $table = array_slice($table, 1);
21
        $versions = array_map(array('Wikipedia', 'extractVersion'), $table);
22
        self::writeEdgeVersions($versions);
23
    }
24
25
    private static function extractVersion($content)
26
    {
27
        $lines = array_slice(array_filter(
28
            explode(PHP_EOL, $content),
29
            function ($val) { return trim($val) && strpos($val, '|') === 0; }
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
30
        ), 0, 2);
31
32
        preg_match("/{[^}{]*Version[^}{]*\| ?([\d\.]+)}/", $lines[0], $edgeVersion);
33
        preg_match("/\| *(\d*\.\d*)/", $lines[1], $edgeHtmlVersion);
34
35
        if (!isset($edgeVersion[1])) throw new Exception(self::$errors['parse_error']);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
36
        if (!isset($edgeHtmlVersion[1])) throw new Exception(self::$errors['parse_error']);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
37
38
        return array($edgeHtmlVersion[1], $edgeVersion[1]);
39
    }
40
41 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...
42
    {
43
        $file = __DIR__ . '/../../src/edgeVersionMap.php';
44
        $currentVersions = require $file;
45
46
        foreach ($versions as $version) {
47
            $currentVersions[$version[0]] = $version[1];
48
        }
49
        ksort($currentVersions);
50
51
        $content = '';
52
        foreach ($currentVersions as $edgeHtml => $edge) {
53
            $content .= "    '{$edgeHtml}' => '{$edge}'," . PHP_EOL;
54
        }
55
        $data = <<<PHP
56
<?php
57
58
return array(
59
    %s
60
);
61
62
PHP;
63
        file_put_contents($file, sprintf($data, trim($content)));
64
    }
65
}
66