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 | trait EmbeddableTrait |
||
| 17 | { |
||
| 18 | use ArrayableTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @inheritdoc |
||
| 22 | */ |
||
| 23 | public abstract function fields(); |
||
|
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * @inheritdoc |
||
| 27 | */ |
||
| 28 | public abstract function extraFields(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @inheritdoc |
||
| 32 | */ |
||
| 33 | public function toArray( |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string property which will contain all the expanded parameters. |
||
| 86 | */ |
||
| 87 | public function getExpandEnvelope(): string |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Determines which fields can be returned by [[toArray()]]. |
||
| 94 | * This method will first extract the root fields from the given fields. |
||
| 95 | * Then it will check the requested root fields against those declared in |
||
| 96 | * [[fields()]] to determine which fields can be returned. |
||
| 97 | * |
||
| 98 | * @param array $fields the fields being requested for exporting |
||
| 99 | * @return array the list of fields to be exported. The array keys are the |
||
| 100 | * field names, and the array values are the corresponding object property |
||
| 101 | * names or PHP callables returning the field values. |
||
| 102 | */ |
||
| 103 | View Code Duplication | protected function resolveFieldList($fields): array |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Determines which expand fields can be returned by [[toArray()]]. |
||
| 123 | * This method will first extract the root fields from the given expand. |
||
| 124 | * Then it will check the requested root fields against those declared in |
||
| 125 | * [[extraFields()]] to determine which fields can be returned. |
||
| 126 | * |
||
| 127 | * @param array $expand the expand fields being requested for exporting |
||
| 128 | * @return array the list of fields to be exported. The array keys are the |
||
| 129 | * field names, and the array values are the corresponding object property |
||
| 130 | * names or PHP callables returning the field values. |
||
| 131 | */ |
||
| 132 | View Code Duplication | protected function resolveExpandList($expand): array |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $field name of the field to be resolved. |
||
| 156 | * @param string|callable $definition the field definition, it its an string |
||
| 157 | * it will be used as a property name, or a callable with signature. |
||
| 158 | * |
||
| 159 | * ```php |
||
| 160 | * function ($model, string $field) |
||
| 161 | * ``` |
||
| 162 | * @return mixed data obtained from the model. |
||
| 163 | */ |
||
| 164 | protected function processField($field, $definition) |
||
| 170 | } |
||
| 171 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.