Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick up docs for filters in condition expressions #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions lib/class-file-reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class File_Reflector extends FileReflector {
protected $location = array();

/**
* Last DocBlock associated with a non-documentable element.
* Last DocBlock(s) associated with a non-documentable element(s).
*
* @var \PHPParser_Comment_Doc
* @var \PHPParser_Comment_Doc[]
*/
protected $last_doc = null;
protected $last_doc = array();

/**
* Add hooks to the queue and update the node stack when we enter a node.
Expand Down Expand Up @@ -83,8 +83,8 @@ public function enterNode( \PHPParser_Node $node ) {

if ( $this->isFilter( $node ) ) {
if ( $this->last_doc && ! $node->getDocComment() ) {
$node->setAttribute( 'comments', array( $this->last_doc ) );
$this->last_doc = null;
$last_doc = array_pop( $this->last_doc );
$node->setAttribute( 'comments', array( $last_doc ) );
}

$hook = new Hook_Reflector( $node, $this->context );
Expand Down Expand Up @@ -127,7 +127,11 @@ public function enterNode( \PHPParser_Node $node ) {
// associated with a named element, and so aren't really from a non-
// documentable element after all.
if ( ! $this->isNodeDocumentable( $node ) && 'Name' !== $node->getType() && ( $docblock = $node->getDocComment() ) ) {
$this->last_doc = $docblock;
// The same docblock can be associated with multiple non-documentable
// elements, so we have to take care not to save the same docblock twice.
if ( ! $this->last_doc || $docblock->getText() !== end( $this->last_doc )->getText() ) {
$this->last_doc[] = $docblock;
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/phpunit/includes/export-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Export_UnitTestCase extends \WP_UnitTestCase {
/**
* The exported data.
*
* @var string
* @var array
*/
protected $export_data;

Expand Down Expand Up @@ -424,8 +424,8 @@ protected function assertPropertyHasDocs( $class, $property, $docs ) {
/**
* Assert that a hook has a docblock.
*
* @param array $hook The hook name.
* @param array $docs The expected data for the hook's docblock.
* @param string $hook The hook name.
* @param array $docs The expected data for the hook's docblock.
*/
protected function assertHookHasDocs( $hook, $docs ) {

Expand All @@ -436,9 +436,9 @@ protected function assertHookHasDocs( $hook, $docs ) {
/**
* Find the exported data for an entity.
*
* @param array $data The data to search in.
* @param string $type The type of entity.
* @param string $entity_name The name of the function.
* @param array $data The data to search in.
* @param string $type The type of entity.
* @param string $entity The name of the entity.
*
* @return array|false The data for the entity, or false if it couldn't be found.
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/phpunit/tests/export/docblocks.inc
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ do_action( 'undocumented_hook' );
* A reference array action.
*/
do_action_ref_array( 'test_ref_array_action', array( &$var ) );

/**
* A filter in a conditional expression.
*/
if ( apply_filters( 'in_conditional_expression', true ) ) {
/**
* A filter in a conditional.
*/
$value = apply_filters( 'in_conditional', true );
}
10 changes: 10 additions & 0 deletions tests/phpunit/tests/export/docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public function test_hook_docblocks() {
'test_ref_array_filter'
, array( 'description' => 'A reference array filter.' )
);

$this->assertHookHasDocs(
'in_conditional_expression'
, array( 'description' => 'A filter in a conditional expression.' )
);

$this->assertHookHasDocs(
'in_conditional'
, array( 'description' => 'A filter in a conditional.' )
);
}

/**
Expand Down