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 SyncTimings extends Command |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var SymfonyStyle |
||
| 26 | */ |
||
| 27 | private $io; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var TogglClient |
||
| 31 | */ |
||
| 32 | private $togglClient; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ReportsClient |
||
| 36 | */ |
||
| 37 | private $reportsClient; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var InvoiceNinjaClient |
||
| 41 | */ |
||
| 42 | private $invoiceNinjaClient; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private $clients; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $projects; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * SyncTimings constructor. |
||
| 56 | * |
||
| 57 | * @param TogglClient $togglClient |
||
| 58 | * @param ReportsClient $reportsClient |
||
| 59 | * @param InvoiceNinjaClient $invoiceNinjaClient |
||
| 60 | * @param array $clients |
||
| 61 | * @param array $projects |
||
| 62 | */ |
||
| 63 | public function __construct( |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Configure the command |
||
| 81 | */ |
||
| 82 | protected function configure() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param TimeEntry $entry |
||
| 132 | * @param array $config |
||
| 133 | * @param $key |
||
| 134 | */ |
||
| 135 | private function logTask(TimeEntry $entry, array $config, $key) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param TimeEntry $entry |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | private function buildTaskDescription(TimeEntry $entry) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param TimeEntry $entry |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | private function buildTimeLog(TimeEntry $entry) |
||
| 178 | } |
||
| 179 |
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.