Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | final class Container implements ContainerInterface |
||
31 | { |
||
32 | protected const PREFIX_CONFLICT_HANDLER = 'conflictHandler.'; |
||
33 | protected const PREFIX_INDEX_BUILDER = 'indexBuilder.'; |
||
34 | protected const PREFIX_INDEX_MERGER = 'indexMerger.'; |
||
35 | protected const PREFIX_LOCK_ADAPTER = 'lockAdapter.'; |
||
36 | protected const PREFIX_OPERATION_LIST_BUILDER = 'operationListBuilder.'; |
||
37 | protected const PREFIX_STORAGE_ADAPTER = 'storageAdapter.'; |
||
38 | protected const PREFIX_VAULT_LAYOUT = 'vaultLayout.'; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * @var InspectableContainer |
||
43 | */ |
||
44 | protected $delegate; |
||
45 | |||
46 | /** |
||
47 | * Sets up the container with all bundled services. |
||
48 | * |
||
49 | * Note: Be very careful with shared services as the container is used shared by all vaults of a storeman instance. |
||
50 | */ |
||
51 | public function __construct() |
||
84 | |||
85 | public function injectConfiguration(Configuration $configuration): Container |
||
96 | |||
97 | public function injectStoreman(Storeman $storeman): Container |
||
108 | |||
109 | /** |
||
110 | * Selects the given vault (or no vault) as the current one. |
||
111 | * |
||
112 | * @param Vault $vault |
||
113 | * @return Container |
||
114 | */ |
||
115 | public function selectVault(Vault $vault = null): Container |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function get($id) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function has($id) |
||
137 | |||
138 | |||
139 | public function getStoreman(): Storeman |
||
143 | |||
144 | public function getConfiguration(): Configuration |
||
148 | |||
149 | public function getVaults(): VaultContainer |
||
153 | |||
154 | public function getSelectedVault(): ?Vault |
||
158 | |||
159 | |||
160 | public function getConflictHandler(string $name): ConflictHandlerInterface |
||
164 | |||
165 | public function addConflictHandler(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
169 | |||
170 | public function getConflictHandlerNames(): array |
||
174 | |||
175 | |||
176 | public function getIndexMerger(string $name): IndexMergerInterface |
||
180 | |||
181 | public function addIndexMerger(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
185 | |||
186 | public function getIndexMergerNames(): array |
||
190 | |||
191 | |||
192 | public function getIndexBuilder(string $name): IndexBuilderInterface |
||
196 | |||
197 | public function addIndexBuilder(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
201 | |||
202 | public function getIndexBuilderNames(): array |
||
206 | |||
207 | |||
208 | public function getLockAdapter(string $name): LockAdapterInterface |
||
212 | |||
213 | public function addLockAdapter(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
217 | |||
218 | public function getLockAdapterNames(): array |
||
222 | |||
223 | |||
224 | public function getOperationListBuilder(string $name): OperationListBuilderInterface |
||
228 | |||
229 | public function addOperationListBuilder(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
233 | |||
234 | public function getOperationListBuilderNames(): array |
||
238 | |||
239 | |||
240 | public function getStorageAdapter(string $name): StorageAdapterInterface |
||
244 | |||
245 | public function addStorageAdapter(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
249 | |||
250 | public function getStorageAdapterNames(): array |
||
254 | |||
255 | |||
256 | public function getVaultLayout(string $name): VaultLayoutInterface |
||
260 | |||
261 | public function addVaultLayout(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
265 | |||
266 | public function getVaultLayoutNames(): array |
||
270 | |||
271 | |||
272 | protected function getConflictHandlerServiceName(string $name): string |
||
276 | |||
277 | protected function getIndexBuilderServiceName(string $name): string |
||
281 | |||
282 | protected function getIndexMergerServiceName(string $name): string |
||
286 | |||
287 | protected function getLockAdapterServiceName(string $name): string |
||
291 | |||
292 | protected function getOperationListBuilderServiceName(string $name): string |
||
296 | |||
297 | protected function getStorageAdapterServiceName(string $name): string |
||
301 | |||
302 | protected function getVaultLayoutServiceName(string $name): string |
||
306 | |||
307 | /** |
||
308 | * Registers a factory for a type of service. |
||
309 | * |
||
310 | * @param string $type |
||
311 | * @param string $configurationKey |
||
312 | */ |
||
313 | protected function registerServiceFactory(string $type, string $configurationKey = null): void |
||
329 | |||
330 | /** |
||
331 | * Registers a factory for a type of vault-specific module. |
||
332 | * |
||
333 | * @param string $type |
||
334 | * @param string $vaultConfigurationKey |
||
335 | */ |
||
336 | protected function registerVaultServiceFactory(string $type, string $vaultConfigurationKey = null): void |
||
352 | |||
353 | /** |
||
354 | * Builds and returns list of added service names starting with a given prefix. |
||
355 | * |
||
356 | * @param string $prefix |
||
357 | * @return string[] |
||
358 | */ |
||
359 | protected function getServiceNamesWithPrefix(string $prefix): array |
||
380 | } |
||
381 |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope