| Conditions | 10 |
| Paths | 192 |
| Total Lines | 82 |
| Code Lines | 56 |
| Lines | 20 |
| Ratio | 24.39 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 86 | protected function buildCommitSelector($project) { |
||
| 87 | // Branches |
||
| 88 | $branches = []; |
||
| 89 | View Code Duplication | foreach ($project->DNBranchList() as $branch) { |
|
| 90 | $sha = $branch->SHA(); |
||
| 91 | $name = $branch->Name(); |
||
| 92 | $branchValue = sprintf("%s (%s, %s old)", |
||
| 93 | $name, |
||
| 94 | substr($sha, 0, 8), |
||
| 95 | $branch->LastUpdated()->TimeDiff() |
||
| 96 | ); |
||
| 97 | $branches[$sha . '-' . $name] = $branchValue; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Tags |
||
| 101 | $tags = []; |
||
| 102 | View Code Duplication | foreach ($project->DNTagList()->setLimit(null) as $tag) { |
|
| 103 | $sha = $tag->SHA(); |
||
| 104 | $name = $tag->Name(); |
||
| 105 | $tagValue = sprintf("%s (%s, %s old)", |
||
| 106 | $name, |
||
| 107 | substr($sha, 0, 8), |
||
| 108 | $branch->LastUpdated()->TimeDiff() |
||
| 109 | ); |
||
| 110 | $tags[$sha . '-' . $name] = $tagValue; |
||
| 111 | } |
||
| 112 | $tags = array_reverse($tags); |
||
| 113 | |||
| 114 | // Past deployments |
||
| 115 | $redeploy = []; |
||
| 116 | foreach ($project->DNEnvironmentList() as $dnEnvironment) { |
||
| 117 | $envName = $dnEnvironment->Name; |
||
| 118 | foreach ($dnEnvironment->DeployHistory()->filter('State', \DNDeployment::STATE_COMPLETED) as $deploy) { |
||
| 119 | $sha = $deploy->SHA; |
||
| 120 | if (!isset($redeploy[$envName])) { |
||
| 121 | $redeploy[$envName] = []; |
||
| 122 | } |
||
| 123 | if (!isset($redeploy[$envName][$sha])) { |
||
| 124 | $pastValue = sprintf("%s (deployed %s)", |
||
| 125 | substr($sha, 0, 8), |
||
| 126 | $deploy->obj('LastEdited')->Ago() |
||
| 127 | ); |
||
| 128 | $redeploy[$envName][$sha] = $pastValue; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | // Merge fields |
||
| 134 | $releaseMethods = []; |
||
| 135 | if (!empty($branches)) { |
||
| 136 | $releaseMethods[] = new SelectionGroup_Item( |
||
| 137 | 'Branch', |
||
| 138 | new DropdownField('Branch', 'Select a branch', $branches), |
||
| 139 | 'Deploy the latest version of a branch' |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | if ($tags) { |
||
| 143 | $releaseMethods[] = new SelectionGroup_Item( |
||
| 144 | 'Tag', |
||
| 145 | new DropdownField('Tag', 'Select a tag', $tags), |
||
| 146 | 'Deploy a tagged release' |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | if ($redeploy) { |
||
| 150 | $releaseMethods[] = new SelectionGroup_Item( |
||
| 151 | 'Redeploy', |
||
| 152 | new GroupedDropdownField('Redeploy', 'Redeploy', $redeploy), |
||
| 153 | 'Redeploy a release that was previously deployed (to any environment)' |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | $releaseMethods[] = new SelectionGroup_Item( |
||
| 158 | 'SHA', |
||
| 159 | new Textfield('SHA', 'Please specify the full SHA'), |
||
| 160 | 'Deploy a specific SHA' |
||
| 161 | ); |
||
| 162 | |||
| 163 | $field = new TabbedSelectionGroup('SelectRelease', $releaseMethods); |
||
| 164 | $field->setValue(reset($releaseMethods)->getValue()); |
||
| 165 | $field->addExtraClass('clearfix'); |
||
| 166 | return $field; |
||
| 167 | } |
||
| 168 | } |
||
| 169 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.