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 |
||
37 | class ItemDataProvider implements ItemDataProviderInterface |
||
38 | { |
||
39 | use IdentifierManagerTrait; |
||
40 | |||
41 | private $managerRegistry; |
||
42 | private $itemExtensions; |
||
43 | |||
44 | /** |
||
45 | * @param ManagerRegistry $managerRegistry |
||
46 | * @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory |
||
47 | * @param PropertyMetadataFactoryInterface $propertyMetadataFactory |
||
48 | * @param QueryItemExtensionInterface[] $itemExtensions |
||
49 | */ |
||
50 | View Code Duplication | public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = []) |
|
|
|||
51 | { |
||
52 | $this->managerRegistry = $managerRegistry; |
||
53 | $this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
||
54 | $this->propertyMetadataFactory = $propertyMetadataFactory; |
||
55 | $this->itemExtensions = $itemExtensions; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | * |
||
61 | * The context may contain a `fetch_data` key representing whether the value should be fetched by Doctrine or if we should return a reference. |
||
62 | * |
||
63 | * @throws RuntimeException |
||
64 | */ |
||
65 | public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
||
103 | |||
104 | /** |
||
105 | * Add WHERE conditions to the query for one or more identifiers (simple or composite). |
||
106 | * |
||
107 | * @param array $identifiers |
||
108 | * @param QueryBuilder $queryBuilder |
||
109 | */ |
||
110 | private function addWhereForIdentifiers(array $identifiers, QueryBuilder $queryBuilder) |
||
124 | } |
||
125 |
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.