Skip to content

Commit

Permalink
Support for array shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jun 11, 2019
1 parent 53d78a9 commit 35446d2
Show file tree
Hide file tree
Showing 31 changed files with 317 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public function getParameterName(): string
}

/**
* @return \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode
* @return \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode
*/
public function getType(): TypeNode
{
if ($this->isInvalid()) {
throw new LogicException(sprintf('Invalid %s annotation.', $this->name));
}

/** @var \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode $type */
/** @var \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode $type */
$type = $this->contentNode->type;
return $type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public function getDescription(): ?string
}

/**
* @return \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode
* @return \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode
*/
public function getType(): TypeNode
{
if ($this->isInvalid()) {
throw new LogicException(sprintf('Invalid %s annotation.', $this->name));
}

/** @var \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode $type */
/** @var \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\CallableTypeNode|\PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode|\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode $type */
$type = $this->contentNode->type;
return $type;
}
Expand Down
49 changes: 49 additions & 0 deletions SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace SlevomatCodingStandard\Helpers;

use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
Expand All @@ -31,6 +34,18 @@ public static function getIdentifierTypeNodes(TypeNode $typeNode): array
return self::getIdentifierTypeNodes($typeNode->type);
}

if ($typeNode instanceof ArrayShapeNode) {
$identifierTypeNodes = [];
foreach ($typeNode->items as $arrayShapeItemNode) {
if ($arrayShapeItemNode->keyName instanceof IdentifierTypeNode) {
$identifierTypeNodes[] = $arrayShapeItemNode->keyName;
}

$identifierTypeNodes = array_merge($identifierTypeNodes, self::getIdentifierTypeNodes($arrayShapeItemNode->valueType));
}
return $identifierTypeNodes;
}

if (
$typeNode instanceof UnionTypeNode
|| $typeNode instanceof IntersectionTypeNode
Expand Down Expand Up @@ -85,6 +100,14 @@ public static function getUnionTypeNodes(TypeNode $typeNode): array
return self::getUnionTypeNodes($typeNode->type);
}

if ($typeNode instanceof ArrayShapeNode) {
$unionTypeNodes = [];
foreach ($typeNode->items as $arrayShapeItemNode) {
$unionTypeNodes = array_merge($unionTypeNodes, self::getUnionTypeNodes($arrayShapeItemNode->valueType));
}
return $unionTypeNodes;
}

