Skip to content

Commit 357150b

Browse files
committed
Added support for doc block comment and @Deprecation annotation
1 parent 519990c commit 357150b

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

src/EnumMethodReflection.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,24 @@ public function getVariants(): array
8686

8787
public function getDocComment(): ?string
8888
{
89-
return null;
89+
$docComment = $this->classReflection->getConstant($this->name)->getDocComment();
90+
91+
if ($docComment) {
92+
// remove @var annotation of constant definition
93+
$docComment = preg_replace('/@var.*/', '', $docComment);
94+
}
95+
96+
return $docComment;
9097
}
9198

9299
public function isDeprecated(): TrinaryLogic
93100
{
94-
return TrinaryLogic::createNo();
101+
return $this->classReflection->getConstant($this->name)->isDeprecated();
95102
}
96103

97104
public function getDeprecatedDescription(): ?string
98105
{
99-
return null;
106+
return $this->classReflection->getConstant($this->name)->getDeprecatedDescription();
100107
}
101108

102109
public function isFinal(): TrinaryLogic

tests/Assets/DeprecatedEnum.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MabeEnumPHPStanTest\Assets;
6+
7+
use MabeEnum\Enum;
8+
9+
class DeprecatedEnum extends Enum
10+
{
11+
/**
12+
* @deprecated Test deprecated reflection
13+
*/
14+
const DEPRECATED = 'deprecated';
15+
16+
const NOT_DEPRECATED = 'not deprecated';
17+
}

tests/Assets/StrEnum.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,27 @@
88

99
class StrEnum extends Enum
1010
{
11+
/**
12+
* String const without visibility declaration
13+
*
14+
* @var string
15+
*/
1116
const STR = 'str';
17+
18+
/**
19+
* Private string const
20+
*/
1221
private const PRIVATE_STR = 'private str';
22+
23+
/**
24+
* Protected string const
25+
*/
1326
protected const PROTECTED_STR = 'protected str';
27+
28+
/**
29+
* Public string const
30+
*/
1431
public const PUBLIC_STR = 'public str';
32+
33+
public const NO_DOC_BLOCK = 'no doc block';
1534
}

tests/EnumMethodReflectionTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use MabeEnumPHPStan\EnumMethodReflection;
88
use MabeEnumPHPStan\EnumMethodsClassReflectionExtension;
9+
use MabeEnumPHPStanTest\Assets\DeprecatedEnum;
910
use MabeEnumPHPStanTest\Assets\NotAnEnum;
1011
use MabeEnumPHPStanTest\Assets\StrEnum;
1112
use PHPStan\Reflection\ParametersAcceptorSelector;
1213
use PHPStan\Testing\TestCase;
14+
use PHPStan\TrinaryLogic;
1315
use PHPStan\Type\VerbosityLevel;
1416

1517
class EnumMethodReflectionTest extends TestCase
@@ -70,4 +72,40 @@ public function testGetVariants()
7072

7173
$this->assertSame(StrEnum::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
7274
}
75+
76+
public function testGetDocComment()
77+
{
78+
$classReflection = $this->broker->getClass(StrEnum::class);
79+
$docMethodRefl = $this->reflectionExtension->getMethod($classReflection, 'STR');
80+
$noDocMethodRefl = $this->reflectionExtension->getMethod($classReflection, 'NO_DOC_BLOCK');
81+
82+
// return null on no doc block
83+
$this->assertSame(null, $noDocMethodRefl->getDocComment());
84+
85+
// return the correct doc block
86+
$this->assertRegExp('/String const without visibility declaration/', $docMethodRefl->getDocComment());
87+
88+
// remove @var declaration
89+
$this->assertNotRegExp('/@var/', $docMethodRefl->getDocComment());
90+
}
91+
92+
public function testIsDeprecated()
93+
{
94+
$classReflection = $this->broker->getClass(DeprecatedEnum::class);
95+
$deprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'DEPRECATED');
96+
$notDeprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'NOT_DEPRECATED');
97+
98+
$this->assertTrue($deprecatedRefl->isDeprecated()->yes());
99+
$this->assertTrue($notDeprecatedRefl->isDeprecated()->no());
100+
}
101+
102+
public function testGetDeprecatedDescription()
103+
{
104+
$classReflection = $this->broker->getClass(DeprecatedEnum::class);
105+
$deprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'DEPRECATED');
106+
$notDeprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'NOT_DEPRECATED');
107+
108+
$this->assertSame('Test deprecated reflection', $deprecatedRefl->getDeprecatedDescription());
109+
$this->assertNull($notDeprecatedRefl->getDeprecatedDescription());
110+
}
73111
}

0 commit comments

Comments
 (0)