Skip to content

Commit

Permalink
phpstan/phpdoc-parser ^1.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jun 13, 2022
1 parent dd2ab21 commit 55e5dc5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
21 changes: 16 additions & 5 deletions SlevomatCodingStandard/Helpers/Annotation/ParameterAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
Expand All @@ -25,10 +26,13 @@
class ParameterAnnotation extends Annotation
{

/** @var ParamTagValueNode|null */
/** @var ParamTagValueNode|TypelessParamTagValueNode|null */
private $contentNode;

public function __construct(string $name, int $startPointer, int $endPointer, ?string $content, ?ParamTagValueNode $contentNode)
/**
* @param ParamTagValueNode|TypelessParamTagValueNode|null $contentNode
*/
public function __construct(string $name, int $startPointer, int $endPointer, ?string $content, $contentNode)
{
if (!in_array($name, ['@param', '@psalm-param', '@phpstan-param'], true)) {
throw new InvalidArgumentException(sprintf('Unsupported annotation %s.', $name));
Expand All @@ -44,7 +48,10 @@ public function isInvalid(): bool
return $this->contentNode === null;
}

public function getContentNode(): ParamTagValueNode
/**
* @return ParamTagValueNode|TypelessParamTagValueNode|null
*/
public function getContentNode()
{
$this->errorWhenInvalid();

Expand All @@ -71,12 +78,16 @@ public function getParameterName(): string
}

/**
* @return GenericTypeNode|CallableTypeNode|IntersectionTypeNode|UnionTypeNode|ArrayTypeNode|ArrayShapeNode|IdentifierTypeNode|ThisTypeNode|NullableTypeNode|ConstTypeNode
* @return GenericTypeNode|CallableTypeNode|IntersectionTypeNode|UnionTypeNode|ArrayTypeNode|ArrayShapeNode|IdentifierTypeNode|ThisTypeNode|NullableTypeNode|ConstTypeNode|null
*/
public function getType(): TypeNode
public function getType(): ?TypeNode
{
$this->errorWhenInvalid();

if ($this->contentNode instanceof TypelessParamTagValueNode) {
return null;
}

/** @var GenericTypeNode|CallableTypeNode|IntersectionTypeNode|UnionTypeNode|ArrayTypeNode|ArrayShapeNode|IdentifierTypeNode|ThisTypeNode|NullableTypeNode|ConstTypeNode $type */
$type = $this->contentNode->type;
return $type;
Expand Down
2 changes: 1 addition & 1 deletion SlevomatCodingStandard/Helpers/AnnotationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function getAnnotationTypes(Annotation $annotation): array
}
} elseif ($annotation instanceof TypeImportAnnotation) {
$annotationTypes[] = $annotation->getImportedFrom();
} else {
} elseif ($annotation->getType() !== null) {
$annotationTypes[] = $annotation->getType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ private function checkTraversableTypeHintSpecification(
$hasTraversableTypeHint = true;
} elseif (
array_key_exists($parameterName, $parametersAnnotations)
&& $parametersAnnotations[$parameterName]->getType() !== null
&& AnnotationTypeHelper::containsTraversableType(
$parametersAnnotations[$parameterName]->getType(),
$phpcsFile,
Expand Down Expand Up @@ -443,6 +444,9 @@ private function checkTraversableTypeHintSpecification(
}

$parameterTypeNode = $parametersAnnotations[$parameterName]->getType();
if ($parameterTypeNode === null) {
continue;
}

if (
(
Expand Down Expand Up @@ -508,6 +512,9 @@ private function checkUselessAnnotations(
}

$parameterAnnotation = $parametersAnnotations[$parameterName];
if ($parameterAnnotation->getType() === null) {
continue;
}

if (!AnnotationHelper::isAnnotationUseless(
$phpcsFile,
Expand Down
10 changes: 9 additions & 1 deletion build/PHPStan/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ parameters:
path: %currentWorkingDirectory%/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php
-
message: '#Parameter \#5 \$contentNode of class SlevomatCodingStandard\\Helpers\\Annotation\\\w+Annotation constructor expects PHPStan\\PhpDocParser\\Ast\\PhpDoc\\\w+TagValueNode\|null, PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagValueNode\|null given.#'
count: 14
count: 13
path: %currentWorkingDirectory%/SlevomatCodingStandard/Helpers/AnnotationHelper.php
-
message: "#^Access to an undefined property PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\AssertTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\MixinTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\ParamTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\PropertyTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\ReturnTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\ThrowsTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\TypeAliasTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\TypelessParamTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\VarTagValueNode\\:\\:\\$type\\.$#"
count: 1
path: %currentWorkingDirectory%/SlevomatCodingStandard/Helpers/AnnotationHelper.php
-
message: "#^Parameter \\#5 \\$contentNode of class SlevomatCodingStandard\\\\Helpers\\\\Annotation\\\\ParameterAnnotation constructor expects PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\ParamTagValueNode\\|PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\TypelessParamTagValueNode\\|null, PHPStan\\\\PhpDocParser\\\\Ast\\\\PhpDoc\\\\PhpDocTagValueNode\\|null given\\.$#"
count: 1
path: %currentWorkingDirectory%/SlevomatCodingStandard/Helpers/AnnotationHelper.php

services:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "^7.2 || ^8.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7",
"phpstan/phpdoc-parser": "^1.5.1",
"phpstan/phpdoc-parser": "^1.6.2",
"squizlabs/php_codesniffer": "^3.7.0"
},
"require-dev": {
Expand Down
14 changes: 11 additions & 3 deletions tests/Sniffs/TypeHints/data/parameterTypeHintNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,21 @@ public function emptyAnnotation(int $a)
}

/**
* @param $a
* @param invalid invalid $a
*/
public function invalidAnnotation(int $a)
{

}

/**
* @param $a
*/
public function noType(int $a)
{

}

/**
* @param $this $a
*/
Expand Down Expand Up @@ -217,7 +225,7 @@ public function withPhpstanAnnotationAndMissingNativeTypeHint($a)

/**
* @psalm-param
* @psalm-param $b Invalid
* @psalm-param invalid invalid $b Invalid
* @psalm-param Whatever<int> $a
*/
public function withPsalmAnnotationAndTraversableNativeTypeHint(array $a)
Expand All @@ -226,7 +234,7 @@ public function withPsalmAnnotationAndTraversableNativeTypeHint(array $a)

/**
* @phpstan-param
* @phpstan-param $b Invalid
* @phpstan-param invalid invalid $b Invalid
* @phpstan-param Whatever<int> $a
*/
public function withPhpstanAnnotationAndTraversableNativeTypeHint(array $a)
Expand Down

0 comments on commit 55e5dc5

Please sign in to comment.