Skip to content

Commit e0ea100

Browse files
committed
[phpstan] Fix few static errors
1 parent db78595 commit e0ea100

File tree

13 files changed

+42
-23
lines changed

13 files changed

+42
-23
lines changed

phpstan.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,8 @@ parameters:
317317
-
318318
message: '#PHPDoc tag @var with type int is not subtype of native type array\|PHPStan\\PhpDocParser\\Ast\\ConstExpr\\ConstExprNode\|Rector\\BetterPhpDocParser\\PhpDoc\\DoctrineAnnotationTagValueNode\|Rector\\BetterPhpDocParser\\PhpDoc\\StringNode\|Rector\\BetterPhpDocParser\\ValueObject\\PhpDoc\\DoctrineAnnotation\\CurlyListNode\|string#'
319319
path: src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php
320+
321+
- '#Parameter \#1 \$phpVersion of method Rector\\Config\\RectorConfig\:\:phpVersion\(\) expects 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|100000, 79999 given#'
322+
323+
# node vs stmts mix
324+
- '#expects array<PhpParser\\Node\\Stmt>, array<PhpParser\\Node> given#'

rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function matchNameWithStmts(string $tag, array $stmts): ?string
4444

4545
/**
4646
* @param array<Use_|GroupUse> $uses
47+
* @return non-empty-string|null
4748
*/
4849
public function matchNameWithUses(string $tag, array $uses): ?string
4950
{

src/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public function __construct(
3131
) {
3232
}
3333

34+
/**
35+
* @return non-empty-string
36+
*/
3437
public function resolveTagFullyQualifiedName(string $tag, Node $node): string
3538
{
3639
$uniqueId = $tag . spl_object_id($node);
@@ -54,6 +57,7 @@ public function resolveTagFullyQualifiedName(string $tag, Node $node): string
5457

5558
/**
5659
* @param array<Use_|GroupUse> $uses
60+
* @return non-empty-string|null
5761
*/
5862
private function resolveFullyQualifiedClass(array $uses, Node $node, string $tag): ?string
5963
{

src/ChangesReporting/Output/ConsoleOutputFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private function addEditorUrl(
167167
if ($editorUrl !== '') {
168168
$editorUrl = str_replace(
169169
['%file%', '%relFile%', '%line%'],
170-
[$absoluteFilePath, $relativeFilePath, $lineNumber],
170+
[(string) $absoluteFilePath, (string) $relativeFilePath, (string) $lineNumber],
171171
$editorUrl,
172172
);
173173
$filePath = '<href=' . OutputFormatter::escape($editorUrl) . '>' . $filePath . '</>';

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public function enterNode(Node $node): ?Node
4646
foreach ($stmt->vars as $variable) {
4747
if ($variable instanceof Variable && ! $variable->name instanceof Expr) {
4848
$variable->setAttribute(AttributeKey::IS_GLOBAL_VAR, true);
49-
$globalVariableNames[] = $variable->name;
49+
50+
/** @var string $variableName */
51+
$variableName = $variable->name;
52+
$globalVariableNames[] = $variableName;
5053
}
5154
}
5255
}

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ public function enterNode(Node $node): ?Node
4444
}
4545

4646
foreach ($stmt->vars as $staticVar) {
47-
if (! $staticVar->var->name instanceof Expr) {
48-
$staticVar->var->setAttribute(AttributeKey::IS_STATIC_VAR, true);
49-
$staticVariableNames[] = $staticVar->var->name;
47+
if (! is_string($staticVar->var->name)) {
48+
continue;
5049
}
50+
51+
$staticVar->var->setAttribute(AttributeKey::IS_STATIC_VAR, true);
52+
$staticVariableNames[] = $staticVar->var->name;
5153
}
5254
}
5355

src/PHPStanStaticTypeMapper/TypeMapper/ObjectWithoutClassTypeMapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PhpParser\Node\Identifier;
99
use PhpParser\Node\Name\FullyQualified;
1010
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
11+
use PHPStan\Type\Accessory\HasMethodType;
12+
use PHPStan\Type\Accessory\HasPropertyType;
1113
use PHPStan\Type\ObjectWithoutClassType;
1214
use PHPStan\Type\Type;
1315
use Rector\NodeTypeResolver\PHPStan\ObjectWithoutClassTypeWithParentTypes;
@@ -39,7 +41,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
3941
}
4042

4143
/**
42-
* @param ObjectWithoutClassType $type
44+
* @param ObjectWithoutClassType|HasMethodType|HasPropertyType $type
4345
*/
4446
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
4547
{

src/PhpAttribute/AttributeArrayNameInliner.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
final class AttributeArrayNameInliner
1717
{
1818
/**
19-
* @param Array_|Arg[] $array
20-
* @return Arg[]
19+
* @param Array_|list<Arg> $array
20+
* @return list<Arg>
2121
*/
2222
public function inlineArrayToArgs(Array_|array $array): array
2323
{
@@ -29,7 +29,7 @@ public function inlineArrayToArgs(Array_|array $array): array
2929
}
3030

3131
/**
32-
* @return Arg[]
32+
* @return list<Arg>
3333
*/
3434
private function inlineArrayNode(Array_ $array): array
3535
{
@@ -53,8 +53,8 @@ private function inlineArrayNode(Array_ $array): array
5353
}
5454

5555
/**
56-
* @param Arg[] $args
57-
* @return Arg[]
56+
* @param list<Arg> $args
57+
* @return list<Arg>
5858
*/
5959
private function inlineArray(array $args): array
6060
{

src/PhpAttribute/NodeFactory/NamedArgsFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class NamedArgsFactory
1616
{
1717
/**
1818
* @param array<string|int, mixed|Expr> $values
19-
* @return Arg[]
19+
* @return list<Arg>
2020
*/
2121
public function createFromValues(array $values): array
2222
{

src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function create(
8181
array $uses
8282
): AttributeGroup {
8383
$values = $doctrineAnnotationTagValueNode->getValuesWithSilentKey();
84-
$args = $this->createArgsFromItems($values, '', $annotationToAttribute->getClassReferenceFields());
84+
$args = $this->createArgsFromItems($values, $annotationToAttribute->getClassReferenceFields());
8585

8686
$this->annotationToAttributeIntegerValueCaster->castAttributeTypes($annotationToAttribute, $args);
8787

@@ -112,13 +112,10 @@ public function create(
112112
* @param ArrayItemNode[]|mixed[] $items
113113
* @param string[] $classReferencedFields
114114
*
115-
* @return Arg[]
115+
* @return list<Arg>
116116
*/
117-
public function createArgsFromItems(
118-
array $items,
119-
string $attributeClass = '',
120-
array $classReferencedFields = []
121-
): array {
117+
public function createArgsFromItems(array $items, array $classReferencedFields = []): array
118+
{
122119
$mappedItems = $this->annotationToAttributeMapper->map($items);
123120

124121
$this->mapClassReferences($mappedItems, $classReferencedFields);

src/PhpAttribute/NodeFactory/PhpNestedAttributeGroupFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function createNested(
114114
}
115115

116116
/**
117-
* @return Arg[]
117+
* @return list<Arg>
118118
*/
119119
private function createAttributeArgs(
120120
DoctrineAnnotationTagValueNode $nestedDoctrineAnnotationTagValueNode
@@ -126,7 +126,7 @@ private function createAttributeArgs(
126126

127127
/**
128128
* @param ArrayItemNode[] $arrayItemNodes
129-
* @return Arg[]
129+
* @return list<Arg>
130130
*/
131131
private function createArgsFromItems(array $arrayItemNodes): array
132132
{

src/PhpParser/ValueObject/StmtsAndTokens.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace Rector\PhpParser\ValueObject;
66

77
use PhpParser\Node\Stmt;
8+
use PhpParser\Token;
89

910
final readonly class StmtsAndTokens
1011
{
1112
/**
1213
* @param Stmt[] $stmts
13-
* @param array<int, array{int, string, int}|string> $tokens
14+
* @param Token[] $tokens
1415
*/
1516
public function __construct(
1617
private array $stmts,
@@ -27,7 +28,7 @@ public function getStmts(): array
2728
}
2829

2930
/**
30-
* @return array<int, array{int, string, int}|string>
31+
* @return array<int, Token[]>
3132
*/
3233
public function getTokens(): array
3334
{

tests/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher/ResolveTagToKnownFullyQualifiedNameTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\Testing\Fixture\FixtureFileFinder;
1717
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
1818
use Rector\Testing\TestingParser\TestingParser;
19+
use Webmozart\Assert\Assert;
1920

2021
final class ResolveTagToKnownFullyQualifiedNameTest extends AbstractLazyTestCase
2122
{
@@ -51,6 +52,7 @@ public function testResolvesClass(string $filePath): void
5152
$varTagValueNode = $phpDoc->getVarTagValueNode();
5253
$this->assertInstanceOf(VarTagValueNode::class, $varTagValueNode);
5354

55+
/** @var non-empty-string $value */
5456
$value = $varTagValueNode->type->__toString();
5557
$propertyName = strtolower($this->nodeNameResolver->getName($property));
5658

@@ -59,6 +61,8 @@ public function testResolvesClass(string $filePath): void
5961
$this->assertStringContainsString('Unknown', $result);
6062
} elseif (str_contains($propertyName, 'aliased')) {
6163
$unaliasedClass = str_replace('Aliased', '', $value);
64+
Assert::notEmpty($unaliasedClass);
65+
6266
$this->assertStringEndsWith($unaliasedClass, $result);
6367
} elseif (str_starts_with($propertyName, 'known')) {
6468
$this->assertStringEndsWith($value, $result);

0 commit comments

Comments
 (0)