Complex classes like Document 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 Document, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Document implements IConfigProvider |
||
29 | { |
||
30 | use TGetter; |
||
31 | |||
32 | /** |
||
33 | * Htsl main object owns this document. |
||
34 | * |
||
35 | * @access private |
||
36 | * |
||
37 | * @var \Htsl\Htsl |
||
38 | */ |
||
39 | private $htsl; |
||
40 | |||
41 | /** |
||
42 | * Parent document. |
||
43 | * |
||
44 | * @access private |
||
45 | * |
||
46 | * @var \Htsl\Parser\Document | null |
||
47 | */ |
||
48 | private $parent; |
||
49 | |||
50 | /** |
||
51 | * Reading buffer of this document. |
||
52 | * |
||
53 | * @access private |
||
54 | * |
||
55 | * @var \Htsl\ReadingBuffer\Contracts\ABuffer |
||
56 | */ |
||
57 | private $buffer; |
||
58 | |||
59 | /** |
||
60 | * The indentation and whether output with linefeed and indent. |
||
61 | * |
||
62 | * @access private |
||
63 | * |
||
64 | * @var string | bool |
||
65 | */ |
||
66 | private $indentation; |
||
67 | |||
68 | /** |
||
69 | * Type of this document. |
||
70 | * |
||
71 | * @access private |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $docType; |
||
76 | |||
77 | /** |
||
78 | * Current embead script. |
||
79 | * |
||
80 | * @access private |
||
81 | * |
||
82 | * @var \Htsl\Embedment\Contract\Embedment |
||
83 | */ |
||
84 | private $embedment; |
||
85 | |||
86 | /** |
||
87 | * Current indent level. |
||
88 | * |
||
89 | * @access private |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | private $level= 0; |
||
|
|||
94 | |||
95 | /** |
||
96 | * Section indent level. |
||
97 | * |
||
98 | * @access private |
||
99 | * |
||
100 | * @var int |
||
101 | */ |
||
102 | private $sectionLevel= 0; |
||
103 | |||
104 | /** |
||
105 | * Opened nodes. |
||
106 | * |
||
107 | * @access private |
||
108 | * |
||
109 | * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
||
110 | */ |
||
111 | private $openedNodes= []; |
||
112 | |||
113 | /** |
||
114 | * Current scopes. |
||
115 | * |
||
116 | * @access private |
||
117 | * |
||
118 | * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
||
119 | */ |
||
120 | private $scopes= []; |
||
121 | |||
122 | /** |
||
123 | * Current line number. |
||
124 | * |
||
125 | * @access private |
||
126 | * |
||
127 | * @var int |
||
128 | */ |
||
129 | private $lineNumber= 0; |
||
130 | |||
131 | /** |
||
132 | * Current line. |
||
133 | * |
||
134 | * @access private |
||
135 | * |
||
136 | * @var \Htsl\ReadingBuffer\Line |
||
137 | */ |
||
138 | private $currentLine; |
||
139 | |||
140 | /** |
||
141 | * Sections that can be show. |
||
142 | * |
||
143 | * @access private |
||
144 | * |
||
145 | * @var [ Htsl\Parser\Section, ] |
||
146 | */ |
||
147 | private $sections=[]; |
||
148 | |||
149 | /** |
||
150 | * Whether the document is executed. |
||
151 | * |
||
152 | * @access private |
||
153 | * |
||
154 | * @var bool |
||
155 | */ |
||
156 | private $isExecuted; |
||
157 | |||
158 | /** |
||
159 | * Current Section. |
||
160 | * |
||
161 | * @access private |
||
162 | * |
||
163 | * @var \Htsl\Parser\Section |
||
164 | */ |
||
165 | private $currentSection; |
||
166 | |||
167 | /** |
||
168 | * The content of this document. |
||
169 | * |
||
170 | * @access private |
||
171 | * |
||
172 | * @var string |
||
173 | */ |
||
174 | private $content; |
||
175 | |||
176 | /** |
||
177 | * Constructor of the Document. |
||
178 | * |
||
179 | * @access public |
||
180 | * |
||
181 | * @param \Htsl\Htsl $htsl |
||
182 | * @param \Htsl\ReadingBuffer\Contracts\ABuffer $buffer |
||
183 | * @param \Htsl\Parser\Document | null $parent |
||
184 | */ |
||
185 | public function __construct( Htsl$htsl, Buffer$buffer, self$parent=null ) |
||
198 | |||
199 | /** |
||
200 | * Executing the document. |
||
201 | * |
||
202 | * @access public |
||
203 | * |
||
204 | * @return \Htsl\Parser\Document |
||
205 | */ |
||
206 | public function execute():self |
||
215 | |||
216 | /** |
||
217 | * Alias of getContent. |
||
218 | * |
||
219 | * @access public |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function __toString():string |
||
227 | |||
228 | /** |
||
229 | * Getting the result of document executing. |
||
230 | * |
||
231 | * @access protected |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | protected function getContent():string |
||
243 | |||
244 | /** |
||
245 | * Getting the next line. |
||
246 | * |
||
247 | * @access protected |
||
248 | * |
||
249 | * @return \Htsl\ReadingBuffer\Line |
||
250 | */ |
||
251 | protected function getLine():Line |
||
259 | |||
260 | /** |
||
261 | * Getting the config of type of this document. |
||
262 | * |
||
263 | * @access public |
||
264 | * |
||
265 | * @param [ string, ] ...$keys |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | public function getConfig( string...$keys ) |
||
273 | |||
274 | /** |
||
275 | * Getting the type of this document. |
||
276 | * |
||
277 | * @access public |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getDoctype():string |
||
285 | |||
286 | /** |
||
287 | * Getting the indentation. |
||
288 | * |
||
289 | * @access public |
||
290 | * |
||
291 | * @return string | bool |
||
292 | */ |
||
293 | public function getIndentation() |
||
297 | |||
298 | /** |
||
299 | * Getting the indent level. |
||
300 | * |
||
301 | * @access public |
||
302 | * |
||
303 | * @return int |
||
304 | */ |
||
305 | public function getIndentLevel():int |
||
309 | |||
310 | /** |
||
311 | * Parsing the first line. |
||
312 | * |
||
313 | * @access protected |
||
314 | * |
||
315 | * @return \Htsl\Parser\Document |
||
316 | */ |
||
317 | protected function parseFirstLine():self |
||
318 | { |
||
319 | $line= $this->getLine(); |
||
320 | |||
321 | if( '@'===$line->getChar(0) ){ |
||
322 | return $this->setExtending($line); |
||
323 | } |
||
324 | |||
325 | $this->docType= $line->content; |
||
326 | $docTypeContent= $this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
||
327 | |||
328 | $this->indentation= $this->htsl->getConfig('indentation',$this->docType) ?? ( function( $scalarOrFalse ){ return is_scalar($scalarOrFalse)?$scalarOrFalse:false; } )($this->htsl->getConfig('indentation')); |
||
329 | |||
330 | return $this->appendLine($docTypeContent); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Setting that this document extends another document. |
||
335 | * |
||
336 | * @access protected |
||
337 | * |
||
338 | * @param \Htsl\ReadingBuffer\Line $firstLine |
||
339 | * |
||
340 | * @return \Htsl\Parser\Document |
||
341 | */ |
||
342 | protected function setExtending( Line$firstLine ):self |
||
360 | |||
361 | /** |
||
362 | * Parsing this document line by line. |
||
363 | * |
||
364 | * @access protected |
||
365 | * |
||
366 | * @return \Htsl\Parser\Document |
||
367 | */ |
||
368 | protected function lineByLine():self |
||
388 | |||
389 | /** |
||
390 | * Parsing embedded line. |
||
391 | * |
||
392 | * @access protected |
||
393 | * |
||
394 | * @param \Htsl\ReadingBuffer\Line $line |
||
395 | * |
||
396 | * @return \Htsl\Parser\Document |
||
397 | */ |
||
398 | protected function embeddingParse( Line$line ):self |
||
408 | |||
409 | /** |
||
410 | * Starting the embedding. |
||
411 | * |
||
412 | * @access protected |
||
413 | * |
||
414 | * @param string $embedType |
||
415 | * |
||
416 | * @return \Htsl\Parser\Document |
||
417 | */ |
||
418 | protected function startEmbedding( string$embedType ):self |
||
427 | |||
428 | /** |
||
429 | * Ending the embedding. |
||
430 | * |
||
431 | * @access public |
||
432 | * |
||
433 | * @return \Htsl\Parser\Document |
||
434 | */ |
||
435 | public function breakEmbedding():self |
||
442 | |||
443 | /** |
||
444 | * Parsing line. |
||
445 | * |
||
446 | * @access protected |
||
447 | * |
||
448 | * @param \Htsl\ReadingBuffer\Line $line |
||
449 | * |
||
450 | * @return \Htsl\Parser\Document |
||
451 | */ |
||
452 | protected function parseLine( Line$line ):self |
||
461 | |||
462 | /** |
||
463 | * Getting line type by analyzing first or first two characters of line. |
||
464 | * |
||
465 | * @access protected |
||
466 | * |
||
467 | * @param \Htsl\ReadingBuffer\Line $line |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | protected function getLineType( Line$line ):string |
||
481 | |||
482 | /** |
||
483 | * Possible line types. |
||
484 | * |
||
485 | * @access public |
||
486 | * |
||
487 | * @const array |
||
488 | * |
||
489 | * @todo Make this const private when php 7.1 |
||
490 | */ |
||
491 | const POSSIBLE_LINE_TYPES= [ |
||
492 | '`'=> [ |
||
493 | '='=> 'expressionHtmlLine', |
||
494 | ' '=> 'htmlLine', |
||
495 | ], |
||
496 | '='=> 'expressionLine', |
||
497 | '!'=> 'commentLine', |
||
498 | '-'=> 'tagLine', |
||
499 | '~'=> 'controlLine', |
||
500 | '@'=> 'docControlLine', |
||
501 | ' '=> 'stringLine', |
||
502 | ]; |
||
503 | |||
504 | /** |
||
505 | * Parsing line as HTML content. |
||
506 | * |
||
507 | * @access protected |
||
508 | * |
||
509 | * @param \Htsl\ReadingBuffer\Line $line |
||
510 | * |
||
511 | * @return \Htsl\Parser\Document |
||
512 | */ |
||
513 | protected function parseHtmlLine( Line$line ):self |
||
517 | |||
518 | /** |
||
519 | * Parsing line as string content. |
||
520 | * |
||
521 | * @access protected |
||
522 | * |
||
523 | * @param \Htsl\ReadingBuffer\Line $line |
||
524 | * |
||
525 | * @return \Htsl\Parser\Document |
||
526 | */ |
||
527 | protected function parseStringLine( Line$line ):self |
||
531 | |||
532 | /** |
||
533 | * Parsing line as PHP expression. |
||
534 | * |
||
535 | * @access protected |
||
536 | * |
||
537 | * @param \Htsl\ReadingBuffer\Line $line |
||
538 | * |
||
539 | * @return \Htsl\Parser\Document |
||
540 | */ |
||
541 | protected function parseExpressionLine( Line$line ):self |
||
549 | |||
550 | /** |
||
551 | * Parsing line as PHP expression with HTML result. |
||
552 | * |
||
553 | * @access protected |
||
554 | * |
||
555 | * @param \Htsl\ReadingBuffer\Line $line |
||
556 | * |
||
557 | * @return \Htsl\Parser\Document |
||
558 | */ |
||
559 | protected function parseExpressionHtmlLine( Line$line ):self |
||
565 | |||
566 | /** |
||
567 | * Parsing line as comment. |
||
568 | * |
||
569 | * @access protected |
||
570 | * |
||
571 | * @param \Htsl\ReadingBuffer\Line $line |
||
572 | * |
||
573 | * @return \Htsl\Parser\Document |
||
574 | */ |
||
575 | protected function parseCommentLine( Line$line ):self |
||
581 | |||
582 | /** |
||
583 | * Parsing line as HTSL tag. |
||
584 | * |
||
585 | * @access protected |
||
586 | * |
||
587 | * @param \Htsl\ReadingBuffer\Line $line |
||
588 | * |
||
589 | * @return \Htsl\Parser\Document |
||
590 | */ |
||
591 | protected function parseTagLine( Line$line ):self |
||
601 | |||
602 | /** |
||
603 | * Parsing line as control node of Htsl.php. |
||
604 | * |
||
605 | * @access protected |
||
606 | * |
||
607 | * @param \Htsl\ReadingBuffer\Line $line |
||
608 | * |
||
609 | * @return \Htsl\Parser\Document |
||
610 | */ |
||
611 | protected function parseControlLine( Line$line ):self |
||
617 | |||
618 | /** |
||
619 | * Parsing line as document control node of Htsl.php. |
||
620 | * |
||
621 | * @access protected |
||
622 | * |
||
623 | * @param \Htsl\ReadingBuffer\Line $line |
||
624 | * |
||
625 | * @return \Htsl\Parser\Document |
||
626 | */ |
||
627 | protected function parseDocControlLine( Line$line ):self |
||
649 | |||
650 | /** |
||
651 | * Parsing extending defination. |
||
652 | * |
||
653 | * @access protected |
||
654 | * |
||
655 | * @param string $fileName |
||
656 | * |
||
657 | * @return \Htsl\Parser\Document |
||
658 | */ |
||
659 | protected function extend( string$fileName ):self |
||
668 | |||
669 | /** |
||
670 | * Insert indentations into given paragraph. |
||
671 | * |
||
672 | * @access protected |
||
673 | * |
||
674 | * @param string $input |
||
675 | * |
||
676 | * @return string |
||
677 | */ |
||
678 | protected function insertIndentations( string$input ):string |
||
682 | |||
683 | /** |
||
684 | * Include another document. |
||
685 | * |
||
686 | * @access protected |
||
687 | * |
||
688 | * @param \Htsl\ReadingBuffer\Line $line |
||
689 | * |
||
690 | * @return \Htsl\Parser\Document |
||
691 | */ |
||
692 | protected function include( Line$line ):self |
||
700 | |||
701 | /** |
||
702 | * Starting to define a section. |
||
703 | * |
||
704 | * @access protected |
||
705 | * |
||
706 | * @param \Htsl\ReadingBuffer\Line $line |
||
707 | * |
||
708 | * @return \Htsl\Parser\Document |
||
709 | */ |
||
710 | protected function defineSection( Line$line ):self |
||
718 | |||
719 | /** |
||
720 | * Showing a section. |
||
721 | * |
||
722 | * @access protected |
||
723 | * |
||
724 | * @param \Htsl\ReadingBuffer\Line $line |
||
725 | * |
||
726 | * @return \Htsl\Parser\Document |
||
727 | */ |
||
728 | protected function showSection( Line$line ):self |
||
747 | |||
748 | /** |
||
749 | * Setting document as section definer. |
||
750 | * |
||
751 | * @access public |
||
752 | * |
||
753 | * @param Section | null $section |
||
754 | */ |
||
755 | public function setSection( Section$section=null ):self |
||
782 | |||
783 | /** |
||
784 | * Bubble the sections to parent document. |
||
785 | * |
||
786 | * @access protected |
||
787 | * |
||
788 | * @return \Htsl\Parser\Document |
||
789 | */ |
||
790 | protected function bubbleSections():self |
||
802 | |||
803 | /** |
||
804 | * Escaping characters to HTML entities. |
||
805 | * |
||
806 | * @access public |
||
807 | * |
||
808 | * @param string $input |
||
809 | * |
||
810 | * @return string |
||
811 | */ |
||
812 | public function htmlEntities( string$input ):string |
||
816 | |||
817 | /** |
||
818 | * Setting indent level of this document. |
||
819 | * |
||
820 | * @access protected |
||
821 | * |
||
822 | * @param int $level |
||
823 | */ |
||
824 | protected function setLevel( int$level ):self |
||
838 | |||
839 | /** |
||
840 | * Opening a node. |
||
841 | * |
||
842 | * @access protected |
||
843 | * |
||
844 | * @param \Htsl\Parser\Node\Contracts\ANode $node |
||
845 | * |
||
846 | * @return \Htsl\Parser\Document |
||
847 | */ |
||
848 | protected function openNode( Node$node ):self |
||
856 | |||
857 | /** |
||
858 | * Closing open node or nodes. |
||
859 | * |
||
860 | * @access protected |
||
861 | * |
||
862 | * @param int $level |
||
863 | * |
||
864 | * @return \Htsl\Parser\Document |
||
865 | */ |
||
866 | protected function closeNodes( int$level=0 ):self |
||
882 | |||
883 | /** |
||
884 | * Pushing a scope to stack. |
||
885 | * |
||
886 | * @access protected |
||
887 | * |
||
888 | * @param \Htsl\Parser\Node\Contracts\ANode $scope |
||
889 | */ |
||
890 | protected function setScope( Node$scope ):int |
||
894 | |||
895 | /** |
||
896 | * Getting current scope on top of stack. |
||
897 | * |
||
898 | * @access public |
||
899 | * |
||
900 | * @return \Htsl\Parser\Node\Contracts\ANode | null |
||
901 | */ |
||
902 | public function getScope() |
||
906 | |||
907 | /** |
||
908 | * Pop a scope from stack. |
||
909 | * |
||
910 | * @access protected |
||
911 | * |
||
912 | * @param \Htsl\Parser\Node\Contracts\ANode $scope |
||
913 | * |
||
914 | * @return \Htsl\Parser\Document |
||
915 | */ |
||
916 | protected function removeScope( Node$scope ):self |
||
924 | |||
925 | /** |
||
926 | * Appending a line of content to parsing result. |
||
927 | * |
||
928 | * @access protected |
||
929 | * |
||
930 | * @param string $content |
||
931 | * |
||
932 | * @return \Htsl\Parser\Document |
||
933 | */ |
||
934 | protected function appendLine( string$content ):self |
||
940 | |||
941 | /** |
||
942 | * Appending some content to parsing result. |
||
943 | * |
||
944 | * @access protected |
||
945 | * |
||
946 | * @param string $content |
||
947 | * |
||
948 | * @return \Htsl\Parser\Document |
||
949 | */ |
||
950 | protected function append( string$content ):self |
||
960 | |||
961 | /** |
||
962 | * Getting the Htsl main object. |
||
963 | * |
||
964 | * @access public |
||
965 | * |
||
966 | * @return \Htsl\Htsl |
||
967 | */ |
||
968 | public function getHtsl() |
||
972 | |||
973 | /** |
||
974 | * Throw exception with document name and line number. |
||
975 | * |
||
976 | * @access public |
||
977 | * |
||
978 | * @param string $message |
||
979 | * |
||
980 | * @throw \Htsl\Parser\HtslParsingException |
||
981 | */ |
||
982 | public function throw( string$message ) |
||
986 | } |
||
987 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.