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

SubCommandCommand::getCommandSubjects()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
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\Command;
21
22
use \GitElephant\Repository;
23
24
/**
25
 * SubCommandCommand
26
 *
27
 * A base class that can handle subcommand parameters ordering, which differs
28
 * for a general command
29
 *
30
 * @package GitElephant\Command
31
 * @author  David Neimeyer <[email protected]>
32
 */
33
class SubCommandCommand extends BaseCommand
34
{
35
36
    /**
37
     * Subjects to a subcommand name
38
     */
39
    private $orderedSubjects = array();
40
41
    /**
42
     * constructor
43
     *
44
     * @param \GitElephant\Repository $repo The repository object this command 
45
     *                                      will interact with
46
     */
47 11
    public function __construct(Repository $repo = null)
48
    {
49 11
        parent::__construct($repo);
50 11
    }
51
52
    /**
53
     * Clear all previous variables
54
     */
55
    public function clearAll()
56
    {
57
        parent::clearAll();
58
        $this->orderedSubjects = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $orderedSubjects.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
    }
60
61 10
    protected function addCommandSubject($subject)
62
    {
63 10
        $this->orderedSubjects[] = $subject;
64 10
    }
65
66 11
    protected function getCommandSubjects()
67
    {
68 11
        return ($this->orderedSubjects) ? $this->orderedSubjects : array();
69
    }
70
71 3
    protected function extractArguments($args)
72
    {
73 3
        $orderArgs = array();
74 3
        foreach ($args as $arg) {
75 3
            if (is_array($arg)) {
76 2
                foreach ($arg as $value) {
77 2
                    if (!is_null($value)) {
78 2
                        $orderArgs[] = escapeshellarg($value);
79 2
                    }
80 2
                }
81 2
            } else {
82 3
                $orderArgs[] = escapeshellarg($arg);
83
            }
84 3
        }
85
86 3
        return implode(' ', $orderArgs);
87
    }
88
89
    /**
90
     * Get the sub command
91
     *
92
     * @return string
93
     * @throws \RuntimeException
94
     */
95 11
    public function getCommand()
96
    {
97 11
        $command = $this->getCommandName();
98
99 11
        if (is_null($command)) {
100
            throw new \RuntimeException("commandName must be specified to build a subcommand");
101
        }
102
103 11
        $command .= ' ';
104 11
        $args = $this->getCommandArguments();
105 11
        if (count($args) > 0) {
106 3
            $command .= $this->extractArguments($args);
107 3
            $command .= ' ';
108 3
        }
109 11
        $subjects = $this->getCommandSubjects();
110 11
        if (count($subjects) > 0) {
111 10
            $command .= implode(' ', array_map('escapeshellarg', $subjects));
112 10
        }
113 11
        $command = preg_replace('/\\s{2,}/', ' ', $command);
114
115 11
        return trim($command);
116
    }
117
}
118