Total Complexity | 49 |
Total Lines | 210 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like StackOverflowWebPageHandler 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.
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 StackOverflowWebPageHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class StackOverflowWebPageHandler extends WebPageHandler |
||
17 | { |
||
18 | private array $results = []; |
||
19 | |||
20 | public function getResults () : array |
||
21 | { |
||
22 | return $this->results; |
||
23 | } |
||
24 | |||
25 | public function read () : void |
||
38 | } |
||
39 | } |
||
40 | |||
41 | private function processListResults ( DOMElement $currentDomNode ) : void |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public static string $JOB_SEPARATOR = "You might be interested in these jobs:"; |
||
57 | |||
58 | private function processChildNodes ( DOMElement $currentChildrenNode ) : void |
||
59 | { |
||
60 | $currentJob = new StackOverflowJob(); |
||
61 | |||
62 | $currentJob = $this->setBasicAttributes( $currentChildrenNode->attributes, $currentJob ); |
||
|
|||
63 | |||
64 | if ( $currentChildrenNode->hasChildNodes() ) { |
||
65 | if ( trim( $currentChildrenNode->nodeValue ) != StackOverflowWebPageHandler::$JOB_SEPARATOR ) { |
||
66 | $extraChildNodes = $currentChildrenNode->childNodes; // DOMNodeList |
||
67 | |||
68 | for ( $i = 0; $i < $extraChildNodes->count(); $i++ ) { |
||
69 | $currentExtraChildNode = $extraChildNodes->item( $i ); // DOMElement or DOMText |
||
70 | |||
71 | if ( $currentExtraChildNode instanceof DOMElement ) { |
||
72 | $currentJob = $this->processExtraChildNodes( $currentExtraChildNode, $currentJob ); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if ( $currentJob->allAttributesDefined() ) { |
||
79 | array_push( $this->results, $currentJob ); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | private function setBasicAttributes ( |
||
84 | DOMNamedNodeMap $attributes, |
||
85 | StackOverflowJob $stackOverflowJob |
||
86 | ) : StackOverflowJob { |
||
87 | if ( $attributes != null ) { |
||
88 | foreach ( $attributes as $currentAttribute ) { |
||
89 | if ( $currentAttribute->name == "data-jobid" ) { |
||
90 | $stackOverflowJob->setJobId( $currentAttribute->value ); |
||
91 | } |
||
92 | |||
93 | if ( $currentAttribute->name == "data-result-id" ) { |
||
94 | $stackOverflowJob->setResultId( $currentAttribute->value ); |
||
95 | } |
||
96 | |||
97 | if ( $currentAttribute->name == "data-preview-url" ) { |
||
98 | $stackOverflowJob->setPreviewUrl( $currentAttribute->value ); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return $stackOverflowJob; |
||
104 | } |
||
105 | |||
106 | private function processExtraChildNodes ( |
||
107 | DOMElement $currentExtraChildNode, |
||
108 | StackOverflowJob $stackOverflowJob |
||
109 | ) : StackOverflowJob { |
||
110 | if ( $currentExtraChildNode->hasChildNodes() ) { |
||
111 | $currentExtraChildNodeChildren = $currentExtraChildNode->childNodes; // DOMNodeList |
||
112 | |||
113 | $nodeCount = $currentExtraChildNodeChildren->count(); |
||
114 | |||
115 | if ( $nodeCount >= 6 ) { |
||
116 | for ( $i = 0; $i < $nodeCount; $i++ ) { |
||
117 | $currentDeepNode = $currentExtraChildNodeChildren->item( $i ); // DOMElement or DOMText |
||
118 | |||
119 | if ( $currentDeepNode instanceof DOMElement ) { |
||
120 | $stackOverflowJob = $this->processImageDivBlocks( $currentDeepNode, $stackOverflowJob ); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | return $stackOverflowJob; |
||
127 | } |
||
128 | |||
129 | private function processImageDivBlocks ( |
||
130 | DOMElement $currentDeepNode, |
||
131 | StackOverflowJob $stackOverflowJob |
||
132 | ) : StackOverflowJob { |
||
133 | if ( $currentDeepNode->childNodes->count() == 2 ) { |
||
134 | $stackOverflowJob = $this->processImageBlock( $currentDeepNode->childNodes->item( 1 )->attributes, |
||
135 | $stackOverflowJob ); |
||
136 | } |
||
137 | |||
138 | if ( $currentDeepNode->tagName == "div" && $currentDeepNode->hasChildNodes() ) { |
||
139 | $stackOverflowJob = $this->processDivBlock( $currentDeepNode->childNodes, $stackOverflowJob ); |
||
140 | } |
||
141 | |||
142 | return $stackOverflowJob; |
||
143 | } |
||
144 | |||
145 | private function processImageBlock ( |
||
146 | DOMNamedNodeMap $currentDeepNodeAttributes, |
||
147 | StackOverflowJob $stackOverflowJob |
||
148 | ) : StackOverflowJob { |
||
149 | if ( $currentDeepNodeAttributes != null && sizeof( $currentDeepNodeAttributes ) == 2 ) { |
||
150 | $attr1 = $currentDeepNodeAttributes[0]; // DOMAttr |
||
151 | $attr2 = $currentDeepNodeAttributes[1]; // DOMAttr |
||
152 | |||
153 | $stackOverflowJob->setLogo( $attr1->name == "src" ? $attr1->value : $attr2->value ); |
||
154 | } |
||
155 | |||
156 | return $stackOverflowJob; |
||
157 | } |
||
158 | |||
159 | private function processDivBlock ( |
||
172 | } |
||
173 | |||
174 | private function refineCompanyName ( string $companyName ) : string |
||
175 | { |
||
176 | if ( strpos( $companyName, "\r\n" ) !== false ) { |
||
177 | $companyNameParts = explode( "\r\n", $companyName ); |
||
178 | |||
179 | foreach ( $companyNameParts as $index => $currentCompanyNamePart ) { |
||
180 | $companyNameParts[$index] = trim( $currentCompanyNamePart ); |
||
181 | } |
||
182 | |||
183 | return implode( " ", $companyNameParts ); |
||
184 | } else { |
||
185 | return $companyName; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | private function setCompanyAndLocation ( |
||
202 | } |
||
203 | |||
204 | private function processH2AndH3Elements ( |
||
226 | } |
||
227 | } |
||
228 |