Skip to content

Commit 8faf375

Browse files
committed
Added PhpDoc::isFinal,isReadonly
1 parent 83932f6 commit 8faf375

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/PhpDocParser/PhpDoc.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,28 @@ public function isDeprecated(): bool
7777
return false;
7878
}
7979

80+
public function isFinal(): bool
81+
{
82+
foreach ($this->tags as $tag) {
83+
if ($tag->name === '@final') {
84+
return true;
85+
}
86+
}
87+
88+
return false;
89+
}
90+
91+
public function isReadonly(): bool
92+
{
93+
foreach ($this->tags as $tag) {
94+
if (\in_array($tag->name, ['@readonly', '@psalm-readonly', '@phpstan-readonly'], true)) {
95+
return true;
96+
}
97+
}
98+
99+
return false;
100+
}
101+
80102
public function varType(): ?TypeNode
81103
{
82104
if ($this->varType !== false) {

tests/unit/PhpDocParser/PhpDocParserTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,68 @@ public function testIsDeprecatedReturnsTrueIfDeprecated(): void
4848
self::assertTrue($deprecated);
4949
}
5050

51+
public function testIsFinalReturnsFalseIfNoFinalTag(): void
52+
{
53+
$parser = new PhpDocParser();
54+
55+
$final = $parser->parsePhpDoc(
56+
<<<'PHP'
57+
/**
58+
* @example
59+
*/
60+
PHP,
61+
)->isFinal();
62+
63+
self::assertFalse($final);
64+
}
65+
66+
public function testIsFinalReturnsTrueIfFinal(): void
67+
{
68+
$parser = new PhpDocParser();
69+
70+
$final = $parser->parsePhpDoc(
71+
<<<'PHP'
72+
/**
73+
* @example
74+
* @final
75+
*/
76+
PHP,
77+
)->isFinal();
78+
79+
self::assertTrue($final);
80+
}
81+
82+
public function testIsReadonlyReturnsFalseIfNoReadonlyTag(): void
83+
{
84+
$parser = new PhpDocParser();
85+
86+
$readonly = $parser->parsePhpDoc(
87+
<<<'PHP'
88+
/**
89+
* @example
90+
*/
91+
PHP,
92+
)->isReadonly();
93+
94+
self::assertFalse($readonly);
95+
}
96+
97+
public function testIsReadonlyReturnsTrueIfReadonly(): void
98+
{
99+
$parser = new PhpDocParser();
100+
101+
$readonly = $parser->parsePhpDoc(
102+
<<<'PHP'
103+
/**
104+
* @example
105+
* @readonly
106+
*/
107+
PHP,
108+
)->isReadonly();
109+
110+
self::assertTrue($readonly);
111+
}
112+
51113
public function testItReturnsNullVarTypeWhenNoVarTag(): void
52114
{
53115
$parser = new PhpDocParser();

0 commit comments

Comments
 (0)