Skip to content

Commit

Permalink
Raise PHPStan level to 9 (#175)
Browse files Browse the repository at this point in the history
* require functionMap on construction

* add doc comment for $fullSymbolName

* Check for class instead of checking for property

* raise phpstan level to 9
  • Loading branch information
IanDelMar authored May 9, 2024
1 parent e611a83 commit 6e1ef76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parameters:
- tests/
excludePaths:
- tests/data/
level: 8
level: 9
featureToggles:
alwaysTrueAlwaysReported: true
listType: true
28 changes: 15 additions & 13 deletions src/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_ as Stmt_Return;
use StubsGenerator\NodeVisitor;
Expand All @@ -35,8 +37,8 @@ class Visitor extends NodeVisitor
{
private \phpDocumentor\Reflection\DocBlockFactory $docBlockFactory;

/** @var ?array<string,array<int|string,string>> */
private ?array $functionMap = null;
/** @var array<string,array<int|string,string>> */
private array $functionMap;

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

/**
Expand Down Expand Up @@ -146,7 +149,7 @@ public function getStubStmts(): array

private function postProcessNode(Node $node): void
{
if (property_exists($node, 'stmts') && is_array($node->stmts)) {
if ($node instanceof ClassLike || $node instanceof Namespace_) {
foreach ($node->stmts as $stmt) {
$this->postProcessNode($stmt);
}
Expand All @@ -156,9 +159,10 @@ private function postProcessNode(Node $node): void
return;
}

$name = $node->getAttribute('fullSymbolName');
/** @var ?string $fullSymbolName */
$fullSymbolName = $node->getAttribute('fullSymbolName');

if ($name === null) {
if ($fullSymbolName === null) {
return;
}

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

$newDocComment = $this->addTags($name, $docComment);
$newDocComment = $this->addTags($fullSymbolName, $docComment);

if ($newDocComment instanceof Doc) {
$node->setDocComment($newDocComment);
}

if (! isset($this->additionalTagStrings[$name])) {
if (! isset($this->additionalTagStrings[$fullSymbolName])) {
return;
}

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

$newDocComment = $this->addStringTags($name, $docComment);
$newDocComment = $this->addStringTags($fullSymbolName, $docComment);

if (! ($newDocComment instanceof Doc)) {
return;
Expand Down Expand Up @@ -426,16 +430,14 @@ static function (WordPressTag $tag) use ($matchNames): bool {
*/
private function getAdditionalTagsFromMap(string $symbolName): array
{
if ($this->functionMap === null) {
$this->functionMap = require sprintf('%s/functionMap.php', dirname(__DIR__));
}

if (! isset($this->functionMap[$symbolName])) {
if (! array_key_exists($symbolName, $this->functionMap)) {
return [];
}

$parameters = $this->functionMap[$symbolName];
$returnType = array_shift($parameters);
/** @var array<string, string> $parameters */

$additions = [];

foreach ($parameters as $paramName => $paramType) {
Expand Down

0 comments on commit 6e1ef76

Please sign in to comment.