Passed
Push — master ( 0a7486...d714ad )
by Sebastian
02:08
created

File::getAddedContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * This file is part of SebastianFeldmann\Git.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace SebastianFeldmann\Git\Diff;
13
14
/**
15
 * Class File
16
 *
17
 * @package SebastianFeldmann\Git
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/git
20
 * @since   Class available since Release 1.2.0
21
 */
22
class File
23
{
24
    public const OP_DELETED = 'deleted';
25
    public const OP_CREATED = 'created';
26
    public const OP_MODIFIED = 'modified';
27
    public const OP_RENAMED = 'renamed';
28
    public const OP_COPIED = 'copied';
29
30
    /**
31
     * List of changes.
32
     *
33
     * @var \SebastianFeldmann\Git\Diff\Change[]
34
     */
35
    private $changes = [];
36
37
    /**
38
     * Filename
39
     *
40
     * @var string
41
     */
42
    private $name;
43
44
    /**
45
     * Operation performed on the file.
46
     *
47
     * @var string
48
     */
49
    private $operation;
50
51
    /**
52
     * File constructor.
53
     *
54
     * @param string $name
55
     * @param string $operation
56 13
     */
57
    public function __construct(string $name, string $operation)
58 13
    {
59 13
        $this->operation = $operation;
60 13
        $this->name      = $name;
61
    }
62
63
    /**
64
     * Returns the filename.
65
     *
66
     * @return string
67 1
     */
68
    public function getName(): string
69 1
    {
70
        return $this->name;
71
    }
72
73
    /**
74
     * Returns the performed operation.
75
     *
76
     * @return string
77 8
     */
78
    public function getOperation(): string
79 8
    {
80
        return $this->operation;
81
    }
82
83
    /**
84
     * Returns the list of changes in this file.
85
     *
86
     * @return \SebastianFeldmann\Git\Diff\Change[]
87 10
     */
88
    public function getChanges(): array
89 10
    {
90
        return $this->changes;
91
    }
92
93
    /**
94
     * Add a change to the list of changes.
95
     *
96
     * @param  \SebastianFeldmann\Git\Diff\Change $change
97
     * @return void
98 9
     */
99
    public function addChange(Change $change): void
100 9
    {
101 9
        $this->changes[] = $change;
102
    }
103
104
    /**
105
     * Return all newly added content
106
     *
107
     * @return string[]
108
     */
109
    public function getAddedContent(): array
110
    {
111
        $content = [];
112
        if ($this->operation === self::OP_DELETED) {
113
            return $content;
114
        }
115
116
        foreach ($this->changes as $change) {
117
            $content = array_merge($content, $change->getAddedContent());
118
        }
119
120
        return $content;
121
    }
122
}
123