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 |
||
| 9 | class FamilyNormalizer implements NormalizerInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var AttributeNormalizer |
||
| 13 | */ |
||
| 14 | protected $attributeNormalizer; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param AttributeNormalizer $attributeNormalizer The entity manager |
||
| 18 | */ |
||
| 19 | public function __construct( |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param object $family |
||
| 30 | * @param null $format |
||
| 31 | * @param array $context |
||
| 32 | * @return array|\Symfony\Component\Serializer\Normalizer\scalar |
||
| 33 | * @throws Exception\NormalizeException |
||
| 34 | */ |
||
| 35 | public function normalize($family, $format = null, array $context = []) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Checks whether the given class is supported for normalization by this normalizer |
||
| 76 | * |
||
| 77 | * @param mixed $data Data to normalize. |
||
| 78 | * @param string $format The format being (de-)serialized from or into. |
||
| 79 | * |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | public function supportsNormalization($data, $format = null) |
||
| 86 | } |
||
| 87 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: