Completed
Pull Request — master (#576)
by Sean
03:37
created

DeployForm::buildCommitSelector()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 81
Code Lines 55

Duplication

Lines 20
Ratio 24.69 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 81
rs 5.2054
cc 10
eloc 55
nc 192
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Validates a multi-source commit selector
5
 *
6
 * @package deploynaut
7
 * @subpackage control
8
 */
9
class DeployForm_CommitValidator extends DeployForm_ValidatorBase {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
10
11
	public function php($data) {
12
		// Check release method
13
		if(empty($data['SelectRelease'])
14
			|| !in_array($data['SelectRelease'], array('Tag', 'Branch', 'Redeploy', 'SHA', 'FilteredCommits'))
15
		) {
16
			$method = empty($data['SelectRelease']) ? '(blank)' : $data['SelectRelease'];
17
			$this->validationError(
18
				'SelectRelease',
19
				"Bad release selection method: $method",
20
				"error"
21
			);
22
			return false;
23
		}
24
25
		// Check sha
26
		return $this->validateCommit(
27
			$this->form->getSelectedBuild($data),
28
			'SelectRelease'
29
		);
30
	}
31
32
}
33
34
/**
35
 * Form for generating deployments from a specified commit
36
 *
37
 * @package deploynaut
38
 * @subpackage control
39
 */
40
class DeployForm extends Form {
41
42
	/**
43
	 * @param DNRoot $controller
44
	 * @param string $name
45
	 * @param DNEnvironment $environment
46
	 * @param DNProject $project
47
	 */
48
	public function __construct($controller, $name, DNEnvironment $environment, DNProject $project) {
49
		$field = $this->buildCommitSelector($project);
50
		$validator = new DeployForm_CommitValidator();
51
		$actions = new FieldList(
52
			new FormAction('showDeploySummary', 'Plan deployment', 'Show deployment plan'),
0 ignored issues
show
Documentation introduced by
'Show deployment plan' is of type string, but the function expects a object<Form>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
			new FormAction('doDeploy', 'Do deploy', 'Do deploy')
0 ignored issues
show
Documentation introduced by
'Do deploy' is of type string, but the function expects a object<Form>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
		);
55
56
		parent::__construct($controller, $name, new FieldList($field), $actions, $validator);
57
	}
58
59
	/**
60
	 * Construct fields to select any commit
61
	 *
62
	 * @param DNProject $project
63
	 * @return FormField
64
	 */
65
	protected function buildCommitSelector($project) {
66
		// Branches
67
		$branches = array();
68 View Code Duplication
		foreach($project->DNBranchList() as $branch) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
			$sha = $branch->SHA();
70
			$name = $branch->Name();
71
			$branchValue = sprintf("%s (%s, %s old)",
72
				$name,
73
				substr($sha, 0, 8),
74
				$branch->LastUpdated()->TimeDiff()
75
			);
76
			$branches[$sha . '-' . $name] = $branchValue;
77
		}
78
79
		// Tags
80
		$tags = array();
81 View Code Duplication
		foreach($project->DNTagList()->setLimit(null) as $tag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
			$sha = $tag->SHA();
83
			$name = $tag->Name();
84
			$tagValue = sprintf("%s (%s, %s old)",
85
				$name,
86
				substr($sha, 0, 8),
87
				$branch->LastUpdated()->TimeDiff()
0 ignored issues
show
Bug introduced by
The variable $branch seems to be defined by a foreach iteration on line 68. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
88
			);
89
			$tags[$sha . '-' . $tag] = $tagValue;
90
		}
91
		$tags = array_reverse($tags);
92
93
		// Past deployments
94
		$redeploy = array();
95
		foreach($project->DNEnvironmentList() as $dnEnvironment) {
96
			$envName = $dnEnvironment->Name;
97
			foreach($dnEnvironment->DeployHistory() as $deploy) {
98
				$sha = $deploy->SHA;
99
				if(!isset($redeploy[$envName])) {
100
					$redeploy[$envName] = array();
101
				}
102
				if(!isset($redeploy[$envName][$sha])) {
103
					$pastValue = sprintf("%s (deployed %s)",
104
						substr($sha, 0, 8),
105
						$deploy->obj('LastEdited')->Ago()
106
					);
107
					$redeploy[$envName][$sha] = $pastValue;
108
				}
109
			}
110
		}
111
112
		// Merge fields
113
		$releaseMethods = array();
114
		if(!empty($branches)) {
115
			$releaseMethods[] = new SelectionGroup_Item(
116
				'Branch',
117
				new DropdownField('Branch', 'Select a branch', $branches),
118
				'Deploy the latest version of a branch'
119
			);
120
		}
121
		if($tags) {
122
			$releaseMethods[] = new SelectionGroup_Item(
123
				'Tag',
124
				new DropdownField('Tag', 'Select a tag', $tags),
125
				'Deploy a tagged release'
126
			);
127
		}
128
		if($redeploy) {
129
			$releaseMethods[] = new SelectionGroup_Item(
130
				'Redeploy',
131
				new GroupedDropdownField('Redeploy', 'Redeploy', $redeploy),
132
				'Redeploy a release that was previously deployed (to any environment)'
133
			);
134
		}
135
136
		$releaseMethods[] = new SelectionGroup_Item(
137
			'SHA',
138
			new Textfield('SHA', 'Please specify the full SHA'),
139
			'Deploy a specific SHA'
140
		);
141
142
		$field = new TabbedSelectionGroup('SelectRelease', $releaseMethods);
143
		$field->setValue(reset($releaseMethods)->getValue());
144
		return $field;
145
	}
146
147
	/**
148
	 * Get the build selected from the given data
149
	 *
150
	 * @param array $data
151
	 * @return string SHA of selected build
152
	 */
153
	public function getSelectedBuild($data) {
154
		if(isset($data['SelectRelease']) && !empty($data[$data['SelectRelease']])) {
155
			// Filter out the tag/branch name if required
156
			$array = explode('-', $data[$data['SelectRelease']]);
157
			return reset($array);
158
		}
159
		if(isset($data['FilteredCommits']) && !empty($data['FilteredCommits'])) {
160
			return $data['FilteredCommits'];
161
		}
162
	}
163
}
164