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
Show file tree
Hide file tree
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
Expand Up @@ -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
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
10 changes: 9 additions & 1 deletion src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\PhpDocParser\Printer;

use LogicException;
use PhpParser\Comment\Doc;
use PhpParser\Node as PhpNode;
use PhpParser\NodeTraverser as PhpParserNodeTraverser;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e104033

Please sign in to comment.