Completed
Pull Request — master (#5)
by mon
02:48
created

MapUpdater::loadMapFromUrl()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 22
cp 0
rs 9.2568
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
1
<?php
2
3
namespace FileEye\MimeMap;
4
5
use SebastianBergmann\Comparator\ComparisonFailure;
6
use SebastianBergmann\Comparator\Factory;
7
use SebastianBergmann\Diff\Differ;
8
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
9
10
/**
11
 * Compiles the MIME type to file extension map.
12
 */
13
class MapUpdater
14
{
15
    /**
16
     * Default URL where to read the specification from.
17
     *
18
     * The apache httpd project contains the most complete list of file
19
     * extension to mime type mapping on the planet. We use it to update our
20
     * own list.
21
     */
22
    const DEFAULT_SOURCE_FILE = 'http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co';
23
24
    /**
25
     * Creates a new type-to-extension map reading from a file.
26
     *
27
     * @param string $source_file
28
     *   (Optional) the source file. Defaults to the Apache source bind_textdomain_codeset
29
     *   repository file where MIME types and file extensions are associated.
30
     *
31
     * @throws \RuntimeException if file I/O error occurs.
32
     *
33
     * @return MapHandler
34
     *   The map handler with the new map.
35
     */
36 5
    public function createMapFromSourceFile($source_file = MapUpdater::DEFAULT_SOURCE_FILE)
37
    {
38 5
        $map = new MapHandler([]);
39 5
        $lines = file($source_file);
40 5
        foreach ($lines as $line) {
41 4
            if ($line{0} == '#') {
42 4
                continue;
43
            }
44 4
            $line = preg_replace("#\\s+#", ' ', trim($line));
45 4
            $parts = explode(' ', $line);
46 4
            $type = array_shift($parts);
47 4
            foreach ($parts as $extension) {
48 4
                $map->addMapping($type, $extension);
49
            }
50
        }
51 5
        $map_array = $map->get();
52 5
        if (empty($map_array)) {
53 1
            throw new \RuntimeException('No data found in file ' . $source_file);
54
        }
55 4
        return $map;
56
    }
57
58
    /**
59
     * Compares two type-to-extension maps by section.
60
     *
61
     * @param MapHandler $old_map
62
     *   The first map to compare.
63
     * @param MapHandler $new_map
64
     *   The second map to compare.
65
     * @param string $section
66
     *   The first-level array key to compare: 'types' or 'extensions'.
67
     *
68
     * @throws \RuntimeException with diff details if the maps differ.
69
     *
70
     * @return bool
71
     *   True if the maps are equal.
72
     */
73 2
    public function compareMaps(MapHandler $old_map, MapHandler $new_map, $section)
74
    {
75 2
        $old_map->sort();
76 2
        $new_map->sort();
77 2
        $old = $old_map->get();
78 2
        $new = $new_map->get();
79
80 2
        $factory = new Factory;
81 2
        $comparator = $factory->getComparatorFor($old[$section], $new[$section]);
82
        try {
83 2
            $comparator->assertEquals($old[$section], $new[$section]);
84 1
            return true;
85 1
        } catch (ComparisonFailure $failure) {
86 1
            $old_string = var_export($old[$section], true);
87 1
            $new_string = var_export($new[$section], true);
88 1
            if (PHP_VERSION_ID >= 70000) {
89 1
                $differ = new Differ(new UnifiedDiffOutputBuilder("--- Removed\n+++ Added\n"));
90 1
                throw new \RuntimeException($differ->diff($old_string, $new_string));
91
            } else {
92
                throw new \RuntimeException(' ');
93
            }
94
        }
95
    }
96
97
    /**
98
     * Updates the map at a destination PHP file.
99
     *
100
     * @param MapHandler $map
101
     *   The map.
102
     * @param string $section
0 ignored issues
show
Bug introduced by
There is no parameter named $section. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
103
     *   The destination PHP file.
104
     *
105
     * @return void
106
     */
107 1
    public function writeMapToCodeFile(MapHandler $map, $output_file)
108
    {
109 1
        $content = preg_replace(
110 1
            '#public static \$map = (.+?);#s',
111 1
            "public static \$map = " . var_export($map->get(), true) . ";",
112 1
            file_get_contents($output_file)
113
        );
114 1
        file_put_contents($output_file, $content);
115 1
    }
116
}
117