Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions components/BlockParser/class-wpblockparser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ class WP_Block_Parser {
* parse. In contrast to the specification parser this does not
* return an error on invalid inputs.
*
* @param string $document Input document being parsed.
* @param string $document Input document being parsed.
*
* @return array[]
* @since 5.0.0
*
*/
public function parse( $document ) {
$this->document = $document;
Expand Down Expand Up @@ -98,7 +99,7 @@ public function proceed() {
switch ( $token_type ) {
case 'no-more-tokens':
// if not in a block then flush output.
if ( $stack_depth === 0 ) {
if ( 0 === $stack_depth ) {
$this->add_freeform();

return false;
Expand All @@ -114,7 +115,7 @@ public function proceed() {
*/

// for the easy case we'll assume an implicit closer.
if ( $stack_depth === 1 ) {
if ( 1 === $stack_depth ) {
$this->add_block_from_stack();

return false;
Expand All @@ -136,7 +137,7 @@ public function proceed() {
* easy case is if we stumbled upon a void block
* in the top-level of the document
*/
if ( $stack_depth === 0 ) {
if ( 0 === $stack_depth ) {
if ( isset( $leading_html_start ) ) {
$this->output[] = (array) $this->freeform(
substr(
Expand Down Expand Up @@ -184,7 +185,7 @@ public function proceed() {
* if we're missing an opener we're in trouble
* This is an error
*/
if ( $stack_depth === 0 ) {
if ( 0 === $stack_depth ) {
/*
* we have options
* - assume an implicit opener
Expand All @@ -197,7 +198,7 @@ public function proceed() {
}

// if we're not nesting then this is easy - close the block.
if ( $stack_depth === 1 ) {
if ( 1 === $stack_depth ) {
$this->add_block_from_stack( $start_offset );
$this->offset = $start_offset + $token_length;

Expand All @@ -209,12 +210,9 @@ public function proceed() {
* block and add it as a new innerBlock to the parent
*/
$stack_top = array_pop( $this->stack );
$html = substr(
$this->document,
$stack_top->prev_offset,
$start_offset - $stack_top->prev_offset
);
$stack_top->block->innerHTML .= $html;
$html = substr( $this->document, $stack_top->prev_offset,
$start_offset - $stack_top->prev_offset );
$stack_top->block->innerHTML .= $html;
$stack_top->block->innerContent[] = $html;
$stack_top->prev_offset = $start_offset + $token_length;

Expand Down Expand Up @@ -267,24 +265,24 @@ public function next_token() {
);

// if we get here we probably have catastrophic backtracking or out-of-memory in the PCRE.
if ( $has_match === false ) {
if ( false === $has_match ) {
return array( 'no-more-tokens', null, null, null, null );
}

// we have no more tokens.
if ( $has_match === 0 ) {
if ( 0 === $has_match ) {
return array( 'no-more-tokens', null, null, null, null );
}

list( $match, $started_at ) = $matches[0];

$length = strlen( $match );
$is_closer = isset( $matches['closer'] ) && $matches['closer'][1] !== - 1;
$is_void = isset( $matches['void'] ) && $matches['void'][1] !== - 1;
$is_closer = isset( $matches['closer'] ) && - 1 !== $matches['closer'][1];
$is_void = isset( $matches['void'] ) && - 1 !== $matches['void'][1];
$namespace = $matches['namespace'];
$namespace = ( isset( $namespace ) && $namespace[1] !== - 1 ) ? $namespace[0] : 'core/';
$namespace = ( isset( $namespace ) && - 1 !== $namespace[1] ) ? $namespace[0] : 'core/';
$name = $namespace . $matches['name'][0];
$has_attrs = isset( $matches['attrs'] ) && $matches['attrs'][1] !== - 1;
$has_attrs = isset( $matches['attrs'] ) && - 1 !== $matches['attrs'][1];

/*
* Fun fact! It's not trivial in PHP to create "an empty associative array" since all arrays
Expand Down Expand Up @@ -316,11 +314,12 @@ public function next_token() {
/**
* Returns a new block object for freeform HTML
*
* @param string $inner_html HTML content of block.
* @param string $inner_html HTML content of block.
*
* @return WP_Block_Parser_Block freeform block object.
* @internal
* @since 3.9.0
*
*/
public function freeform( $inner_html ) {
return new WP_Block_Parser_Block( null, array(), array(), $inner_html, array( $inner_html ) );
Expand All @@ -330,15 +329,15 @@ public function freeform( $inner_html ) {
* Pushes a length of text from the input document
* to the output list as a freeform block.
*
* @param null $length how many bytes of document text to output.
* @param null $length how many bytes of document text to output.
*
* @since 5.0.0
* @internal
*/
public function add_freeform( $length = null ) {
$length = $length ? $length : strlen( $this->document ) - $this->offset;

if ( $length === 0 ) {
if ( 0 === $length ) {
return;
}

Expand All @@ -349,10 +348,10 @@ public function add_freeform( $length = null ) {
* Given a block structure from memory pushes
* a new block to the output list.
*
* @param WP_Block_Parser_Block $block The block to add to the output.
* @param int $token_start Byte offset into the document where the first token for the block starts.
* @param int $token_length Byte length of entire block from start of opening token to end of closing token.
* @param int|null $last_offset Last byte offset into document if continuing form earlier output.
* @param WP_Block_Parser_Block $block The block to add to the output.
* @param int $token_start Byte offset into the document where the first token for the block starts.
* @param int $token_length Byte length of entire block from start of opening token to end of closing token.
* @param int|null $last_offset Last byte offset into document if continuing form earlier output.
*
* @internal
* @since 5.0.0
Expand All @@ -363,7 +362,7 @@ public function add_inner_block( WP_Block_Parser_Block $block, $token_start, $to
$html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );

if ( ! empty( $html ) ) {
$parent->block->innerHTML .= $html;
$parent->block->innerHTML .= $html;
$parent->block->innerContent[] = $html;
}

Expand All @@ -374,7 +373,7 @@ public function add_inner_block( WP_Block_Parser_Block $block, $token_start, $to
/**
* Pushes the top block from the parsing stack to the output list.
*
* @param int|null $end_offset byte offset into document for where we should stop sending text output as HTML.
* @param int|null $end_offset byte offset into document for where we should stop sending text output as HTML.
*
* @since 5.0.0
* @internal
Expand All @@ -388,7 +387,7 @@ public function add_block_from_stack( $end_offset = null ) {
: substr( $this->document, $prev_offset );

if ( ! empty( $html ) ) {
$stack_top->block->innerHTML .= $html;
$stack_top->block->innerHTML .= $html;
$stack_top->block->innerContent[] = $html;
}

Expand Down
5 changes: 3 additions & 2 deletions components/BlockParser/class-wpblockparserblock.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ class WP_Block_Parser_Block {
*
* Will populate object properties from the provided arguments.
*
* @param string $name Name of block.
* @param string $name Name of block.
* @param array $attrs Optional set of attributes from block comment delimiters.
* @param array $inner_blocks List of inner blocks (of this same class).
* @param string $inner_html Resultant HTML from inside block comment delimiters after removing inner blocks.
* @param string $inner_html Resultant HTML from inside block comment delimiters after removing inner blocks.
* @param array $inner_content List of string fragments and null markers where inner blocks were found.
*
* @since 5.0.0
*
*/
public function __construct( $name, $attrs, $inner_blocks, $inner_html, $inner_content ) {
$this->blockName = $name; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
Expand Down
4 changes: 2 additions & 2 deletions components/BlockParser/class-wpblockparsererror.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class WP_Block_Parser_Error extends Exception {
private $type;

const TYPE_SUSPICIOUS_DELIMITER = 'suspicious-delimiter';
const TYPE_MISMATCHED_CLOSER = 'mismatched-block-closer';
const TYPE_PARSE_ERROR = 'parse-error';
const TYPE_MISMATCHED_CLOSER = 'mismatched-block-closer';
const TYPE_PARSE_ERROR = 'parse-error';

public function __construct( $message, $type = self::TYPE_PARSE_ERROR, ?Exception $previous = null ) {
$this->type = $type;
Expand Down
13 changes: 7 additions & 6 deletions components/BlockParser/class-wpblockparserframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ class WP_Block_Parser_Frame {
*
* Will populate object properties from the provided arguments.
*
* @param WP_Block_Parser_Block $block Full or partial block.
* @param int $token_start Byte offset into document for start of parse token.
* @param int $token_length Byte length of entire parse token string.
* @param int|null $prev_offset Optional. Byte offset into document for after parse token ends. Default null.
* @param int|null $leading_html_start Optional. Byte offset into document where leading HTML before token starts.
* Default null.
* @param WP_Block_Parser_Block $block Full or partial block.
* @param int $token_start Byte offset into document for start of parse token.
* @param int $token_length Byte length of entire parse token string.
* @param int|null $prev_offset Optional. Byte offset into document for after parse token ends. Default null.
* @param int|null $leading_html_start Optional. Byte offset into document where leading HTML before token starts.
* Default null.
*
* @since 5.0.0
*
*/
public function __construct( $block, $token_start, $token_length, $prev_offset = null, $leading_html_start = null ) {
$this->block = $block;
Expand Down
12 changes: 6 additions & 6 deletions components/Blueprints/DataReference/class-absolutelocalpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class AbsoluteLocalPath extends DataReference {
/**
* Constructor.
*
* @param string $path The path.
* @param string $path The path.
*/
public function __construct( string $path ) {
$this->path = realpath( $path );
$this->path = realpath($path);
parent::__construct();
}

Expand All @@ -40,18 +40,18 @@ public function get_filename(): string {
* Checks if a string is a valid absolute local path. Useful
* for resolving a Blueprint reference to an actual file before
* we know the execution context.
*
*
* Windows paths can be really comples:
*
*
* https://www.fileside.app/blog/2023-03-17_windows-file-paths/
*
*
* Instead of parsing them, we'll just ask the OS whether the path exists.
*
* @param $path The path to check.
* @return bool Whether the path is valid.
*/
public static function is_valid( $path ): bool {
return realpath( $path ) !== false;
return false !== realpath($path);
}

/**
Expand Down
22 changes: 10 additions & 12 deletions components/Blueprints/DataReference/class-datareference.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,18 @@ class DataReference {
*/
private static $instanceCounter = 0;

public function __construct( $original_definition = null ) {
$this->id = self::$instanceCounter++;
public function __construct($original_definition = null) {
$this->id = self::$instanceCounter ++;
$this->original_definition = $original_definition;
}

public static function create( $reference, array $additional_reference_classes = array() ) {
$classes = array_merge(
array(
URLReference::class,
GitPath::class,
InlineDirectory::class,
InlineFile::class,
),
$additional_reference_classes
);
static public function create( $reference, array $additional_reference_classes = [] ) {
$classes = array_merge( [
URLReference::class,
GitPath::class,
InlineDirectory::class,
InlineFile::class,
], $additional_reference_classes );
foreach ( $classes as $class ) {
if ( $class::is_valid( $reference ) ) {
return new $class( $reference );
Expand All @@ -57,4 +54,5 @@ public function get_filename(): string {
public function get_human_readable_name(): string {
throw new NotImplementedException( 'get_human_readable_name is not implemented for this data reference' );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function startEagerResolution( array $dataReferences, Tracker $dataResolu
}

/** Core service method shared by runner, target resolvers and steps
*
* @return File|Directory
*/
public function resolve( DataReference $reference ) {
Expand All @@ -82,9 +81,9 @@ public function resolve( DataReference $reference ) {
}

// @TODO: Clean up the semantics of this class. Resolve() and separate resolve_uncached() seem confusing. There's
// a bunch of implicit behaviors related to caching. Ideally we would either have a self-contained resolution
// method, or co-locate the resolution logic with the data reference classes and only use this class for
// caching.
// a bunch of implicit behaviors related to caching. Ideally we would either have a self-contained resolution
// method, or co-locate the resolution logic with the data reference classes and only use this class for
// caching.
public function resolve_uncached( DataReference $reference ) {
$progress_tracker = $this->subTrackers[ $reference->id ] ?? new Tracker();

Expand Down
6 changes: 3 additions & 3 deletions components/Blueprints/DataReference/class-directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Directory extends DataReference {
/**
* Constructor.
*
* @param Filesystem $filesystem The filesystem representing the directory content.
* @param string $dirname The name of the directory.
* @param Filesystem $filesystem The filesystem representing the directory content.
* @param string $dirname The name of the directory.
*/
public function __construct( Filesystem $filesystem, string $dirname ) {
$this->filesystem = $filesystem;
Expand All @@ -38,6 +38,6 @@ public function __construct( Filesystem $filesystem, string $dirname ) {
* @return string The human-readable name.
*/
public function get_human_readable_name(): string {
return 'Directory: ' . $this->dirname;
return "Directory: " . $this->dirname;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class ExecutionContextPath extends DataReference {
/**
* Constructor.
*
* @param string $path The path.
* @param string $path The path.
*/
public function __construct( string $path ) {
$this->path = $path;
parent::__construct( $path );
parent::__construct($path);
}

/**
Expand Down Expand Up @@ -48,10 +48,10 @@ public function get_filename(): string {
* @return bool Whether the path is valid.
*/
public static function is_valid( $path ): bool {
if ( ! is_string( $path ) ) {
if(!is_string($path)) {
return false;
}
if ( strpos( $path, './' ) === 0 || strpos( $path, '/' ) === 0 ) {
if(strpos($path, './') === 0 || strpos($path, '/') === 0) {
return true;
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions components/Blueprints/DataReference/class-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class File extends DataReference {
/**
* Constructor.
*
* @param ByteReadStream $stream The stream representing the file content.
* @param string $filename The name of the file.
* @param ByteReadStream $stream The stream representing the file content.
* @param string $filename The name of the file.
*/
public function __construct( ByteReadStream $stream, string $filename ) {
$this->stream = $stream;
Expand All @@ -38,7 +38,7 @@ public function __construct( ByteReadStream $stream, string $filename ) {
* @return string The human-readable name.
*/
public function get_human_readable_name(): string {
return 'File: ' . $this->filename;
return "File: " . $this->filename;
}

public function getStream(): ByteReadStream {
Expand Down
Loading