Skip to content

Commit 1263e48

Browse files
authored
Block delimiter: update class to be closer to original codebase (#43683)
* Block delimiter: update class to be closer to original codebase Since the class does not rely on WordPress, we can afford to be more permissive, and not necessarily stick to WordPress coding standards. This reverts some of the changes made in #43648. * Add changelog * Update Phan baseline to avoid errors * Update phpcs config to avoid warnings * Ignore more rules to match original file * Update readme to clarify coding standards
1 parent 0ce3fc3 commit 1263e48

File tree

5 files changed

+48
-58
lines changed

5 files changed

+48
-58
lines changed

projects/packages/block-delimiter/.phan/baseline.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
* (can be combined with --load-baseline)
99
*/
1010
return [
11-
// This baseline has no suppressions
11+
// # Issue statistics:
12+
// PhanPossiblyUndeclaredVariable : 10+ occurrences
13+
// PhanPossiblyNullTypeMismatchProperty : 7 occurrences
14+
// PhanTypeMismatchArgumentInternalProbablyReal : 1 occurrence
15+
1216
// Currently, file_suppressions and directory_suppressions are the only supported suppressions
1317
'file_suppressions' => [
18+
'src/class-block-delimiter.php' => ['PhanPossiblyNullTypeMismatchProperty', 'PhanPossiblyUndeclaredVariable', 'PhanTypeMismatchArgumentInternalProbablyReal'],
1419
],
1520
// 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed.
1621
// (directory_suppressions will currently be ignored by subsequent calls to --save-baseline, but may be preserved in future Phan releases)
Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
<?xml version="1.0"?>
22
<ruleset>
3-
4-
<rule ref="WordPress.WP.I18n">
5-
<properties>
6-
<property name="text_domain" type="array">
7-
<element value="jetpack-block-delimiter" />
8-
</property>
9-
</properties>
3+
<rule ref="Jetpack-Compat-NoWP" />
4+
<rule ref="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition">
5+
<exclude-pattern>*</exclude-pattern>
106
</rule>
11-
<rule ref="Jetpack.Functions.I18n">
12-
<properties>
13-
<property name="text_domain" value="jetpack-block-delimiter" />
14-
</properties>
7+
<rule ref="Universal.Operators.DisallowStandalonePostIncrementDecrement.PostIncrementFound">
8+
<exclude-pattern>*</exclude-pattern>
159
</rule>
16-
17-
<rule ref="WordPress.Utils.I18nTextDomainFixer">
18-
<properties>
19-
<property name="old_text_domain" type="array" />
20-
<property name="new_text_domain" value="jetpack-block-delimiter" />
21-
</properties>
10+
<rule ref="Universal.Arrays.DisallowShortArraySyntax.Found">
11+
<exclude-pattern>*</exclude-pattern>
2212
</rule>
23-
24-
</ruleset>
13+
</ruleset>

projects/packages/block-delimiter/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,20 @@ This makes it ideal for operations like finding specific blocks, counting block
364364
365365
You can contribute to this package by submitting a pull request to the [Jetpack repository](https://github.com/Automattic/jetpack/tree/trunk/projects/packages/block-delimiter).
366366
367+
### Coding standards
368+
369+
This package follows standards set by the [Jetpack Codesniffer package](https://packagist.org/packages/automattic/jetpack-codesniffer), with a few exceptions documented in the package's `.phpcs.dir.xml` file.
370+
371+
### Testing
372+
373+
When introducing new features or making changes to existing code, please add tests.
374+
375+
To run the tests, you can use the following command:
376+
377+
```bash
378+
composer phpunit
379+
```
380+
367381
## Using this package in your WordPress plugin
368382
369383
If you plan on using this package in your WordPress plugin, we would recommend that you use [Jetpack Autoloader](https://packagist.org/packages/automattic/jetpack-autoloader) as your autoloader. This will allow for maximum interoperability with other plugins that use this package as well.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Update phpcs and Phan configuration to be more permissive, and update main class accordingly.

projects/packages/block-delimiter/src/class-block-delimiter.php

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,6 @@ public static function next_delimiter( string $text, int $starting_byte_offset,
183183
$delimiter = null;
184184
static::$last_error = null;
185185

186-
// Initialize all variables that will be used later.
187-
$comment_opening_at = 0;
188-
$comment_closing_at = 0;
189-
$namespace_at = 0;
190-
$namespace_length = 0;
191-
$name_at = 0;
192-
$name_length = 0;
193-
$json_at = 0;
194-
$json_length = 0;
195-
$has_closer = false;
196-
$has_void_flag = false;
197-
198186
$close_html_comment = function ( $comment_starting_at ) use ( $text, &$at, $end ) {
199187
// Find span-of-dashes comments which look like `<!----->`.
200188
$span_of_dashes = strspn( $text, '-', $comment_starting_at + 2 );
@@ -218,15 +206,15 @@ public static function next_delimiter( string $text, int $starting_byte_offset,
218206

219207
$closer_must_be_at = $dashes_at + 2 + strspn( $text, '-', $dashes_at + 2 );
220208
if ( $closer_must_be_at < $end && '!' === $text[ $closer_must_be_at ] ) {
221-
++$closer_must_be_at;
209+
$closer_must_be_at++;
222210
}
223211

224212
if ( $closer_must_be_at < $end && '>' === $text[ $closer_must_be_at ] ) {
225213
$at = $closer_must_be_at + 1;
226214
return;
227215
}
228216

229-
++$now_at;
217+
$now_at++;
230218
}
231219
};
232220

@@ -461,23 +449,22 @@ public static function scan_delimiters( string $text, ?string $freeform_blocks =
461449
/*
462450
* Although `phpcs` confidently asserts that `$match_at` and `$match_length`
463451
* are undefined, it is not aware enough to realize that they are set by the
464-
* call to `next_delimiter` and so it's necessary to alter the code so it
465-
* doesn't get confused and reject valid code.
452+
* call to `next_delimiter` and so its necessary to alter the code so it
453+
* doesnt get confused and reject valid code.
466454
*/
467455
$match_at = 0;
468456
$match_length = 0;
469457

470-
$delimiter = self::next_delimiter( $text, $at, $match_at, $match_length );
471-
while ( null !== $delimiter ) {
458+
while ( null !== ( $delimiter = self::next_delimiter( $text, $at, $match_at, $match_length ) ) ) {
472459
// Handle top-level text as freeform blocks
473460
if ( 0 === $depth && $match_at > $at && 'visit' === $freeform_blocks ) {
474461
list( $text_opener, $text_closer ) = static::freeform_pair( $text, $at, $match_at - $at );
475462

476463
++$depth;
477-
yield array( $at, 0 ) => $text_opener;
464+
yield [ $at, 0 ] => $text_opener;
478465

479466
--$depth;
480-
yield array( $match_at, 0 ) => $text_closer;
467+
yield [ $match_at, 0 ] => $text_closer;
481468
}
482469

483470
$delimiter_type = $delimiter->get_delimiter_type();
@@ -493,25 +480,24 @@ public static function scan_delimiters( string $text, ?string $freeform_blocks =
493480
break;
494481
}
495482

496-
yield array( $match_at, $match_length ) => $delimiter;
483+
yield [ $match_at, $match_length ] => $delimiter;
497484

498485
if ( static::VOID === $delimiter_type ) {
499486
--$depth;
500487
}
501488

502-
$at = $match_at + $match_length;
503-
$delimiter = self::next_delimiter( $text, $at, $match_at, $match_length );
489+
$at = $match_at + $match_length;
504490
}
505491

506492
$end = strlen( $text );
507493
if ( 'visit' === $freeform_blocks && $at < $end ) {
508494
list( $text_opener, $text_closer ) = static::freeform_pair( $text, $at, $end - $at );
509495

510496
++$depth;
511-
yield array( $at, 0 ) => $text_opener;
497+
yield [ $at, 0 ] => $text_opener;
512498

513499
--$depth;
514-
yield array( $end, 0 ) => $text_closer;
500+
yield [ $end, 0 ] => $text_closer;
515501
}
516502
}
517503

@@ -553,7 +539,7 @@ private static function freeform_pair( string $text, int $at, int $length ): arr
553539
$closer->json_at = $end_at;
554540
$closer->type = static::CLOSER;
555541

556-
return array( $opener, $closer );
542+
return [ $opener, $closer ];
557543
}
558544

559545
/**
@@ -567,7 +553,7 @@ public static function get_last_error() {
567553
}
568554

569555
/**
570-
* Indicates if the last attempt to parse a block's JSON attributes failed.
556+
* Indicates if the last attempt to parse a blocks JSON attributes failed.
571557
*
572558
* @see JSON_ERROR_NONE, JSON_ERROR_DEPTH, etc…
573559
*
@@ -661,7 +647,7 @@ public function has_void_flag(): bool {
661647
* @return bool Whether this delimiter represents a block of the given type.
662648
*/
663649
public function is_block_type( string $block_type ): bool {
664-
// This is a core/freeform text block, it's special.
650+
// This is a core/freeform text block, its special.
665651
if ( 0 === $this->name_length ) {
666652
return 'core/freeform' === $block_type || 'freeform' === $block_type;
667653
}
@@ -714,7 +700,7 @@ public function is_block_type( string $block_type ): bool {
714700
* @return string Fully-qualified block namespace and type, e.g. "core/paragraph".
715701
*/
716702
public function allocate_and_return_block_type(): string {
717-
// This is a core/freeform text block, it's special.
703+
// This is a core/freeform text block, its special.
718704
if ( 0 === $this->name_length ) {
719705
return 'core/freeform';
720706
}
@@ -801,12 +787,7 @@ public function allocate_and_return_parsed_attributes(): ?array {
801787
}
802788

803789
$json_span = substr( $this->source_text, $this->json_at, $this->json_length );
804-
$parsed = json_decode(
805-
$json_span,
806-
null, // @phan-suppress-current-line PhanTypeMismatchArgumentInternalProbablyReal -- json_decode does accept null.
807-
512,
808-
JSON_OBJECT_AS_ARRAY | JSON_INVALID_UTF8_SUBSTITUTE
809-
);
790+
$parsed = json_decode( $json_span, null, 512, JSON_OBJECT_AS_ARRAY | JSON_INVALID_UTF8_SUBSTITUTE );
810791

811792
$last_error = json_last_error();
812793
$this->last_json_error = $last_error;
@@ -817,7 +798,6 @@ public function allocate_and_return_parsed_attributes(): ?array {
817798
}
818799

819800
// Debugging methods not meant for production use.
820-
// @codeCoverageIgnoreStart
821801

822802
/**
823803
* Prints a debugging message showing the structure of the parsed delimiter.
@@ -879,8 +859,6 @@ public function debug_print_structure(): void {
879859
echo "{$c( "\e[0;36m" )}{$void_flag}{$c("\e[90m")}-->\n"; // phpcs:ignore
880860
}
881861

882-
// @codeCoverageIgnoreEnd
883-
884862
// Constant declarations that would otherwise pollute the top of the class.
885863

886864
/**

0 commit comments

Comments
 (0)