Skip to content

Commit

Permalink
ReflectionClassConstant::__toString() ReflectionEnumCase::__toString(…
Browse files Browse the repository at this point in the history
…) return also the doccomment
  • Loading branch information
kukulich committed Nov 28, 2024
1 parent cee199b commit 6e37a83
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 29 deletions.
18 changes: 16 additions & 2 deletions src/Reflection/StringCast/ReflectionClassConstantStringCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use function gettype;
use function is_array;
use function preg_replace;
use function sprintf;

/** @internal */
Expand All @@ -18,13 +19,14 @@ final class ReflectionClassConstantStringCast
*
* @psalm-pure
*/
public static function toString(ReflectionClassConstant $constantReflection): string
public static function toString(ReflectionClassConstant $constantReflection, bool $indent = true): string
{
/** @psalm-var scalar|array<scalar> $value */
$value = $constantReflection->getValue();

return sprintf(
"Constant [ %s%s %s %s ] { %s }\n",
"%sConstant [ %s%s %s %s ] { %s }\n",
self::docCommentToString($constantReflection, $indent),
$constantReflection->isFinal() ? 'final ' : '',
self::visibilityToString($constantReflection),
gettype($value),
Expand All @@ -33,6 +35,18 @@ public static function toString(ReflectionClassConstant $constantReflection): st
);
}

/** @psalm-pure */
private static function docCommentToString(ReflectionClassConstant $constantReflection, bool $indent): string
{
$docComment = $constantReflection->getDocComment();

if ($docComment === null) {
return '';
}

return ($indent ? preg_replace('/(\n)(?!\n)/', '\1 ', $docComment) : $docComment) . "\n";
}

/** @psalm-pure */
private static function visibilityToString(ReflectionClassConstant $constantReflection): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/StringCast/ReflectionClassStringCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ private static function constantsToString(array $constants, array $enumCases): s
return '';
}

$items = array_map(static fn (ReflectionEnumCase $enumCaseReflection): string => trim(ReflectionEnumCaseStringCast::toString($enumCaseReflection)), $enumCases)
+ array_map(static fn (ReflectionClassConstant $constantReflection): string => trim(ReflectionClassConstantStringCast::toString($constantReflection)), $constants);
$items = array_map(static fn (ReflectionEnumCase $enumCaseReflection): string => trim(ReflectionEnumCaseStringCast::toString($enumCaseReflection, indent: false)), $enumCases)
+ array_map(static fn (ReflectionClassConstant $constantReflection): string => trim(ReflectionClassConstantStringCast::toString($constantReflection, indent: false)), $constants);

return self::itemsToString($items);
}
Expand Down
18 changes: 16 additions & 2 deletions src/Reflection/StringCast/ReflectionEnumCaseStringCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Roave\BetterReflection\Reflection\ReflectionEnumCase;

use function gettype;
use function preg_replace;
use function sprintf;

/** @internal */
Expand All @@ -17,18 +18,31 @@ final class ReflectionEnumCaseStringCast
*
* @psalm-pure
*/
public static function toString(ReflectionEnumCase $enumCaseReflection): string
public static function toString(ReflectionEnumCase $enumCaseReflection, bool $indent = true): string
{
$enumReflection = $enumCaseReflection->getDeclaringEnum();

$value = $enumReflection->isBacked() ? $enumCaseReflection->getValue() : 'Object';
$type = $enumReflection->isBacked() ? gettype($value) : $enumReflection->getName();

return sprintf(
"Constant [ public %s %s ] { %s }\n",
"%sConstant [ public %s %s ] { %s }\n",
self::docCommentToString($enumCaseReflection, $indent),
$type,
$enumCaseReflection->getName(),
$value,
);
}

/** @psalm-pure */
private static function docCommentToString(ReflectionEnumCase $enumCaseReflection, bool $indent): string
{
$docComment = $enumCaseReflection->getDocComment();

if ($docComment === null) {
return '';
}

return ($indent ? preg_replace('/(\n)(?!\n)/', '\1 ', $docComment) : $docComment) . "\n";
}
}
4 changes: 4 additions & 0 deletions test/unit/Fixture/Enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

