Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Manager |
||
29 | { |
||
30 | /** |
||
31 | * @var string Manager name |
||
32 | */ |
||
33 | private $name; |
||
34 | |||
35 | /** |
||
36 | * @var array Manager configuration |
||
37 | */ |
||
38 | private $config = []; |
||
39 | |||
40 | /** |
||
41 | * @var Client |
||
42 | */ |
||
43 | private $client; |
||
44 | |||
45 | /** |
||
46 | * @var Converter |
||
47 | */ |
||
48 | private $converter; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $readOnly; |
||
54 | |||
55 | /** |
||
56 | * @var array Container for bulk queries |
||
57 | */ |
||
58 | private $bulkQueries = []; |
||
59 | |||
60 | /** |
||
61 | * @var array Holder for consistency, refresh and replication parameters |
||
62 | */ |
||
63 | private $bulkParams = []; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $indexSettings; |
||
69 | |||
70 | /** |
||
71 | * @var MetadataCollector |
||
72 | */ |
||
73 | private $metadataCollector; |
||
74 | |||
75 | /** |
||
76 | * After commit to make data available the refresh or flush operation is needed |
||
77 | * so one of those methods has to be defined, the default is refresh. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $commitMode = 'refresh'; |
||
82 | |||
83 | /** |
||
84 | * The size that defines after how much document inserts call commit function. |
||
85 | * |
||
86 | * @var int |
||
87 | */ |
||
88 | private $bulkCommitSize = 100; |
||
89 | |||
90 | /** |
||
91 | * Container to count how many documents was passed to the bulk query. |
||
92 | * |
||
93 | * @var int |
||
94 | */ |
||
95 | private $bulkCount = 0; |
||
96 | |||
97 | /** |
||
98 | * @var Repository[] Repository local cache |
||
99 | */ |
||
100 | private $repositories; |
||
101 | |||
102 | /** |
||
103 | * @param string $name Manager name |
||
104 | * @param array $config Manager configuration |
||
105 | * @param Client $client |
||
106 | * @param array $indexSettings |
||
107 | * @param MetadataCollector $metadataCollector |
||
108 | * @param Converter $converter |
||
109 | */ |
||
110 | public function __construct( |
||
127 | |||
128 | /** |
||
129 | * Returns Elasticsearch connection. |
||
130 | * |
||
131 | * @return Client |
||
132 | */ |
||
133 | public function getClient() |
||
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getName() |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getConfig() |
||
153 | |||
154 | /** |
||
155 | * Returns repository by document class. |
||
156 | * |
||
157 | * @param string $className FQCN or string in Bundle:Document format |
||
158 | * |
||
159 | * @return Repository |
||
160 | */ |
||
161 | public function getRepository($className) |
||
178 | |||
179 | /** |
||
180 | * @return MetadataCollector |
||
181 | */ |
||
182 | public function getMetadataCollector() |
||
186 | |||
187 | /** |
||
188 | * @return Converter |
||
189 | */ |
||
190 | public function getConverter() |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getCommitMode() |
||
202 | |||
203 | /** |
||
204 | * @param string $commitMode |
||
205 | */ |
||
206 | public function setCommitMode($commitMode) |
||
214 | |||
215 | /** |
||
216 | * @return int |
||
217 | */ |
||
218 | public function getBulkCommitSize() |
||
222 | |||
223 | /** |
||
224 | * @param int $bulkCommitSize |
||
225 | */ |
||
226 | public function setBulkCommitSize($bulkCommitSize) |
||
230 | |||
231 | /** |
||
232 | * Creates a repository. |
||
233 | * |
||
234 | * @param string $className |
||
235 | * |
||
236 | * @return Repository |
||
237 | */ |
||
238 | private function createRepository($className) |
||
242 | |||
243 | /** |
||
244 | * Executes search query in the index. |
||
245 | * |
||
246 | * @param array $types List of types to search in. |
||
247 | * @param array $query Query to execute. |
||
248 | * @param array $queryStringParams Query parameters. |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | public function search(array $types, array $query, array $queryStringParams = []) |
||
265 | |||
266 | /** |
||
267 | * Adds document to next flush. |
||
268 | * |
||
269 | * @param object $document |
||
270 | */ |
||
271 | public function persist($document) |
||
278 | |||
279 | /** |
||
280 | * Flushes elasticsearch index. |
||
281 | * |
||
282 | * @param array $params |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | public function flush(array $params = []) |
||
290 | |||
291 | /** |
||
292 | * Refreshes elasticsearch index. |
||
293 | * |
||
294 | * @param array $params |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | public function refresh(array $params = []) |
||
302 | |||
303 | /** |
||
304 | * Inserts the current query container to the index, used for bulk queries execution. |
||
305 | * |
||
306 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
307 | * |
||
308 | * @return null|array |
||
309 | */ |
||
310 | public function commit(array $params = []) |
||
335 | |||
336 | /** |
||
337 | * Adds query to bulk queries container. |
||
338 | * |
||
339 | * @param string $operation One of: index, update, delete, create. |
||
340 | * @param string|array $type Elasticsearch type name. |
||
341 | * @param array $query DSL to execute. |
||
342 | * |
||
343 | * @throws \InvalidArgumentException |
||
344 | */ |
||
345 | public function bulk($operation, $type, array $query) |
||
387 | |||
388 | /** |
||
389 | * Optional setter to change bulk query params. |
||
390 | * |
||
391 | * @param array $params Possible keys: |
||
392 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
393 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
394 | * ['replication'] = (enum) Explicitly set the replication type. |
||
395 | */ |
||
396 | public function setBulkParams(array $params) |
||
400 | |||
401 | /** |
||
402 | * Creates fresh elasticsearch index. |
||
403 | * |
||
404 | * @param bool $noMapping Determines if mapping should be included. |
||
405 | * |
||
406 | * @return array |
||
407 | */ |
||
408 | public function createIndex($noMapping = false) |
||
418 | |||
419 | /** |
||
420 | * Drops elasticsearch index. |
||
421 | */ |
||
422 | public function dropIndex() |
||
428 | |||
429 | /** |
||
430 | * Tries to drop and create fresh elasticsearch index. |
||
431 | * |
||
432 | * @param bool $noMapping Determines if mapping should be included. |
||
433 | * |
||
434 | * @return array |
||
435 | */ |
||
436 | public function dropAndCreateIndex($noMapping = false) |
||
446 | |||
447 | /** |
||
448 | * Puts mapping into elasticsearch client. |
||
449 | * |
||
450 | * @param array $types Specific types to put. |
||
451 | * @param bool $ignoreConflicts Ignore elasticsearch merge conflicts. |
||
452 | */ |
||
453 | public function updateMapping(array $types = [], $ignoreConflicts = true) |
||
493 | |||
494 | /** |
||
495 | * Checks if connection index is already created. |
||
496 | * |
||
497 | * @return bool |
||
498 | */ |
||
499 | public function indexExists() |
||
503 | |||
504 | /** |
||
505 | * Returns index name this connection is attached to. |
||
506 | * |
||
507 | * @return string |
||
508 | */ |
||
509 | public function getIndexName() |
||
513 | |||
514 | /** |
||
515 | * Sets index name for this connection. |
||
516 | * |
||
517 | * @param string $name |
||
518 | */ |
||
519 | public function setIndexName($name) |
||
523 | |||
524 | /** |
||
525 | * Returns Elasticsearch version number. |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | public function getVersionNumber() |
||
533 | |||
534 | /** |
||
535 | * Clears elasticsearch client cache. |
||
536 | */ |
||
537 | public function clearCache() |
||
543 | |||
544 | /** |
||
545 | * Set connection to read only state. |
||
546 | * |
||
547 | * @param bool $readOnly |
||
548 | */ |
||
549 | public function setReadOnly($readOnly) |
||
553 | |||
554 | /** |
||
555 | * Checks if connection is read only. |
||
556 | * |
||
557 | * @param string $message Error message. |
||
558 | * |
||
559 | * @throws Forbidden403Exception |
||
560 | */ |
||
561 | public function isReadOnly($message = '') |
||
567 | |||
568 | /** |
||
569 | * Returns a single document by ID. Returns NULL if document was not found. |
||
570 | * |
||
571 | * @param string $className Document class name or Elasticsearch type name |
||
572 | * @param string $id Document ID to find |
||
573 | * |
||
574 | * @return object |
||
575 | */ |
||
576 | public function find($className, $id) |
||
594 | |||
595 | /** |
||
596 | * Executes given search. |
||
597 | * |
||
598 | * @param array $types |
||
599 | * @param Search $search |
||
600 | * @param string $resultsType |
||
601 | * |
||
602 | * @return DocumentIterator|RawIterator|array |
||
603 | */ |
||
604 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
614 | |||
615 | /** |
||
616 | * Parses raw result. |
||
617 | * |
||
618 | * @param array $raw |
||
619 | * @param string $resultsType |
||
620 | * @param string $scrollDuration |
||
621 | * |
||
622 | * @return DocumentIterator|RawIterator|array |
||
623 | * |
||
624 | * @throws \Exception |
||
625 | */ |
||
626 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
647 | |||
648 | /** |
||
649 | * Normalizes response array. |
||
650 | * |
||
651 | * @param array $data |
||
652 | * |
||
653 | * @return array |
||
654 | */ |
||
655 | private function convertToNormalizedArray($data) |
||
675 | |||
676 | /** |
||
677 | * Fetches next set of results. |
||
678 | * |
||
679 | * @param string $scrollId |
||
680 | * @param string $scrollDuration |
||
681 | * @param string $resultsType |
||
682 | * |
||
683 | * @return AbstractResultsIterator |
||
684 | * |
||
685 | * @throws \Exception |
||
686 | */ |
||
687 | public function scroll( |
||
696 | |||
697 | /** |
||
698 | * Resolves type name by class name. |
||
699 | * |
||
700 | * @param string $className |
||
701 | * |
||
702 | * @return string |
||
703 | */ |
||
704 | private function resolveTypeName($className) |
||
712 | } |
||
713 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.