From cb3f899bbbd4819d0245d20faedf0afd6c062fd3 Mon Sep 17 00:00:00 2001 From: sanketio Date: Wed, 14 Oct 2026 11:17:41 +0530 Subject: [PATCH 1/2] Export docblocks for file-level constants --- lib/runner.php | 1 + tests/phpunit/tests/export/constants.inc | 19 ++++++++++++++++ tests/phpunit/tests/export/constants.php | 29 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/phpunit/tests/export/constants.inc create mode 100644 tests/phpunit/tests/export/constants.php diff --git a/lib/runner.php b/lib/runner.php index 1794fae..6d390b7 100644 --- a/lib/runner.php +++ b/lib/runner.php @@ -91,6 +91,7 @@ function parse_files( $files, $root ) { 'name' => $constant->getShortName(), 'line' => $constant->getLineNumber(), 'value' => $constant->getValue(), + 'doc' => export_docblock( $constant, $file_setup_blueprints, $path ), ); } 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.' ) + ); + } +} From 070b3b2af96c91644c6b9bf931d96ca6af22a336 Mon Sep 17 00:00:00 2001 From: sanketio Date: Wed, 14 Oct 2026 11:17:42 +0530 Subject: [PATCH 2/2] Export class constants with their docblocks --- lib/runner.php | 25 ++++++++++++++++++++++++ tests/phpunit/tests/export/constants.php | 20 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/runner.php b/lib/runner.php index 6d390b7..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; @@ -135,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, @@ -706,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.php b/tests/phpunit/tests/export/constants.php index a52598e..6d0d8ad 100644 --- a/tests/phpunit/tests/export/constants.php +++ b/tests/phpunit/tests/export/constants.php @@ -26,4 +26,24 @@ public function test_file_constant_docblocks() { , 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.', + ) + ); + } }