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 |
||
| 18 | abstract class HasOneOrMany extends Relation |
||
| 19 | { |
||
| 20 | use HasCollectionResults; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $type = 'hasMany'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | public function save() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | public function hasResults() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param Record $item |
||
| 51 | */ |
||
| 52 | 1 | public function saveResult(Record $item) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @throws \Exception |
||
| 62 | */ |
||
| 63 | 1 | public function initResults() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param RecordCollection $collection |
||
| 76 | * @param Collection $items |
||
| 77 | */ |
||
| 78 | public function populateCollection(RecordCollection $collection, $items) |
||
| 84 | |||
| 85 | /** @noinspection PhpMissingParentCallCommonInspection |
||
| 86 | * @inheritdoc |
||
| 87 | */ |
||
| 88 | 1 | public function populateEagerQueryFromFkList($query, $fkList) |
|
| 93 | |||
| 94 | /** @noinspection PhpMissingParentCallCommonInspection |
||
| 95 | * @param RecordCollection $collection |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | 1 | public function getEagerFkList(RecordCollection $collection) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param array $dictionary |
||
| 113 | * @param Collection $collection |
||
| 114 | * @param Record $record |
||
| 115 | * @return AssociatedCollection |
||
| 116 | */ |
||
| 117 | public function getResultsFromCollectionDictionary($dictionary, $collection, $record) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Build model dictionary keyed by the relation's foreign key. |
||
| 133 | * |
||
| 134 | * @param RecordCollection $collection |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | View Code Duplication | protected function buildDictionary(RecordCollection $collection) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | protected function getDictionaryKey() |
||
| 154 | } |
||
| 155 |
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.