Completed
Push — master ( c1a738...2c88a4 )
by
unknown
02:35 queued 01:13
created

StatusFile::setDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Status;
22
23
/**
24
 * Class StatusFile
25
 *
26
 * @package GitElephant\Status
27
 */
28
class StatusFile
29
{
30
    public const UNTRACKED = '?';
31
    public const IGNORED = '!';
32
    public const UNMODIFIED = '';
33
    public const MODIFIED = 'M';
34
    public const ADDED = 'A';
35
    public const DELETED = 'D';
36
    public const RENAMED = 'R';
37
    public const COPIED = 'C';
38
    public const UPDATED_BUT_UNMERGED = 'U';
39
40
    /**
41
     * @var string
42
     */
43
    private $x;
44
45
    /**
46
     * @var string
47
     */
48
    private $y;
49
50
    /**
51
     * @var string
52
     */
53
    private $name;
54
55
    /**
56
     * @var string
57
     */
58
    private $renamed;
59
60
    /**
61
     * @var string
62
     */
63
    private $type;
64
65
    /**
66
     * @var string
67
     */
68
    private $description;
69
70
    /**
71
     * @param string $x       X section of the status --porcelain output
72
     * @param string $y       Y section of the status --porcelain output
73
     * @param string $name    file name
74
     * @param string $renamed new file name (if renamed)
75
     */
76 9
    private function __construct(string $x, string $y, string $name, string $renamed = null)
77
    {
78 9
        $this->x = ' ' === $x ? null : $x;
79 9
        $this->y = ' ' === $y ? null : $y;
80 9
        $this->name = $name;
81 9
        $this->renamed = $renamed;
82 9
    }
83
84
    /**
85
     * @param string $x       X section of the status --porcelain output
86
     * @param string $y       Y section of the status --porcelain output
87
     * @param string $name    file name
88
     * @param string $renamed new file name (if renamed)
89
     *
90
     * @return StatusFile
91
     */
92 9
    public static function create(
93
        string $x,
94
        string $y,
95
        string $name,
96
        string $renamed = null
97
    ): \GitElephant\Status\StatusFile {
98 9
        return new self($x, $y, $name, $renamed);
99
    }
100
101
    /**
102
     * @return bool
103
     */
104 5
    public function isRenamed(): bool
105
    {
106 5
        return $this->renamed !== null;
107
    }
108
109
    /**
110
     * Get the file name
111
     *
112
     * @return string
113
     */
114 1
    public function getName(): string
115
    {
116 1
        return $this->name;
117
    }
118
119
    /**
120
     * Get the status of the index
121
     *
122
     * @return string
123
     */
124 5
    public function getIndexStatus(): ?string
125
    {
126 5
        return $this->x;
127
    }
128
129
    /**
130
     * Get the status of the working tree
131
     *
132
     * @return string|null
133
     */
134 6
    public function getWorkingTreeStatus(): ?string
135
    {
136 6
        return $this->y;
137
    }
138
139
    /**
140
     * description of the status
141
     *
142
     * @return void
143
     */
144 1
    public function calculateDescription(): void
145
    {
146 1
        $status = $this->x . $this->y;
147
        $matching = [
148 1
            '/ [MD]/' => 'not updated',
149
            '/M[MD]/' => 'updated in index',
150
            '/A[MD]/' => 'added to index',
151
            '/D[M]/' => 'deleted from index',
152
            '/R[MD]/' => 'renamed in index',
153
            '/C[MD]/' => 'copied in index',
154
            '/[MARC] /' => 'index and work tree matches',
155
            '/[MARC]M/' => 'work tree changed since index',
156
            '/[MARC]D/' => 'deleted in work tree',
157
            '/DD/' => 'unmerged, both deleted',
158
            '/AU/' => 'unmerged, added by us',
159
            '/UD/' => 'unmerged, deleted by them',
160
            '/UA/' => 'unmerged, added by them',
161
            '/DU/' => 'unmerged, deleted by us',
162
            '/AA/' => 'unmerged, both added',
163
            '/UU/' => 'unmerged, both modified',
164
            '/\?\?/' => 'untracked',
165
            '/!!/' => 'ignored',
166
        ];
167 1
        $out = [];
168 1
        foreach ($matching as $pattern => $label) {
169 1
            if (preg_match($pattern, $status)) {
170 1
                $out[] = $label;
171
            }
172
        }
173
174 1
        $this->description = implode(', ', $out);
175 1
    }
176
177
    /**
178
     * Set Description
179
     *
180
     * @param string $description the description variable
181
     */
182 1
    public function setDescription(string $description): void
183
    {
184 1
        $this->description = $description;
185 1
    }
186
187
    /**
188
     * Get Description.
189
     * Note that in certain environments, git might
190
     * format the output differently, leading to the description
191
     * being an empty string. Use setDescription(string) to set it yourself.
192
     *
193
     * @see #calulcateDescription()
194
     * @see #setDescription($description)
195
     * @return string
196
     */
197 2
    public function getDescription(): string
198
    {
199 2
        if ($this->description === null) {
200 1
            $this->calculateDescription();
201
        }
202
203 2
        return $this->description;
204
    }
205
206
    /**
207
     * Set Type
208
     *
209
     * @param string $type the type variable
210
     */
211 1
    public function setType(string $type): void
212
    {
213 1
        $this->type = $type;
214 1
    }
215
216
    /**
217
     * Get the Type of status/change.
218
     * Please note that this type might not be set by default.
219
     *
220
     * @return string
221
     */
222 1
    public function getType(): ?string
223
    {
224 1
        return $this->type;
225
    }
226
}
227