Complex classes like Typo3PageIndexer 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 Typo3PageIndexer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Typo3PageIndexer |
||
45 | { |
||
46 | |||
47 | /** |
||
48 | * ID of the current page's Solr document. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected static $pageSolrDocumentId = ''; |
||
53 | /** |
||
54 | * The Solr document generated for the current page. |
||
55 | * |
||
56 | * @var \Apache_Solr_Document |
||
57 | */ |
||
58 | protected static $pageSolrDocument = null; |
||
59 | /** |
||
60 | * The mount point parameter used in the Frontend controller. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $mountPointParameter; |
||
65 | /** |
||
66 | * Solr server connection. |
||
67 | * |
||
68 | * @var SolrService |
||
69 | */ |
||
70 | protected $solrConnection = null; |
||
71 | /** |
||
72 | * Frontend page object (TSFE). |
||
73 | * |
||
74 | * @var TypoScriptFrontendController |
||
75 | */ |
||
76 | protected $page = null; |
||
77 | /** |
||
78 | * Content extractor to extract content from TYPO3 pages |
||
79 | * |
||
80 | * @var Typo3PageContentExtractor |
||
81 | */ |
||
82 | protected $contentExtractor = null; |
||
83 | /** |
||
84 | * URL to be indexed as the page's URL |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $pageUrl = ''; |
||
89 | /** |
||
90 | * The page's access rootline |
||
91 | * |
||
92 | * @var Rootline |
||
93 | */ |
||
94 | protected $pageAccessRootline = null; |
||
95 | /** |
||
96 | * Documents that have been sent to Solr |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $documentsSentToSolr = []; |
||
101 | |||
102 | /** |
||
103 | * @var TypoScriptConfiguration |
||
104 | */ |
||
105 | protected $configuration; |
||
106 | |||
107 | /** |
||
108 | * @var Item |
||
109 | */ |
||
110 | protected $indexQueueItem; |
||
111 | |||
112 | /** |
||
113 | * Constructor |
||
114 | * |
||
115 | * @param TypoScriptFrontendController $page The page to index |
||
116 | */ |
||
117 | 35 | public function __construct(TypoScriptFrontendController $page) |
|
137 | |||
138 | /** |
||
139 | * @param Item $indexQueueItem |
||
140 | */ |
||
141 | 5 | public function setIndexQueueItem($indexQueueItem) |
|
145 | |||
146 | |||
147 | /** |
||
148 | * Initializes the Solr server connection. |
||
149 | * |
||
150 | * @throws \Exception when no Solr connection can be established. |
||
151 | */ |
||
152 | 35 | protected function initializeSolrConnection() |
|
166 | |||
167 | /** |
||
168 | * Logs messages to devlog and TS log (admin panel) |
||
169 | * |
||
170 | * @param string $message Message to set |
||
171 | * @param int $errorNum Error number |
||
172 | * @param array $data Additional data to log |
||
173 | * @return void |
||
174 | */ |
||
175 | 35 | protected function log($message, $errorNum = 0, array $data = []) |
|
192 | |||
193 | /** |
||
194 | * Gets the current page's Solr document ID. |
||
195 | * |
||
196 | * @return string|NULL The page's Solr document ID or NULL in case no document was generated yet. |
||
197 | */ |
||
198 | public static function getPageSolrDocumentId() |
||
202 | |||
203 | /** |
||
204 | * Gets the Solr document generated for the current page. |
||
205 | * |
||
206 | * @return \Apache_Solr_Document|NULL The page's Solr document or NULL if it has not been generated yet. |
||
207 | */ |
||
208 | 5 | public static function getPageSolrDocument() |
|
212 | |||
213 | /** |
||
214 | * Allows to provide a Solr server connection other than the one |
||
215 | * initialized by the constructor. |
||
216 | * |
||
217 | * @param SolrService $solrConnection Solr connection |
||
218 | * @throws \Exception if the Solr server cannot be reached |
||
219 | */ |
||
220 | 5 | public function setSolrConnection(SolrService $solrConnection) |
|
231 | |||
232 | /** |
||
233 | * Indexes a page. |
||
234 | * |
||
235 | * @return bool TRUE after successfully indexing the page, FALSE on error |
||
236 | * @throws \UnexpectedValueException if a page document post processor fails to implement interface ApacheSolrForTypo3\Solr\PageDocumentPostProcessor |
||
237 | */ |
||
238 | 35 | public function indexPage() |
|
266 | |||
267 | /** |
||
268 | * Applies the configured post processors (indexPagePostProcessPageDocument) |
||
269 | * |
||
270 | * @param \Apache_Solr_Document $pageDocument |
||
271 | */ |
||
272 | 35 | protected function applyIndexPagePostProcessors($pageDocument) |
|
287 | |||
288 | /** |
||
289 | * Builds the Solr document for the current page. |
||
290 | * |
||
291 | * @return \Apache_Solr_Document A document representing the page |
||
292 | */ |
||
293 | 35 | protected function getPageDocument() |
|
346 | |||
347 | /** |
||
348 | * Adds the access field to the document if needed. |
||
349 | * |
||
350 | * @param \Apache_Solr_Document $document |
||
351 | */ |
||
352 | 35 | protected function addAccessField(\Apache_Solr_Document $document) |
|
359 | |||
360 | /** |
||
361 | * @param $document |
||
362 | * @param $pageRecord |
||
363 | */ |
||
364 | 35 | protected function addEndtimeField(\Apache_Solr_Document $document, $pageRecord) |
|
370 | |||
371 | /** |
||
372 | * Adds keywords, multi valued. |
||
373 | * |
||
374 | * @param \Apache_Solr_Document $document |
||
375 | * @param array $pageRecord |
||
376 | */ |
||
377 | 35 | protected function addKeywordsField(\Apache_Solr_Document $document, $pageRecord) |
|
384 | |||
385 | /** |
||
386 | * Add content from several tags like headers, anchors, ... |
||
387 | * |
||
388 | * @param \Apache_Solr_Document $document |
||
389 | */ |
||
390 | 35 | protected function addTagContentFields(\Apache_Solr_Document $document) |
|
397 | |||
398 | /** |
||
399 | * Builds the content for the rootline field. |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | 35 | protected function getRootLineFieldValue() |
|
412 | |||
413 | /** |
||
414 | * Gets a comma separated list of frontend user groups to use for the |
||
415 | * document ID. |
||
416 | * |
||
417 | * @return string A comma separated list of frontend user groups. |
||
418 | */ |
||
419 | 35 | protected function getDocumentIdGroups() |
|
432 | |||
433 | // Logging |
||
434 | // TODO replace by a central logger |
||
435 | |||
436 | /** |
||
437 | * Gets the mount point parameter that is used in the Frontend controller. |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | 35 | public function getMountPointParameter() |
|
445 | |||
446 | // Misc |
||
447 | |||
448 | /** |
||
449 | * Sets the mount point parameter that is used in the Frontend controller. |
||
450 | * |
||
451 | * @param string $mountPointParameter |
||
452 | */ |
||
453 | 5 | public function setMountPointParameter($mountPointParameter) |
|
457 | |||
458 | /** |
||
459 | * Allows third party extensions to replace or modify the page document |
||
460 | * created by this indexer. |
||
461 | * |
||
462 | * @param \Apache_Solr_Document $pageDocument The page document created by this indexer. |
||
463 | * @return \Apache_Solr_Document An Apache Solr document representing the currently indexed page |
||
464 | */ |
||
465 | 35 | protected function substitutePageDocument(\Apache_Solr_Document $pageDocument) |
|
494 | |||
495 | /** |
||
496 | * Retrieves the indexConfigurationName from the related queueItem, or falls back to pages when no queue item set. |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | 5 | protected function getIndexConfigurationNameForCurrentPage() |
|
504 | |||
505 | /** |
||
506 | * Allows third party extensions to provide additional documents which |
||
507 | * should be indexed for the current page. |
||
508 | * |
||
509 | * @param \Apache_Solr_Document $pageDocument The main document representing this page. |
||
510 | * @param \Apache_Solr_Document[] $existingDocuments An array of documents already created for this page. |
||
511 | * @return array An array of additional \Apache_Solr_Document objects to index |
||
512 | */ |
||
513 | 35 | protected function getAdditionalDocuments(\Apache_Solr_Document $pageDocument, array $existingDocuments) |
|
537 | |||
538 | /** |
||
539 | * Sends the given documents to the field processing service which takes |
||
540 | * care of manipulating fields as defined in the field's configuration. |
||
541 | * |
||
542 | * @param array $documents An array of documents to manipulate |
||
543 | */ |
||
544 | 35 | protected function processDocuments(array $documents) |
|
552 | |||
553 | /** |
||
554 | * Adds the collected documents to the Solr index. |
||
555 | * |
||
556 | * @param array $documents An array of \Apache_Solr_Document objects. |
||
557 | * @return bool TRUE if documents were added successfully, FALSE otherwise |
||
558 | */ |
||
559 | 35 | protected function addDocumentsToSolrIndex(array $documents) |
|
592 | |||
593 | /** |
||
594 | * Gets the current page's URL. |
||
595 | * |
||
596 | * @return string URL of the current page. |
||
597 | */ |
||
598 | public function getPageUrl() |
||
602 | |||
603 | /** |
||
604 | * Sets the URL to use for the page document. |
||
605 | * |
||
606 | * @param string $url The page's URL. |
||
607 | */ |
||
608 | 5 | public function setPageUrl($url) |
|
612 | |||
613 | /** |
||
614 | * Gets the page's access rootline. |
||
615 | * |
||
616 | * @return Rootline The page's access rootline |
||
617 | */ |
||
618 | public function getPageAccessRootline() |
||
622 | |||
623 | /** |
||
624 | * Sets the page's access rootline. |
||
625 | * |
||
626 | * @param Rootline $accessRootline The page's access rootline |
||
627 | */ |
||
628 | 34 | public function setPageAccessRootline(Rootline $accessRootline) |
|
632 | |||
633 | /** |
||
634 | * Gets the documents that have been sent to Solr |
||
635 | * |
||
636 | * @return array An array of \Apache_Solr_Document objects |
||
637 | */ |
||
638 | 5 | public function getDocumentsSentToSolr() |
|
642 | } |
||
643 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.