Skip to content
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
26 changes: 26 additions & 0 deletions lib/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use phpDocumentor\Reflection\BaseReflector;
use phpDocumentor\Reflection\ClassReflector;
use phpDocumentor\Reflection\ClassReflector\ConstantReflector;
use phpDocumentor\Reflection\ClassReflector\MethodReflector;
use phpDocumentor\Reflection\ClassReflector\PropertyReflector;
use phpDocumentor\Reflection\FunctionReflector;
Expand Down Expand Up @@ -91,6 +92,7 @@ function parse_files( $files, $root ) {
'name' => $constant->getShortName(),
'line' => $constant->getLineNumber(),
'value' => $constant->getValue(),
'doc' => export_docblock( $constant, $file_setup_blueprints, $path ),
);
}

Expand Down Expand Up @@ -134,6 +136,7 @@ function parse_files( $files, $root ) {
'abstract' => $class->isAbstract(),
'extends' => $class->getParentClass(),
'implements' => $class->getInterfaces(),
'constants' => export_constants( $class->getConstants(), $class_setup_blueprints, $path ),
'properties' => export_properties( $class->getProperties(), $class_setup_blueprints, $path ),
'methods' => export_methods( $class->getMethods(), $class_setup_blueprints, $path ),
'doc' => $class_doc,
Expand Down Expand Up @@ -705,6 +708,29 @@ function export_arguments( array $arguments ) {
return $output;
}

/**
* @param ConstantReflector[] $constants
* @param array $inherited_setup_blueprints Optional. Setup Blueprints inherited from the file or class DocBlock.
* @param string $source_file Optional. Source path used in invalid snippet metadata errors.
*
* @return array
*/
function export_constants( array $constants, array $inherited_setup_blueprints = array(), $source_file = '' ) {
$out = array();

foreach ( $constants as $constant ) {
$out[] = array(
'name' => $constant->getShortName(),
'line' => $constant->getLineNumber(),
'end_line' => $constant->getNode()->getAttribute( 'endLine' ),
'value' => $constant->getValue(),
'doc' => export_docblock( $constant, $inherited_setup_blueprints, $source_file ),
);
}

return $out;
}

/**
* @param PropertyReflector[] $properties
* @param array $inherited_setup_blueprints Optional. Setup Blueprints inherited from the file or class DocBlock.
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/tests/export/constants.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* A constant defined with define().
*/
define( 'TEST_DEFINED_CONSTANT', 1 );

/**
* A constant declared with const.
*/
const TEST_CONST_CONSTANT = 'value';

class Test_Constants_Class {

/**
* A class constant.
*/
const TEST_CLASS_CONSTANT = 2;
}
49 changes: 49 additions & 0 deletions tests/phpunit/tests/export/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* A test case for exporting constants.
*/

namespace WP_Parser\Tests;

/**
* Test that constants are exported correctly.
*/
class Export_Constants extends Export_UnitTestCase {

/**
* Test that file constant docblocks are exported.
*/
public function test_file_constant_docblocks() {

$this->assertEntityHasDocs(
$this->find_entity_data_in( $this->export_data, 'constants', 'TEST_DEFINED_CONSTANT' )
, array( 'description' => 'A constant defined with define().' )
);

$this->assertEntityHasDocs(
$this->find_entity_data_in( $this->export_data, 'constants', 'TEST_CONST_CONSTANT' )
, array( 'description' => 'A constant declared with const.' )
);
}

/**
* Test that class constants are exported.
*/
public function test_class_constants() {

$class = $this->find_entity_data_in( $this->export_data, 'classes', 'Test_Constants_Class' );

$this->assertEntityContains(
$class
, 'constants'
, array(
'name' => 'TEST_CLASS_CONSTANT',
'line' => 18,
'end_line' => 18,
'value' => '2',
'doc.description' => 'A class constant.',
)
);
}
}
Loading