Conditions | 10 |
Paths | 17 |
Total Lines | 62 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Tests | 21 |
CRAP Score | 14.8069 |
Changes | 6 | ||
Bugs | 1 | 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 |
||
130 | 1 | private function processGeneratorContainer(GeneratorContainer $gc, Deferred $deferred = null) |
|
131 | 1 | { |
|
132 | // If generator has no more yields... |
||
133 | 1 | if (!$gc->valid()) { |
|
134 | // If exception has been thrown in generator, we have to propagate it as rejected value |
||
135 | 1 | if ($gc->thrown()) { |
|
136 | $deferred->reject($gc->getReturnOrThrown()); |
||
137 | return; |
||
138 | } |
||
139 | // Now we normalize returned value |
||
140 | try { |
||
141 | 1 | $returned = Utils::normalize($gc->getReturnOrThrown(), $gc->getOptions()); |
|
142 | 1 | $yieldables = Utils::getYieldables($returned); |
|
143 | // If normalized value contains yieldables, we have to chain resolver |
||
144 | 1 | if ($yieldables) { |
|
1 ignored issue
–
show
|
|||
145 | $this->promiseAll($yieldables, true)->then( |
||
146 | self::getApplier($returned, $yieldables, [$deferred, 'resolve']), |
||
147 | [$deferred, 'reject'] |
||
148 | ); |
||
149 | return; |
||
150 | } |
||
151 | // Propagate normalized returned value |
||
152 | 1 | $deferred && $deferred->resolve($returned); |
|
153 | } catch (\RuntimeException $e) { |
||
154 | // Propagate exception thrown in normalization |
||
155 | $deferred && $deferred->reject($e); |
||
156 | } |
||
157 | 1 | return; |
|
158 | } |
||
159 | |||
160 | // Now we normalize yielded value |
||
161 | try { |
||
162 | 1 | $yielded = Utils::normalize($gc->current(), $gc->getOptions(), $gc->key()); |
|
163 | } catch (\RuntimeException $e) { |
||
164 | // If exception thrown in normalization... |
||
165 | // - If generator accepts exception, we throw it into generator |
||
166 | // - If generator does not accept exception, we assume it as non-exception value |
||
167 | $gc->throwAcceptable() ? $gc->throw_($e) : $gc->send($e); |
||
168 | // Continue |
||
169 | $this->processGeneratorContainer($gc, $deferred); |
||
170 | return; |
||
171 | } |
||
172 | |||
173 | // Search yieldables from yielded value |
||
174 | 1 | $yieldables = Utils::getYieldables($yielded); |
|
175 | 1 | if (!$yieldables) { |
|
1 ignored issue
–
show
|
|||
176 | // If there are no yieldables, send yielded value back into generator |
||
177 | 1 | $gc->send($yielded); |
|
178 | // Continue |
||
179 | 1 | $this->processGeneratorContainer($gc, $deferred); |
|
180 | 1 | return; |
|
181 | } |
||
182 | |||
183 | // Chain resolver |
||
184 | 1 | $this->promiseAll($yieldables, $gc->throwAcceptable())->then( |
|
1 ignored issue
–
show
|
|||
185 | 1 | self::getApplier($yielded, $yieldables, [$gc, 'send']), |
|
186 | 1 | [$gc, 'throw_'] |
|
187 | )->always(function () use ($gc, $deferred) { |
||
188 | // Continue |
||
189 | 1 | $this->processGeneratorContainer($gc, $deferred); |
|
190 | 1 | }); |
|
191 | 1 | } |
|
192 | |||
249 |
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: