Completed
Pull Request — master (#112)
by Pieter
04:24 queued 01:13
created

GitBranches   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 8
c 6
b 3
f 1
lcom 1
cbo 1
dl 0
loc 94
ccs 14
cts 21
cp 0.6667
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetchBranches() 0 8 2
A trimBranch() 0 4 1
A remote() 0 4 1
A head() 0 4 1
A getIterator() 0 5 1
A all() 0 4 1
1
<?php
2
3
/**
4
 * A PHP wrapper around the Git command line utility.
5
 */
6
7
namespace GitWrapper;
8
9
/**
10
 * Class that parses and returnes an array of branches.
11
 */
12
class GitBranches implements \IteratorAggregate
13
{
14
    /**
15
     * The working copy that branches are being collected from.
16
     *
17
     * @var \GitWrapper\GitWorkingCopy
18
     */
19
    protected $git;
20
21
    /**
22
     * Constructs a GitBranches object.
23
     *
24
     * @param \GitWrapper\GitWorkingCopy $git
25
     *   The working copy that branches are being collected from.
26
     *
27
     * @throws \GitWrapper\GitException
28
     */
29 24
    public function __construct(GitWorkingCopy $git)
30
    {
31 24
        $this->git = clone $git;
32 24
        $output = (string) $git->branch(array('a' => true));
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33 24
    }
34
35
    /**
36
     * Fetches the branches via the `git branch` command.
37
     *
38
     * @param boolean $onlyRemote
39
     *   Whether to fetch only remote branches, defaults to false which returns
40
     *   all branches.
41
     *
42
     * @return array
43
     */
44 24
    public function fetchBranches($onlyRemote = false)
45
    {
46 24
        $this->git->clearOutput();
47 24
        $options = ($onlyRemote) ? array('r' => true) : array('a' => true);
48 24
        $output = (string) $this->git->branch($options);
49 24
        $branches = preg_split("/\r\n|\n|\r/", rtrim($output));
50 24
        return array_map(array($this, 'trimBranch'), $branches);
51
    }
52
53
    /**
54
     * Strips unwanted characters from the branch.
55
     *
56
     * @param string $branch
57
     *   The raw branch returned in the output of the Git command.
58
     *
59
     * @return string
60
     *   The processed branch name.
61
     */
62 24
    public function trimBranch($branch)
63
    {
64 24
        return ltrim($branch, ' *');
65
    }
66
67
    /**
68
     * Implements \IteratorAggregate::getIterator().
69
     */
70
    public function getIterator()
71
    {
72
        $branches = $this->all();
73
        return new \ArrayIterator($branches);
74
    }
75
76
    /**
77
     * Returns all branches.
78
     *
79
     * @return array
80
     */
81
    public function all()
82
    {
83
        return $this->fetchBranches();
84
    }
85
86
    /**
87
     * Returns only remote branches.
88
     *
89
     * @return array
90
     */
91 24
    public function remote()
92
    {
93 24
        return $this->fetchBranches(true);
94
    }
95
96
    /**
97
     * Returns currently active branch (HEAD) of the working copy.
98
     *
99
     * @return string
100
     */
101
    public function head()
102
    {
103
        return trim((string) $this->git->run(array('rev-parse --abbrev-ref HEAD')));
104
    }
105
}
106