We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 8 |
| Paths | 9 |
| Total Lines | 100 |
| Code Lines | 62 |
| Lines | 6 |
| Ratio | 6 % |
| 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 |
||
| 145 | public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
||
| 146 | if ( empty( $settings['smartQuotes'] ) ) { |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | |||
| 150 | // Clone the node's data attribute for the duration. |
||
| 151 | $node_data = $textnode->data; |
||
| 152 | |||
| 153 | // Need to get context of adjacent characters outside adjacent inline tags or HTML comment |
||
| 154 | // if we have adjacent characters add them to the text. |
||
| 155 | $previous_character = DOM::get_prev_chr( $textnode ); |
||
| 156 | $next_character = DOM::get_next_chr( $textnode ); |
||
| 157 | $node_data = "{$previous_character}{$node_data}{$next_character}"; |
||
| 158 | |||
| 159 | // Various special characters and regular expressions. |
||
| 160 | $double = $settings->primary_quote_style(); |
||
| 161 | $single = $settings->secondary_quote_style(); |
||
| 162 | |||
| 163 | // Mark quotes to ensure proper removal of replaced adjacent characters. |
||
| 164 | $double_open = RE::ESCAPE_MARKER . $double->open() . RE::ESCAPE_MARKER; |
||
| 165 | $double_close = RE::ESCAPE_MARKER . $double->close() . RE::ESCAPE_MARKER; |
||
| 166 | $single_open = RE::ESCAPE_MARKER . $single->open() . RE::ESCAPE_MARKER; |
||
| 167 | $single_close = RE::ESCAPE_MARKER . $single->close() . RE::ESCAPE_MARKER; |
||
| 168 | |||
| 169 | if ( $double != $this->cached_primary_quotes || $single != $this->cached_secondary_quotes ) { // WPCS: loose comparison ok. |
||
| 170 | $this->update_smart_quotes_brackets( $double_open, $double_close, $single_open, $single_close ); |
||
| 171 | $this->cached_primary_quotes = $double; |
||
| 172 | $this->cached_secondary_quotes = $single; |
||
| 173 | } |
||
| 174 | |||
| 175 | // Before primes, handle quoted numbers (and quotes ending in numbers). |
||
| 176 | $node_data = preg_replace( self::SINGLE_QUOTED_NUMBERS, "{$single_open}\$1{$single_close}", $node_data ); |
||
| 177 | $node_data = preg_replace( self::DOUBLE_QUOTED_NUMBERS, "{$double_open}\$1{$double_close}", $node_data ); |
||
| 178 | |||
| 179 | // Guillemets. |
||
| 180 | $node_data = str_replace( '<<', U::GUILLEMET_OPEN, $node_data ); |
||
| 181 | $node_data = str_replace( '<<', U::GUILLEMET_OPEN, $node_data ); |
||
| 182 | $node_data = str_replace( '>>', U::GUILLEMET_CLOSE, $node_data ); |
||
| 183 | $node_data = str_replace( '>>', U::GUILLEMET_CLOSE, $node_data ); |
||
| 184 | |||
| 185 | // Primes. |
||
| 186 | $node_data = preg_replace( self::SINGLE_DOUBLE_PRIME, '$1' . U::SINGLE_PRIME . '$2$3' . U::DOUBLE_PRIME, $node_data ); |
||
| 187 | $node_data = preg_replace( self::SINGLE_DOUBLE_PRIME_1_GLYPH, '$1' . U::SINGLE_PRIME . '$2$3' . U::DOUBLE_PRIME, $node_data ); |
||
| 188 | $node_data = preg_replace( self::DOUBLE_PRIME, '$1' . U::DOUBLE_PRIME, $node_data ); // should not interfere with regular quote matching. |
||
| 189 | $node_data = preg_replace( self::SINGLE_PRIME, '$1' . U::SINGLE_PRIME, $node_data ); |
||
| 190 | $node_data = preg_replace( self::SINGLE_PRIME_COMPOUND, '$1' . U::SINGLE_PRIME, $node_data ); |
||
| 191 | $node_data = preg_replace( self::DOUBLE_PRIME_COMPOUND, '$1' . U::DOUBLE_PRIME, $node_data ); |
||
| 192 | $node_data = preg_replace( self::DOUBLE_PRIME_1_GLYPH, '$1' . U::DOUBLE_PRIME, $node_data ); // should not interfere with regular quote matching. |
||
| 193 | $node_data = preg_replace( self::DOUBLE_PRIME_1_GLYPH_COMPOUND, '$1' . U::DOUBLE_PRIME, $node_data ); |
||
| 194 | |||
| 195 | // Backticks. |
||
| 196 | $node_data = str_replace( '``', $double_open, $node_data ); |
||
| 197 | $node_data = str_replace( '`', $single_open, $node_data ); |
||
| 198 | $node_data = str_replace( "''", $double_close, $node_data ); |
||
| 199 | |||
| 200 | // Comma quotes. |
||
| 201 | $node_data = str_replace( ',,', U::DOUBLE_LOW_9_QUOTE, $node_data ); |
||
| 202 | $node_data = preg_replace( self::COMMA_QUOTE, U::SINGLE_LOW_9_QUOTE, $node_data ); // like _,¿hola?'_. |
||
| 203 | |||
| 204 | // Apostrophes. |
||
| 205 | $node_data = preg_replace( self::APOSTROPHE_WORDS, U::APOSTROPHE, $node_data ); |
||
| 206 | $node_data = preg_replace( self::APOSTROPHE_DECADES, U::APOSTROPHE . '$1', $node_data ); // decades: '98. |
||
| 207 | $node_data = str_replace( $this->apostrophe_exception_matches, $this->apostrophe_exception_replacements, $node_data ); |
||
| 208 | |||
| 209 | // Quotes. |
||
| 210 | $node_data = str_replace( $this->brackets_matches, $this->brackets_replacements, $node_data ); |
||
| 211 | $node_data = preg_replace( self::SINGLE_QUOTE_OPEN, $single_open, $node_data ); |
||
| 212 | $node_data = preg_replace( self::SINGLE_QUOTE_CLOSE, $single_close, $node_data ); |
||
| 213 | $node_data = preg_replace( self::SINGLE_QUOTE_OPEN_SPECIAL, $single_open, $node_data ); // like _'¿hola?'_. |
||
| 214 | $node_data = preg_replace( self::SINGLE_QUOTE_CLOSE_SPECIAL, $single_close, $node_data ); |
||
| 215 | $node_data = preg_replace( self::DOUBLE_QUOTE_OPEN, $double_open, $node_data ); |
||
| 216 | $node_data = preg_replace( self::DOUBLE_QUOTE_CLOSE, $double_close, $node_data ); |
||
| 217 | $node_data = preg_replace( self::DOUBLE_QUOTE_OPEN_SPECIAL, $double_open, $node_data ); |
||
| 218 | $node_data = preg_replace( self::DOUBLE_QUOTE_CLOSE_SPECIAL, $double_close, $node_data ); |
||
| 219 | |||
| 220 | // Quote catch-alls - assume left over quotes are closing - as this is often the most complicated position, thus most likely to be missed. |
||
| 221 | $node_data = str_replace( "'", $single_close, $node_data ); |
||
| 222 | $node_data = str_replace( '"', $double_close, $node_data ); |
||
| 223 | |||
| 224 | // Check if adjacent characters where replaced with multi-byte replacements. |
||
| 225 | $quotes = [ $double_open, $double_close, $single_open, $single_close ]; |
||
| 226 | $func = Strings::functions( $node_data ); |
||
| 227 | $substr = $func['substr']; |
||
| 228 | $strlen = $func['strlen']; |
||
| 229 | $previous_length = $strlen( $previous_character ); |
||
| 230 | $next_length = $strlen( $next_character ); |
||
| 231 | |||
| 232 | View Code Duplication | if ( $previous_length > 0 && $previous_character !== $substr( $node_data, $previous_length ) ) { |
|
|
|
|||
| 233 | $previous_length = self::calc_adjacent_length( $previous_length, $node_data, $quotes, $substr, $strlen, false ); |
||
| 234 | } |
||
| 235 | View Code Duplication | if ( $next_length > 0 && $next_character !== $substr( $node_data, -$next_length ) ) { |
|
| 236 | $next_length = self::calc_adjacent_length( $next_length, $node_data, $quotes, $substr, $strlen, true ); |
||
| 237 | } |
||
| 238 | |||
| 239 | // If we have adjacent characters, remove them from the text. |
||
| 240 | $node_data = self::remove_adjacent_characters( $node_data, $previous_length, $next_length ); |
||
| 241 | |||
| 242 | // Remove the escape markers and restore the text to the actual node. |
||
| 243 | $textnode->data = str_replace( RE::ESCAPE_MARKER, '', $node_data ); |
||
| 244 | } |
||
| 245 | |||
| 305 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.