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 |
||
| 16 | class SearchResultAdCrawler extends CrawlerAbstract |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @param $url |
||
| 20 | * @return SearchResultUrlParser |
||
| 21 | */ |
||
| 22 | 10 | protected function setUrlParser($url) |
|
| 26 | |||
| 27 | /** |
||
| 28 | * Return the Ad's ID |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | 10 | public function getId() |
|
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Return the title |
||
| 40 | * |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | 10 | public function getTitle() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Return the price |
||
| 50 | * |
||
| 51 | * @return int |
||
| 52 | */ |
||
| 53 | 10 | public function getPrice() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Return the Ad's URL |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 10 | public function getUrl() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Return the data and time the ad was created |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 10 | public function getCreatedAt() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Return the thumb picture url |
||
| 97 | * |
||
| 98 | * @return null|string |
||
| 99 | */ |
||
| 100 | 10 | public function getThumb() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Return the number of picture of the ad |
||
| 118 | * |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | 10 | public function getNbImage() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @return mixed |
||
| 132 | */ |
||
| 133 | 10 | View Code Duplication | public function getPlacement() |
| 141 | |||
| 142 | /** |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | 10 | View Code Duplication | public function getType() |
| 157 | |||
| 158 | /** |
||
| 159 | * @return object |
||
| 160 | */ |
||
| 161 | 10 | public function getAll() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Return the field's value |
||
| 178 | * |
||
| 179 | * @param Crawler $node |
||
| 180 | * @param mixed $defaultValue |
||
| 181 | * @param \Closure $callback |
||
| 182 | * @param string $funcName |
||
| 183 | * @param string $funcParam |
||
| 184 | * |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | 10 | private function getFieldValue( |
|
| 200 | } |
||
| 201 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: