diff --git a/lib/runner.php b/lib/runner.php index 1794fae..459fc34 100644 --- a/lib/runner.php +++ b/lib/runner.php @@ -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; @@ -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 ), ); } @@ -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, @@ -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. diff --git a/tests/phpunit/tests/export/constants.inc b/tests/phpunit/tests/export/constants.inc new file mode 100644 index 0000000..ba680ec --- /dev/null +++ b/tests/phpunit/tests/export/constants.inc @@ -0,0 +1,19 @@ +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.', + ) + ); + } +}