Completed
Push — 3.0 ( f23b72...fb09f5 )
by Daniel
04:42
created

GitIgnore::removeDuplicates()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 20
cp 0
rs 8.5806
cc 4
eloc 14
nc 2
nop 1
crap 20
1
<?php
2
3
namespace MagentoHackathon\Composer\Magento;
4
5
/**
6
 * Class GitIgnore
7
 * @package MagentoHackathon\Composer\Magento
8
 * @author  Aydin Hassan <[email protected]>
9
 */
10
class GitIgnore
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $lines = array();
16
17
    /**
18
     * @var string|null
19
     */
20
    protected $gitIgnoreLocation;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $hasChanges = false;
26
27
    /**
28
     * @param string $fileLocation
29
     */
30
    public function __construct($fileLocation)
31
    {
32
        $this->gitIgnoreLocation = $fileLocation;
33
        if (file_exists($fileLocation)) {
34
            $this->lines = $this->removeDuplicates(file($fileLocation, FILE_IGNORE_NEW_LINES));
35
        }
36
    }
37
38
    /**
39
     * @param string $file
40
     */
41
    public function addEntry($file)
42
    {
43
        $file = $this->prependSlashIfNotExist($file);
44
        if (!in_array($file, $this->lines)) {
45
            $this->lines[] = $file;
46
        }
47
        $this->hasChanges = true;
48
    }
49
50
    /**
51
     * @param array $files
52
     */
53
    public function addMultipleEntries(array $files)
54
    {
55
        foreach ($files as $file) {
56
            $this->addEntry($file);
57
        }
58
    }
59
60
    /**
61
     * @param string $file
62
     */
63
    public function removeEntry($file)
64
    {
65
        $file = $this->prependSlashIfNotExist($file);
66
        $key = array_search($file, $this->lines);
67
        if (false !== $key) {
68
            unset($this->lines[$key]);
69
            $this->hasChanges = true;
70
71
            // renumber array
72
            $this->lines = array_values($this->lines);
73
        }
74
    }
75
76
    /**
77
     * @param array $files
78
     */
79
    public function removeMultipleEntries(array $files)
80
    {
81
        foreach ($files as $file) {
82
            $this->removeEntry($file);
83
        }
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function getEntries()
90
    {
91
        return $this->lines;
92
    }
93
94
    /**
95
     * Write the file
96
     */
97
    public function write()
98
    {
99
        if ($this->hasChanges) {
100
            file_put_contents($this->gitIgnoreLocation, implode("\n", $this->lines));
101
        }
102
    }
103
104
    /**
105
     * Prepend a forward slash to a path
106
     * if it does not already start with one.
107
     *
108
     * @param string $file
109
     * @return string
110
     */
111
    private function prependSlashIfNotExist($file)
112
    {
113
        return sprintf('/%s', ltrim($file, '/'));
114
    }
115
116
    /**
117
     * Removes duplicate patterns from the input array, without touching comments, line breaks etc.
118
     * Will remove the last duplicate pattern.
119
     *
120
     * @param array $lines
121
     * @return array
122
     */
123
    private function removeDuplicates($lines)
124
    {
125
        // remove empty lines
126
        $duplicates = array_filter($lines);
127
128
        // remove comments
129
        $duplicates = array_filter($duplicates, function ($line) {
130
            return strpos($line, '#') !== 0;
131
        });
132
133
        // check if duplicates exist
134
        if (count($duplicates) !== count(array_unique($duplicates))) {
135
            $duplicates = array_filter(array_count_values($duplicates), function ($count) {
136
                return $count > 1;
137
            });
138
139
            // search from bottom to top
140
            $lines = array_reverse($lines);
141
            foreach ($duplicates as $duplicate => $count) {
142
                // remove all duplicates, except the first one
143
                for ($i = 1; $i < $count; $i++) {
144
                    $key = array_search($duplicate, $lines);
145
                    unset($lines[$key]);
146
                }
147
            }
148
149
            // restore original order
150
            $lines = array_values(array_reverse($lines));
151
        }
152
153
        return $lines;
154
    }
155
}
156