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 |
||
| 10 | class ArrayHydrator |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The keys in the data array are entity field names |
||
| 14 | */ |
||
| 15 | const HYDRATE_BY_FIELD = 1; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The keys in the data array are database column names |
||
| 19 | */ |
||
| 20 | const HYDRATE_BY_COLUMN = 2; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var EntityManagerInterface |
||
| 24 | */ |
||
| 25 | protected $entityManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * If true, then associations are filled only with reference proxies. This is faster than querying them from |
||
| 29 | * database, but if the associated entity does not really exist, it will cause: |
||
| 30 | * * The insert/update to fail, if there is a foreign key defined in database |
||
| 31 | * * The record ind database also pointing to a non-existing record |
||
| 32 | * |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $hydrateAssociationReferences = true; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Tells whether the input data array keys are entity field names or database column names |
||
| 39 | * |
||
| 40 | * @var int one of ArrayHydrator::HIDRATE_BY_* constants |
||
| 41 | */ |
||
| 42 | protected $hydrateBy = self::HYDRATE_BY_FIELD; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * If true, hydrate the primary key too. Useful if the primary key is not automatically generated by the database |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $hydrateId = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param EntityManagerInterface $entityManager |
||
| 53 | */ |
||
| 54 | public function __construct(EntityManagerInterface $entityManager) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param $entity |
||
| 61 | * @param array $data |
||
| 62 | * @return mixed|object |
||
| 63 | * @throws Exception |
||
| 64 | */ |
||
| 65 | public function hydrate($entity, array $data) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param boolean $hydrateAssociationReferences |
||
| 81 | */ |
||
| 82 | public function setHydrateAssociationReferences($hydrateAssociationReferences) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param bool $hydrateId |
||
| 89 | */ |
||
| 90 | public function setHydrateId($hydrateId) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param int $hydrateBy |
||
| 97 | */ |
||
| 98 | public function setHydrateBy($hydrateBy) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param object $entity the doctrine entity |
||
| 105 | * @param array $data |
||
| 106 | * @return object |
||
| 107 | */ |
||
| 108 | protected function hydrateProperties($entity, $data) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $entity |
||
| 142 | * @param $data |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | protected function hydrateAssociations($entity, $data) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Retrieves the associated entity's id from $data |
||
| 166 | * |
||
| 167 | * @param string $fieldName name of field that stores the associated entity |
||
| 168 | * @param array $mapping doctrine's association mapping array for the field |
||
| 169 | * @param array $data the hydration data |
||
| 170 | * |
||
| 171 | * @return mixed null, if the association is not found |
||
| 172 | */ |
||
| 173 | protected function getAssociatedId($fieldName, $mapping, $data) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param $entity |
||
| 195 | * @param $propertyName |
||
| 196 | * @param $mapping |
||
| 197 | * @param $value |
||
| 198 | * @return mixed |
||
| 199 | */ |
||
| 200 | protected function hydrateToOneAssociation($entity, $propertyName, $mapping, $value) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param $entity |
||
| 214 | * @param $propertyName |
||
| 215 | * @param $mapping |
||
| 216 | * @param $value |
||
| 217 | * @return mixed |
||
| 218 | */ |
||
| 219 | protected function hydrateToManyAssociation($entity, $propertyName, $mapping, $value) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param $entity |
||
| 249 | * @param $propertyName |
||
| 250 | * @param $value |
||
| 251 | * @param null $reflectionObject |
||
| 252 | * @return mixed |
||
| 253 | */ |
||
| 254 | protected function setProperty($entity, $propertyName, $value, $reflectionObject = null) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param $className |
||
| 265 | * @param $id |
||
| 266 | * @return bool|\Doctrine\Common\Proxy\Proxy|null|object |
||
| 267 | * @throws \Doctrine\ORM\ORMException |
||
| 268 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 269 | * @throws \Doctrine\ORM\TransactionRequiredException |
||
| 270 | */ |
||
| 271 | protected function fetchAssociationEntity($className, $id) |
||
| 279 | } |
||
| 280 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: