Conditions | 19 |
Paths | 630 |
Total Lines | 68 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
146 | private function parseField( DOMDocument $html, $field ){ |
||
147 | $this->owner->extend( 'beforeParseField', $html, $field ); |
||
148 | |||
149 | // Remove Tags from Content we wown't be using |
||
150 | $excluded = array(); |
||
151 | foreach( $this->excludeTags as $eTag ){ |
||
152 | while( $tags = $html->getElementsByTagName( $eTag ) ){ |
||
153 | if( !$tags->length ) break 1; |
||
154 | $tag = $tags->item(0); |
||
155 | $value = $html->saveHTML( $tag ); |
||
156 | $key = (string) crc32( $value ); |
||
157 | |||
158 | // Convert back children nodes of this node if they were already hashed |
||
159 | $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
||
160 | |||
161 | $tag->parentNode->replaceChild( $html->createTextNode( $key ), $tag ); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | $body = (string)$html->saveHTML( $html->getElementsByTagName('body')->item(0) ); |
||
166 | $content = preg_replace( array( '/\<body\>/is', '/\<\/body\>/is' ), '', $body, 1 ); |
||
167 | |||
168 | // Create the links |
||
169 | $links = AutomatedLink::get()->sort('Priority'); |
||
170 | foreach( $links as $link ){ |
||
171 | // Check if self-linking is allowed and if current pagetype is allowed |
||
172 | if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
||
173 | |||
174 | $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
||
175 | $escape = (string) preg_quote( $link->Phrase, '/' ); |
||
176 | $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
||
177 | |||
178 | // Count the matches |
||
179 | preg_match_all( $regex, $content, $count ); |
||
180 | $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
||
181 | if( $count < 1 ) continue; |
||
182 | |||
183 | if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
||
184 | $max -= $this->maxLinksPerPage[ $link->ID ]; |
||
185 | else |
||
186 | $this->maxLinksPerPage[ $link->ID ] = 0; |
||
187 | |||
188 | for( $x = 0; $x < $count; $x++ ){ |
||
189 | // Stop adding links if we reached the link or page limit |
||
190 | if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
||
191 | |||
192 | // Check if there is anything else to replace else stop |
||
193 | preg_match( $regex, $content, $match ); |
||
194 | if( !is_array( $match ) || !count( $match ) ) break; |
||
195 | |||
196 | if( !$html = (string) $link->getHTML( $match[0] ) ) continue; |
||
197 | $key = (string) crc32( $html ); |
||
198 | $excluded[ $key ] = (string) $html; |
||
199 | |||
200 | $content = preg_replace( $regex, $key, $content, 1 ); |
||
201 | $this->linkCount++; |
||
202 | $this->maxLinksPerPage[ $link->ID ]++; |
||
203 | } |
||
204 | |||
205 | // Stop Adding links if we reached the page limit |
||
206 | if( $this->linkCount >= $this->maxLinks ) break; |
||
207 | } |
||
208 | |||
209 | // Re-add the excluded Tags |
||
210 | $content = str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
||
211 | |||
212 | return $content; |
||
213 | } |
||
214 | } |
||
215 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.