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 |
||
42 | class ConfigurationFactory |
||
43 | { |
||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $mappings = [ |
||
48 | 'oneToOne' => Configuration\Mapping\OneToOne::class, |
||
49 | 'manyToOne' => Configuration\Mapping\ManyToOne::class, |
||
50 | 'oneToMany' => Configuration\Mapping\OneToMany::class, |
||
51 | 'manyToMany' => Configuration\Mapping\ManyToMany::class, |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * @param array|\Traversable $configuration |
||
56 | * @return Configuration |
||
57 | */ |
||
58 | 1 | public function factory($configuration) |
|
59 | { |
||
60 | 1 | $configurationArray = ArrayUtils::iteratorToArray($configuration, true); |
|
61 | |||
62 | 1 | $configurationObject = new Configuration; |
|
63 | 1 | foreach ($configurationArray as $entityName => $entityConfig) { |
|
64 | 1 | $entity = new Configuration\Entity($entityName); |
|
65 | |||
66 | 1 | if (isset($entityConfig['options'])) { |
|
67 | 1 | $this->configureOptions($entity, $entityConfig['options']); |
|
68 | } |
||
69 | |||
70 | 1 | if (isset($entityConfig['fields'])) { |
|
71 | 1 | $this->configureFields($entity, $entityConfig['fields']); |
|
72 | } |
||
73 | 1 | $configurationObject->add($entity); |
|
74 | } |
||
75 | 1 | return $configurationObject; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param \Fabiang\DoctrineDynamic\Configuration\Entity $entity |
||
80 | * @param array $entityConfig |
||
81 | */ |
||
82 | 1 | private function configureFields(Configuration\Entity $entity, array $entityConfig) |
|
83 | { |
||
84 | 1 | foreach ($entityConfig as $fieldName => $fieldConfig) { |
|
85 | 1 | $field = new Configuration\Field($fieldName); |
|
86 | 1 | $this->configureMappings( |
|
87 | $field, |
||
88 | $fieldConfig, |
||
89 | 1 | $entity->getName() |
|
90 | ); |
||
91 | 1 | $entity->addField($field); |
|
92 | } |
||
93 | 1 | } |
|
94 | |||
95 | 1 | private function configureOptions(Configuration\Entity $entity, array $options) |
|
96 | { |
||
97 | 1 | foreach ($options as $option => $value) { |
|
98 | 1 | $setter = 'set' . ucfirst($option); |
|
99 | 1 | $entity->{$setter}($value); |
|
100 | } |
||
101 | 1 | } |
|
102 | |||
103 | /** |
||
104 | * @param \Fabiang\DoctrineDynamic\Configuration\Field $field |
||
105 | * @param array $fieldConfig |
||
106 | * @param string $entityName |
||
107 | * @throws UnexpectedValueException |
||
108 | */ |
||
109 | 1 | private function configureMappings(Configuration\Field $field, array $fieldConfig, $entityName) |
|
110 | { |
||
111 | 1 | foreach ($this->mappings as $mappingType => $mappingClassName) { |
|
112 | 1 | if (isset($fieldConfig[$mappingType])) { |
|
113 | 1 | if (!ArrayUtils::isList($fieldConfig[$mappingType])) { |
|
114 | throw new UnexpectedValueException(sprintf( |
||
115 | 'Mapping definition for field "%s" at entity "%s" of mapping type "%s" must be an list', |
||
116 | $field->getName(), |
||
117 | $entityName, |
||
118 | $mappingType |
||
119 | )); |
||
120 | } |
||
121 | |||
122 | 1 | $mappingConfigList = $fieldConfig[$mappingType]; |
|
123 | |||
124 | 1 | $mappingMethod = 'configure' . ucfirst($mappingType); |
|
125 | 1 | $fieldMethod = 'add' . ucfirst($mappingType); |
|
126 | 1 | foreach ($mappingConfigList as $mappingConfig) { |
|
127 | 1 | $field->$fieldMethod($this->$mappingMethod($mappingConfig)); |
|
128 | } |
||
129 | } |
||
130 | } |
||
131 | 1 | } |
|
132 | |||
133 | /** |
||
134 | * @param array $mappingConfig |
||
135 | * @return \Fabiang\DoctrineDynamic\Configuration\Mapping\OneToOne |
||
136 | */ |
||
137 | 1 | View Code Duplication | private function configureOneToOne(array $mappingConfig) |
|
|||
138 | { |
||
139 | 1 | $oneToOne = new Configuration\Mapping\OneToOne; |
|
140 | 1 | $oneToOne->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true)); |
|
141 | 1 | $oneToOne->setInversedBy($this->getOption($mappingConfig, 'inversedBy')); |
|
142 | 1 | $oneToOne->setMappedBy($this->getOption($mappingConfig, 'mappedBy')); |
|
143 | |||
144 | 1 | if (isset($mappingConfig['joinColumns'])) { |
|
145 | 1 | $oneToOne->setJoinColumn($this->configureJoinColumn($mappingConfig['joinColumns'])); |
|
146 | } |
||
147 | |||
148 | 1 | return $oneToOne; |
|
149 | } |
||
150 | |||
151 | 1 | private function configureManyToOne(array $mappingConfig) |
|
152 | { |
||
153 | 1 | $manyToOne = new Configuration\Mapping\ManyToOne; |
|
154 | 1 | $manyToOne->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true)); |
|
155 | 1 | $manyToOne->setInversedBy($this->getOption($mappingConfig, 'inversedBy')); |
|
156 | |||
157 | 1 | if (isset($mappingConfig['joinColumns'])) { |
|
158 | 1 | $manyToOne->setJoinColumn($this->configureJoinColumn($mappingConfig['joinColumns'])); |
|
159 | } |
||
160 | |||
161 | 1 | return $manyToOne; |
|
162 | } |
||
163 | |||
164 | 1 | private function configureOneToMany(array $mappingConfig) |
|
171 | |||
172 | 1 | View Code Duplication | private function configureManyToMany(array $mappingConfig) |
173 | { |
||
174 | 1 | $manyToMany = new Configuration\Mapping\ManyToMany; |
|
175 | 1 | $manyToMany->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true)); |
|
176 | 1 | $manyToMany->setInversedBy($this->getOption($mappingConfig, 'inversedBy')); |
|
177 | 1 | $manyToMany->setMappedBy($this->getOption($mappingConfig, 'mappedBy')); |
|
178 | |||
179 | 1 | if (isset($mappingConfig['joinTable'])) { |
|
180 | 1 | $manyToMany->setJoinTable($this->configureJoinTable($mappingConfig['joinTable'])); |
|
181 | } |
||
182 | |||
183 | 1 | return $manyToMany; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param array $joinColumnConfig |
||
188 | * @return \Fabiang\DoctrineDynamic\Configuration\Mapping\JoinColumn |
||
189 | */ |
||
190 | 1 | private function configureJoinColumn(array $joinColumnConfig) |
|
198 | |||
199 | 1 | private function configureJoinTable(array $joinTableConfig) |
|
206 | |||
207 | /** |
||
208 | * @param array $config |
||
209 | * @param string $option |
||
210 | * @param boolean $required |
||
211 | * @return array |
||
212 | * @throws RuntimeException |
||
213 | */ |
||
214 | 1 | private function getOption(array $config, $option, $required = false) |
|
229 | } |
||
230 |
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.