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)); |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.