From 8faf375f768ed45bee6a5535a4572df46fd3bf12 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Fri, 22 Sep 2023 04:18:47 +0300 Subject: [PATCH] Added PhpDoc::isFinal,isReadonly --- src/PhpDocParser/PhpDoc.php | 22 +++++++ tests/unit/PhpDocParser/PhpDocParserTest.php | 62 ++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/PhpDocParser/PhpDoc.php b/src/PhpDocParser/PhpDoc.php index 4d838ad..09d5528 100644 --- a/src/PhpDocParser/PhpDoc.php +++ b/src/PhpDocParser/PhpDoc.php @@ -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) { diff --git a/tests/unit/PhpDocParser/PhpDocParserTest.php b/tests/unit/PhpDocParser/PhpDocParserTest.php index 97303a6..63eb920 100644 --- a/tests/unit/PhpDocParser/PhpDocParserTest.php +++ b/tests/unit/PhpDocParser/PhpDocParserTest.php @@ -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();