enum PureEnum implements InterfaceForEnum
{

/**
* One
*/
case ONE;
case TWO;
case THREE;
Expand Down
3 changes: 3 additions & 0 deletions test/unit/Fixture/ExampleClassExport.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Class [ <user> class Roave\BetterReflectionTest\Fixture\ExampleClass ] {

- Constants [8] {
Constant [ public integer MY_CONST_1 ] { 123 }
/**
* This comment for constant should be used.
*/
Constant [ public integer MY_CONST_2 ] { 234 }
Constant [ public integer MY_CONST_3 ] { 345 }
Constant [ protected integer MY_CONST_4 ] { 456 }
Expand Down
5 changes: 5 additions & 0 deletions test/unit/Fixture/StringCastBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ enum StringCastBackedEnum: string
case ENUM_CASE = 'string';

const CONSTANT = 'constant';

/**
* Something
*/
case ENUM_CASE_WITH_DOC_COMMENT = 'something';
}
8 changes: 6 additions & 2 deletions test/unit/Fixture/StringCastBackedEnumExpected.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Class [ <user> final class Roave\BetterReflectionTest\Fixture\StringCastBackedEnum implements UnitEnum, BackedEnum ] {
@@ %s/Fixture/StringCastBackedEnum.php 5-10
@@ %s/Fixture/StringCastBackedEnum.php 5-15

- Constants [2] {
- Constants [3] {
Constant [ public string ENUM_CASE ] { string }
/**
* Something
*/
Constant [ public string ENUM_CASE_WITH_DOC_COMMENT ] { something }
Constant [ public string CONSTANT ] { constant }
}

Expand Down
5 changes: 5 additions & 0 deletions test/unit/Fixture/StringCastClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ abstract class StringCastClass extends StringCastClassParent implements StringCa
private const PRIVATE_CONSTANT = 'string';
const NO_VISIBILITY_CONSTANT = [];

/**
* @var string
*/
const WITH_DOC_COMMENT_CONSTANT = 'string';

private $privateProperty = 'string';
protected $protectedProperty = 0;
public $publicProperty = true;
Expand Down
5 changes: 5 additions & 0 deletions test/unit/Fixture/StringCastClassConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ class StringCastConstants
private const PRIVATE_CONSTANT = 'string';
const NO_VISIBILITY_CONSTANT = [];
final public const FINAL_CONSTANT = 'final';

/**
* @var string
*/
const WITH_DOC_COMMENT_CONSTANT = 'string';
}
32 changes: 18 additions & 14 deletions test/unit/Fixture/StringCastClassExpected.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
Class [ <user> abstract class Roave\BetterReflectionTest\Fixture\StringCastClass extends Roave\BetterReflectionTest\Fixture\StringCastClassParent implements Roave\BetterReflectionTest\Fixture\StringCastClassInterface2, Roave\BetterReflectionTest\Fixture\StringCastClassInterface ] {
@@ %s/Fixture/StringCastClass.php 26-94
@@ %s/Fixture/StringCastClass.php 26-99

- Constants [4] {
- Constants [5] {
Constant [ public boolean PUBLIC_CONSTANT ] { 1 }
Constant [ protected integer PROTECTED_CONSTANT ] { 0 }
Constant [ private string PRIVATE_CONSTANT ] { string }
Constant [ public array NO_VISIBILITY_CONSTANT ] { Array }
/**
* @var string
*/
Constant [ public string WITH_DOC_COMMENT_CONSTANT ] { string }
}

- Static properties [1] {
Expand All @@ -14,7 +18,7 @@ Class [ <user> abstract class Roave\BetterReflectionTest\Fixture\StringCastClass

- Static methods [1] {
Method [ <user> static public method staticPublicMethod ] {
@@ %s/Fixture/StringCastClass.php 75 - 77
@@ %s/Fixture/StringCastClass.php 80 - 82
}
}

Expand All @@ -34,47 +38,47 @@ Class [ <user> abstract class Roave\BetterReflectionTest\Fixture\StringCastClass

- Methods [12] {
Method [ <user, ctor> public method __construct ] {
@@ %s/Fixture/StringCastClass.php 49 - 51
@@ %s/Fixture/StringCastClass.php 54 - 56
}

Method [ <user, dtor> public method __destruct ] {
@@ %s/Fixture/StringCastClass.php 53 - 55
@@ %s/Fixture/StringCastClass.php 58 - 60
}

Method [ <user> public method publicMethod ] {
@@ %s/Fixture/StringCastClass.php 57 - 59
@@ %s/Fixture/StringCastClass.php 62 - 64
}

Method [ <user> protected method protectedMethod ] {
@@ %s/Fixture/StringCastClass.php 61 - 63
@@ %s/Fixture/StringCastClass.php 66 - 68
}

Method [ <user> private method privateMethod ] {
@@ %s/Fixture/StringCastClass.php 65 - 67
@@ %s/Fixture/StringCastClass.php 70 - 72
}

Method [ <user> final public method finalPublicMethod ] {
@@ %s/Fixture/StringCastClass.php 69 - 71
@@ %s/Fixture/StringCastClass.php 74 - 76
}

Method [ <user> abstract public method abstractPublicMethod ] {
@@ %s/Fixture/StringCastClass.php 73 - 73
@@ %s/Fixture/StringCastClass.php 78 - 78
}

Method [ <user> public method noVisibility ] {
@@ %s/Fixture/StringCastClass.php 79 - 81
@@ %s/Fixture/StringCastClass.php 84 - 86
}

Method [ <user, overwrites Roave\BetterReflectionTest\Fixture\StringCastClassParent, prototype Roave\BetterReflectionTest\Fixture\StringCastClassParent> public method overwrittenMethod ] {
@@ %s/Fixture/StringCastClass.php 83 - 85
@@ %s/Fixture/StringCastClass.php 88 - 90
}

Method [ <user, prototype Roave\BetterReflectionTest\Fixture\StringCastClassInterface> public method prototypeMethod ] {
@@ %s/Fixture/StringCastClass.php 87 - 89
@@ %s/Fixture/StringCastClass.php 92 - 94
}

Method [ <user> public method methodWithParameters ] {
@@ %s/Fixture/StringCastClass.php 91 - 93
@@ %s/Fixture/StringCastClass.php 96 - 98

- Parameters [2] {
Parameter #0 [ <required> $a ]
Expand Down
5 changes: 5 additions & 0 deletions test/unit/Fixture/StringCastPureEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ enum StringCastPureEnum
case ENUM_CASE;

const CONSTANT = 'constant';

/**
* Something
*/
case ENUM_CASE_WITH_DOC_COMMENT;
}
8 changes: 6 additions & 2 deletions test/unit/Fixture/StringCastPureEnumExpected.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Class [ <user> final class Roave\BetterReflectionTest\Fixture\StringCastPureEnum implements UnitEnum ] {
@@ %s/Fixture/StringCastPureEnum.php 5-10
@@ %s/Fixture/StringCastPureEnum.php 5-15

- Constants [2] {
- Constants [3] {
Constant [ public Roave\BetterReflectionTest\Fixture\StringCastPureEnum ENUM_CASE ] { Object }
/**
* Something
*/
Constant [ public Roave\BetterReflectionTest\Fixture\StringCastPureEnum ENUM_CASE_WITH_DOC_COMMENT ] { Object }
Constant [ public string CONSTANT ] { constant }
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/Reflection/ReflectionClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testProtectedFinal(): void

public function testToString(): void
{
self::assertSame("Constant [ public integer MY_CONST_1 ] { 123 }\n", (string) $this->getExampleConstant('MY_CONST_1'));
self::assertSame("/**\n * This comment for constant should be used.\n */\nConstant [ public integer MY_CONST_2 ] { 234 }\n", (string) $this->getExampleConstant('MY_CONST_2'));
}

/** @param non-empty-string $const */
Expand Down
8 changes: 4 additions & 4 deletions test/unit/Reflection/ReflectionEnumCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public function testGetValueThrowsExceptionForPureEnum(): void
public static function dataLinesAndColumns(): array
{
return [
[PureEnum::class, 'ONE', 7, 7, 5, 13],
[IntEnum::class, 'TWO', 19, 19, 5, 17],
[StringEnum::class, 'THREE', 34, 35, 5, 18],
[PureEnum::class, 'ONE', 11, 11, 5, 13],
[IntEnum::class, 'TWO', 23, 23, 5, 17],
[StringEnum::class, 'THREE', 38, 39, 5, 18],
];
}

Expand Down Expand Up @@ -251,6 +251,6 @@ public function testToString(): void

self::assertInstanceOf(ReflectionEnum::class, $enumReflection);

self::assertSame("Constant [ public Roave\BetterReflectionTest\Fixture\PureEnum ONE ] { Object }\n", (string) $enumReflection->getCase('ONE'));
self::assertSame("/**\n * One\n */\nConstant [ public Roave\BetterReflectionTest\Fixture\PureEnum ONE ] { Object }\n", (string) $enumReflection->getCase('ONE'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function toStringProvider(): array
['PRIVATE_CONSTANT', "Constant [ private string PRIVATE_CONSTANT ] { string }\n"],
['NO_VISIBILITY_CONSTANT', "Constant [ public array NO_VISIBILITY_CONSTANT ] { Array }\n"],
['FINAL_CONSTANT', "Constant [ final public string FINAL_CONSTANT ] { final }\n"],
['WITH_DOC_COMMENT_CONSTANT', "/**\n * @var string\n */\nConstant [ public string WITH_DOC_COMMENT_CONSTANT ] { string }\n"],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public function testPureEnumCaseToString(): void
self::assertSame("Constant [ public Roave\BetterReflectionTest\Fixture\StringCastPureEnum ENUM_CASE ] { Object }\n", (string) $enumReflection->getCase('ENUM_CASE'));
}

public function testPureEnumCaseWithDocCommentToString(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../../Fixture/StringCastPureEnum.php', $this->astLocator));
$enumReflection = $reflector->reflectClass(StringCastPureEnum::class);

self::assertInstanceOf(ReflectionEnum::class, $enumReflection);
self::assertSame("/**\n * Something\n */\nConstant [ public Roave\BetterReflectionTest\Fixture\StringCastPureEnum ENUM_CASE_WITH_DOC_COMMENT ] { Object }\n", (string) $enumReflection->getCase('ENUM_CASE_WITH_DOC_COMMENT'));
}

public function testBackedEnumCaseToString(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../../Fixture/StringCastBackedEnum.php', $this->astLocator));
Expand All @@ -44,4 +53,13 @@ public function testBackedEnumCaseToString(): void
self::assertInstanceOf(ReflectionEnum::class, $enumReflection);
self::assertSame("Constant [ public string ENUM_CASE ] { string }\n", (string) $enumReflection->getCase('ENUM_CASE'));
}

public function testBackedEnumCaseWithDocCommentToString(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../../Fixture/StringCastBackedEnum.php', $this->astLocator));
$enumReflection = $reflector->reflectClass(StringCastBackedEnum::class);

self::assertInstanceOf(ReflectionEnum::class, $enumReflection);
self::assertSame("/**\n * Something\n */\nConstant [ public string ENUM_CASE_WITH_DOC_COMMENT ] { something }\n", (string) $enumReflection->getCase('ENUM_CASE_WITH_DOC_COMMENT'));
}
}

0 comments on commit 6e37a83

Please sign in to comment.