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 | * Adds document for removal. |
||
281 | * |
||
282 | * @param object $document |
||
283 | */ |
||
284 | public function remove($document) |
||
298 | |||
299 | /** |
||
300 | * Flushes elasticsearch index. |
||
301 | * |
||
302 | * @param array $params |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | public function flush(array $params = []) |
||
310 | |||
311 | /** |
||
312 | * Refreshes elasticsearch index. |
||
313 | * |
||
314 | * @param array $params |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | public function refresh(array $params = []) |
||
322 | |||
323 | /** |
||
324 | * Inserts the current query container to the index, used for bulk queries execution. |
||
325 | * |
||
326 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
327 | * |
||
328 | * @return null|array |
||
329 | */ |
||
330 | public function commit(array $params = []) |
||
355 | |||
356 | /** |
||
357 | * Adds query to bulk queries container. |
||
358 | * |
||
359 | * @param string $operation One of: index, update, delete, create. |
||
360 | * @param string|array $type Elasticsearch type name. |
||
361 | * @param array $query DSL to execute. |
||
362 | * |
||
363 | * @throws \InvalidArgumentException |
||
364 | */ |
||
365 | public function bulk($operation, $type, array $query) |
||
407 | |||
408 | /** |
||
409 | * Optional setter to change bulk query params. |
||
410 | * |
||
411 | * @param array $params Possible keys: |
||
412 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
413 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
414 | * ['replication'] = (enum) Explicitly set the replication type. |
||
415 | */ |
||
416 | public function setBulkParams(array $params) |
||
420 | |||
421 | /** |
||
422 | * Creates fresh elasticsearch index. |
||
423 | * |
||
424 | * @param bool $noMapping Determines if mapping should be included. |
||
425 | * |
||
426 | * @return array |
||
427 | */ |
||
428 | public function createIndex($noMapping = false) |
||
438 | |||
439 | /** |
||
440 | * Drops elasticsearch index. |
||
441 | */ |
||
442 | public function dropIndex() |
||
448 | |||
449 | /** |
||
450 | * Tries to drop and create fresh elasticsearch index. |
||
451 | * |
||
452 | * @param bool $noMapping Determines if mapping should be included. |
||
453 | * |
||
454 | * @return array |
||
455 | */ |
||
456 | public function dropAndCreateIndex($noMapping = false) |
||
466 | |||
467 | /** |
||
468 | * Puts mapping into elasticsearch client. |
||
469 | * |
||
470 | * @param array $types Specific types to put. |
||
471 | * @param bool $ignoreConflicts Ignore elasticsearch merge conflicts. |
||
472 | */ |
||
473 | public function updateMapping(array $types = [], $ignoreConflicts = true) |
||
513 | |||
514 | /** |
||
515 | * Checks if connection index is already created. |
||
516 | * |
||
517 | * @return bool |
||
518 | */ |
||
519 | public function indexExists() |
||
523 | |||
524 | /** |
||
525 | * Returns index name this connection is attached to. |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | public function getIndexName() |
||
533 | |||
534 | /** |
||
535 | * Sets index name for this connection. |
||
536 | * |
||
537 | * @param string $name |
||
538 | */ |
||
539 | public function setIndexName($name) |
||
543 | |||
544 | /** |
||
545 | * Returns Elasticsearch version number. |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | public function getVersionNumber() |
||
553 | |||
554 | /** |
||
555 | * Clears elasticsearch client cache. |
||
556 | */ |
||
557 | public function clearCache() |
||
563 | |||
564 | /** |
||
565 | * Set connection to read only state. |
||
566 | * |
||
567 | * @param bool $readOnly |
||
568 | */ |
||
569 | public function setReadOnly($readOnly) |
||
573 | |||
574 | /** |
||
575 | * Checks if connection is read only. |
||
576 | * |
||
577 | * @param string $message Error message. |
||
578 | * |
||
579 | * @throws Forbidden403Exception |
||
580 | */ |
||
581 | public function isReadOnly($message = '') |
||
587 | |||
588 | /** |
||
589 | * Returns a single document by ID. Returns NULL if document was not found. |
||
590 | * |
||
591 | * @param string $className Document class name or Elasticsearch type name |
||
592 | * @param string $id Document ID to find |
||
593 | * |
||
594 | * @return object |
||
595 | */ |
||
596 | public function find($className, $id) |
||
614 | |||
615 | /** |
||
616 | * Executes given search. |
||
617 | * |
||
618 | * @param array $types |
||
619 | * @param Search $search |
||
620 | * @param string $resultsType |
||
621 | * |
||
622 | * @return DocumentIterator|RawIterator|array |
||
623 | */ |
||
624 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
634 | |||
635 | /** |
||
636 | * Parses raw result. |
||
637 | * |
||
638 | * @param array $raw |
||
639 | * @param string $resultsType |
||
640 | * @param string $scrollDuration |
||
641 | * |
||
642 | * @return DocumentIterator|RawIterator|array |
||
643 | * |
||
644 | * @throws \Exception |
||
645 | */ |
||
646 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
667 | |||
668 | /** |
||
669 | * Normalizes response array. |
||
670 | * |
||
671 | * @param array $data |
||
672 | * |
||
673 | * @return array |
||
674 | */ |
||
675 | private function convertToNormalizedArray($data) |
||
695 | |||
696 | /** |
||
697 | * Fetches next set of results. |
||
698 | * |
||
699 | * @param string $scrollId |
||
700 | * @param string $scrollDuration |
||
701 | * @param string $resultsType |
||
702 | * |
||
703 | * @return AbstractResultsIterator |
||
704 | * |
||
705 | * @throws \Exception |
||
706 | */ |
||
707 | public function scroll( |
||
716 | |||
717 | /** |
||
718 | * Clears scroll. |
||
719 | * |
||
720 | * @param string $scrollId |
||
721 | */ |
||
722 | public function clearScroll($scrollId) |
||
726 | |||
727 | /** |
||
728 | * Resolves type name by class name. |
||
729 | * |
||
730 | * @param string $className |
||
731 | * |
||
732 | * @return string |
||
733 | */ |
||
734 | private function resolveTypeName($className) |
||
742 | } |
||
743 |
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.