Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5886053

Browse files
committed
Bleeding edge - detect wrong usage of@var PHPDoc tag
1 parent4f7ac53 commit5886053

File tree

6 files changed

+80
-6
lines changed

6 files changed

+80
-6
lines changed

‎conf/bleedingEdge.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ parameters:
1212
checkLogicalAndConstantCondition:true
1313
checkLogicalOrConstantCondition:true
1414
checkMissingTemplateTypeInParameter:true
15+
wrongVarUsage:true

‎conf/config.level2.neon‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ rules:
3030
- PHPStan\Rules\PhpDoc\InvalidPhpDocTagValueRule
3131
- PHPStan\Rules\PhpDoc\InvalidPHPStanDocTagRule
3232
- PHPStan\Rules\PhpDoc\InvalidThrowsPhpDocValueRule
33-
- PHPStan\Rules\PhpDoc\WrongVariableNameInVarTagRule
3433

3534
services:
3635
-
@@ -52,3 +51,9 @@ services:
5251
checkMissingVarTagTypehint:%checkMissingVarTagTypehint%
5352
tags:
5453
- phpstan.rules.rule
54+
-
55+
class:PHPStan\Rules\PhpDoc\WrongVariableNameInVarTagRule
56+
arguments:
57+
checkWrongVarUsage:%featureToggles.wrongVarUsage%
58+
tags:
59+
- phpstan.rules.rule

‎conf/config.neon‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ parameters:
2525
checkLogicalAndConstantCondition:false
2626
checkLogicalOrConstantCondition:false
2727
checkMissingTemplateTypeInParameter:false
28+
wrongVarUsage:false
2829
fileExtensions:
2930
- php
3031
checkAlwaysTrueCheckTypeFunctionCall:false
@@ -176,7 +177,8 @@ parametersSchema:
176177
detectDuplicateStubFiles:bool(),
177178
checkLogicalAndConstantCondition:bool(),
178179
checkLogicalOrConstantCondition:bool(),
179-
checkMissingTemplateTypeInParameter:bool()
180+
checkMissingTemplateTypeInParameter:bool(),
181+
wrongVarUsage:bool()
180182
])
181183
fileExtensions:listOf(string())
182184
checkAlwaysTrueCheckTypeFunctionCall:bool()

‎src/Rules/PhpDoc/WrongVariableNameInVarTagRule.php‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
usePhpParser\Node;
77
usePhpParser\Node\Expr;
88
usePHPStan\Analyser\Scope;
9-
usePHPStan\Node\UnreachableStatementNode;
9+
usePHPStan\Node\InClassMethodNode;
10+
usePHPStan\Node\InClassNode;
11+
usePHPStan\Node\InFunctionNode;
12+
usePHPStan\Node\VirtualNode;
1013
usePHPStan\Rules\Rule;
1114
usePHPStan\Rules\RuleErrorBuilder;
1215
usePHPStan\Type\FileTypeMapper;
@@ -19,9 +22,15 @@ class WrongVariableNameInVarTagRule implements Rule
1922

2023
privateFileTypeMapper$fileTypeMapper;
2124

22-
publicfunction__construct(FileTypeMapper$fileTypeMapper)
25+
privatebool$checkWrongVarUsage;
26+
27+
publicfunction__construct(
28+
FileTypeMapper$fileTypeMapper,
29+
bool$checkWrongVarUsage =false
30+
)
2331
{
2432
$this->fileTypeMapper =$fileTypeMapper;
33+
$this->checkWrongVarUsage =$checkWrongVarUsage;
2534
}
2635

2736
publicfunctiongetNodeType():string
@@ -36,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
3645
||$nodeinstanceofNode\Stmt\PropertyProperty
3746
||$nodeinstanceofNode\Stmt\ClassConst
3847
||$nodeinstanceofNode\Stmt\Const_
39-
||$nodeinstanceofUnreachableStatementNode
48+
||($nodeinstanceofVirtualNode && !$nodeinstanceof InFunctionNode && !$nodeinstanceof InClassMethodNode && !$nodeinstanceof InClassNode)
4049
) {
4150
return [];
4251
}
@@ -83,6 +92,30 @@ public function processNode(Node $node, Scope $scope): array
8392
return$this->processGlobal($scope,$node,$varTags);
8493
}
8594

95+
if ($nodeinstanceof InClassNode ||$nodeinstanceof InClassMethodNode ||$nodeinstanceof InFunctionNode) {
96+
if ($this->checkWrongVarUsage) {
97+
$description ='a function';
98+
$originalNode =$node->getOriginalNode();
99+
if ($originalNodeinstanceofNode\Stmt\Interface_) {
100+
$description ='an interface';
101+
}elseif ($originalNodeinstanceofNode\Stmt\Class_) {
102+
$description ='a class';
103+
}elseif ($originalNodeinstanceofNode\Stmt\Trait_) {
104+
thrownew \PHPStan\ShouldNotHappenException();
105+
}elseif ($originalNodeinstanceofNode\Stmt\ClassMethod) {
106+
$description ='a method';
107+
}
108+
return [
109+
RuleErrorBuilder::message(sprintf(
110+
'PHPDoc tag @var above %s has no effect.',
111+
$description
112+
))->build(),
113+
];
114+
}
115+
116+
return [];
117+
}
118+
86119
return$this->processStmt($scope,$varTags,null);
87120
}
88121

‎tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class WrongVariableNameInVarTagRuleTest extends RuleTestCase
1515
protectedfunctiongetRule():Rule
1616
{
1717
returnnewWrongVariableNameInVarTagRule(
18-
self::getContainer()->getByType(FileTypeMapper::class)
18+
self::getContainer()->getByType(FileTypeMapper::class),
19+
true
1920
);
2021
}
2122

@@ -110,6 +111,18 @@ public function testRule(): void
110111
'Variable $slots in PHPDoc tag @var does not match assigned variable $itemSlots.',
111112
280,
112113
],
114+
[
115+
'PHPDoc tag @var above a class has no effect.',
116+
300,
117+
],
118+
[
119+
'PHPDoc tag @var above a method has no effect.',
120+
304,
121+
],
122+
[
123+
'PHPDoc tag @var above a function has no effect.',
124+
312,
125+
],
113126
]);
114127
}
115128

‎tests/PHPStan/Rules/PhpDoc/data/wrong-variable-name-var.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,23 @@ public function doSit(): void
293293
}
294294

295295
}
296+
297+
/**
298+
* @var string
299+
*/
300+
class VarInWrongPlaces
301+
{
302+
303+
/** @var int $a */
304+
publicfunctiondoFoo($a)
305+
{
306+
307+
}
308+
309+
}
310+
311+
/** @var int */
312+
functiondoFoo():void
313+
{
314+
315+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp