Conditions | 6 |
Paths | 11 |
Total Lines | 96 |
Lines | 34 |
Ratio | 35.42 % |
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 declare(strict_types=1); |
||
91 | private function executeCommand(Command $command, int $index, int $totalCount) |
||
92 | { |
||
93 | switch ($command) { |
||
94 | case $command instanceof BashCommand: |
||
95 | $originalContent = file_get_contents($command->getScript()->getPath()); |
||
|
|||
96 | |||
97 | try { |
||
98 | file_put_contents($command->getScript()->getPath(), $this->templateEngine->render($originalContent, $this->environment->getAllValues())); |
||
99 | |||
100 | $process = $this->environment->createProcess($command->getScript()->getPath()); |
||
101 | $this->setProcessDefaults($process, false); |
||
102 | |||
103 | $this->logger->logStart( |
||
104 | 'Executing', |
||
105 | $command->getScript()->getPath(), |
||
106 | $command->getLineNumber(), |
||
107 | false, |
||
108 | $index, |
||
109 | $totalCount |
||
110 | ); |
||
111 | |||
112 | $this->runProcess($process); |
||
113 | $this->testProcessResultValid($process, false); |
||
114 | } finally { |
||
115 | file_put_contents($command->getScript()->getPath(), $originalContent); |
||
116 | } |
||
117 | |||
118 | |||
119 | break; |
||
120 | View Code Duplication | case $command instanceof SynchronusProcessCommand: |
|
121 | $parsedCommand = $this->getParsedShellCommand($command); |
||
122 | $process = $this->environment->createProcess($parsedCommand); |
||
123 | $this->setProcessDefaults($process, $command->isTTy()); |
||
124 | |||
125 | $this->logger->logStart( |
||
126 | 'Starting', |
||
127 | $parsedCommand, |
||
128 | $command->getLineNumber(), |
||
129 | $command->isIgnoreError(), |
||
130 | $index, |
||
131 | $totalCount |
||
132 | ); |
||
133 | |||
134 | $this->runProcess($process); |
||
135 | $this->testProcessResultValid($process, $command->isIgnoreError()); |
||
136 | |||
137 | break; |
||
138 | |||
139 | |||
140 | View Code Duplication | case $command instanceof DeferredProcessCommand: |
|
141 | $parsedCommand = $this->getParsedShellCommand($command); |
||
142 | $process = $this->environment->createProcess($parsedCommand); |
||
143 | $this->setProcessDefaults($process, $command->isTTy()); |
||
144 | |||
145 | $this->logger->logStart( |
||
146 | 'Defering', |
||
147 | $parsedCommand, |
||
148 | $command->getLineNumber(), |
||
149 | $command->isIgnoreError(), |
||
150 | $index, |
||
151 | $totalCount |
||
152 | ); |
||
153 | $this->deferProcess($parsedCommand, $command, $process); |
||
154 | |||
155 | break; |
||
156 | |||
157 | |||
158 | case $command instanceof TemplateCommand: |
||
159 | $template = $command->createTemplate(); |
||
160 | |||
161 | $this->logger->logStart( |
||
162 | 'Template', |
||
163 | $template->getDestination(), |
||
164 | $command->getLineNumber(), |
||
165 | false, |
||
166 | $index, |
||
167 | $totalCount |
||
168 | ); |
||
169 | |||
170 | $this->renderTemplate($template); |
||
171 | break; |
||
172 | |||
173 | case $command instanceof WaitCommand: |
||
174 | $this->logger->logStart( |
||
175 | 'Waiting', |
||
176 | '', |
||
177 | $command->getLineNumber(), |
||
178 | false, |
||
179 | $index, |
||
180 | $totalCount |
||
181 | ); |
||
182 | |||
183 | $this->waitForDeferredProcesses(); |
||
184 | break; |
||
185 | } |
||
186 | } |
||
187 | |||
323 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: