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 |
||
14 | class PhpcrOdmAgent implements AgentInterface |
||
15 | { |
||
16 | private $documentManager; |
||
17 | |||
18 | public function __construct( |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function find($identifier, string $class = null) |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function save($object) |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function delete($object) |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | View Code Duplication | public function getIdentifier($object) |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | View Code Duplication | public function setParent($object, $parent) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function supports(string $class): bool |
|
105 | { |
||
106 | $metadataFactory = $this->documentManager->getMetadataFactory(); |
||
107 | |||
108 | $supports = false; |
||
109 | try { |
||
110 | $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class)); |
||
111 | $supports = true; |
||
112 | } catch (MappingException $exception) { |
||
113 | // no metadata - class is not known to phpcr-odm |
||
114 | } |
||
115 | |||
116 | return $supports; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function query(Query $query): \Traversable |
||
135 | |||
136 | /** |
||
137 | * Return the document mangaer instance (for use in events). |
||
138 | */ |
||
139 | public function getDocumentManager(): DocumentManagerInterface |
||
143 | } |
||
144 |
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.