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 |
||
11 | class PriorityQueueCallbacks extends \SplPriorityQueue |
||
12 | { |
||
13 | use \PHPDaemon\Traits\ClassWatchdog; |
||
14 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
15 | |||
16 | /** |
||
17 | * Insert callback |
||
18 | * @param callable $cb Callback |
||
19 | * @param integer $pri Priority |
||
20 | * @return void |
||
21 | */ |
||
22 | public function insert($cb, $pri = 0) |
||
26 | |||
27 | /** |
||
28 | * Enqueue callback |
||
29 | * @param callable $cb Callback |
||
30 | * @param integer $pri Priority |
||
31 | * @return void |
||
32 | */ |
||
33 | public function enqueue($cb, $pri = 0) |
||
37 | |||
38 | /** |
||
39 | * Dequeue |
||
40 | * @return callable |
||
41 | */ |
||
42 | public function dequeue() |
||
46 | |||
47 | /** |
||
48 | * Compare two priorities |
||
49 | * @param integer $pri1 |
||
50 | * @param integer $pri2 |
||
51 | * @return integer |
||
52 | */ |
||
53 | public function compare($pri1, $pri2) |
||
60 | |||
61 | /** |
||
62 | * Executes one callback from the top of queue with arbitrary arguments |
||
63 | * @param mixed ...$args Arguments |
||
64 | * @return boolean |
||
65 | */ |
||
66 | public function executeOne(...$args) |
||
77 | |||
78 | /** |
||
79 | * Executes all callbacks from the top of queue to bottom with arbitrary arguments |
||
80 | * @param mixed ...$args Arguments |
||
81 | * @return integer |
||
82 | */ |
||
83 | View Code Duplication | public function executeAll(...$args) |
|
98 | } |
||
99 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.