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 DiMaria 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 DiMaria, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class DiMaria implements ContainerInterface |
||
12 | { |
||
13 | protected $aliases = []; |
||
14 | protected $factory = []; |
||
15 | protected $injections = []; |
||
16 | protected $params = []; |
||
17 | protected $preferences = []; |
||
18 | protected $shared = []; |
||
19 | protected $sharedInstance = []; |
||
20 | |||
21 | 77 | public function __construct() |
|
25 | |||
26 | /** |
||
27 | * Alias a class/interface/alias to a string |
||
28 | * @param string $alias the name of the alias |
||
29 | * @param string $class the name of the class |
||
30 | * @param array $params a key/value array of parameter names and values |
||
31 | * @return self |
||
32 | */ |
||
33 | 24 | public function setAlias(string $alias, string $class, array $params = []): self |
|
41 | |||
42 | /** |
||
43 | * Set rule to call a method after constructing a class |
||
44 | * @param string $class the name of the class |
||
45 | * @param string $method the name of the method |
||
46 | * @param array $params a key/value array of parameter names and values |
||
47 | * @return self |
||
48 | */ |
||
49 | 18 | public function setInjection(string $class, string $method, array $params = []): self |
|
54 | |||
55 | /** |
||
56 | * Set parameters of a class |
||
57 | * @param string $class the name of the class |
||
58 | * @param array $params a key/value array of parameter names and values |
||
59 | * @return self |
||
60 | */ |
||
61 | 20 | public function setParams(string $class, array $params): self |
|
66 | |||
67 | /** |
||
68 | * Set a preferred implementation of a class/interface |
||
69 | * @param string $alias the name of the alias |
||
70 | * @param string $class the name of the class/interface |
||
71 | * @return self |
||
72 | */ |
||
73 | 3 | public function setPreference(string $alias, string $class): self |
|
78 | |||
79 | /** |
||
80 | * Set an instance to always return a shared or new instance, regardless of method used |
||
81 | * @param string $class the name of class/alias |
||
82 | * @param bool $isShared true will always return a shared instance. false will always return a new instance |
||
83 | * @return self |
||
84 | */ |
||
85 | 6 | public function setShared(string $class, bool $isShared = true): self |
|
90 | |||
91 | /** |
||
92 | * Set a value. Retrievable with the get method |
||
93 | * @param string $key a name to retrieve the value by |
||
94 | * @param mixed $value the content to store against the key |
||
95 | * @return self |
||
96 | */ |
||
97 | 4 | public function set(string $key, $value): self |
|
102 | |||
103 | /** |
||
104 | * Set a factory. Retrievable with the create method. The get method will fetch but also cache the response |
||
105 | * @param string $key a name to retrieve the content by |
||
106 | * @param callable $factory a callable to be invoked when fetched |
||
107 | * @return self |
||
108 | */ |
||
109 | 6 | public function setFactory(string $key, callable $factory): self |
|
114 | |||
115 | /** |
||
116 | * Returns true if DiMaria can return an entry for the given string. Returns false otherwise. |
||
117 | * @param string $class identifier of the entry to look for |
||
118 | * @return boolean |
||
119 | */ |
||
120 | 71 | public function has($class): bool |
|
124 | |||
125 | /** |
||
126 | * Get an instance of a class |
||
127 | * @param string $class the name of class/alias to create |
||
128 | * @param array $params a key/value array of parameter names and values |
||
129 | * @throws NotFoundException when no entry was found |
||
130 | * @throws ContainerException if class could not be constructed |
||
131 | * @return mixed an instance of the class requested |
||
132 | */ |
||
133 | 68 | public function get($class, array $params = []) |
|
147 | |||
148 | /** |
||
149 | * Get a new instance of a class |
||
150 | * @param string $class the name of class/alias to create |
||
151 | * @param array $params a key/value array of parameter names and values |
||
152 | * @throws NotFoundException if no entry was found |
||
153 | * @throws ContainerException if class could not be constructed |
||
154 | * @return mixed an instance of the class requested |
||
155 | */ |
||
156 | 30 | public function create(string $class, array $params = []) |
|
163 | |||
164 | 68 | protected function getClassName(string $class): string |
|
174 | |||
175 | 66 | protected function getObject(string $class, array $params) |
|
191 | |||
192 | 63 | protected function getCallback(string $class, string $originalClass): callable |
|
210 | |||
211 | 63 | protected function generateCallback(string $class): callable |
|
226 | |||
227 | 57 | protected function getMethodInfo(\ReflectionMethod $method): array |
|
243 | |||
244 | 57 | protected function getParameters(array $methodInfo, array $params): array |
|
263 | |||
264 | 40 | protected function determineParameter($param, bool $isVariadic): array |
|
278 | } |
||
279 |
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.