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 |
||
24 | trait EagerLoadingTrait |
||
25 | { |
||
26 | private $forceEager; |
||
27 | private $fetchPartial; |
||
28 | private $resourceMetadataFactory; |
||
29 | |||
30 | /** |
||
31 | * Checks if an operation has a `force_eager` attribute. |
||
32 | * |
||
33 | * @param string $resourceClass |
||
34 | * @param array $options |
||
35 | * |
||
36 | * @return bool |
||
37 | */ |
||
38 | private function shouldOperationForceEager(string $resourceClass, array $options): bool |
||
42 | |||
43 | /** |
||
44 | * Checks if an operation has a `fetch_partial` attribute. |
||
45 | * |
||
46 | * @param string $resourceClass |
||
47 | * @param array $options |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | private function shouldOperationFetchPartial(string $resourceClass, array $options): bool |
||
55 | |||
56 | /** |
||
57 | * Get the boolean attribute of an operation or the resource metadata. |
||
58 | * |
||
59 | * @param string $resourceClass |
||
60 | * @param array $options |
||
61 | * @param string $attributeName |
||
62 | * @param bool $default |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | private function getBooleanOperationAttribute(string $resourceClass, array $options, string $attributeName, bool $default): bool |
||
80 | |||
81 | /** |
||
82 | * Checkes if the class has an associationMapping with FETCH=EAGER. |
||
83 | * |
||
84 | * @param EntityManager $em |
||
85 | * @param ClassMetadataInfo $classMetadata |
||
86 | * @param array $checked array cache of tested metadata classes |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $classMetadata, array &$checked = []): bool |
||
112 | } |
||
113 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.