Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[PHPStanRules] Fix commented print node (#2704)
Browse files Browse the repository at this point in the history
* simplify getName

* [PHPStanRules] Fix commented print node
  • Loading branch information
TomasVotruba authored Dec 25, 2020
1 parent dcc78c0 commit 181da5e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
5 changes: 5 additions & 0 deletions packages/phpstan-rules/src/Printer/NodeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Param;
use PhpParser\PrettyPrinter\Standard;
use Symplify\PHPStanRules\ValueObject\PhpParserAttributeKey;

final class NodeComparator
{
Expand All @@ -23,6 +24,10 @@ public function __construct(Standard $standard)

public function areNodesEqual(Node $firstNode, Node $secondNode): bool
{
// remove comments from nodes
$firstNode->setAttribute(PhpParserAttributeKey::COMMENTS, null);
$secondNode->setAttribute(PhpParserAttributeKey::COMMENTS, null);

return $this->standard->prettyPrint([$firstNode]) === $this->standard->prettyPrint([$secondNode]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,15 @@ private function getParameterTypes(array $params): array
*/
private function getParamType(Node $node): ?string
{
if ($node instanceof Identifier) {
return $node->name;
if ($node instanceof Identifier || $node instanceof Name) {
return (string) $node;
}

if ($node instanceof NullableType) {
$node = $node->type;
return $this->getParamType($node);
}

if (method_exists($node, 'toString')) {
return $node->toString();
}

return null;
}

Expand Down
12 changes: 4 additions & 8 deletions packages/phpstan-rules/src/Rules/CheckTypehintCallerTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ public function getRuleDefinition(): RuleDefinition
class SomeClass
{
public function run(Node $node)
public function run(MethodCall $node)
{
if ($node instanceof MethodCall) {
$this->isCheck($node);
}
$this->isCheck($node);
}
private function isCheck(Node $node)
Expand All @@ -116,11 +114,9 @@ private function isCheck(Node $node)
class SomeClass
{
public function run(Node $node)
public function run(MethodCall $node)
{
if ($node instanceof MethodCall) {
$this->isCheck($node);
}
$this->isCheck($node);
}
private function isCheck(MethodCall $node)
Expand Down
13 changes: 13 additions & 0 deletions packages/phpstan-rules/src/ValueObject/PhpParserAttributeKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\ValueObject;

final class PhpParserAttributeKey
{
/**
* @var string
*/
public const COMMENTS = 'comments';
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function testRule(string $filePath, array $expectedErrorsWithLines): void

public function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/SkipDuplicatedCallOfSameMethodWithComment.php', []];

yield [__DIR__ . '/Fixture/SkipCorrectUnionType.php', []];
yield [__DIR__ . '/Fixture/SkipRecursive.php', []];
yield [__DIR__ . '/Fixture/SkipMixed.php', []];
Expand All @@ -43,7 +45,6 @@ public function provideData(): Iterator

$argErrorMessage = sprintf(CheckTypehintCallerTypeRule::ERROR_MESSAGE, 1, Arg::class);
$paramErrorMessage = sprintf(CheckTypehintCallerTypeRule::ERROR_MESSAGE, 2, Param::class);

yield [__DIR__ . '/Fixture/DoubleShot.php', [[$argErrorMessage, 13], [$paramErrorMessage, 13]]];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\CheckTypehintCallerTypeRule\Fixture;

use PHPStan\Node\FileNode;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;

final class SkipDuplicatedCallOfSameMethodWithComment
{
public function firstMethod()
{
$this->printFile(new FileWithoutNamespace([]));
}

/**
* @param FileWithoutNamespace|FileNode $node
*/
private function printFile(\PhpParser\Node $node)
{
}

public function secondMethod()
{
// some comment
$this->printFile(new FileNode([]));
}
}

0 comments on commit 181da5e

Please sign in to comment.