Skip to content

Commit fc8fc47

Browse files
authored
Apply PHPCBF (#158)
A part of WordPress/php-toolkit#157. This PR applies PHPCBF to project classes and fixes some formatting issues and Yoda conditions.
1 parent ff2b28c commit fc8fc47

13 files changed

Lines changed: 892 additions & 946 deletions

‎class-wphtmlactiveformattingelements.php‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ class WP_HTML_Active_Formatting_Elements {
4646
/**
4747
* Reports if a specific node is in the stack of active formatting elements.
4848
*
49-
* @param WP_HTML_Token $token Look for this node in the stack.
49+
* @param WP_HTML_Token $token Look for this node in the stack.
5050
*
5151
* @return bool Whether the referenced node is in the stack of active formatting elements.
5252
* @since 6.4.0
53-
*
5453
*/
5554
public function contains_node( WP_HTML_Token $token ) {
5655
foreach ( $this->walk_up() as $item ) {
@@ -67,7 +66,6 @@ public function contains_node( WP_HTML_Token $token ) {
6766
*
6867
* @return int How many node are in the stack of active formatting elements.
6968
* @since 6.4.0
70-
*
7169
*/
7270
public function count() {
7371
return count( $this->stack );
@@ -79,7 +77,6 @@ public function count() {
7977
*
8078
* @return WP_HTML_Token|null Last node in the stack of active formatting elements, if one exists, otherwise null.
8179
* @since 6.4.0
82-
*
8380
*/
8481
public function current_node() {
8582
$current_node = end( $this->stack );
@@ -106,12 +103,11 @@ public function insert_marker(): void {
106103
/**
107104
* Pushes a node onto the stack of active formatting elements.
108105
*
109-
* @param WP_HTML_Token $token Push this node onto the stack.
106+
* @param WP_HTML_Token $token Push this node onto the stack.
110107
*
111108
* @see https://html.spec.whatwg.org/#push-onto-the-list-of-active-formatting-elements
112109
*
113110
* @since 6.4.0
114-
*
115111
*/
116112
public function push( WP_HTML_Token $token ) {
117113
/*
@@ -132,11 +128,10 @@ public function push( WP_HTML_Token $token ) {
132128
/**
133129
* Removes a node from the stack of active formatting elements.
134130
*
135-
* @param WP_HTML_Token $token Remove this node from the stack, if it's there already.
131+
* @param WP_HTML_Token $token Remove this node from the stack, if it's there already.
136132
*
137133
* @return bool Whether the node was found and removed from the stack of active formatting elements.
138134
* @since 6.4.0
139-
*
140135
*/
141136
public function remove_node( WP_HTML_Token $token ) {
142137
foreach ( $this->walk_up() as $position_from_end => $item ) {
@@ -175,7 +170,7 @@ public function remove_node( WP_HTML_Token $token ) {
175170
public function walk_down() {
176171
$count = count( $this->stack );
177172

178-
for ( $i = 0; $i < $count; $i ++ ) {
173+
for ( $i = 0; $i < $count; $i++ ) {
179174
yield $this->stack[ $i ];
180175
}
181176
}
@@ -200,7 +195,7 @@ public function walk_down() {
200195
* @since 6.4.0
201196
*/
202197
public function walk_up() {
203-
for ( $i = count( $this->stack ) - 1; $i >= 0; $i -- ) {
198+
for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) {
204199
yield $this->stack[ $i ];
205200
}
206201
}
@@ -225,7 +220,7 @@ public function walk_up() {
225220
public function clear_up_to_last_marker(): void {
226221
foreach ( $this->walk_up() as $item ) {
227222
array_pop( $this->stack );
228-
if ( 'marker' === $item->node_name ) {
223+
if ( $item->node_name === 'marker' ) {
229224
break;
230225
}
231226
}

‎class-wphtmlattributetoken.php‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,15 @@ class WP_HTML_Attribute_Token {
9595
/**
9696
* Constructor.
9797
*
98-
* @param string $name Attribute name.
99-
* @param int $value_start Attribute value.
100-
* @param int $value_length Number of bytes attribute value spans.
101-
* @param int $start The string offset where the attribute name starts.
102-
* @param int $length Byte length of the entire attribute name or name and value pair expression.
103-
* @param bool $is_true Whether the attribute is a boolean attribute with true value.
98+
* @param string $name Attribute name.
99+
* @param int $value_start Attribute value.
100+
* @param int $value_length Number of bytes attribute value spans.
101+
* @param int $start The string offset where the attribute name starts.
102+
* @param int $length Byte length of the entire attribute name or name and value pair expression.
103+
* @param bool $is_true Whether the attribute is a boolean attribute with true value.
104104
*
105105
* @since 6.2.0
106106
* @since 6.5.0 Replaced `end` with `length` to more closely match `substr()`.
107-
*
108107
*/
109108
public function __construct( $name, $value_start, $value_length, $start, $length, $is_true ) {
110109
$this->name = $name;

‎class-wphtmldecoder.php‎

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ class WP_HTML_Decoder {
2323
* true === WP_HTML_Decoder::attribute_starts_with( $value, 'http:', 'ascii-case-insensitive' );
2424
* false === WP_HTML_Decoder::attribute_starts_with( $value, 'https:', 'ascii-case-insensitive' );
2525
*
26-
* @param string $haystack String containing the raw non-decoded attribute value.
27-
* @param string $search_text Does the attribute value start with this plain string.
28-
* @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching.
29-
* Default 'case-sensitive'.
26+
* @param string $haystack String containing the raw non-decoded attribute value.
27+
* @param string $search_text Does the attribute value start with this plain string.
28+
* @param string $case_sensitivity Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching.
29+
* Default 'case-sensitive'.
3030
*
3131
* @return bool Whether the attribute value starts with the given string.
3232
* @since 6.6.0
33-
*
3433
*/
3534
public static function attribute_starts_with( $haystack, $search_text, $case_sensitivity = 'case-sensitive' ): bool {
3635
$search_length = strlen( $search_text );
37-
$loose_case = 'ascii-case-insensitive' === $case_sensitivity;
36+
$loose_case = $case_sensitivity === 'ascii-case-insensitive';
3837
$haystack_end = strlen( $haystack );
3938
$search_at = 0;
4039
$haystack_at = 0;
@@ -44,25 +43,25 @@ public static function attribute_starts_with( $haystack, $search_text, $case_sen
4443
? strtolower( $haystack[ $haystack_at ] ) === strtolower( $search_text[ $search_at ] )
4544
: $haystack[ $haystack_at ] === $search_text[ $search_at ];
4645

47-
$is_introducer = '&' === $haystack[ $haystack_at ];
46+
$is_introducer = $haystack[ $haystack_at ] === '&';
4847
$next_chunk = $is_introducer
4948
? self::read_character_reference( 'attribute', $haystack, $haystack_at, $token_length )
5049
: null;
5150

5251
// If there's no character reference and the characters don't match, the match fails.
53-
if ( null === $next_chunk && ! $chars_match ) {
52+
if ( $next_chunk === null && ! $chars_match ) {
5453
return false;
5554
}
5655

5756
// If there's no character reference but the character do match, then it could still match.
58-
if ( null === $next_chunk && $chars_match ) {
59-
++ $haystack_at;
60-
++ $search_at;
57+
if ( $next_chunk === null && $chars_match ) {
58+
++$haystack_at;
59+
++$search_at;
6160
continue;
6261
}
6362

6463
// If there is a character reference, then the decoded value must exactly match what follows in the search string.
65-
if ( 0 !== substr_compare( $search_text, $next_chunk, $search_at, strlen( $next_chunk ), $loose_case ) ) {
64+
if ( substr_compare( $search_text, $next_chunk, $search_at, strlen( $next_chunk ), $loose_case ) !== 0 ) {
6665
return false;
6766
}
6867

@@ -86,11 +85,10 @@ public static function attribute_starts_with( $haystack, $search_text, $case_sen
8685
*
8786
* '“😄”' === WP_HTML_Decode::decode_text_node( '&#x93;&#x1f604;&#x94' );
8887
*
89-
* @param string $text Text containing raw and non-decoded text node to decode.
88+
* @param string $text Text containing raw and non-decoded text node to decode.
9089
*
9190
* @return string Decoded UTF-8 value of given text node.
9291
* @since 6.6.0
93-
*
9492
*/
9593
public static function decode_text_node( $text ): string {
9694
return static::decode( 'data', $text );
@@ -107,11 +105,10 @@ public static function decode_text_node( $text ): string {
107105
*
108106
* '“😄”' === WP_HTML_Decode::decode_attribute( '&#x93;&#x1f604;&#x94' );
109107
*
110-
* @param string $text Text containing raw and non-decoded attribute value to decode.
108+
* @param string $text Text containing raw and non-decoded attribute value to decode.
111109
*
112110
* @return string Decoded UTF-8 value of given attribute value.
113111
* @since 6.6.0
114-
*
115112
*/
116113
public static function decode_attribute( $text ): string {
117114
return static::decode( 'attribute', $text );
@@ -128,14 +125,13 @@ public static function decode_attribute( $text ): string {
128125
*
129126
* '©' = WP_HTML_Decoder::decode( 'data', '&copy;' );
130127
*
131-
* @param string $context `attribute` for decoding attribute values, `data` otherwise.
132-
* @param string $text Text document containing span of text to decode.
128+
* @param string $context `attribute` for decoding attribute values, `data` otherwise.
129+
* @param string $text Text document containing span of text to decode.
133130
*
134131
* @return string Decoded UTF-8 string.
135132
* @since 6.6.0
136133
*
137134
* @access private
138-
*
139135
*/
140136
public static function decode( $context, $text ): string {
141137
$decoded = '';
@@ -145,24 +141,24 @@ public static function decode( $context, $text ): string {
145141

146142
while ( $at < $end ) {
147143
$next_character_reference_at = strpos( $text, '&', $at );
148-
if ( false === $next_character_reference_at ) {
144+
if ( $next_character_reference_at === false ) {
149145
break;
150146
}
151147

152148
$character_reference = self::read_character_reference( $context, $text, $next_character_reference_at, $token_length );
153149
if ( isset( $character_reference ) ) {
154-
$at = $next_character_reference_at;
150+
$at = $next_character_reference_at;
155151
$decoded .= substr( $text, $was_at, $at - $was_at );
156152
$decoded .= $character_reference;
157153
$at += $token_length;
158-
$was_at = $at;
154+
$was_at = $at;
159155
continue;
160156
}
161157

162-
++ $at;
158+
++$at;
163159
}
164160

165-
if ( 0 === $was_at ) {
161+
if ( $was_at === 0 ) {
166162
return $text;
167163
}
168164

@@ -198,17 +194,16 @@ public static function decode( $context, $text ): string {
198194
* '∉' === WP_HTML_Decoder::read_character_reference( 'data', '&notin;', 0, $token_length );
199195
* 7 === $token_length; // `&notin;`
200196
*
201-
* @param string $context `attribute` for decoding attribute values, `data` otherwise.
202-
* @param string $text Text document containing span of text to decode.
203-
* @param int $at Optional. Byte offset into text where span begins, defaults to the beginning (0).
197+
* @param string $context `attribute` for decoding attribute values, `data` otherwise.
198+
* @param string $text Text document containing span of text to decode.
199+
* @param int $at Optional. Byte offset into text where span begins, defaults to the beginning (0).
204200
* @param int &$match_byte_length Optional. Set to byte-length of character reference if provided and if a match
205201
* is found, otherwise not set. Default null.
206202
*
207203
* @return string|false Decoded character reference in UTF-8 if found, otherwise `false`.
208204
* @global WP_Token_Map $html5_named_character_references Mappings for HTML5 named character references.
209205
*
210206
* @since 6.6.0
211-
*
212207
*/
213208
public static function read_character_reference( $context, $text, $at = 0, &$match_byte_length = null ) {
214209
/**
@@ -223,7 +218,7 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
223218
return null;
224219
}
225220

226-
if ( '&' !== $text[ $at ] ) {
221+
if ( $text[ $at ] !== '&' ) {
227222
return null;
228223
}
229224

@@ -237,19 +232,19 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
237232
* - fail to parse and return plaintext `&#x1f1`.
238233
* - fail to parse and return the replacement character `�`
239234
*/
240-
if ( '#' === $text[ $at + 1 ] ) {
235+
if ( $text[ $at + 1 ] === '#' ) {
241236
if ( $at + 2 >= $length ) {
242237
return null;
243238
}
244239

245240
/** Tracks inner parsing within the numeric character reference. */
246241
$digits_at = $at + 2;
247242

248-
if ( 'x' === $text[ $digits_at ] || 'X' === $text[ $digits_at ] ) {
243+
if ( $text[ $digits_at ] === 'x' || $text[ $digits_at ] === 'X' ) {
249244
$numeric_base = 16;
250245
$numeric_digits = '0123456789abcdefABCDEF';
251246
$max_digits = 6; // &#x10FFFF;
252-
++ $digits_at;
247+
++$digits_at;
253248
} else {
254249
$numeric_base = 10;
255250
$numeric_digits = '0123456789';
@@ -260,16 +255,16 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
260255
$zero_count = strspn( $text, '0', $digits_at );
261256
$digit_count = strspn( $text, $numeric_digits, $digits_at + $zero_count );
262257
$after_digits = $digits_at + $zero_count + $digit_count;
263-
$has_semicolon = $after_digits < $length && ';' === $text[ $after_digits ];
258+
$has_semicolon = $after_digits < $length && $text[ $after_digits ] === ';';
264259
$end_of_span = $has_semicolon ? $after_digits + 1 : $after_digits;
265260

266261
// `&#` or `&#x` without digits returns into plaintext.
267-
if ( 0 === $digit_count && 0 === $zero_count ) {
262+
if ( $digit_count === 0 && $zero_count === 0 ) {
268263
return null;
269264
}
270265

271266
// Whereas `&#` and only zeros is invalid.
272-
if ( 0 === $digit_count ) {
267+
if ( $digit_count === 0 ) {
273268
$match_byte_length = $end_of_span - $at;
274269

275270
return '�';
@@ -282,7 +277,7 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
282277
return '�';
283278
}
284279

285-
$digits = substr( $text, $digits_at + $zero_count, $digit_count );
280+
$digits = substr( $text, $digits_at + $zero_count, $digit_count );
286281
$codepoint = intval( $digits, $numeric_base );
287282

288283
/*
@@ -369,14 +364,14 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
369364

370365
$name_length = 0;
371366
$replacement = $html5_named_character_references->read_token( $text, $name_at, $name_length );
372-
if ( false === $replacement ) {
367+
if ( $replacement === false ) {
373368
return null;
374369
}
375370

376371
$after_name = $name_at + $name_length;
377372

378373
// If the match ended with a semicolon then it should always be decoded.
379-
if ( ';' === $text[ $name_at + $name_length - 1 ] ) {
374+
if ( $text[ $name_at + $name_length - 1 ] === ';' ) {
380375
$match_byte_length = $after_name - $at;
381376

382377
return $replacement;
@@ -392,7 +387,7 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
392387
$name_at < $length &&
393388
(
394389
ctype_alnum( $text[ $after_name ] ) ||
395-
'=' === $text[ $after_name ]
390+
$text[ $after_name ] === '='
396391
)
397392
);
398393

@@ -404,7 +399,7 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
404399
}
405400

406401
// It's ambiguous, which isn't allowed inside attributes.
407-
if ( 'attribute' === $context ) {
402+
if ( $context === 'attribute' ) {
408403
return null;
409404
}
410405

@@ -427,13 +422,12 @@ public static function read_character_reference( $context, $text, $at = 0, &$mat
427422
* // Half of a surrogate pair is an invalid code point.
428423
* '�' === WP_HTML_Decoder::codepoint_to_utf8_bytes( 0xd83c );
429424
*
430-
* @param int $codepoint Which code point to convert.
425+
* @param int $codepoint Which code point to convert.
431426
*
432427
* @return string Converted code point, or `�` if invalid.
433428
* @since 6.6.0
434429
*
435430
* @see https://www.rfc-editor.org/rfc/rfc3629 For the UTF-8 standard.
436-
*
437431
*/
438432
public static function codepoint_to_utf8_bytes( $codepoint ): string {
439433
// Pre-check to ensure a valid code point.

0 commit comments

Comments
 (0)