Completed
Push — 3.0 ( 46e3af...f23b72 )
by Daniel
03:20
created

ModmanParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.1%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 9
c 3
b 0
f 1
lcom 1
cbo 0
dl 0
loc 62
ccs 27
cts 29
cp 0.931
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B parseMappings() 0 26 6
A getMappings() 0 9 2
1
<?php
2
/**
3
 * Composer Magento Installer
4
 */
5
6
namespace MagentoHackathon\Composer\Magento\Parser;
7
8
/**
9
 * Parsers modman files
10
 */
11
class ModmanParser implements Parser
12
{
13
14
    /**
15
     * @var \SplFileObject The modman file
16
     */
17
    protected $file;
18
19
    /**
20
     *
21
     * @param string $modManFile
22
     */
23 4
    public function __construct($modManFile)
24
    {
25 4
        $this->file = new \SplFileObject($modManFile);
26 4
    }
27
28
    /**
29
     * @return array
30
     * @throws \ErrorException
31
     */
32 4
    public function getMappings()
33
    {
34 4
        if (!$this->file->isReadable()) {
35 1
            throw new \ErrorException(sprintf('modman file "%s" not readable', $this->file->getPathname()));
36
        }
37
38 3
        $map = $this->parseMappings();
39 2
        return $map;
40
    }
41
42
    /**
43
     * @throws \ErrorException
44
     * @return array
45
     */
46 2
    protected function parseMappings()
47
    {
48 2
        $map = array();
49 2
        foreach ($this->file as $line => $row) {
50 2
            $row = trim($row);
51 2
            if ('' === $row || in_array($row[0], array('#', '@'))) {
52 1
                continue;
53
            }
54 2
            $parts = preg_split('/\s+/', $row, -1, PREG_SPLIT_NO_EMPTY);
55 2
            if (count($parts) === 1) {
56
                $part = reset($parts);
57
                $map[] = array($part, $part);
58 2
            } elseif (is_int(count($parts) / 2)) {
59 1
                $partCountSplit = count($parts) / 2;
60 1
                $map[] = array(
61 1
                    implode(' ', array_slice($parts, 0, $partCountSplit)),
62 1
                    implode(' ', array_slice($parts, $partCountSplit)),
63
                );
64 1
            } else {
65 1
                throw new \ErrorException(
66 1
                    sprintf('Invalid row on line %d has %d parts, expected 2', $line, count($parts))
67 1
                );
68
            }
69 1
        }
70 1
        return $map;
71
    }
72
}
73