LsTreeCommand::fullTree()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.7
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\Branch;
24
use GitElephant\Objects\NodeObject;
25
use GitElephant\Objects\TreeishInterface;
26
use GitElephant\Repository;
27
28
/**
29
 * ls-tree command generator
30
 *
31
 * @author Matteo Giachino <[email protected]>
32
 */
33
class LsTreeCommand extends BaseCommand
34
{
35
    public const LS_TREE_COMMAND = 'ls-tree';
36
37
    /**
38
     * constructor
39
     *
40
     * @param \GitElephant\Repository $repo The repository object this command
41
     *                                      will interact with
42
     */
43 15
    public function __construct(Repository $repo = null)
44
    {
45 15
        parent::__construct($repo);
46 15
    }
47
48
    /**
49
     * build a ls-tree command
50
     *
51
     * @param string|Branch $ref The reference to build the tree from
52
     *
53
     * @throws \RuntimeException
54
     * @return string
55
     */
56 1
    public function fullTree($ref = 'HEAD'): string
57
    {
58 1
        $what = $ref;
59 1
        if ($ref instanceof TreeishInterface) {
60 1
            $what = $ref->getSha();
61
        }
62 1
        $this->clearAll();
63 1
        $this->addCommandName(self::LS_TREE_COMMAND);
64
        // recurse
65 1
        $this->addCommandArgument('-r');
66
        // show trees
67 1
        $this->addCommandArgument('-t');
68 1
        $this->addCommandArgument('-l');
69 1
        $this->addCommandSubject($what);
0 ignored issues
show
Bug introduced by
It seems like $what defined by $ref on line 58 can also be of type object<GitElephant\Objects\Branch>; however, GitElephant\Command\Base...nd::addCommandSubject() does only seem to accept object<GitElephant\Comma...ndCommand>|array|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
70
71 1
        return $this->getCommand();
72
    }
73
74
    /**
75
     * tree of a given path
76
     *
77
     * @param string|TreeishInterface        $ref  reference
78
     * @param string|NodeObject $path path
79
     *
80
     * @throws \RuntimeException
81
     * @return string
82
     */
83 15
    public function tree($ref = 'HEAD', $path = null): string
84
    {
85 15
        if ($path instanceof NodeObject) {
86 9
            $subjectPath = $path->getFullPath() . ($path->isTree() ? '/' : '');
87
        } else {
88 15
            $subjectPath = $path;
89
        }
90
91 15
        $what = $ref;
92 15
        if ($ref instanceof TreeishInterface) {
93 1
            $what = $ref->getSha();
94
        }
95 15
        $subject = $what;
96
97 15
        $this->clearAll();
98
99 15
        $this->addCommandName(self::LS_TREE_COMMAND);
100 15
        $this->addCommandArgument('-l');
101 15
        $this->addCommandSubject($subject);
102 15
        $this->addPath($subjectPath);
103
104 15
        return $this->getCommand();
105
    }
106
107
    /**
108
     * build ls-tree command that list all
109
     *
110
     * @param null|string $ref the reference to build the tree from
111
     *
112
     * @throws \RuntimeException
113
     * @return string
114
     */
115 1
    public function listAll($ref = null): string
116
    {
117 1
        if (is_null($ref)) {
118 1
            $ref = 'HEAD';
119
        }
120 1
        $this->clearAll();
121 1
        $this->addCommandName(self::LS_TREE_COMMAND);
122 1
        $this->addCommandSubject($ref);
123
124 1
        return $this->getCommand();
125
    }
126
}
127