Complex classes like Promise often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Promise, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Promise implements PromiseInterface |
||
| 10 | { |
||
| 11 | use PromiseTrait; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var PromiseInterface|null |
||
| 15 | */ |
||
| 16 | protected $result; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var callable[] |
||
| 20 | */ |
||
| 21 | protected $handlers; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var callable |
||
| 25 | */ |
||
| 26 | protected $canceller; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | protected $currentCancellations; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $requiredCancellations; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param callable|null $resolver |
||
| 40 | * @param callable|null $canceller |
||
| 41 | */ |
||
| 42 | 374 | public function __construct(callable $resolver = null, callable $canceller = null) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * |
||
| 65 | */ |
||
| 66 | 103 | public function __destruct() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @override |
||
| 77 | * @inheritDoc |
||
| 78 | */ |
||
| 79 | 222 | public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @override |
||
| 116 | * @inheritDoc |
||
| 117 | */ |
||
| 118 | 80 | public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @override |
||
| 133 | * @inheritDoc |
||
| 134 | */ |
||
| 135 | 18 | public function spread(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @override |
||
| 152 | * @inheritDoc |
||
| 153 | */ |
||
| 154 | 8 | public function success(callable $onSuccess) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @override |
||
| 161 | * @inheritDoc |
||
| 162 | */ |
||
| 163 | 10 | public function failure(callable $onFailure) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @override |
||
| 170 | * @inheritDoc |
||
| 171 | */ |
||
| 172 | 8 | public function abort(callable $onCancel) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @override |
||
| 179 | * @inheritDoc |
||
| 180 | */ |
||
| 181 | 58 | public function always(callable $onFulfilledOrRejected) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @override |
||
| 204 | * @inheritDoc |
||
| 205 | */ |
||
| 206 | 34 | public function isPending() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @override |
||
| 213 | * @inheritDoc |
||
| 214 | */ |
||
| 215 | 8 | public function isFulfilled() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @override |
||
| 222 | * @inheritDoc |
||
| 223 | */ |
||
| 224 | 8 | public function isRejected() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @override |
||
| 231 | * @inheritDoc |
||
| 232 | */ |
||
| 233 | 10 | public function isCancelled() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @override |
||
| 240 | * @inheritDoc |
||
| 241 | */ |
||
| 242 | public function getPromise() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @override |
||
| 249 | * @inheritDoc |
||
| 250 | */ |
||
| 251 | 144 | public function resolve($value = null) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @override |
||
| 263 | * @inheritDoc |
||
| 264 | */ |
||
| 265 | 114 | public function reject($reason = null) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @override |
||
| 277 | * @inheritDoc |
||
| 278 | */ |
||
| 279 | 121 | public function cancel($reason = null) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Return primitive value associated with Promise. |
||
| 305 | * |
||
| 306 | * @return mixed|null |
||
| 307 | */ |
||
| 308 | protected function getValue() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Return rejection or cancellation reason for Promise. |
||
| 315 | * |
||
| 316 | * @return Error|Exception|string|null |
||
| 317 | */ |
||
| 318 | protected function getReason() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Settle Promise with another Promise. |
||
| 325 | * |
||
| 326 | * @see PromiseInterface::resolve |
||
| 327 | * @see PromiseInterface::reject |
||
| 328 | * @see PromiseInterface::cancel |
||
| 329 | * |
||
| 330 | * @param PromiseInterface $promise |
||
| 331 | * @return PromiseInterface |
||
| 332 | * @resolves mixed|null |
||
| 333 | * @rejects Error|Exception|string|null |
||
| 334 | * @cancels Error|Exception|string|null |
||
| 335 | */ |
||
| 336 | 330 | protected function settle(PromiseInterface $promise) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get Promise result. Returns fulfilled, rejected or cancelled Promise for settled Promises or null for pending. |
||
| 353 | * |
||
| 354 | * @return PromiseInterface|null |
||
| 355 | */ |
||
| 356 | 168 | protected function getResult() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Mutate resolver. |
||
| 368 | * |
||
| 369 | * @param callable|null $resolver |
||
| 370 | */ |
||
| 371 | 374 | protected function mutate(callable $resolver = null) |
|
| 401 | } |
||
| 402 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: