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:
Complex classes like Builder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Builder implements BuilderInterface |
||
| 18 | { |
||
| 19 | |||
| 20 | protected $repository; |
||
| 21 | protected $cache; |
||
| 22 | protected $container; |
||
| 23 | protected $hashAlgo; |
||
| 24 | protected $storage; |
||
| 25 | |||
| 26 | 22 | public function __construct( |
|
| 40 | |||
| 41 | 3 | public function getConfigurationRepository() |
|
| 45 | |||
| 46 | 1 | public function getStorage() |
|
| 47 | { |
||
| 48 | 1 | return $this->storage; |
|
| 49 | } |
||
| 50 | |||
| 51 | 6 | public function setValue($path, $value, $requestedContext = ConfigurationRepository::CONTEXT_DEFAULT) |
|
| 80 | |||
| 81 | 8 | public function getContainer() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Retrieves a list of files that have been registered |
||
| 95 | * |
||
| 96 | * @return ConfigurationFileRepository |
||
| 97 | */ |
||
| 98 | |||
| 99 | 6 | public function getRegisteredConfigurationFiles() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param ConfigurationRepository|null $config |
||
| 106 | * @return ConfigurationRepository |
||
| 107 | * @throws InvalidConfigurationLocationException |
||
| 108 | * @throws InvalidFileException |
||
| 109 | * @throws |
||
| 110 | */ |
||
| 111 | |||
| 112 | 6 | public function build($context = ConfigurationRepository::CONTEXT_DEFAULT, ConfigInterface $config = null) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @return \SimpleXMLElement |
||
| 138 | * @throws InvalidFileException |
||
| 139 | * @throws MissingConfigurationException |
||
| 140 | */ |
||
| 141 | |||
| 142 | 8 | public function getMergedStructure() |
|
| 164 | |||
| 165 | 6 | protected function executeCallback($callbackString, $value) |
|
| 166 | { |
||
| 167 | 6 | if (function_exists($callbackString)) { |
|
| 168 | 1 | $execute = $callbackString; |
|
| 169 | } else { |
||
| 170 | 5 | $container = $this->getContainer(); |
|
| 171 | try { |
||
| 172 | 5 | $callbackObject = $this->getContainer()->get($callbackString); |
|
| 173 | 3 | if (!$callbackObject instanceof CallbackInterface) { |
|
| 174 | 1 | throw new UncallableCallbackException('Callback must implement ' . CallbackInterface::class); |
|
| 175 | } |
||
| 176 | $execute = [ |
||
| 177 | 2 | $callbackObject, |
|
| 178 | 2 | 'filter' |
|
| 179 | ]; |
||
| 180 | |||
| 181 | 2 | if (!is_callable($execute)) { |
|
| 182 | throw new UncallableCallbackException('Unable to execute callback: ' . $callbackString); |
||
| 183 | } |
||
| 184 | 3 | } catch (\Exception $e) { |
|
| 185 | /* |
||
| 186 | * This is slightly a hack but the purpose is to throw the insufficient exception if it's an actual |
||
| 187 | * problem with the generic container we provide. |
||
| 188 | */ |
||
| 189 | 3 | if ($container instanceof GenericContainer && !$e instanceof UncallableCallbackException) { |
|
| 190 | 2 | throw new InsufficientContainerException( |
|
| 191 | 'You are using functionality that requires either a fully functional DI Container or Service Locator. ' |
||
| 192 | 2 | . 'The container, or container adapter, must implement Interop\Container\ContainerInterface.' |
|
| 193 | ); |
||
| 194 | } |
||
| 195 | 1 | throw $e; |
|
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | 3 | return call_user_func($execute, $value); |
|
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @param \SimpleXMLElement $structure The object representing the merged configuration structure |
||
| 205 | * @param \SimpleXmlElement $config An empty config object to be populated |
||
| 206 | * @param string $context The name of the context |
||
| 207 | * @return ConfigurationRepository The resulting configuration object |
||
| 208 | */ |
||
| 209 | |||
| 210 | 14 | public function buildConfigurationObject( |
|
| 211 | MergedStructure $structure, |
||
| 212 | ConfigInterface $config, |
||
| 213 | $context = ConfigurationRepository::CONTEXT_DEFAULT |
||
| 214 | ) |
||
| 215 | { |
||
| 216 | 14 | $structure->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration'); |
|
| 217 | 14 | $elements = $structure->xpath('/*/s:section/s:group/s:element'); |
|
| 218 | 14 | foreach ($elements as $element) { |
|
| 219 | 14 | if ($element instanceof MergedStructure) { |
|
| 220 | 14 | $elementId = $element['identifier']; |
|
| 221 | 14 | $group = $element->xpath('..')[0]; |
|
| 222 | 14 | $groupId = $group['identifier']; |
|
| 223 | 14 | $section = $group->xpath('..')[0]; |
|
| 224 | 14 | $sectionId = $section['identifier']; |
|
| 225 | 14 | $configPath = sprintf('%s/%s/%s', $sectionId, $groupId, $elementId); |
|
| 226 | 14 | $value = $this->storage->getValue($configPath, $context); |
|
| 227 | 14 | if (!$value) { |
|
| 228 | 7 | $xpath = sprintf('/*/s:section[@identifier="%s"]/s:group[@identifier="%s"]/s:element[@identifier="%s"]/s:value', |
|
| 229 | 7 | $sectionId, |
|
| 230 | 7 | $groupId, |
|
| 231 | 7 | $elementId |
|
| 232 | ); |
||
| 233 | 7 | $result = $structure->xpath($xpath); |
|
| 234 | 7 | if (!empty($result)) { |
|
| 235 | 7 | $value = trim((string)$result[0]); |
|
| 236 | } |
||
| 237 | } |
||
| 238 | 14 | if (isset($element['callbackFromStorage'])) { |
|
| 239 | 6 | $callbackString = (string)$element['callbackFromStorage']; |
|
| 240 | |||
| 241 | 6 | $value = $this->executeCallback($callbackString, $value); |
|
| 242 | } |
||
| 243 | |||
| 244 | 11 | if ($value) { |
|
| 245 | 11 | $config->$sectionId->$groupId->$elementId = $value; |
|
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | 11 | } |
|
| 250 | |||
| 251 | 5 | View Code Duplication | public function mergeStructure(MergedStructure $base, \SimpleXMLElement $new) |
| 274 | |||
| 275 | 5 | View Code Duplication | protected function mergeGroup(\SimpleXMLElement $section, \SimpleXMLElement $newGroups) |
| 295 | |||
| 296 | 5 | View Code Duplication | protected function mergeElements(\SimpleXMLElement $group, \SimpleXMLElement $newElements) |
| 316 | |||
| 317 | 5 | protected function mergeAllChildren(\SimpleXMLElement $destination, \SimpleXMLElement $source) |
|
| 334 | |||
| 335 | } |
||
| 336 |