We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 5 |
Paths | 5 |
Total Lines | 129 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Tests | 64 |
CRAP Score | 5 |
Changes | 5 | ||
Bugs | 0 | Features | 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 |
||
96 | 52 | public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
|
97 | 52 | if ( empty( $settings[ Settings::SMART_QUOTES ] ) ) { |
|
98 | 16 | return; |
|
99 | } |
||
100 | |||
101 | // Need to get context of adjacent characters outside adjacent inline tags or HTML comment |
||
102 | // if we have adjacent characters add them to the text. |
||
103 | 36 | $previous_character = DOM::get_prev_chr( $textnode ); |
|
104 | 36 | $next_character = DOM::get_next_chr( $textnode ); |
|
105 | 36 | $node_data = "{$previous_character}{$textnode->data}{$next_character}"; |
|
106 | 36 | $f = Strings::functions( $node_data ); |
|
107 | |||
108 | // Various special characters and regular expressions. |
||
109 | 36 | $double = $settings->primary_quote_style(); |
|
110 | 36 | $single = $settings->secondary_quote_style(); |
|
111 | |||
112 | // Mark quotes to ensure proper removal of replaced adjacent characters. |
||
113 | 36 | $double_open = RE::ESCAPE_MARKER . $double->open() . RE::ESCAPE_MARKER; |
|
114 | 36 | $double_close = RE::ESCAPE_MARKER . $double->close() . RE::ESCAPE_MARKER; |
|
115 | 36 | $single_open = RE::ESCAPE_MARKER . $single->open() . RE::ESCAPE_MARKER; |
|
116 | 36 | $single_close = RE::ESCAPE_MARKER . $single->close() . RE::ESCAPE_MARKER; |
|
117 | |||
118 | 36 | if ( $double != $this->cached_primary_quotes || $single != $this->cached_secondary_quotes ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- object value comparison. |
|
119 | 36 | $this->update_smart_quotes_brackets( $double_open, $double_close, $single_open, $single_close ); |
|
120 | 36 | $this->cached_primary_quotes = $double; |
|
121 | 36 | $this->cached_secondary_quotes = $single; |
|
122 | } |
||
123 | |||
124 | // Handle excpetions first. |
||
125 | 36 | if ( ! empty( $settings[ Settings::SMART_QUOTES_EXCEPTIONS ] ) ) { |
|
126 | 36 | $node_data = \str_replace( $settings[ Settings::SMART_QUOTES_EXCEPTIONS ]['patterns'], $settings[ Settings::SMART_QUOTES_EXCEPTIONS ]['replacements'], $node_data ); |
|
127 | } |
||
128 | |||
129 | // Before primes, handle quoted numbers (and quotes ending in numbers). |
||
130 | 36 | $node_data = \preg_replace( |
|
131 | [ |
||
132 | 36 | self::SINGLE_QUOTED_NUMBERS . $f['u'], |
|
133 | 36 | self::DOUBLE_QUOTED_NUMBERS . $f['u'], |
|
134 | ], |
||
135 | [ |
||
136 | 36 | "{$single_open}\$1{$single_close}", |
|
137 | 36 | "{$double_open}\$1{$double_close}", |
|
138 | ], |
||
139 | 36 | $node_data |
|
140 | ); |
||
141 | |||
142 | // Guillemets. |
||
143 | 36 | $node_data = \str_replace( [ '<<', '>>' ], [ U::GUILLEMET_OPEN, U::GUILLEMET_CLOSE ], $node_data ); |
|
144 | |||
145 | // Primes. |
||
146 | 36 | $node_data = \preg_replace( |
|
147 | [ |
||
148 | 36 | self::SINGLE_DOUBLE_PRIME . $f['u'], |
|
149 | 36 | self::DOUBLE_PRIME . $f['u'], // should not interfere with regular quote matching. |
|
150 | 36 | self::SINGLE_PRIME . $f['u'], |
|
151 | ], |
||
152 | [ |
||
153 | '$1' . U::SINGLE_PRIME . '$2$3' . U::DOUBLE_PRIME, // @codeCoverageIgnoreStart |
||
154 | '$1' . U::DOUBLE_PRIME, |
||
155 | '$1' . U::SINGLE_PRIME, // @codeCoverageIgnoreEnd |
||
156 | ], |
||
157 | 36 | $node_data |
|
158 | ); |
||
159 | |||
160 | // Backticks & comma quotes. |
||
161 | 36 | $node_data = \str_replace( |
|
162 | 36 | [ '``', '`', "''", ',,' ], |
|
163 | 36 | [ $double_open, $single_open, $double_close, U::DOUBLE_LOW_9_QUOTE ], |
|
164 | 36 | $node_data |
|
165 | ); |
||
166 | 36 | $node_data = \preg_replace( self::COMMA_QUOTE . $f['u'], U::SINGLE_LOW_9_QUOTE, $node_data ); // like _,¿hola?'_. |
|
167 | |||
168 | // Apostrophes. |
||
169 | 36 | $node_data = \preg_replace( |
|
170 | 36 | [ self::APOSTROPHE_WORDS . $f['u'], self::APOSTROPHE_DECADES . $f['u'] ], |
|
171 | 36 | [ U::APOSTROPHE, U::APOSTROPHE . '$1' ], |
|
172 | 36 | $node_data |
|
173 | ); |
||
174 | |||
175 | // Quotes. |
||
176 | 36 | $node_data = \str_replace( $this->brackets_matches, $this->brackets_replacements, $node_data ); |
|
177 | 36 | $node_data = \preg_replace( |
|
178 | [ |
||
179 | 36 | self::SINGLE_QUOTE_OPEN . $f['u'], |
|
180 | 36 | self::SINGLE_QUOTE_CLOSE . $f['u'], |
|
181 | 36 | self::DOUBLE_QUOTE_OPEN . $f['u'], |
|
182 | 36 | self::DOUBLE_QUOTE_CLOSE . $f['u'], |
|
183 | ], |
||
184 | [ |
||
185 | 36 | $single_open, |
|
186 | 36 | $single_close, |
|
187 | 36 | $double_open, |
|
188 | 36 | $double_close, |
|
189 | ], |
||
190 | 36 | $node_data |
|
191 | ); |
||
192 | |||
193 | // Quote catch-alls - assume left over quotes are closing - as this is often the most complicated position, thus most likely to be missed. |
||
194 | 36 | $node_data = \str_replace( [ "'", '"' ], [ $single_close, $double_close ], $node_data ); |
|
195 | |||
196 | // Add a thin non-breaking space between secondary and primary quotes. |
||
197 | 36 | $node_data = \str_replace( |
|
198 | 36 | [ "{$double_open}{$single_open}", "{$single_close}{$double_close}" ], |
|
199 | 36 | [ $double_open . U::NO_BREAK_NARROW_SPACE . $single_open, $single_close . U::NO_BREAK_NARROW_SPACE . $double_close ], |
|
200 | 36 | $node_data |
|
201 | ); |
||
202 | |||
203 | // Check if adjacent characters where replaced with multi-byte replacements. |
||
204 | $quotes = [ |
||
205 | 36 | $double_open, |
|
206 | 36 | $double_close, |
|
207 | 36 | $single_open, |
|
208 | 36 | $single_close, |
|
209 | U::GUILLEMET_OPEN, // @codeCoverageIgnoreStart |
||
210 | U::GUILLEMET_CLOSE, |
||
211 | U::DOUBLE_PRIME, |
||
212 | U::SINGLE_PRIME, |
||
213 | U::APOSTROPHE, |
||
214 | U::DOUBLE_LOW_9_QUOTE, |
||
215 | U::SINGLE_LOW_9_QUOTE, // @codeCoverageIgnoreEnd |
||
216 | ]; |
||
217 | 36 | $previous_length = self::calc_adjacent_length( $f['strlen']( $previous_character ), $previous_character, $node_data, $quotes, $f['substr'], $f['strlen'], false ); |
|
218 | 36 | $next_length = self::calc_adjacent_length( $f['strlen']( $next_character ), $next_character, $node_data, $quotes, $f['substr'], $f['strlen'], true ); |
|
219 | |||
220 | // If we have adjacent characters, remove them from the text. |
||
221 | 36 | $node_data = self::remove_adjacent_characters( $node_data, $f['strlen'], $f['substr'], $previous_length, $next_length ); |
|
222 | |||
223 | // Remove the escape markers and restore the text to the actual node. |
||
224 | 36 | $textnode->data = \str_replace( RE::ESCAPE_MARKER, '', $node_data ); |
|
225 | 36 | } |
|
289 |