diff --git a/bin/Handbook_Command.php b/bin/Handbook_Command.php index 00630d9f..64900adc 100644 --- a/bin/Handbook_Command.php +++ b/bin/Handbook_Command.php @@ -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 ) { @@ -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} $phpdoc + * @return array{description: string, short_description?: string, long_description?:string, behat_step?: string, parameters: array} + */ + 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. *