-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for @phpstan-assert/@psalm-assert annotations
- Loading branch information
Showing
7 changed files
with
298 additions
and
9 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
SlevomatCodingStandard/Helpers/Annotation/AssertAnnotation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Helpers\Annotation; | ||
|
||
use InvalidArgumentException; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use SlevomatCodingStandard\Helpers\AnnotationTypeHelper; | ||
use function in_array; | ||
use function sprintf; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class AssertAnnotation extends Annotation | ||
{ | ||
|
||
/** @var AssertTagValueNode|null */ | ||
private $contentNode; | ||
|
||
public function __construct(string $name, int $startPointer, int $endPointer, ?string $content, ?AssertTagValueNode $contentNode) | ||
{ | ||
if (!in_array( | ||
$name, | ||
['@phpstan-assert', '@phpstan-assert-if-true', '@phpstan-assert-if-false', '@psalm-assert', '@psalm-assert-if-true', '@psalm-assert-if-false'], | ||
true | ||
)) { | ||
throw new InvalidArgumentException(sprintf('Unsupported annotation %s.', $name)); | ||
} | ||
|
||
parent::__construct($name, $startPointer, $endPointer, $content); | ||
|
||
$this->contentNode = $contentNode; | ||
} | ||
|
||
public function isInvalid(): bool | ||
{ | ||
return $this->contentNode === null; | ||
} | ||
|
||
public function getContentNode(): AssertTagValueNode | ||
{ | ||
$this->errorWhenInvalid(); | ||
|
||
return $this->contentNode; | ||
} | ||
|
||
public function hasDescription(): bool | ||
{ | ||
return $this->getDescription() !== null; | ||
} | ||
|
||
public function getDescription(): ?string | ||
{ | ||
$this->errorWhenInvalid(); | ||
|
||
return $this->contentNode->description !== '' ? $this->contentNode->description : null; | ||
} | ||
|
||
public function getType(): TypeNode | ||
{ | ||
$this->errorWhenInvalid(); | ||
|
||
return $this->contentNode->type; | ||
} | ||
|
||
public function export(): string | ||
{ | ||
$exported = sprintf('%s %s %s', $this->name, AnnotationTypeHelper::export($this->getType()), $this->contentNode->parameter); | ||
|
||
$description = $this->getDescription(); | ||
if ($description !== null) { | ||
$exported .= sprintf(' %s', $this->fixDescription($description)); | ||
} | ||
|
||
return $exported; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Helpers\Annotation; | ||
|
||
use InvalidArgumentException; | ||
use LogicException; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use SlevomatCodingStandard\Helpers\TestCase; | ||
|
||
class AssertAnnotationTest extends TestCase | ||
{ | ||
|
||
public function testAnnotation(): void | ||
{ | ||
$annotation = new AssertAnnotation( | ||
'@phpstan-assert', | ||
1, | ||
10, | ||
'Description', | ||
new AssertTagValueNode( | ||
new IdentifierTypeNode('string'), | ||
'$parameter', | ||
false, | ||
'Description' | ||
) | ||
); | ||
|
||
self::assertSame('@phpstan-assert', $annotation->getName()); | ||
self::assertSame(1, $annotation->getStartPointer()); | ||
self::assertSame(10, $annotation->getEndPointer()); | ||
self::assertSame('Description', $annotation->getContent()); | ||
|
||
self::assertFalse($annotation->isInvalid()); | ||
self::assertTrue($annotation->hasDescription()); | ||
self::assertSame('Description', $annotation->getDescription()); | ||
self::assertSame('@phpstan-assert string $parameter Description', $annotation->export()); | ||
} | ||
|
||
public function testUnsupportedAnnotation(): void | ||
{ | ||
self::expectException(InvalidArgumentException::class); | ||
self::expectExceptionMessage('Unsupported annotation @param.'); | ||
new AssertAnnotation('@param', 1, 1, null, null); | ||
} | ||
|
||
public function testGetContentNodeWhenInvalid(): void | ||
{ | ||
self::expectException(LogicException::class); | ||
self::expectExceptionMessage('Invalid @phpstan-assert annotation.'); | ||
$annotation = new AssertAnnotation('@phpstan-assert', 1, 1, null, null); | ||
$annotation->getContentNode(); | ||
} | ||
|
||
public function testGetDescriptionWhenInvalid(): void | ||
{ | ||
self::expectException(LogicException::class); | ||
self::expectExceptionMessage('Invalid @phpstan-assert annotation.'); | ||
$annotation = new AssertAnnotation('@phpstan-assert', 1, 1, null, null); | ||
$annotation->getDescription(); | ||
} | ||
|
||
public function testGetTypeWhenInvalid(): void | ||
{ | ||
self::expectException(LogicException::class); | ||
self::expectExceptionMessage('Invalid @phpstan-assert annotation.'); | ||
$annotation = new AssertAnnotation('@phpstan-assert', 1, 1, null, null); | ||
$annotation->getType(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.