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 |
||
| 11 | class ChainResolver implements ReferenceResolverBagInterface, EnumerableReferenceResolverInterface, EmbeddedReferenceResolverInterface |
||
| 12 | { |
||
| 13 | /** @var ReferenceResolverInterface[] $resolvers */ |
||
| 14 | protected $resolvers = array(); |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param ReferenceResolverInterface[] $resolvers |
||
| 18 | 73 | */ |
|
| 19 | public function __construct(array $resolvers) |
||
| 23 | 73 | ||
| 24 | public function addResolver(ReferenceResolverInterface $resolver) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $stringIdentifier |
||
| 31 | * @return bool |
||
| 32 | 24 | */ |
|
| 33 | public function isReference($stringIdentifier) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $stringIdentifier |
||
| 46 | * @return mixed |
||
| 47 | * @throws \Exception |
||
| 48 | 23 | */ |
|
| 49 | public function getReferenceValue($stringIdentifier) |
||
| 67 | 24 | ||
| 68 | public function resolveReference($stringIdentifier) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Tries to add the reference to one of the resolvers in the chain (the first accepting it) |
||
| 78 | * |
||
| 79 | * @param string $identifier |
||
| 80 | * @param mixed $value |
||
| 81 | * @param bool $overwrite do overwrite the existing ref if it exist without raising an exception |
||
| 82 | * @return bool |
||
| 83 | 24 | */ |
|
| 84 | public function addReference($identifier, $value, $overwrite = false) |
||
| 96 | 1 | ||
| 97 | public function listReferences() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $string |
||
| 115 | * @return bool true if the given $stringIdentifier contains at least one occurrence of the reference(s) |
||
| 116 | * @throws \Exception if any resolver in the chain is not an EmbeddedReferenceResolverInterface |
||
| 117 | */ |
||
| 118 | public function hasEmbeddedReferences($string) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the $string with eventual refs resolved. |
||
| 135 | * Q: SHALL WE GUARANTEE THAT ALL RESOLVERS IN THE CHAIN CAN TAKE PART IN THIS? |
||
| 136 | * |
||
| 137 | * @param string $string |
||
| 138 | * @return string |
||
| 139 | * @throws \Exception if any resolver in the chain is not an EmbeddedReferenceResolverInterface |
||
| 140 | */ |
||
| 141 | public function resolveEmbeddedReferences($string) |
||
| 153 | } |
||
| 154 |
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.