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 |
||
| 24 | class RaCandidateSearchQuery implements HttpQuery |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $institution; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $commonName; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $email; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private $pageNumber = 1; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $orderBy; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $orderDirection; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $institution |
||
| 58 | * @param int $pageNumber |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function __construct($institution, $pageNumber) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $commonName |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | public function setCommonName($commonName) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $email |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | public function setEmail($email) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $orderBy |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function setOrderBy($orderBy) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $orderDirection |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function setOrderDirection($orderDirection) |
|
| 115 | { |
||
| 116 | $this->assertNonEmptyString($orderDirection, 'orderDirection'); |
||
| 117 | Assert\that($orderDirection)->choice( |
||
| 118 | ['asc', 'desc', '', null], |
||
| 119 | "Invalid order direction, must be one of 'asc', 'desc'" |
||
| 120 | ); |
||
| 121 | |||
| 122 | $this->orderDirection = $orderDirection; |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function toHttpQuery() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param mixed $value |
||
| 148 | * @param string $parameterName |
||
| 149 | */ |
||
| 150 | View Code Duplication | private function assertNonEmptyString($value, $parameterName) |
|
| 160 | } |
||
| 161 |
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.