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 |
||
| 22 | class GearmanWorkerService { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var ILogger |
||
| 26 | */ |
||
| 27 | private $logger; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * GearmanWorkerService constructor. |
||
| 31 | * |
||
| 32 | * @param ILogger $logger |
||
| 33 | */ |
||
| 34 | 2 | public function __construct(ILogger $logger) { |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Checks if a worker is active and registered at the Gearman Job Server. |
||
| 40 | * returns false if not. |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public function workerExists(){ |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 66 | * |
||
| 67 | * @param Exception $e |
||
| 68 | * @throws Exception |
||
| 69 | * @throws NotFoundException |
||
| 70 | */ |
||
| 71 | View Code Duplication | private function handleException($e) { |
|
| 79 | |||
| 80 | } |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.