GitignoreHandler::parsePath()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 8
nop 2
dl 0
loc 18
ccs 0
cts 17
cp 0
crap 30
rs 9.5555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\handlers;
12
13
/**
14
 * Handler for `.gitignore` file.
15
 */
16
class GitignoreHandler extends BaseHandler
17
{
18
    public $type = 'gitignore';
19
20
    public function parsePath($path, $minimal = null)
21
    {
22
        $items = [];
23
        $lines = is_file($path) ? $this->readArray($path) : [];
24
        $comment = '';
25
        foreach ($lines as $str) {
26
            $str = trim($str);
27
            if (!$str) {
28
                continue;
29
            }
30
            if ($str[0] === '#') {
31
                $comment = trim(substr($str, 1));
32
            } else {
33
                $items[$str] = $comment;
34
            }
35
        }
36
37
        return $items;
38
    }
39
40
    public function render($items)
41
    {
42
        $comments = [];
43
        foreach ($items as $item => $comment) {
44
            $comments[$comment][$item] = $item;
45
        }
46
47
        $res = '';
48
        foreach ($comments as $comment => $items) {
0 ignored issues
show
introduced by
$items is overwriting one of the parameters of this function.
Loading history...
49
            ksort($items);
50
            $res .= static::renderComment($comment) . implode("\n", $items) . "\n";
51
        }
52
53
        return ltrim($res);
54
    }
55
56
    public static function renderComment($comment)
57
    {
58
        return "\n#" . ($comment[0] === '#' ? '' : ' ') . "$comment\n";
59
    }
60
}
61