| 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 |
||
| 132 | private function parseField( DOMDocument $html, $field ){ |
||
| 133 | $this->owner->extend( 'beforeParseField', $html, $field ); |
||
| 134 | |||
| 135 | // Remove Tags from Content we wown't be using |
||
| 136 | $excluded = array(); |
||
| 137 | foreach( $this->excludeTags as $eTag ){ |
||
| 138 | while( $tags = $html->getElementsByTagName( $eTag ) ){ |
||
| 139 | if( !$tags->length ) break 1; |
||
| 140 | $tag = $tags->item(0); |
||
| 141 | $value = $html->saveHTML( $tag ); |
||
| 142 | $key = (string) crc32( $value ); |
||
| 143 | |||
| 144 | // Convert back children nodes of this node if they were already hashed |
||
| 145 | $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
||
| 146 | |||
| 147 | $tag->parentNode->replaceChild( $html->createTextNode( $key ), $tag ); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $body = (string)$html->saveHTML( $html->getElementsByTagName('body')->item(0) ); |
||
| 152 | $content = preg_replace( array( '/\<body\>/is', '/\<\/body\>/is' ), '', $body, 1 ); |
||
| 153 | |||
| 154 | // Create the links |
||
| 155 | $links = AutomatedLink::get()->sort('Priority'); |
||
| 156 | foreach( $links as $link ){ |
||
| 157 | // Check if self-linking is allowed and if current pagetype is allowed |
||
| 158 | if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
||
| 159 | |||
| 160 | $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
||
| 161 | $escape = (string) preg_quote( $link->Phrase, '/' ); |
||
| 162 | $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
||
| 163 | |||
| 164 | // Count the matches |
||
| 165 | preg_match_all( $regex, $content, $count ); |
||
| 166 | $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
||
| 167 | if( $count < 1 ) continue; |
||
| 168 | |||
| 169 | if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
||
| 170 | $max -= $this->maxLinksPerPage[ $link->ID ]; |
||
| 171 | else |
||
| 172 | $this->maxLinksPerPage[ $link->ID ] = 0; |
||
| 173 | |||
| 174 | for( $x = 0; $x < $count; $x++ ){ |
||
| 175 | // Stop adding links if we reached the link or page limit |
||
| 176 | if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
||
| 177 | |||
| 178 | // Check if there is anything else to replace else stop |
||
| 179 | preg_match( $regex, $content, $match ); |
||
| 180 | if( !is_array( $match ) || !count( $match ) ) break; |
||
| 181 | |||
| 182 | if( !$html = (string) $link->getHTML( $match[0] ) ) continue; |
||
| 183 | $key = (string) crc32( $html ); |
||
| 184 | $excluded[ $key ] = (string) $html; |
||
| 185 | |||
| 186 | $content = preg_replace( $regex, $key, $content, 1 ); |
||
| 187 | $this->linkCount++; |
||
| 188 | $this->maxLinksPerPage[ $link->ID ]++; |
||
| 189 | } |
||
| 190 | |||
| 191 | // Stop Adding links if we reached the page limit |
||
| 192 | if( $this->linkCount >= $this->maxLinks ) break; |
||
| 193 | } |
||
| 194 | |||
| 195 | // Re-add the excluded Tags |
||
| 196 | $content = str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
||
| 197 | |||
| 198 | return $content; |
||
| 199 | } |
||
| 200 | } |
||
| 201 |