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 |
||
22 | class InstanceStateManager extends AbstractManager implements EventSubscriberInterface |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * The number of cycles to wait until retrying an unreachable instance |
||
27 | */ |
||
28 | const UNREACHABLE_CYCLES_UNTIL_RETRY = 10; |
||
29 | |||
30 | /** |
||
31 | * @var \SplObjectStorage the instances to connect to and their individual state |
||
32 | */ |
||
33 | private $_instances; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * InstanceStateManager constructor. |
||
38 | * |
||
39 | * @param Configuration $configuration |
||
40 | * @param LoggerInterface $logger |
||
41 | * @param EventDispatcher $eventDispatcher |
||
42 | */ |
||
43 | public function __construct(Configuration $configuration, LoggerInterface $logger, EventDispatcher $eventDispatcher) |
||
53 | |||
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | public static function getSubscribedEvents() |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Handler for the INSTANCE_COLLECTION_REQUEST event |
||
71 | */ |
||
72 | public function onInstanceCollectionRequest() |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Called when an instance was tried and reachable |
||
82 | * |
||
83 | * @param InstanceStateEvent $event |
||
84 | */ |
||
85 | View Code Duplication | public function onInstanceReachable(InstanceStateEvent $event) |
|
101 | |||
102 | |||
103 | /** |
||
104 | * Called when an instance was tried and wasn't reachable |
||
105 | * |
||
106 | * @param InstanceStateEvent $event |
||
107 | */ |
||
108 | View Code Duplication | public function onInstanceUnreachable(InstanceStateEvent $event) |
|
123 | |||
124 | |||
125 | /** |
||
126 | * Called when a previously unreachable instance was left untried for the current tick |
||
127 | * |
||
128 | * @param InstanceStateEvent $event |
||
129 | */ |
||
130 | public function onInstanceMaybeReachable(InstanceStateEvent $event) |
||
148 | |||
149 | |||
150 | /** |
||
151 | * @param Instance $instance |
||
152 | * |
||
153 | * @return InstanceState |
||
154 | */ |
||
155 | private function getInstanceState(Instance $instance) |
||
159 | |||
160 | } |
||
161 |
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.