Skip to content
Merged
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
45 changes: 45 additions & 0 deletions bin/Handbook_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ private static function get_simple_representation( $reflection ) {
$signature = $signature . '()';
}
$phpdoc = self::parse_docblock( $reflection->getDocComment() );
$phpdoc = self::parse_behat_attributes( $reflection, $phpdoc );
$type = strtolower( str_replace( 'Reflection', '', get_class( $reflection ) ) );
$class = '';
switch ( $type ) {
Expand Down Expand Up @@ -1197,6 +1198,50 @@ private static function get_simple_representation( $reflection ) {
];
}

/**
* Add Behat step definitions declared as PHP attributes to the parsed PHPDoc.
*
* Since wp-cli-tests v5.6.0, step definitions are declared using
* attributes like `#[Given( 'an empty directory' )]` instead of
* `@Given an empty directory` docblock annotations.
*
* @param \ReflectionMethod|\ReflectionFunction $reflection
* @param array{description: string, short_description?: string, long_description?:string, behat_step?: string, parameters: array<string, mixed>} $phpdoc
* @return array{description: string, short_description?: string, long_description?:string, behat_step?: string, parameters: array<string, mixed>}
*/
private static function parse_behat_attributes( $reflection, $phpdoc ) {
if ( ! method_exists( $reflection, 'getAttributes' ) ) {
return $phpdoc;
}

foreach ( $reflection->getAttributes() as $attribute ) {
$parts = explode( '\\', $attribute->getName() );
$step = end( $parts );

if ( ! in_array( $step, [ 'Given', 'When', 'Then' ], true ) ) {
continue;
}

$arguments = $attribute->getArguments();
$pattern = $arguments[0] ?? $arguments['pattern'] ?? null;

if ( ! is_string( $pattern ) || '' === $pattern ) {
continue;
}

if ( ! isset( $phpdoc['behat_step'] ) ) {
$phpdoc['behat_step'] = "$step $pattern";
}

if ( ! isset( $phpdoc['parameters'][ $step ] ) ) {
$phpdoc['parameters'][ $step ] = [];
}
$phpdoc['parameters'][ $step ][] = preg_split( '/[\s]+/', $pattern, 3 );
}

return $phpdoc;
}

/**
* Parse PHPDoc into a structured representation.
*
Expand Down