Total Complexity | 50 |
Total Lines | 218 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | 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 | private function processChildNodes ( DOMElement $currentChildrenNode ) : void |
||
83 | } |
||
84 | } |
||
85 | |||
86 | private function setBasicAttributes ( |
||
87 | DOMNamedNodeMap $attributes, |
||
88 | StackOverflowJob $stackOverflowJob |
||
89 | ) : StackOverflowJob { |
||
90 | if ( $attributes != null ) { |
||
91 | foreach ( $attributes as $currentAttribute ) { |
||
92 | if ( $currentAttribute->name == "data-jobid" ) { |
||
93 | $stackOverflowJob->setJobId( $currentAttribute->value ); |
||
94 | } |
||
95 | |||
96 | if ( $currentAttribute->name == "data-result-id" ) { |
||
97 | $stackOverflowJob->setResultId( $currentAttribute->value ); |
||
98 | } |
||
99 | |||
100 | if ( $currentAttribute->name == "data-preview-url" ) { |
||
101 | $stackOverflowJob->setPreviewUrl( $currentAttribute->value ); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | return $stackOverflowJob; |
||
107 | } |
||
108 | |||
109 | private function processExtraChildNodes ( |
||
110 | DOMElement $currentExtraChildNode, |
||
111 | StackOverflowJob $stackOverflowJob |
||
112 | ) : StackOverflowJob { |
||
113 | if ( $currentExtraChildNode->hasChildNodes() ) { |
||
114 | $currentExtraChildNodeChildren = $currentExtraChildNode->childNodes; // DOMNodeList |
||
115 | |||
116 | $nodeCount = $currentExtraChildNodeChildren->count(); |
||
117 | |||
118 | if ( $nodeCount >= 6 ) { |
||
119 | for ( $i = 0; $i < $nodeCount; $i++ ) { |
||
120 | $currentDeepNode = $currentExtraChildNodeChildren->item( $i ); // DOMElement or DOMText |
||
121 | |||
122 | if ( $currentDeepNode instanceof DOMElement ) { |
||
123 | $stackOverflowJob = $this->processImageDivBlocks( $currentDeepNode, $stackOverflowJob ); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | return $stackOverflowJob; |
||
130 | } |
||
131 | |||
132 | private function processImageDivBlocks ( |
||
133 | DOMElement $currentDeepNode, |
||
134 | StackOverflowJob $stackOverflowJob |
||
135 | ) : StackOverflowJob { |
||
136 | if ( $currentDeepNode->childNodes->count() == 2 ) { |
||
137 | $stackOverflowJob = $this->processImageBlock( $currentDeepNode->childNodes->item( 1 )->attributes, |
||
138 | $stackOverflowJob ); |
||
139 | } |
||
140 | |||
141 | if ( $currentDeepNode->tagName == "div" && $currentDeepNode->hasChildNodes() ) { |
||
142 | $stackOverflowJob = $this->processDivBlock( $currentDeepNode->childNodes, $stackOverflowJob ); |
||
143 | } |
||
144 | |||
145 | return $stackOverflowJob; |
||
146 | } |
||
147 | |||
148 | private function processImageBlock ( |
||
149 | DOMNamedNodeMap $currentDeepNodeAttributes, |
||
150 | StackOverflowJob $stackOverflowJob |
||
151 | ) : StackOverflowJob { |
||
152 | if ( $currentDeepNodeAttributes != null && sizeof( $currentDeepNodeAttributes ) == 2 ) { |
||
153 | $attr1 = $currentDeepNodeAttributes[0]; // DOMAttr |
||
154 | $attr2 = $currentDeepNodeAttributes[1]; // DOMAttr |
||
155 | |||
156 | if ( $attr1->name == "src" ) { |
||
157 | $stackOverflowJob->setLogo( $attr1->value ); |
||
158 | } |
||
159 | |||
160 | if ( $attr2->name == "src" ) { |
||
161 | $stackOverflowJob->setLogo( $attr2->value ); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $stackOverflowJob; |
||
166 | } |
||
167 | |||
168 | private function processDivBlock ( |
||
181 | } |
||
182 | |||
183 | private function refineCompanyName ( string $companyName ) : string |
||
184 | { |
||
185 | if ( strpos( $companyName, "\r\n" ) !== false ) { |
||
186 | $companyNameParts = explode( "\r\n", $companyName ); |
||
187 | |||
188 | foreach ( $companyNameParts as $index => $currentCompanyNamePart ) { |
||
189 | $companyNameParts[$index] = trim( $currentCompanyNamePart ); |
||
190 | } |
||
191 | |||
192 | return implode( " ", $companyNameParts ); |
||
193 | } else { |
||
194 | return $companyName; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | private function setCompanyAndLocation ( |
||
211 | } |
||
212 | |||
213 | private function processH2AndH3Elements ( |
||
234 | } |
||
235 | } |
||
236 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.