Skip to content

Commit

Permalink
Fix PHPStan failures
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 26, 2024
1 parent 426fd7a commit e104033
Showing 5 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -11,3 +11,6 @@ parameters:
level: 8
ignoreErrors:
- '#^Dynamic call to static method PHPUnit\\Framework\\Assert#'
-
identifier: return.unusedType
path: tests/*
2 changes: 1 addition & 1 deletion src/Ast/NodeVisitor/CloningVisitor.php
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
final class CloningVisitor extends AbstractNodeVisitor
{

public function enterNode(Node $originalNode)
public function enterNode(Node $originalNode): Node
{
$node = clone $originalNode;
$node->setAttribute(Attribute::ORIGINAL_NODE, $originalNode);
3 changes: 1 addition & 2 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
use PHPStan\PhpDocParser\ParserConfig;
use PHPStan\ShouldNotHappenException;
use function array_key_exists;
use function array_values;
use function count;
use function rtrim;
use function str_replace;
@@ -120,7 +119,7 @@ public function parse(TokenIterator $tokens): Ast\PhpDoc\PhpDocNode
return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocNode([$this->enrichWithAttributes($tokens, $tag, $startLine, $startIndex)]), 1, 0);
}

return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocNode(array_values($children)), 1, 0);
return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocNode($children), 1, 0);
}


10 changes: 9 additions & 1 deletion src/Printer/Printer.php
Original file line number Diff line number Diff line change
@@ -554,7 +554,11 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
if (!$newNode instanceof Node || !$originalNode instanceof Node) {
return null;
}

/** @var int $itemStartPos */
$itemStartPos = $originalNode->getAttribute(Attribute::START_INDEX);

/** @var int $itemEndPos */
$itemEndPos = $originalNode->getAttribute(Attribute::END_INDEX);
if ($itemStartPos < 0 || $itemEndPos < 0 || $itemStartPos < $tokenIndex) {
throw new LogicException();
@@ -617,6 +621,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
continue;
}

/** @var int $itemEndPos */
$itemEndPos = $tokenIndex - 1;
if ($insertNewline) {
$result .= $insertStr . sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
@@ -642,7 +647,10 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
return null;
}

/** @var int $itemStartPos */
$itemStartPos = $originalNode->getAttribute(Attribute::START_INDEX);

/** @var int $itemEndPos */
$itemEndPos = $originalNode->getAttribute(Attribute::END_INDEX);
if ($itemStartPos < 0 || $itemEndPos < 0) {
throw new LogicException();
@@ -702,7 +710,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
}

/**
* @param Node[] $nodes
* @param array<Node|null> $nodes
* @return array{bool, string, string}
*/
private function isMultiline(int $initialIndex, array $nodes, TokenIterator $originalTokens): array
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace PHPStan\PhpDocParser\Printer;

use LogicException;
use PhpParser\Comment\Doc;
use PhpParser\Node as PhpNode;
use PhpParser\NodeTraverser as PhpParserNodeTraverser;
@@ -78,8 +79,10 @@ public function testPrint(string $file, string $expectedFile, NodeVisitor $visit
$this->fail('Could not read ' . $file);
}

/** @var PhpNode[] $oldStmts */
$oldStmts = $phpParser->parse($fileContents);
if ($oldStmts === null) {
throw new LogicException();
}
$oldTokens = $phpParser->getTokens();

$phpTraverser2 = new PhpParserNodeTraverser();

0 comments on commit e104033

Please sign in to comment.