if ($typeNode instanceof IntersectionTypeNode) {
$unionTypeNodes = [];
foreach ($typeNode->types as $innerTypeNode) {
Expand Down Expand Up @@ -122,6 +145,14 @@ public static function getArrayTypeNodes(TypeNode $typeNode): array
return array_merge([$typeNode], self::getArrayTypeNodes($typeNode->type));
}

if ($typeNode instanceof ArrayShapeNode) {
$arrayTypeNodes = [];
foreach ($typeNode->items as $arrayShapeItemNode) {
$arrayTypeNodes = array_merge($arrayTypeNodes, self::getArrayTypeNodes($arrayShapeItemNode->valueType));
}
return $arrayTypeNodes;
}

if ($typeNode instanceof NullableTypeNode) {
return self::getArrayTypeNodes($typeNode->type);
}
Expand Down Expand Up @@ -220,6 +251,24 @@ public static function change(TypeNode $masterTypeNode, TypeNode $typeNodeToChan
return new ArrayTypeNode(self::change($masterTypeNode->type, $typeNodeToChange, $changedTypeNode));
}

if ($masterTypeNode instanceof ArrayShapeNode) {
$arrayShapeItemNodes = [];
foreach ($masterTypeNode->items as $arrayShapeItemNode) {
$arrayShapeItemNodes[] = self::change($arrayShapeItemNode, $typeNodeToChange, $changedTypeNode);
}

return new ArrayShapeNode($arrayShapeItemNodes);
}

if ($masterTypeNode instanceof ArrayShapeItemNode) {
/** @var \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|null $keyName */
$keyName = $masterTypeNode->keyName instanceof IdentifierTypeNode
? self::change($masterTypeNode->keyName, $typeNodeToChange, $changedTypeNode)
: $masterTypeNode->keyName;

return new ArrayShapeItemNode($keyName, $masterTypeNode->optional, self::change($masterTypeNode->valueType, $typeNodeToChange, $changedTypeNode));
}

if ($masterTypeNode instanceof NullableTypeNode) {
return new NullableTypeNode(self::change($masterTypeNode->type, $typeNodeToChange, $changedTypeNode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
Expand Down Expand Up @@ -210,9 +211,11 @@ private function checkParametersTypeHints(File $phpcsFile, int $functionPointer)
}

if ($annotationContainsOneType) {
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode $parameterTypeNode */
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode $parameterTypeNode */
$parameterTypeNode = $parameterTypeNode;
$possibleParameterTypeHint = $parameterTypeNode instanceof ArrayTypeNode ? 'array' : $this->getTypeHintFromOneType($parameterTypeNode);
$possibleParameterTypeHint = $parameterTypeNode instanceof ArrayTypeNode || $parameterTypeNode instanceof ArrayShapeNode
? 'array'
: $this->getTypeHintFromOneType($parameterTypeNode);
$nullableParameterTypeHint = false;

} else {
Expand All @@ -227,9 +230,11 @@ private function checkParametersTypeHints(File $phpcsFile, int $functionPointer)
}

if ($this->annotationTypeContainsNullType($parameterTypeNode)) {
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode $notNullTypeHintNode */
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode $notNullTypeHintNode */
$notNullTypeHintNode = $this->getTypeFromNullableType($parameterTypeNode);
$possibleParameterTypeHint = $notNullTypeHintNode instanceof ArrayTypeNode ? 'array' : $this->getTypeHintFromOneType($notNullTypeHintNode);
$possibleParameterTypeHint = $notNullTypeHintNode instanceof ArrayTypeNode || $notNullTypeHintNode instanceof ArrayShapeNode
? 'array'
: $this->getTypeHintFromOneType($notNullTypeHintNode);
$nullableParameterTypeHint = true;
} else {

Expand Down Expand Up @@ -439,9 +444,11 @@ private function checkReturnTypeHints(File $phpcsFile, int $functionPointer): vo
}

if ($annotationContainsOneType) {
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode $returnTypeNode */
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode $returnTypeNode */
$returnTypeNode = $returnTypeNode;
$possibleReturnTypeHint = $returnTypeNode instanceof ArrayTypeNode ? 'array' : $this->getTypeHintFromOneType($returnTypeNode);
$possibleReturnTypeHint = $returnTypeNode instanceof ArrayTypeNode || $returnTypeNode instanceof ArrayShapeNode
? 'array'
: $this->getTypeHintFromOneType($returnTypeNode);
$nullableReturnTypeHint = false;

} else {
Expand All @@ -456,9 +463,11 @@ private function checkReturnTypeHints(File $phpcsFile, int $functionPointer): vo
}

if ($this->annotationTypeContainsNullType($returnTypeNode)) {
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode $notNullTypeHintNode */
/** @var \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode|\PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode|\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode|\PHPStan\PhpDocParser\Ast\Type\ThisTypeNode|\PHPStan\PhpDocParser\Ast\Type\GenericTypeNode $notNullTypeHintNode */
$notNullTypeHintNode = $this->getTypeFromNullableType($returnTypeNode);
$possibleReturnTypeHint = $notNullTypeHintNode instanceof ArrayTypeNode ? 'array' : $this->getTypeHintFromOneType($notNullTypeHintNode);
$possibleReturnTypeHint = $notNullTypeHintNode instanceof ArrayTypeNode || $notNullTypeHintNode instanceof ArrayShapeNode
? 'array'
: $this->getTypeHintFromOneType($notNullTypeHintNode);
$nullableReturnTypeHint = true;
} else {
$itemsSpecificationTypeHint = $this->getItemsSpecificationTypeFromType($returnTypeNode);
Expand Down Expand Up @@ -859,6 +868,10 @@ private function annotationContainsOneType(TypeNode $typeNode): bool
return true;
}

if ($typeNode instanceof ArrayShapeNode) {
return true;
}

return $typeNode instanceof ArrayTypeNode;
}

Expand Down Expand Up @@ -891,6 +904,10 @@ private function annotationContainsTraversableType(File $phpcsFile, int $pointer
return true;
}

if ($typeNode instanceof ArrayShapeNode) {
return true;
}

if ($typeNode instanceof ArrayTypeNode) {
return true;
}
Expand Down Expand Up @@ -926,6 +943,16 @@ private function annotationContainsItemsSpecificationForTraversable(File $phpcsF
return true;
}

if ($typeNode instanceof ArrayShapeNode) {
foreach ($typeNode->items as $arrayShapeItemNode) {
if (!$this->annotationContainsItemsSpecificationForTraversable($phpcsFile, $pointer, $arrayShapeItemNode->valueType, true)) {
return false;
}
}

return true;
}

if ($typeNode instanceof IdentifierTypeNode) {
if (!$inTraversable) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"require": {
"php": "^7.2",
"phpstan/phpdoc-parser": "0.3.1 - 0.3.4",
"phpstan/phpdoc-parser": "0.3.5",
"squizlabs/php_codesniffer": "^3.4.1"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedClassNameInAnnotationErrors.php');

self::assertSame(47, $report->getErrorCount());
self::assertSame(49, $report->getErrorCount());

self::assertSniffError($report, 16, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \XXX\PropertySameNamespace in @var should be referenced via a fully qualified name');
self::assertSniffError($report, 19, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \YYY\PropertyUsed in @var should be referenced via a fully qualified name');
Expand Down Expand Up @@ -67,6 +67,9 @@ public function testErrors(): void
self::assertSniffError($report, 140, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \Iterator in @return should be referenced via a fully qualified name');
self::assertSniffError($report, 140, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \Traversable in @return should be referenced via a fully qualified name');

self::assertSniffError($report, 149, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \Iterator in @var should be referenced via a fully qualified name');
self::assertSniffError($report, 152, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \Iterator in @var should be referenced via a fully qualified name');

self::assertAllFixedInFile($report);
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ public function testSearchingInAnnotations(): void
]
);

self::assertSame(25, $report->getErrorCount());
self::assertSame(27, $report->getErrorCount());

self::assertSniffError($report, 8, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');
self::assertSniffError($report, 9, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');
Expand Down Expand Up @@ -912,6 +912,9 @@ public function testSearchingInAnnotations(): void
self::assertSniffError($report, 104, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\ArrayObject should not be referenced via a fully qualified name, but via a use statement.');
self::assertSniffError($report, 104, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');

self::assertSniffError($report, 113, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');
self::assertSniffError($report, 116, ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME, 'Class \Foo\DateTime should not be referenced via a fully qualified name, but via a use statement.');

self::assertAllFixedInFile($report);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Sniffs/Namespaces/UnusedUsesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function testUsedUseInAnnotationWithDisabledSearchAnnotations(): void
'searchAnnotations' => false,
]);

self::assertSame(57, $report->getErrorCount());
self::assertSame(59, $report->getErrorCount());

self::assertSniffError($report, 5, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Assert is not used in this file.');
self::assertSniffError($report, 6, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Doctrine\ORM\Mapping (as ORM) is not used in this file.');
Expand Down Expand Up @@ -169,6 +169,8 @@ public function testUsedUseInAnnotationWithDisabledSearchAnnotations(): void
self::assertSniffError($report, 59, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Iag14 is not used in this file.');
self::assertSniffError($report, 60, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Callable1 is not used in this file.');
self::assertSniffError($report, 61, UnusedUsesSniff::CODE_UNUSED_USE, 'Type Callable2 is not used in this file.');
self::assertSniffError($report, 62, UnusedUsesSniff::CODE_UNUSED_USE, 'Type ArrayShape1 is not used in this file.');
self::assertSniffError($report, 63, UnusedUsesSniff::CODE_UNUSED_USE, 'Type ArrayShape2 is not used in this file.');
}

public function testUsedUseInAnnotationWithEnabledSearchAnnotations(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,9 @@ public function returnsCallable()
}

}

/** @var array{int, \Iterator} $arrayShape1 */
$arrayShape1 = [];

/** @var array{\Iterator: int} $arrayShape2 */
$arrayShape2 = [];
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,9 @@ public function returnsCallable()
}

}

/** @var array{int, Iterator} $arrayShape1 */
$arrayShape1 = [];

/** @var array{Iterator: int} $arrayShape2 */
$arrayShape2 = [];
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@ public function returnsCallable()
}

}

/** @var array{int, DateTime} $arrayShape1 */
$arrayShape1 = [];

/** @var array{DateTime: int} $arrayShape2 */
$arrayShape2 = [];
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ public function returnsCallable()
}

}

/** @var array{int, \Foo\DateTime} $arrayShape1 */
$arrayShape1 = [];

/** @var array{\Foo\DateTime: int} $arrayShape2 */
$arrayShape2 = [];
8 changes: 8 additions & 0 deletions tests/Sniffs/Namespaces/data/unusedUsesInAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
use Iag14;
use Callable1;
use Callable2;
use ArrayShape1;
use ArrayShape2;

/**
* @ORM\Entity()
Expand Down Expand Up @@ -218,3 +220,9 @@ public function returnsCallable()
}

}

/** @var array{int, ArrayShape1} $arrayShape1 */
$arrayShape1 = [];

/** @var array{ArrayShape2: int} $arrayShape2 */
$arrayShape2 = [];
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testErrors(): void
],
]);

self::assertSame(22, $report->getErrorCount());
self::assertSame(23, $report->getErrorCount());

self::assertSniffError($report, 6, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "\DateTimeImmutable[]" is disallowed, use generic type hint syntax instead.');
self::assertSniffError($report, 7, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "bool[]" is disallowed, use generic type hint syntax instead.');
Expand All @@ -45,6 +45,7 @@ public function testErrors(): void
self::assertSniffError($report, 55, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "string[][]" is disallowed, use generic type hint syntax instead.');
self::assertSniffError($report, 60, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "string[][]" is disallowed, use generic type hint syntax instead.');
self::assertSniffError($report, 65, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "string[][]" is disallowed, use generic type hint syntax instead.');
self::assertSniffError($report, 70, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "int[]" is disallowed, use generic type hint syntax instead.');

self::assertAllFixedInFile($report);
}
Expand Down

0 comments on commit 35446d2

Please sign in to comment.