Completed
Push — develop ( 29df12...700ecb )
by Matteo
03:39
created

StatusFile::getIndexStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
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($x, $y, $name, $renamed)
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
        $this->calculateDescription();
82 8
    }
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 8
    public static function create($x, $y, $name, $renamed)
93
    {
94 8
        return new self($x, $y, $name, $renamed);
95
    }
96
97
    /**
98
     * @return bool
99
     */
100 5
    public function isRenamed()
101
    {
102 5
        return $this->renamed !== null;
103
    }
104
105
    /**
106
     * Get Name
107
     *
108
     * @return string
109
     */
110
    public function getName()
111
    {
112
        return $this->name;
113
    }
114
115
    /**
116
     * Get the status of the index
117
     *
118
     * @return string
119
     */
120 5
    public function getIndexStatus()
121
    {
122 5
        return $this->x;
123
    }
124
125
    /**
126
     * Get the status of the working tree
127
     *
128
     * @return string
129
     */
130 6
    public function getWorkingTreeStatus()
131
    {
132 6
        return $this->y;
133
    }
134
135
    /**
136
     * description of the status
137
     *
138
     * @return string
139
     */
140 8
    public function calculateDescription()
141
    {
142 8
        $status = $this->x.$this->y;
143
        $matching = array(
144 8
            '/ [MD]/' => 'not updated',
145 8
            '/M[MD]/' => 'updated in index',
146 8
            '/A[MD]/' => 'added to index',
147 8
            '/D[M]/' => 'deleted from index',
148 8
            '/R[MD]/' => 'renamed in index',
149 8
            '/C[MD]/' => 'copied in index',
150 8
            '/[MARC] /' => 'index and work tree matches',
151 8
            '/[MARC]M/' => 'work tree changed since index',
152 8
            '/[MARC]D/' => 'deleted in work tree',
153 8
            '/DD/' => 'unmerged, both deleted',
154 8
            '/AU/' => 'unmerged, added by us',
155 8
            '/UD/' => 'unmerged, deleted by them',
156 8
            '/UA/' => 'unmerged, added by them',
157 8
            '/DU/' => 'unmerged, deleted by us',
158 8
            '/AA/' => 'unmerged, both added',
159 8
            '/UU/' => 'unmerged, both modified',
160 8
            '/\?\?/' => 'untracked',
161 8
            '/!!/' => 'ignored',
162 8
        );
163 8
        $out = array();
164 8
        foreach ($matching as $pattern => $label) {
165 8
            if (preg_match($pattern, $status)) {
166 4
                $out[] = $label;
167 4
            }
168 8
        }
169
170 8
        $this->description = implode(', ', $out);
171 8
    }
172
173
    /**
174
     * Set Description
175
     *
176
     * @param string $description the description variable
177
     */
178
    public function setDescription($description)
179
    {
180
        $this->description = $description;
181
    }
182
183
    /**
184
     * Get Description
185
     *
186
     * @return string
187
     */
188 1
    public function getDescription()
189
    {
190 1
        return $this->description;
191
    }
192
193
    /**
194
     * Set Type
195
     *
196
     * @param string $type the type variable
197
     */
198
    public function setType($type)
199
    {
200
        $this->type = $type;
201
    }
202
203
    /**
204
     * Get Type
205
     *
206
     * @return string
207
     */
208
    public function getType()
209
    {
210
        return $this->type;
211
    }
212
}
213