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 $cache = []; |
||
| 15 | protected $injections = []; |
||
| 16 | protected $params = []; |
||
| 17 | protected $preferences = []; |
||
| 18 | protected $shared = []; |
||
| 19 | protected $sharedInstance = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Alias a class/interface/alias to a string |
||
| 23 | * @param string $alias the name of the alias |
||
| 24 | * @param string $class the name of the class |
||
| 25 | * @param array $params a key/value array of parameter names and values |
||
| 26 | * @return self |
||
| 27 | */ |
||
| 28 | 23 | public function setAlias(string $alias, string $class, array $params = []): self |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Set rule to call a method after constructing a class |
||
| 39 | * @param string $class the name of the class |
||
| 40 | * @param string $method the name of the method |
||
| 41 | * @param array $params a key/value array of parameter names and values |
||
| 42 | * @return self |
||
| 43 | */ |
||
| 44 | 18 | public function setInjection(string $class, string $method, array $params = []): self |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Set parameters of a class |
||
| 52 | * @param string $class the name of the class |
||
| 53 | * @param array $params a key/value array of parameter names and values |
||
| 54 | * @return self |
||
| 55 | */ |
||
| 56 | 20 | public function setParams(string $class, array $params): self |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Set a preferred implementation of a class/interface |
||
| 64 | * @param string $alias the name of the alias |
||
| 65 | * @param string $class the name of the class/interface |
||
| 66 | * @return self |
||
| 67 | */ |
||
| 68 | 3 | public function setPreference(string $alias, string $class): self |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Mark a class/alias as shared |
||
| 76 | * @param string $class the name of class/alias |
||
| 77 | * @return self |
||
| 78 | */ |
||
| 79 | 4 | public function setShared(string $class): self |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set a value. Retrievable with the get method |
||
| 87 | * @param string $key |
||
| 88 | * @param mixed $value |
||
| 89 | * @return self |
||
| 90 | */ |
||
| 91 | 4 | public function set(string $key, $value): self |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Returns true if DiMaria can return an entry for the given string. Returns false otherwise. |
||
| 99 | * @param string $class identifier of the entry to look for |
||
| 100 | * @return boolean |
||
| 101 | */ |
||
| 102 | 64 | public function has($class): bool |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Get an instance of a class |
||
| 109 | * @param string $class the name of class/alias to create |
||
| 110 | * @param array $params a key/value array of parameter names and values |
||
| 111 | * @throws NotFoundException when no entry was found |
||
| 112 | * @throws ContainerException if class could not be constructed |
||
| 113 | * @return mixed an instance of the class requested |
||
| 114 | */ |
||
| 115 | 62 | public function get($class, array $params = []) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Get a new instance of a class |
||
| 129 | * @param string $class the name of class/alias to create |
||
| 130 | * @param array $params a key/value array of parameter names and values |
||
| 131 | * @throws NotFoundException if no entry was found |
||
| 132 | * @throws ContainerException if class could not be constructed |
||
| 133 | * @return mixed an instance of the class requested |
||
| 134 | */ |
||
| 135 | 27 | public function create(string $class, array $params = []) |
|
| 142 | |||
| 143 | 61 | protected function getClassName(string $class): string |
|
| 153 | |||
| 154 | 59 | protected function getObject(string $class, array $params) |
|
| 170 | |||
| 171 | 59 | protected function getCallback(string $class, string $originalClass): callable |
|
| 189 | |||
| 190 | 59 | protected function generateCallback(string $class): callable |
|
| 205 | |||
| 206 | 56 | protected function getMethodInfo(\ReflectionMethod $method): array |
|
| 222 | |||
| 223 | 56 | public function getParameters(array $methodInfo, array $params): array |
|
| 242 | |||
| 243 | 39 | protected function determineParameter($param, bool $isVariadic): array |
|
| 257 | } |
||
| 258 |