Completed
Pull Request — develop (#169)
by Pol
02:01 queued 46s
created

MainCommand   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.36%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 3
dl 0
loc 258
ccs 76
cts 87
cp 0.8736
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 0 10 2
A status() 0 12 2
A add() 0 9 1
A unstage() 0 9 1
B commit() 0 32 7
A checkout() 0 17 3
A move() 0 20 3
A remove() 0 23 4
A validatePath() 0 14 3
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
     * @throws \RuntimeException
141
     * @throws \InvalidArgumentException
142
     * @return string
143
     */
144 95
    public function commit(?string $message, bool $stageAll = false, $author = null, bool $allowEmpty = false, \DateTimeInterface $date = null): string
145
    {
146 95
        $this->clearAll();
147
148 95
        if (trim($message) === '' || is_null($message)) {
149
            throw new \InvalidArgumentException(sprintf('You can\'t commit without message'));
150
        }
151 95
        $this->addCommandName(self::GIT_COMMIT);
152
153 95
        if ($stageAll) {
154 89
            $this->addCommandArgument('-a');
155
        }
156
157 95
        if ($author !== null) {
158 1
            $this->addCommandArgument('--author');
159 1
            $this->addCommandArgument($author);
0 ignored issues
show
Bug introduced by
It seems like $author defined by parameter $author on line 144 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...
160
        }
161
162 95
        if ($allowEmpty) {
163 1
            $this->addCommandArgument('--allow-empty');
164
        }
165
        
166 95
        if (null !== $date) {
167
            $this->addCommandArgument('--date');
168
            $this->addCommandArgument($date->format(\DateTimeInterface::RFC822));
169
        }
170
171 95
        $this->addCommandArgument('-m');
172 95
        $this->addCommandSubject($message);
173
174 95
        return $this->getCommand();
175
    }
176
177
    /**
178
     * Checkout a treeish reference
179
     *
180
     * @param string|Branch|TreeishInterface $ref the reference to checkout
181
     *
182
     * @throws \RuntimeException
183
     * @return string
184
     */
185 23
    public function checkout($ref): string
186
    {
187 23
        $this->clearAll();
188
189 23
        $what = $ref;
190 23
        if ($ref instanceof Branch) {
191 2
            $what = $ref->getName();
192 22
        } elseif ($ref instanceof TreeishInterface) {
193
            $what = $ref->getSha();
194
        }
195
196 23
        $this->addCommandName(self::GIT_CHECKOUT);
197 23
        $this->addCommandArgument('-q');
198 23
        $this->addCommandSubject($what);
199
200 23
        return $this->getCommand();
201
    }
202
203
    /**
204
     * Move a file/directory
205
     *
206
     * @param string|Object $from source path
207
     * @param string|Object $to   destination path
208
     *
209
     * @throws \RuntimeException
210
     * @throws \InvalidArgumentException
211
     * @return string
212
     */
213 1
    public function move($from, $to): string
214
    {
215 1
        $this->clearAll();
216
217 1
        $from = trim($from);
218 1
        if (!$this->validatePath($from)) {
219
            throw new \InvalidArgumentException('Invalid source path');
220
        }
221
222 1
        $to = trim($to);
223 1
        if (!$this->validatePath($to)) {
224
            throw new \InvalidArgumentException('Invalid destination path');
225
        }
226
227 1
        $this->addCommandName(self::GIT_MOVE);
228 1
        $this->addCommandSubject($from);
229 1
        $this->addCommandSubject2($to);
230
231 1
        return $this->getCommand();
232
    }
233
234
    /**
235
     * Remove a file/directory
236
     *
237
     * @param string|Object $path      the path to remove
238
     * @param bool          $recursive recurse
239
     * @param bool          $force     force
240
     *
241
     * @throws \RuntimeException
242
     * @throws \InvalidArgumentException
243
     * @return string
244
     */
245 1
    public function remove($path, $recursive, $force): string
246
    {
247 1
        $this->clearAll();
248
249 1
        $path = trim($path);
250 1
        if (!$this->validatePath($path)) {
251
            throw new \InvalidArgumentException('Invalid path');
252
        }
253
254 1
        $this->addCommandName(self::GIT_REMOVE);
255
256 1
        if ($recursive) {
257
            $this->addCommandArgument('-r');
258
        }
259
260 1
        if ($force) {
261
            $this->addCommandArgument('-f');
262
        }
263
264 1
        $this->addPath($path);
265
266 1
        return $this->getCommand();
267
    }
268
269
    /**
270
     * Validates a path
271
     *
272
     * @param string $path path
273
     *
274
     * @return bool
275
     */
276 2
    protected function validatePath($path): bool
277
    {
278 2
        if (empty($path)) {
279
            return false;
280
        }
281
282
        // we are always operating from root directory
283
        // so forbid relative paths
284 2
        if (false !== strpos($path, '..')) {
285
            return false;
286
        }
287
288 2
        return true;
289
    }
290
}
291