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 | abstract class AbstractSerializer implements SerializerInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The type. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $type; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | 42 | public function getType($model) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | 42 | public function getId($model) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function getAttributes($model, array $fields = null) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | 24 | public function getLinks($model) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | 24 | public function getMeta($model) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | * |
||
| 68 | * @throws \LogicException |
||
| 69 | */ |
||
| 70 | 24 | public function getRelationship($model, $name) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Get the serializer method name for the given relationship. |
||
| 87 | * |
||
| 88 | * snake_case and kebab-case are converted into camelCase. |
||
| 89 | * |
||
| 90 | * @param string $name |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 24 | private function getRelationshipMethodName($name) |
|
| 106 | } |
||
| 107 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.