Conditions | 15 |
Paths | 154 |
Total Lines | 105 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 11 | ||
Bugs | 5 | Features | 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 |
||
51 | protected function execute(InputInterface $input, OutputInterface $output) |
||
52 | { |
||
53 | $hieranchy = [self::DEVELOP, self::STAGING, self::QUALITY, self::MASTER]; |
||
54 | $validBranchs = [self::STAGING, self::QUALITY, self::MASTER]; |
||
55 | $nameOfProject = $input->getArgument('project'); |
||
56 | $branchOfProject = $input->getArgument('branch'); |
||
57 | $helper = $this->getHelper('question'); |
||
58 | |||
59 | $nameOfProject = $this->getNameOfProject($input, $output, $helper, $nameOfProject); |
||
60 | |||
61 | if (!$branchOfProject) { |
||
62 | $question = new Question('<question>What is the branch to deploy?</question>: '); |
||
63 | do { |
||
64 | $branchOfProject = trim($helper->ask($input, $output, $question)); |
||
65 | } while (empty($branchOfProject) || !in_array($branchOfProject, $validBranchs)); |
||
66 | } |
||
67 | |||
68 | $jsonDb = $this->databaseService->getProjects(); |
||
69 | |||
70 | if (is_null($jsonDb)) { |
||
71 | $output->writeln(''); |
||
72 | $output->writeln('<info>0 projects configurated</info>'); |
||
73 | } else { |
||
74 | if (isset($jsonDb[$nameOfProject])) { |
||
75 | $merge = self::DEVELOP; |
||
76 | $final = self::STAGING; |
||
77 | foreach ($hieranchy as $k => $v) { |
||
78 | if ($v == $branchOfProject) { |
||
79 | $merge = $hieranchy[$k-1]; |
||
80 | $final = $v; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | chdir($jsonDb[$nameOfProject]); |
||
85 | |||
86 | $pipetask = [ |
||
87 | 'git checkout ' .$merge, |
||
88 | 'git pull', |
||
89 | 'git checkout ' .$final, |
||
90 | 'git merge '.$merge, |
||
91 | 'git push --progress 2>&1', |
||
92 | 'git checkout develop', |
||
93 | ]; |
||
94 | |||
95 | $progressBar = new ProgressBar($output, count($pipetask)); |
||
96 | $progressBar->setBarCharacter('<fg=magenta>=</>'); |
||
97 | $progressBar->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%"); |
||
98 | $progressBar->setBarWidth(50); |
||
99 | |||
100 | $table = new Table($output); |
||
101 | $table->setHeaders(array('<fg=white>Command</>', '<fg=white>Result</>')); |
||
102 | |||
103 | $exitCode = 0; |
||
104 | $exitCodeMessage = ""; |
||
105 | foreach ($pipetask as $t){ |
||
106 | if ($exitCode == 0){ |
||
107 | $command = new \mikehaertl\shellcommand\Command($t); |
||
108 | if ($command->execute()) { |
||
109 | $message = $command->getOutput(); |
||
110 | $message = trim(preg_replace('/\t+/', '', $message)); |
||
111 | $message = trim(preg_replace('/Ma\n+/', '', $message)); |
||
112 | $message = trim(preg_replace('/a\n+/', '', $message)); |
||
113 | $message = trim(preg_replace('/|/', '', $message)); |
||
114 | $exitCode = $command->getExitCode(); |
||
115 | } else { |
||
116 | $message = $command->getError(); |
||
117 | $message = trim(preg_replace('/\t+/', '', $message)); |
||
118 | $message = trim(preg_replace('/Ma\n+/', '', $message)); |
||
119 | $message = trim(preg_replace('/a\n+/', '', $message)); |
||
120 | $message = trim(preg_replace('/|/', '', $message)); |
||
121 | $exitCode = $command->getExitCode(); |
||
122 | } |
||
123 | } else { |
||
124 | $message = ''; |
||
125 | $exitCode = -1; |
||
126 | } |
||
127 | |||
128 | if ($exitCode == 0){ |
||
129 | $exitCodeMessage = '<fg=green>SUCCESS</>'; |
||
130 | } elseif ($exitCode == 1){ |
||
131 | $exitCodeMessage = '<fg=red>FAILED</>'; |
||
132 | } elseif ($exitCode == -1){ |
||
133 | $exitCodeMessage = '<fg=blue>ABORTED</>'; |
||
134 | } |
||
135 | |||
136 | $command = '<fg=magenta>'.$t.'</>'; |
||
137 | if (strlen(trim($message))>0){ |
||
138 | $command .= "\n".$message; |
||
139 | } |
||
140 | $table->addRow([$command, $exitCodeMessage]); |
||
141 | usleep(300000); |
||
142 | $progressBar->setMessage($t); |
||
143 | $progressBar->advance(); |
||
144 | } |
||
145 | $progressBar->setMessage(""); |
||
146 | |||
147 | $progressBar->finish(); |
||
148 | |||
149 | $output->writeln(''); |
||
150 | $table->render(); |
||
151 | } |
||
152 | |||
153 | } |
||
154 | |||
155 | } |
||
156 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: