Skip to content

Commit 6e1ef76

Browse files
authored
Raise PHPStan level to 9 (#175)
* require functionMap on construction * add doc comment for $fullSymbolName * Check for class instead of checking for property * raise phpstan level to 9
1 parent e611a83 commit 6e1ef76

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ parameters:
77
- tests/
88
excludePaths:
99
- tests/data/
10-
level: 8
10+
level: 9
1111
featureToggles:
1212
alwaysTrueAlwaysReported: true
1313
listType: true

src/Visitor.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
use PhpParser\Node\Expr\FuncCall;
2323
use PhpParser\Node\Scalar\String_;
2424
use PhpParser\Node\Stmt\Class_;
25+
use PhpParser\Node\Stmt\ClassLike;
2526
use PhpParser\Node\Stmt\ClassMethod;
2627
use PhpParser\Node\Stmt\Expression;
2728
use PhpParser\Node\Stmt\Function_;
29+
use PhpParser\Node\Stmt\Namespace_;
2830
use PhpParser\Node\Stmt\Property;
2931
use PhpParser\Node\Stmt\Return_ as Stmt_Return;
3032
use StubsGenerator\NodeVisitor;
@@ -35,8 +37,8 @@ class Visitor extends NodeVisitor
3537
{
3638
private \phpDocumentor\Reflection\DocBlockFactory $docBlockFactory;
3739

38-
/** @var ?array<string,array<int|string,string>> */
39-
private ?array $functionMap = null;
40+
/** @var array<string,array<int|string,string>> */
41+
private array $functionMap;
4042

4143
/** @var array<string, list<\PhpStubs\WordPress\Core\WordPressTag>> */
4244
private array $additionalTags = [];
@@ -50,6 +52,7 @@ public function __construct()
5052
{
5153
$this->docBlockFactory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
5254
$this->nodeFinder = new NodeFinder();
55+
$this->functionMap = require sprintf('%s/functionMap.php', dirname(__DIR__));
5356
}
5457

5558
/**
@@ -146,7 +149,7 @@ public function getStubStmts(): array
146149

147150
private function postProcessNode(Node $node): void
148151
{
149-
if (property_exists($node, 'stmts') && is_array($node->stmts)) {
152+
if ($node instanceof ClassLike || $node instanceof Namespace_) {
150153
foreach ($node->stmts as $stmt) {
151154
$this->postProcessNode($stmt);
152155
}
@@ -156,9 +159,10 @@ private function postProcessNode(Node $node): void
156159
return;
157160
}
158161

159-
$name = $node->getAttribute('fullSymbolName');
162+
/** @var ?string $fullSymbolName */
163+
$fullSymbolName = $node->getAttribute('fullSymbolName');
160164

161-
if ($name === null) {
165+
if ($fullSymbolName === null) {
162166
return;
163167
}
164168

@@ -168,13 +172,13 @@ private function postProcessNode(Node $node): void
168172
return;
169173
}
170174

171-
$newDocComment = $this->addTags($name, $docComment);
175+
$newDocComment = $this->addTags($fullSymbolName, $docComment);
172176

173177
if ($newDocComment instanceof Doc) {
174178
$node->setDocComment($newDocComment);
175179
}
176180

177-
if (! isset($this->additionalTagStrings[$name])) {
181+
if (! isset($this->additionalTagStrings[$fullSymbolName])) {
178182
return;
179183
}
180184

@@ -184,7 +188,7 @@ private function postProcessNode(Node $node): void
184188
return;
185189
}
186190

187-
$newDocComment = $this->addStringTags($name, $docComment);
191+
$newDocComment = $this->addStringTags($fullSymbolName, $docComment);
188192

189193
if (! ($newDocComment instanceof Doc)) {
190194
return;
@@ -426,16 +430,14 @@ static function (WordPressTag $tag) use ($matchNames): bool {
426430
*/
427431
private function getAdditionalTagsFromMap(string $symbolName): array
428432
{
429-
if ($this->functionMap === null) {
430-
$this->functionMap = require sprintf('%s/functionMap.php', dirname(__DIR__));
431-
}
432-
433-
if (! isset($this->functionMap[$symbolName])) {
433+
if (! array_key_exists($symbolName, $this->functionMap)) {
434434
return [];
435435
}
436436

437437
$parameters = $this->functionMap[$symbolName];
438438
$returnType = array_shift($parameters);
439+
/** @var array<string, string> $parameters */
440+
439441
$additions = [];
440442

441443
foreach ($parameters as $paramName => $paramType) {

0 commit comments

Comments
 (0)