DNBranchList::get_refs_dir()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class DNBranchList
5
 */
6
class DNBranchList extends ArrayList {
0 ignored issues
show
Coding Style introduced by
The property $refs_dir is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
7
8
	/**
9
	 * @var string
10
	 */
11
	protected static $refs_dir = '';
12
13
	/**
14
	 * @var bool
15
	 */
16
	protected $loaded = false;
17
18
	/**
19
	 * @var array
20
	 */
21
	protected $builds = array();
22
23
	/**
24
	 * @var DNProject
25
	 */
26
	protected $project;
27
28
	/**
29
	 * @var DNData
30
	 */
31
	protected $data;
32
33
	public static function set_refs_dir($refsDir) {
34
		self::$refs_dir = $refsDir;
35
	}
36
37
	public static function get_refs_dir() {
38
		return self::$refs_dir;
39
	}
40
41
	/**
42
	 * @param \DNProject $project
43
	 * @param \DNData $data
44
	 */
45
	public function __construct(\DNProject $project, \DNData $data) {
46
		$this->project = $project;
47
		$this->data = $data;
48
		parent::__construct(array());
49
	}
50
51
	/**
52
	 * @return array
53
	 */
54
	protected function getReferences() {
55
		$branches = array();
56
		// Placeholder to put master branch first
57
		$firstBranch = null;
58
59
		try {
60
			$repository = new Gitonomy\Git\Repository($this->project->getLocalCVSPath());
61
		} catch(Exception $e) {
62
			return $branches;
63
		}
64
65
		foreach($repository->getReferences()->getBranches() as $branch) {
66
			/** @var DNBranch $obj */
67
			$obj = DNBranch::create($branch, $this->project, $this->data);
68
			if($branch->getName() == 'master') {
69
				$firstBranch = array($branch->getName() => $obj);
70
			} else {
71
				$branches[$branch->getName()] = $obj;
72
			}
73
		}
74
		if($firstBranch) {
75
			$branches = $firstBranch + $branches;
76
		}
77
78
		return $branches;
79
	}
80
81
	/**
82
	 * Find a branch in this set by branch name.
83
	 *
84
	 * @param $name
85
	 *
86
	 * @return string
87
	 */
88
	public function byName($name) {
89
		if($this->loaded === false) {
90
			$this->getIterator();
91
		}
92
93
		if(isset($this->items[$name])) {
94
			return $this->items[$name];
95
		}
96
97
		return '';
98
	}
99
100
	/**
101
	 * Returns an Iterator for this ArrayList.
102
	 * This function allows you to use ArrayList in foreach loops
103
	 *
104
	 * @return ArrayIterator
105
	 */
106 View Code Duplication
	public function getIterator() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
		if($this->loaded === false) {
108
			$this->items = $this->getReferences();
109
			$this->loaded = true;
110
		}
111
		foreach($this->items as $i => $item) {
112
			if(is_array($item)) {
113
				$this->items[$i] = new ArrayData($item);
114
			}
115
		}
116
		return new ArrayIterator($this->items);
117
	}
118
}
119