Completed
Push — master ( 7cb780...532294 )
by
unknown
04:14 queued 02:10
created

StatusFile::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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