Completed
Push — 3.0 ( 46111f...d5ff83 )
by Daniel
02:06
created

ModmanParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 14.52 %

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 9
loc 62
ccs 27
cts 29
cp 0.931
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getMappings() 9 9 2
B parseMappings() 0 26 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function getMappings()
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...
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