Skip to content

Commit

Permalink
Added PhpDoc::isFinal,isReadonly
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Sep 22, 2023
1 parent 83932f6 commit 8faf375
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/PhpDocParser/PhpDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ public function isDeprecated(): bool
return false;
}

public function isFinal(): bool
{
foreach ($this->tags as $tag) {
if ($tag->name === '@final') {
return true;
}
}

return false;
}

public function isReadonly(): bool
{
foreach ($this->tags as $tag) {
if (\in_array($tag->name, ['@readonly', '@psalm-readonly', '@phpstan-readonly'], true)) {
return true;
}
}

return false;
}

public function varType(): ?TypeNode
{
if ($this->varType !== false) {
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/PhpDocParser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,68 @@ public function testIsDeprecatedReturnsTrueIfDeprecated(): void
self::assertTrue($deprecated);
}

public function testIsFinalReturnsFalseIfNoFinalTag(): void
{
$parser = new PhpDocParser();

$final = $parser->parsePhpDoc(
<<<'PHP'
/**
* @example
*/
PHP,
)->isFinal();

self::assertFalse($final);
}

public function testIsFinalReturnsTrueIfFinal(): void
{
$parser = new PhpDocParser();

$final = $parser->parsePhpDoc(
<<<'PHP'
/**
* @example
* @final
*/
PHP,
)->isFinal();

self::assertTrue($final);
}

public function testIsReadonlyReturnsFalseIfNoReadonlyTag(): void
{
$parser = new PhpDocParser();

$readonly = $parser->parsePhpDoc(
<<<'PHP'
/**
* @example
*/
PHP,
)->isReadonly();

self::assertFalse($readonly);
}

public function testIsReadonlyReturnsTrueIfReadonly(): void
{
$parser = new PhpDocParser();

$readonly = $parser->parsePhpDoc(
<<<'PHP'
/**
* @example
* @readonly
*/
PHP,
)->isReadonly();

self::assertTrue($readonly);
}

public function testItReturnsNullVarTypeWhenNoVarTag(): void
{
$parser = new PhpDocParser();
Expand Down

0 comments on commit 8faf375

Please sign in to comment.