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 |
||
33 | class CerbereProgressBarListener implements EventSubscriberInterface |
||
34 | { |
||
35 | /** |
||
36 | * @var OutputInterface |
||
37 | */ |
||
38 | protected $output = null; |
||
39 | |||
40 | /** |
||
41 | * @var ProgressBar |
||
42 | */ |
||
43 | protected $progress = null; |
||
44 | |||
45 | /** |
||
46 | * CerbereConsoleListener constructor. |
||
47 | * @param OutputInterface|null $output |
||
48 | */ |
||
49 | View Code Duplication | public function __construct(OutputInterface $output = null) |
|
|
|||
50 | { |
||
51 | if (null === $output) { |
||
52 | $output = new ConsoleOutput(); |
||
53 | } |
||
54 | |||
55 | $this->output = $output; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return OutputInterface |
||
60 | */ |
||
61 | public function getOutput() |
||
65 | |||
66 | /** |
||
67 | * @param OutputInterface $output |
||
68 | */ |
||
69 | public function setOutput($output) |
||
73 | |||
74 | /** |
||
75 | * Returns an array of event names this subscriber wants to listen to. |
||
76 | * |
||
77 | * The array keys are event names and the value can be: |
||
78 | * |
||
79 | * * The method name to call (priority defaults to 0) |
||
80 | * * An array composed of the method name to call and the priority |
||
81 | * * An array of arrays composed of the method names to call and respective |
||
82 | * priorities, or 0 if unset |
||
83 | * |
||
84 | * For instance: |
||
85 | * |
||
86 | * * array('eventName' => 'methodName') |
||
87 | * * array('eventName' => array('methodName', $priority)) |
||
88 | * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) |
||
89 | * |
||
90 | * @return array The event names to listen to |
||
91 | */ |
||
92 | public static function getSubscribedEvents() |
||
100 | |||
101 | /** |
||
102 | * @param CerbereDoActionEvent $event |
||
103 | */ |
||
104 | public function onCerbereDoAction(CerbereDoActionEvent $event) |
||
111 | |||
112 | /** |
||
113 | * @param CerberePostActionEvent $event |
||
114 | */ |
||
115 | public function onCerberePostAction(CerberePostActionEvent $event) |
||
127 | |||
128 | /** |
||
129 | * @param CerberePreActionEvent $event |
||
130 | */ |
||
131 | public function onCerberePreAction(CerberePreActionEvent $event) |
||
148 | } |
||
149 |
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.