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 |
||
| 10 | class Order |
||
| 11 | { |
||
| 12 | const ASCENDING = 'asc'; |
||
| 13 | const DESCENDING = 'desc'; |
||
| 14 | const ASC = 'asc'; |
||
| 15 | const DESC = 'desc'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $column; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $order; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string[][] $orders |
||
| 29 | * @return Order[] |
||
| 30 | */ |
||
| 31 | 17 | public static function createMany(array $orders) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Order constructor. |
||
| 40 | * |
||
| 41 | * @param string $column |
||
| 42 | * @param string $order |
||
| 43 | */ |
||
| 44 | 17 | public function __construct($column, $order) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param $order |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 17 | View Code Duplication | protected static function validate($order) |
| 62 | |||
| 63 | /** |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | 17 | public function toArray() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 10 | public function column() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | 10 | public function order() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function ascending() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function descending() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return static |
||
| 105 | */ |
||
| 106 | 12 | public function inverse() |
|
| 110 | } |
||
| 111 |
This check looks for function calls that miss required arguments.