Completed
Pull Request — develop (#169)
by Pol
02:20
created

MainCommand::status()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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\Command;
22
23
use GitElephant\Objects\Author;
24
use GitElephant\Objects\Branch;
25
use GitElephant\Objects\TreeishInterface;
26
use GitElephant\Repository;
27
28
/**
29
 * Main command generator (init, status, add, commit, checkout)
30
 *
31
 * @author Matteo Giachino <[email protected]>
32
 */
33
class MainCommand extends BaseCommand
34
{
35
    public const GIT_INIT = 'init';
36
    public const GIT_STATUS = 'status';
37
    public const GIT_ADD = 'add';
38
    public const GIT_COMMIT = 'commit';
39
    public const GIT_CHECKOUT = 'checkout';
40
    public const GIT_MOVE = 'mv';
41
    public const GIT_REMOVE = 'rm';
42
    public const GIT_RESET = 'reset';
43
44
    /**
45
     * constructor
46
     *
47
     * @param \GitElephant\Repository $repo The repository object this command
48
     *                                      will interact with
49
     */
50 105
    public function __construct(Repository $repo = null)
51
    {
52 105
        parent::__construct($repo);
53 105
    }
54
55
    /**
56
     * Init the repository
57
     *
58
     * @param bool $bare
59
     *
60
     * @throws \RuntimeException
61
     * @return string
62
     */
63 100
    public function init($bare = false): string
64
    {
65 100
        $this->clearAll();
66 100
        if ($bare) {
67 5
            $this->addCommandArgument('--bare');
68
        }
69 100
        $this->addCommandName(self::GIT_INIT);
70
71 100
        return $this->getCommand();
72
    }
73
74
    /**
75
     * Get the repository status
76
     *
77
     * @param bool $porcelain
78
     *
79
     * @throws \RuntimeException
80
     * @return string
81
     */
82 13
    public function status($porcelain = false): string
83
    {
84 13
        $this->clearAll();
85 13
        $this->addCommandName(self::GIT_STATUS);
86 13
        if ($porcelain) {
87 10
            $this->addCommandArgument('--porcelain');
88
        } else {
89 3
            $this->addConfigs(['color.status' => 'false']);
90
        }
91
92 13
        return $this->getCommand();
93
    }
94
95
    /**
96
     * Add a node to the stage
97
     *
98
     * @param string $what what should be added to the repository
99
     *
100
     * @throws \RuntimeException
101
     * @return string
102
     */
103 95
    public function add($what = '.'): string
104
    {
105 95
        $this->clearAll();
106 95
        $this->addCommandName(self::GIT_ADD);
107 95
        $this->addCommandArgument('--all');
108 95
        $this->addCommandSubject($what);
109
110 95
        return $this->getCommand();
111
    }
112
113
    /**
114
     * Remove a node from the stage and put in the working tree
115
     *
116
     * @param string $what what should be removed from the stage
117
     *
118
     * @throws \RuntimeException
119
     * @return string
120
     */
121 2
    public function unstage($what): string
122
    {
123 2
        $this->clearAll();
124 2
        $this->addCommandName(self::GIT_RESET);
125 2
        $this->addCommandArgument('HEAD');
126 2
        $this->addPath($what);
127
128 2
        return $this->getCommand();
129
    }
130
131
    /**
132
     * Commit
133
     *
134
     * @param string|null             $message the commit message
135
     * @param bool                    $stageAll commit all changes
136
     * @param string|Author           $author override the author for this commit
137
     * @param bool                    $allowEmpty whether to add param `--allow-empty` to commit command
138
     * @param \DateTimeInterface|null $date
139
     *
140
     * @return string
141
     */
142 95
    public function commit(?string $message, bool $stageAll = false, $author = null, bool $allowEmpty = false, \DateTimeInterface $date = null): string
143
    {
144 95
        $this->clearAll();
145
146 95
        if (trim($message) === '' || is_null($message)) {
147
            throw new \InvalidArgumentException(sprintf('You can\'t commit without message'));
148
        }
149 95
        $this->addCommandName(self::GIT_COMMIT);
150
151 95
        if ($stageAll) {
152 89
            $this->addCommandArgument('-a');
153
        }
154
155 95
        if ($author !== null) {
156 1
            $this->addCommandArgument('--author');
157 1
            $this->addCommandArgument($author);
0 ignored issues
show
Bug introduced by
It seems like $author defined by parameter $author on line 142 can also be of type object<GitElephant\Objects\Author>; however, GitElephant\Command\Base...d::addCommandArgument() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
158
        }
159
160 95
        if ($allowEmpty) {
161 1
            $this->addCommandArgument('--allow-empty');
162
        }
163
        
164 95
        if (null !== $date) {
165
            $this->addCommandArgument('--date');
166
            $this->addCommandArgument($date->format(\DateTimeInterface::RFC822));
167
        }
168
169 95
        $this->addCommandArgument('-m');
170 95
        $this->addCommandSubject($message);
171
172 95
        return $this->getCommand();
173
    }
174
175
    /**
176
     * Checkout a treeish reference
177
     *
178
     * @param string|Branch|TreeishInterface $ref the reference to checkout
179
     *
180
     * @throws \RuntimeException
181
     * @return string
182
     */
183 23
    public function checkout($ref): string
184
    {
185 23
        $this->clearAll();
186
187 23
        $what = $ref;
188 23
        if ($ref instanceof Branch) {
189 2
            $what = $ref->getName();
190 22
        } elseif ($ref instanceof TreeishInterface) {
191
            $what = $ref->getSha();
192
        }
193
194 23
        $this->addCommandName(self::GIT_CHECKOUT);
195 23
        $this->addCommandArgument('-q');
196 23
        $this->addCommandSubject($what);
197
198 23
        return $this->getCommand();
199
    }
200
201
    /**
202
     * Move a file/directory
203
     *
204
     * @param string|Object $from source path
205
     * @param string|Object $to   destination path
206
     *
207
     * @throws \RuntimeException
208
     * @throws \InvalidArgumentException
209
     * @return string
210
     */
211 1
    public function move($from, $to): string
212
    {
213 1
        $this->clearAll();
214
215 1
        $from = trim($from);
216 1
        if (!$this->validatePath($from)) {
217
            throw new \InvalidArgumentException('Invalid source path');
218
        }
219
220 1
        $to = trim($to);
221 1
        if (!$this->validatePath($to)) {
222
            throw new \InvalidArgumentException('Invalid destination path');
223
        }
224
225 1
        $this->addCommandName(self::GIT_MOVE);
226 1
        $this->addCommandSubject($from);
227 1
        $this->addCommandSubject2($to);
228
229 1
        return $this->getCommand();
230
    }
231
232
    /**
233
     * Remove a file/directory
234
     *
235
     * @param string|Object $path      the path to remove
236
     * @param bool          $recursive recurse
237
     * @param bool          $force     force
238
     *
239
     * @throws \RuntimeException
240
     * @throws \InvalidArgumentException
241
     * @return string
242
     */
243 1
    public function remove($path, $recursive, $force): string
244
    {
245 1
        $this->clearAll();
246
247 1
        $path = trim($path);
248 1
        if (!$this->validatePath($path)) {
249
            throw new \InvalidArgumentException('Invalid path');
250
        }
251
252 1
        $this->addCommandName(self::GIT_REMOVE);
253
254 1
        if ($recursive) {
255
            $this->addCommandArgument('-r');
256
        }
257
258 1
        if ($force) {
259
            $this->addCommandArgument('-f');
260
        }
261
262 1
        $this->addPath($path);
263
264 1
        return $this->getCommand();
265
    }
266
267
    /**
268
     * Validates a path
269
     *
270
     * @param string $path path
271
     *
272
     * @return bool
273
     */
274 2
    protected function validatePath($path): bool
275
    {
276 2
        if (empty($path)) {
277
            return false;
278
        }
279
280
        // we are always operating from root directory
281
        // so forbid relative paths
282 2
        if (false !== strpos($path, '..')) {
283
            return false;
284
        }
285
286 2
        return true;
287
    }
288
}
289