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

SubCommandCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.88%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 88
ccs 29
cts 33
cp 0.8788
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A clearAll() 0 5 1
A addCommandSubject() 0 4 1
A getCommandSubjects() 0 4 1
A extractArguments() 0 17 5
A getCommand() 0 18 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\Repository;
24
25
/**
26
 * SubCommandCommand
27
 *
28
 * A base class that can handle subcommand parameters ordering, which differs
29
 * for a general command
30
 *
31
 * @package GitElephant\Command
32
 * @author  David Neimeyer <[email protected]>
33
 */
34
class SubCommandCommand extends BaseCommand
35
{
36
    /**
37
     * Subjects to a subcommand name
38
     *
39
     * @var array<SubCommandCommand|array|string>
40
     */
41
    private $orderedSubjects = [];
42
43
    /**
44
     * constructor
45
     *
46
     * @param \GitElephant\Repository $repo The repository object this command
47
     *                                      will interact with
48
     */
49 11
    public function __construct(Repository $repo = null)
50
    {
51 11
        parent::__construct($repo);
52 11
    }
53
54
    /**
55
     * Clear all previous variables
56
     */
57
    public function clearAll(): void
58
    {
59
        parent::clearAll();
60
        $this->orderedSubjects = [];
61
    }
62
63
    /**
64
     * Add a subject to this subcommand
65
     *
66
     * @param SubCommandCommand|array|string $subject
67
     * @return void
68
     */
69 10
    protected function addCommandSubject($subject): void
70
    {
71 10
        $this->orderedSubjects[] = $subject;
72 10
    }
73
74 11
    protected function getCommandSubjects(): array
75
    {
76 11
        return $this->orderedSubjects;
77
    }
78
79 3
    protected function extractArguments(array $args): string
80
    {
81 3
        $orderArgs = [];
82 3
        foreach ($args as $arg) {
83 3
            if (is_array($arg)) {
84 2
                foreach ($arg as $value) {
85 2
                    if (!is_null($value)) {
86 2
                        $orderArgs[] = escapeshellarg($value);
87
                    }
88
                }
89
            } else {
90 3
                $orderArgs[] = escapeshellarg($arg);
91
            }
92
        }
93
94 3
        return implode(' ', $orderArgs);
95
    }
96
97
    /**
98
     * Get the sub command
99
     *
100
     * @return string
101
     * @throws \RuntimeException
102
     */
103 11
    public function getCommand(): string
104
    {
105 11
        $command = $this->getCommandName();
106
107 11
        $command .= ' ';
108 11
        $args = $this->getCommandArguments();
109 11
        if (count($args) > 0) {
110 3
            $command .= $this->extractArguments($args);
111 3
            $command .= ' ';
112
        }
113 11
        $subjects = $this->getCommandSubjects();
114 11
        if (!empty($subjects)) {
115 10
            $command .= implode(' ', array_map('escapeshellarg', $subjects));
116
        }
117 11
        $command = preg_replace('/\\s{2,}/', ' ', $command);
118
119 11
        return trim($command);
120
    }
121
}
122