| Conditions | 2 |
| Paths | 2 |
| Total Lines | 90 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 11 | public function forDNDeployment(\DNDeployment $obj) { |
||
| 12 | $loader = new Finite\Loader\ArrayLoader([ |
||
| 13 | 'class' => 'DNDeployment', |
||
| 14 | 'states' => [ |
||
| 15 | DNDeployment::STATE_NEW => ['type' => StateInterface::TYPE_INITIAL], |
||
| 16 | DNDeployment::STATE_SUBMITTED => ['type' => StateInterface::TYPE_NORMAL], |
||
| 17 | DNDeployment::STATE_INVALID => ['type' => StateInterface::TYPE_NORMAL], |
||
| 18 | DNDeployment::STATE_APPROVED => ['type' => StateInterface::TYPE_NORMAL], |
||
| 19 | DNDeployment::STATE_REJECTED => ['type' => StateInterface::TYPE_NORMAL], |
||
| 20 | DNDeployment::STATE_QUEUED => ['type' => StateInterface::TYPE_NORMAL], |
||
| 21 | DNDeployment::STATE_DEPLOYING => ['type' => StateInterface::TYPE_NORMAL], |
||
| 22 | DNDeployment::STATE_ABORTING => ['type' => StateInterface::TYPE_NORMAL], |
||
| 23 | DNDeployment::STATE_COMPLETED => ['type' => StateInterface::TYPE_FINAL], |
||
| 24 | DNDeployment::STATE_FAILED => ['type' => StateInterface::TYPE_FINAL], |
||
| 25 | DNDeployment::STATE_DELETED => ['type' => StateInterface::TYPE_FINAL], |
||
| 26 | ], |
||
| 27 | 'transitions' => [ |
||
| 28 | DNDeployment::TR_NEW => ['from' => [DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_NEW], |
||
| 29 | DNDeployment::TR_SUBMIT => ['from' => [DNDeployment::STATE_NEW], 'to' => DNDeployment::STATE_SUBMITTED], |
||
| 30 | DNDeployment::TR_QUEUE => [ |
||
| 31 | 'from' => [ |
||
| 32 | DNDeployment::STATE_NEW, // @deprecated. Remove this when DNRoot::startDeploy() is removed. |
||
| 33 | DNDeployment::STATE_APPROVED |
||
| 34 | ], |
||
| 35 | 'to' => DNDeployment::STATE_QUEUED |
||
| 36 | ], |
||
| 37 | DNDeployment::TR_INVALIDATE => [ |
||
| 38 | 'from' => [ |
||
| 39 | DNDeployment::STATE_NEW, |
||
| 40 | DNDeployment::STATE_SUBMITTED, |
||
| 41 | DNDeployment::STATE_APPROVED, |
||
| 42 | DNDeployment::STATE_REJECTED |
||
| 43 | ], |
||
| 44 | 'to' => DNDeployment::STATE_INVALID |
||
| 45 | ], |
||
| 46 | DNDeployment::TR_APPROVE => ['from' => [ |
||
| 47 | DNDeployment::STATE_NEW, |
||
| 48 | DNDeployment::STATE_SUBMITTED |
||
| 49 | ], |
||
| 50 | 'to' => DNDeployment::STATE_APPROVED |
||
| 51 | ], |
||
| 52 | DNDeployment::TR_REJECT => ['from' => [DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_REJECTED], |
||
| 53 | DNDeployment::TR_DEPLOY => ['from' => [DNDeployment::STATE_QUEUED], 'to' => DNDeployment::STATE_DEPLOYING], |
||
| 54 | DNDeployment::TR_ABORT => [ |
||
| 55 | 'from' => [ |
||
| 56 | DNDeployment::STATE_QUEUED, |
||
| 57 | DNDeployment::STATE_DEPLOYING, |
||
| 58 | DNDeployment::STATE_ABORTING |
||
| 59 | ], |
||
| 60 | 'to' => DNDeployment::STATE_ABORTING |
||
| 61 | ], |
||
| 62 | DNDeployment::TR_COMPLETE => ['from' => [DNDeployment::STATE_DEPLOYING], 'to' => DNDeployment::STATE_COMPLETED], |
||
| 63 | DNDeployment::TR_FAIL => [ |
||
| 64 | 'from' => [ |
||
| 65 | DNDeployment::STATE_NEW, |
||
| 66 | DNDeployment::STATE_SUBMITTED, |
||
| 67 | DNDeployment::STATE_INVALID, |
||
| 68 | DNDeployment::STATE_REJECTED, |
||
| 69 | DNDeployment::STATE_APPROVED, |
||
| 70 | DNDeployment::STATE_QUEUED, |
||
| 71 | DNDeployment::STATE_DEPLOYING, |
||
| 72 | DNDeployment::STATE_ABORTING |
||
| 73 | ], |
||
| 74 | 'to' => DNDeployment::STATE_FAILED |
||
| 75 | ], |
||
| 76 | DNDeployment::TR_DELETE => [ |
||
| 77 | 'from' => [ |
||
| 78 | DNDeployment::STATE_NEW, |
||
| 79 | DNDeployment::STATE_SUBMITTED, |
||
| 80 | DNDeployment::STATE_INVALID, |
||
| 81 | DNDeployment::STATE_APPROVED, |
||
| 82 | DNDeployment::STATE_REJECTED, |
||
| 83 | ], |
||
| 84 | 'to' => DNDeployment::STATE_DELETED |
||
| 85 | ], |
||
| 86 | ] |
||
| 87 | ]); |
||
| 88 | $stateMachine = Injector::inst()->get('Finite\StateMachine\StateMachine', true, [$obj]); |
||
| 89 | |||
| 90 | // if we have already initialised a state machine for the given \DNDeployment |
||
| 91 | // then don't re-initialise it again, as this causes bugs with transition events |
||
| 92 | // being called multiple times. |
||
| 93 | if (empty(self::$initialised[$obj->ID])) { |
||
| 94 | $loader->load($stateMachine); |
||
| 95 | $stateMachine->initialize(); |
||
| 96 | $this->addHandlers($stateMachine); |
||
| 97 | self::$initialised[$obj->ID] = true; |
||
| 98 | } |
||
| 99 | return $stateMachine; |
||
| 100 | } |
||
| 101 | |||
| 132 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.