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 | public function initResults() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param RecordCollection $collection |
||
| 74 | * @param Collection $items |
||
| 75 | */ |
||
| 76 | public function populateCollection(RecordCollection $collection, $items) |
||
| 82 | |||
| 83 | /** @noinspection PhpMissingParentCallCommonInspection |
||
| 84 | * @inheritdoc |
||
| 85 | */ |
||
| 86 | 1 | public function populateEagerQueryFromFkList($query, $fkList) |
|
| 91 | |||
| 92 | /** @noinspection PhpMissingParentCallCommonInspection |
||
| 93 | * @param RecordCollection $collection |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | 1 | public function getEagerFkList(RecordCollection $collection) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param array $dictionary |
||
| 111 | * @param Collection $collection |
||
| 112 | * @param Record $record |
||
| 113 | * @return AssociatedCollection |
||
| 114 | */ |
||
| 115 | public function getResultsFromCollectionDictionary($dictionary, $collection, $record) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Build model dictionary keyed by the relation's foreign key. |
||
| 131 | * |
||
| 132 | * @param RecordCollection $collection |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | View Code Duplication | protected function buildDictionary(RecordCollection $collection) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | protected function getDictionaryKey() |
||
| 152 | } |
||
| 153 |
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.