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 |
||
28 | final class Container implements ContainerInterface |
||
29 | { |
||
30 | protected const PREFIX_CONFLICT_HANDLER = 'conflictHandler.'; |
||
31 | protected const PREFIX_INDEX_MERGER = 'indexMerger.'; |
||
32 | protected const PREFIX_LOCK_ADAPTER = 'lockAdapter.'; |
||
33 | protected const PREFIX_OPERATION_LIST_BUILDER = 'operationListBuilder.'; |
||
34 | protected const PREFIX_STORAGE_ADAPTER = 'storageAdapter.'; |
||
35 | protected const PREFIX_VAULT_LAYOUT = 'vaultLayout.'; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * @var InspectableContainer |
||
40 | */ |
||
41 | protected $delegate; |
||
42 | |||
43 | /** |
||
44 | * Sets up the container with all bundled services. |
||
45 | * |
||
46 | * Note: Be very careful with shared services as the container is used shared by all vaults of a storeman instance. |
||
47 | */ |
||
48 | public function __construct() |
||
78 | |||
79 | public function injectConfiguration(Configuration $configuration): Container |
||
90 | |||
91 | public function injectStoreman(Storeman $storeman): Container |
||
102 | |||
103 | /** |
||
104 | * Selects the given vault (or no vault) as the current one. |
||
105 | * |
||
106 | * @param Vault $vault |
||
107 | * @return Container |
||
108 | */ |
||
109 | public function selectVault(Vault $vault = null): Container |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function get($id) |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function has($id) |
||
131 | |||
132 | |||
133 | public function getStoreman(): Storeman |
||
137 | |||
138 | public function getConfiguration(): Configuration |
||
142 | |||
143 | public function getVaults(): VaultContainer |
||
147 | |||
148 | public function getSelectedVault(): ?Vault |
||
152 | |||
153 | |||
154 | public function getConflictHandler(string $name): ConflictHandlerInterface |
||
158 | |||
159 | public function addConflictHandler(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
163 | |||
164 | public function getConflictHandlerNames(): array |
||
168 | |||
169 | |||
170 | public function getIndexMerger(string $name): IndexMergerInterface |
||
174 | |||
175 | public function addIndexMerger(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
179 | |||
180 | public function getIndexMergerNames(): array |
||
184 | |||
185 | |||
186 | public function getLockAdapter(string $name): LockAdapterInterface |
||
190 | |||
191 | public function addLockAdapter(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
195 | |||
196 | public function getLockAdapterNames(): array |
||
200 | |||
201 | |||
202 | public function getOperationListBuilder(string $name): OperationListBuilderInterface |
||
206 | |||
207 | public function addOperationListBuilder(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
211 | |||
212 | public function getOperationListBuilderNames(): array |
||
216 | |||
217 | |||
218 | public function getStorageAdapter(string $name): StorageAdapterInterface |
||
222 | |||
223 | public function addStorageAdapter(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
227 | |||
228 | public function getStorageAdapterNames(): array |
||
232 | |||
233 | |||
234 | public function getVaultLayout(string $name): VaultLayoutInterface |
||
238 | |||
239 | public function addVaultLayout(string $name, $concrete, bool $shared = false): DefinitionInterface |
||
243 | |||
244 | public function getVaultLayoutNames(): array |
||
248 | |||
249 | |||
250 | protected function getConflictHandlerServiceName(string $name): string |
||
254 | |||
255 | protected function getIndexMergerServiceName(string $name): string |
||
259 | |||
260 | protected function getLockAdapterServiceName(string $name): string |
||
264 | |||
265 | protected function getOperationListBuilderServiceName(string $name): string |
||
269 | |||
270 | protected function getStorageAdapterServiceName(string $name): string |
||
274 | |||
275 | protected function getVaultLayoutServiceName(string $name): string |
||
279 | |||
280 | /** |
||
281 | * Registers a factory for a type of vault-specific module. |
||
282 | * |
||
283 | * @param string $type |
||
284 | * @param string $vaultConfigurationKey |
||
285 | */ |
||
286 | protected function registerVaultServiceFactory(string $type, string $vaultConfigurationKey = null): void |
||
302 | |||
303 | /** |
||
304 | * Builds and returns list of added service names starting with a given prefix. |
||
305 | * |
||
306 | * @param string $prefix |
||
307 | * @return string[] |
||
308 | */ |
||
309 | protected function getServiceNamesWithPrefix(string $prefix): array |
||
330 | } |
||
331 |
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