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 |
||
20 | class SubscriptionMonitor implements MonitorInterface |
||
21 | { |
||
22 | use MonitorTrait; |
||
23 | |||
24 | const SUBSCRIPTION_CREATE_TOPIC = 'wamp.subscription.on_create'; |
||
25 | const SUBSCRIPTION_SUB_TOPIC = 'wamp.subscription.on_subscribe'; |
||
26 | const SUBSCRIPTION_UNSUB_TOPIC = 'wamp.subscription.on_unsubscribe'; |
||
27 | const SUBSCRIPTION_DELETE_TOPIC = 'wamp.subscription.on_delete'; |
||
28 | const SUBSCRIPTION_LIST_TOPIC = 'wamp.subscription.list'; |
||
29 | const SUBSCRIPTION_LOOKUP_TOPIC = 'wamp.subscription.lookup'; |
||
30 | const SUBSCRIPTION_MATCH_TOPIC = 'wamp.subscription.match'; |
||
31 | const SUBSCRIPTION_GET_TOPIC = 'wamp.subscription.get'; |
||
32 | const SUBSCRIPTION_SUBLIST_TOPIC = 'wamp.subscription.list_subscribers'; |
||
33 | const SUBSCRIPTION_SUBCOUNT_TOPIC = 'wamp.subscription.count_subscribers'; |
||
34 | |||
35 | protected $sessionIds = []; |
||
36 | |||
37 | /** |
||
38 | * @var \stdClass Objects withs lists of subscriptions (exact, prefix, wildcard) |
||
39 | */ |
||
40 | protected $subscriptionIds = null; |
||
41 | |||
42 | /** |
||
43 | * Constructor. |
||
44 | * |
||
45 | * @param ClientSession $session |
||
46 | */ |
||
47 | public function __construct(ClientSession $session) |
||
52 | |||
53 | /** |
||
54 | * @param string $topic |
||
55 | * |
||
56 | * @return PromiseInterface |
||
57 | */ |
||
58 | public function getSubscriptionInfo($topic) |
||
69 | |||
70 | public function getSubscriptionIds() |
||
80 | |||
81 | /** |
||
82 | * Initializes the subscription to the meta-events. |
||
83 | */ |
||
84 | View Code Duplication | protected function initSetupCalls() |
|
96 | |||
97 | protected function retrieveSubscriptionIds() |
||
105 | |||
106 | protected function setList($list) |
||
110 | |||
111 | protected function getList() |
||
115 | } |
||
116 |
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.