Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 15 | class InstanceStateManager |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The number of cycles to wait until retrying an unreachable instance |
||
| 20 | */ |
||
| 21 | const UNREACHABLE_CYCLES_UNTIL_RETRY = 10; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var LoggerInterface |
||
| 25 | */ |
||
| 26 | private $_logger; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \SplObjectStorage the instances to connect to and their individual state |
||
| 30 | */ |
||
| 31 | private $_instances; |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * @param LoggerInterface $logger |
||
| 36 | * @param Instance[] $instances |
||
| 37 | */ |
||
| 38 | public function __construct(LoggerInterface $logger, array $instances) |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Called when an instance was tried and reachable |
||
| 51 | * |
||
| 52 | * @param InstanceStateEvent $event |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function onInstanceReachable(InstanceStateEvent $event) |
|
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Called when an instance was tried and wasn't reachable |
||
| 74 | * |
||
| 75 | * @param InstanceStateEvent $event |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function onInstanceUnreachable(InstanceStateEvent $event) |
|
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Called when a previously unreachable instance was left untried for the current tick |
||
| 96 | * |
||
| 97 | * @param InstanceStateEvent $event |
||
| 98 | */ |
||
| 99 | public function onInstanceMaybeReachable(InstanceStateEvent $event) |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @return \SplObjectStorage |
||
| 121 | */ |
||
| 122 | public function getInstances() |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @param Instance $instance |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function isReachable(Instance $instance) |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @param Instance $instance |
||
| 141 | * |
||
| 142 | * @return InstanceState |
||
| 143 | */ |
||
| 144 | private function getInstanceState(Instance $instance) |
||
| 148 | |||
| 149 | } |
||
| 150 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.