Total Complexity | 44 |
Total Lines | 249 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
33 | class Builder extends NetteContainerBuilder |
||
34 | { |
||
35 | public function __construct() |
||
36 | { |
||
37 | parent::__construct(); |
||
38 | |||
39 | $this->removeDefinition(self::THIS_CONTAINER); |
||
40 | $this->addImportedDefinition(self::THIS_CONTAINER)->setType(Container::class); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Adds the service definitions. |
||
45 | * |
||
46 | * @param Definition[] $definitions An array of service definitions |
||
47 | */ |
||
48 | public function addDefinitions(array $definitions): void |
||
49 | { |
||
50 | foreach ($definitions as $id => $definition) { |
||
51 | $this->addDefinition($id, $definition); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Registers a service definition. |
||
57 | * |
||
58 | * This methods allows for simple registration of service definition |
||
59 | * with a fluid interface. |
||
60 | * |
||
61 | * @param array<mixed>|Definition|Definitions\Reference|Definitions\Statement|string $class |
||
62 | * |
||
63 | * @return Definitions\ServiceDefinition A Definition instance |
||
64 | */ |
||
65 | public function register(string $id, $class = null, ?Definition $definition = null): Definition |
||
66 | { |
||
67 | return $this->addDefinition($id, $definition)->setFactory($class); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Registers an autowired service definition. |
||
72 | * |
||
73 | * This method implements a shortcut for using addDefinition() with |
||
74 | * an autowired definition. |
||
75 | * |
||
76 | * @return Definitions\ServiceDefinition The created definition |
||
77 | */ |
||
78 | public function autowire(string $id, string $class = null) |
||
79 | { |
||
80 | return $this->register($id, $class)->setAutowired(true); |
||
81 | } |
||
82 | |||
83 | public function addInterfaceDefinition(string $name): InterfaceDefinition |
||
84 | { |
||
85 | return $this->addDefinition($name, new InterfaceDefinition()); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Computes a reasonably unique hash of a value. |
||
90 | * |
||
91 | * @param mixed $value A serializable value |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public static function hash($value) |
||
96 | { |
||
97 | $hash = \substr(\base64_encode(\hash('sha256', \serialize($value), true)), 0, 7); |
||
98 | |||
99 | return \str_replace(['/', '+'], ['.', '_'], $hash); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Adds the service aliases. |
||
104 | */ |
||
105 | public function addAliases(array $aliases): void |
||
106 | { |
||
107 | foreach ($aliases as $alias => $id) { |
||
108 | $this->addAlias($alias, $id); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Gets a parameter. |
||
114 | * |
||
115 | * @param string $name The parameter name |
||
116 | * |
||
117 | * @throws ParameterNotFoundException if the parameter is not defined |
||
118 | * |
||
119 | * @return mixed The parameter value |
||
120 | */ |
||
121 | public function getParameter(string $name) |
||
122 | { |
||
123 | if (!\array_key_exists($name, $this->parameters)) { |
||
124 | $alternatives = []; |
||
125 | |||
126 | foreach ($this->parameters as $key => $parameterValue) { |
||
127 | $lev = \levenshtein($name, $key); |
||
128 | |||
129 | if ($lev <= \strlen($name) / 3 || false !== \strpos($key, $name)) { |
||
130 | $alternatives[] = $key; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | $nonNestedAlternative = null; |
||
135 | |||
136 | if (!\count($alternatives) && false !== \strpos($name, '.')) { |
||
137 | $namePartsLength = \explode('.', $name); |
||
138 | $key = \array_shift($namePartsLength); |
||
139 | |||
140 | while (\count($namePartsLength)) { |
||
141 | if ($this->hasParameter($key)) { |
||
142 | if (!\is_array($this->getParameter($key))) { |
||
143 | $nonNestedAlternative = $key; |
||
144 | |||
145 | throw new ParameterNotFoundException( |
||
146 | $name, |
||
147 | null, |
||
148 | $alternatives, |
||
149 | $nonNestedAlternative |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | return self::arrayGet($this->parameters, $name); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return $this->parameters[$name] ?? null; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Checks if a parameter exists. |
||
164 | * |
||
165 | * @internal should not be used to check parameters existence |
||
166 | * |
||
167 | * @param string $name The parameter name |
||
168 | * |
||
169 | * @return bool The presence of parameter in container |
||
170 | */ |
||
171 | public function hasParameter(string $name) |
||
172 | { |
||
173 | return \array_key_exists($name, $this->parameters); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Adds parameters to the service container parameters. |
||
178 | * |
||
179 | * @param string $name The parameter name |
||
180 | * @param mixed $value The parameter value |
||
181 | */ |
||
182 | public function setParameter(string $name, $value): void |
||
183 | { |
||
184 | if (\strpos($name, '.') !== false) { |
||
185 | $parameters = &$this->parameters; |
||
186 | $keys = \explode('.', $name); |
||
187 | |||
188 | while (\count($keys) > 1) { |
||
189 | $key = \array_shift($keys); |
||
190 | |||
191 | if (!isset($parameters[$key]) || !\is_array($parameters[$key])) { |
||
192 | $parameters[$key] = []; |
||
193 | } |
||
194 | |||
195 | $parameters = &$parameters[$key]; |
||
196 | } |
||
197 | |||
198 | $parameters[\array_shift($keys)] = $value; |
||
199 | } else { |
||
200 | $this->parameters[$name] = $value; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Removes a parameter. |
||
206 | * |
||
207 | * @param string $name The parameter name |
||
208 | */ |
||
209 | public function removeParameter(string $name): void |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @internal |
||
231 | * |
||
232 | * Gets a dot-notated key from an array/object, with a default value if it does |
||
233 | * not exist |
||
234 | * |
||
235 | * @param array|object $array The search array |
||
236 | * @param mixed $key The dot-notated key or array of keys |
||
237 | * @param string $default The default value |
||
238 | * |
||
239 | * @throws InvalidArgumentException |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public static function arrayGet($array, $key, $default = null) |
||
282 | } |
||
283 | } |
||
284 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.