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 |
||
| 5 | class MultipleAdvancedModelTransformer extends AdvancedModelTransformer |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * A list of Models to include to the results, even if they are not |
||
| 9 | * specified by the user |
||
| 10 | * |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | private $included = array(); |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Transforms data to an object |
||
| 17 | * |
||
| 18 | * @param string $data |
||
| 19 | * @return \Model[] |
||
| 20 | */ |
||
| 21 | 1 | public function reverseTransform($data) |
|
| 22 | { |
||
| 23 | // Handle the data provided by Javascript, if any |
||
| 24 | 1 | $transformed = self::transformJSON($data, $this->included); |
|
| 25 | |||
| 26 | 1 | if ($transformed !== false) { |
|
| 27 | return $transformed; |
||
| 28 | } |
||
| 29 | |||
| 30 | 1 | $models = array(); |
|
| 31 | |||
| 32 | 1 | foreach ($this->types as $type) { |
|
| 33 | 1 | if (trim($data[$type]) === '') { |
|
| 34 | 1 | continue; |
|
| 35 | } |
||
| 36 | |||
| 37 | // Array to store IDs for quick access so we can be sure that no |
||
| 38 | // duplicates are saved |
||
| 39 | 1 | $ids = array(); |
|
| 40 | |||
| 41 | // Add force-included models |
||
| 42 | 1 | if (isset($this->included[$type])) { |
|
| 43 | foreach ($this->included[$type] as $model) { |
||
| 44 | $ids[$model->getID()] = true; // Prevent duplication |
||
| 45 | $models[] = $model; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | 1 | $input = explode(',', $data[$type]); |
|
| 50 | 1 | $input = array_unique(array_map('trim', $input)); |
|
| 51 | |||
| 52 | 1 | foreach ($input as $name) { |
|
| 53 | 1 | if ($name === '') { |
|
| 54 | 1 | continue; |
|
| 55 | } |
||
| 56 | |||
| 57 | 1 | $model = $this->getModelFromName($name, $type); |
|
|
|
|||
| 58 | |||
| 59 | 1 | if (!$model) { |
|
| 60 | // No model was found matching that name |
||
| 61 | $models[] = $this->invalidModel($type); |
||
| 62 | 1 | View Code Duplication | } elseif (!$model->isValid() || !isset($ids[$model->getID()])) { |
| 63 | // We can proceed, since we're not storing a duplicate |
||
| 64 | // (We don't check invalid models, since they might be |
||
| 65 | // corresponding to different names) |
||
| 66 | 1 | $models[] = $model; |
|
| 67 | 1 | $ids[$model->getID()] = true; |
|
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | 1 | return $models; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Add a model to the list of models that should be included in all cases |
||
| 77 | * |
||
| 78 | * @param \Model $model The model to include |
||
| 79 | * @throws \Exception When a model is of an unsupported type |
||
| 80 | */ |
||
| 81 | 1 | public function addInclude(\Model $model) |
|
| 93 | } |
||
| 94 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.