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 | 2 | public function __construct(Configuration $configuration, LoggerInterface $logger, EventDispatcher $eventDispatcher) |
|
53 | |||
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | 2 | public static function getSubscribedEvents() |
|
59 | { |
||
60 | return [ |
||
61 | 2 | Events::INSTANCE_STATUS_COLLECTION_REQUEST => 'onInstanceStatusCollectionRequest', |
|
62 | 2 | Events::INSTANCE_STATE_REACHABLE => 'onInstanceReachable', |
|
63 | 2 | Events::INSTANCE_STATE_UNREACHABLE => 'onInstanceUnreachable', |
|
64 | 2 | Events::INSTANCE_STATE_MAYBE_REACHABLE => 'onInstanceMaybeReachable', |
|
65 | ]; |
||
66 | } |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Handler for the INSTANCE_STATUS_COLLECTION_EVENT event |
||
71 | * |
||
72 | * @param InstanceStatusCollectionRequestEvent $event |
||
73 | */ |
||
74 | public function onInstanceStatusCollectionRequest(InstanceStatusCollectionRequestEvent $event) |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Called when an instance was tried and reachable |
||
82 | * |
||
83 | * @param InstanceStateEvent $event |
||
84 | */ |
||
85 | 1 | View Code Duplication | public function onInstanceReachable(InstanceStateEvent $event) |
|
|||
86 | { |
||
87 | 1 | $instance = $event->getInstance(); |
|
88 | 1 | $instanceState = $this->getInstanceState($instance); |
|
89 | |||
90 | // Update reachability state now that we know the instance is reachable |
||
91 | 1 | if ($instanceState->getReachability() === InstanceState::MAYBE_REACHABLE) |
|
92 | { |
||
93 | 1 | $this->logger->notice('Instance {instanceName} is now reachable, will start polling for updates', [ |
|
94 | 1 | 'instanceName' => $instance->getName(), |
|
95 | ]); |
||
96 | |||
97 | 1 | $instanceState->setReachability(InstanceState::REACHABLE); |
|
98 | } |
||
99 | 1 | } |
|
100 | |||
101 | |||
102 | /** |
||
103 | * Called when an instance was tried and wasn't reachable |
||
104 | * |
||
105 | * @param InstanceStateEvent $event |
||
106 | */ |
||
107 | 1 | View Code Duplication | public function onInstanceUnreachable(InstanceStateEvent $event) |
108 | { |
||
109 | 1 | $instance = $event->getInstance(); |
|
110 | 1 | $instanceState = $this->getInstanceState($instance); |
|
111 | |||
112 | // Mark the instance as unreachable |
||
113 | 1 | $message = 'Instance {instanceName} not reachable, will wait for {cycles} cycles before retrying'; |
|
114 | |||
115 | 1 | $this->logger->warning($message, [ |
|
116 | 1 | 'instanceName' => $instance->getName(), |
|
117 | 1 | 'cycles' => self::UNREACHABLE_CYCLES_UNTIL_RETRY, |
|
118 | ]); |
||
119 | |||
120 | 1 | $instanceState->setReachability(InstanceState::UNREACHABLE); |
|
121 | 1 | } |
|
122 | |||
123 | |||
124 | /** |
||
125 | * Called when a previously unreachable instance was left untried for the current tick |
||
126 | * |
||
127 | * @param InstanceStateEvent $event |
||
128 | */ |
||
129 | 1 | public function onInstanceMaybeReachable(InstanceStateEvent $event) |
|
130 | { |
||
131 | 1 | $instance = $event->getInstance(); |
|
132 | 1 | $instanceState = $this->getInstanceState($instance); |
|
133 | |||
134 | // Wait for some cycles and then mark unreachable instances as maybe reachable |
||
135 | 1 | if ($instanceState->getRetryCount() === self::UNREACHABLE_CYCLES_UNTIL_RETRY - 1) |
|
136 | { |
||
137 | 1 | $instanceState->setReachability(InstanceState::MAYBE_REACHABLE); |
|
138 | 1 | $instanceState->resetRetryCount(); |
|
139 | |||
140 | 1 | $this->logger->info('Retrying instance {instanceName} during next cycle', [ |
|
141 | 1 | 'instanceName' => $instance->getName(), |
|
142 | ]); |
||
143 | } |
||
144 | else |
||
145 | 1 | $instanceState->incrementRetryCount(); |
|
146 | 1 | } |
|
147 | |||
148 | |||
149 | /** |
||
150 | * @param Instance $instance |
||
151 | * |
||
152 | * @return InstanceState |
||
153 | */ |
||
154 | 1 | public function getInstanceState(Instance $instance) |
|
158 | |||
159 | } |
||
160 |
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.