Skip to content

Commit

Permalink
Added strict rules to tests (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
lulco authored Oct 11, 2023
1 parent 9b079af commit eb0d99d
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 3 deletions.
1 change: 1 addition & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ services:
- addNodeVisitor(200, Efabrica\PHPStanLatte\Compiler\NodeVisitor\TransformForeachWithIteratorNodeVisitor())
- addNodeVisitor(200, Efabrica\PHPStanLatte\Compiler\NodeVisitor\LinkNodeVisitor())
- addNodeVisitor(200, Efabrica\PHPStanLatte\Compiler\NodeVisitor\ChangeNotNullToEqualsNullNodeVisitor())
- addNodeVisitor(200, Efabrica\PHPStanLatte\Compiler\NodeVisitor\ChangeGetParentNameToCompareWithNullNodeVisitor())
- addNodeVisitor(300, Efabrica\PHPStanLatte\Compiler\NodeVisitor\AddFormClassesNodeVisitor())
- addNodeVisitor(300, Efabrica\PHPStanLatte\Compiler\NodeVisitor\ReportNonExistingFieldOptionNodeVisitor())
- addNodeVisitor(9900, Efabrica\PHPStanLatte\Compiler\NodeVisitor\CleanupNodeVisitor())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Efabrica\PHPStanLatte\Compiler\NodeVisitor;

use Efabrica\PHPStanLatte\Resolver\NameResolver\NameResolver;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeVisitorAbstract;

/**
* changed output from:
* <code>
* if ($this->getParentName()) {
* return \get_defined_vars();
* }
* </code>
*
* to:
* <code>
* if ($this->getParentName() !== null) {
* return \get_defined_vars();
* }
* </code>
*/
final class ChangeGetParentNameToCompareWithNullNodeVisitor extends NodeVisitorAbstract
{
private NameResolver $nameResolver;

public function __construct(NameResolver $nameResolver)
{
$this->nameResolver = $nameResolver;
}

public function enterNode(Node $node): ?Node
{
if (!$node instanceof If_) {
return null;
}

if (!$node->cond instanceof MethodCall) {
return null;
}

if (!$node->cond->var instanceof Variable) {
return null;
}

if ($node->cond->var->name !== 'this') {
return null;
}

if ($this->nameResolver->resolve($node->cond->name) !== 'getParentName') {
return null;
}

$node->cond = new NotIdentical($node->cond, new ConstFetch(new Name('null')));
return $node;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public function testVariables(): void
98,
'default.latte',
],
[
'Only booleans are allowed in an if condition, string|null given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
100,
'default.latte',
],
[
'Undefined variable: $fromRenderDefault',
8,
Expand Down Expand Up @@ -226,6 +231,11 @@ public function testVariables(): void
4,
'parent.latte',
],
[
'Only booleans are allowed in a ternary operator condition, int|false given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
30,
'specialConstructs.latte',
],
[
'Undefined variable: $nonExistingVariable',
5,
Expand Down Expand Up @@ -668,7 +678,7 @@ public function testComponents(): void

public function testForms(): void
{
$this->analyse([__DIR__ . '/Fixtures/FormsPresenter.php'], [
$expectedErrors = [
[
'Form control with name "password" probably does not exist.',
4,
Expand Down Expand Up @@ -794,7 +804,36 @@ public function testForms(): void
10,
'@layout.latte',
],
]);
];
if (LatteVersion::isLatte3()) {
$expectedErrors[] = [
'PHPDoc tag @var with type Nette\Forms\Controls\BaseControl is not subtype of type Nette\Forms\Controls\TextArea.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
55,
'default.latte',
];
} else {
$expectedErrors[] = [
'Only booleans are allowed in an if condition, Nette\Utils\Html|null given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
10,
'default.latte',
];
$expectedErrors[] = [
'Only booleans are allowed in an if condition, Nette\Utils\Html|null given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
11,
'default.latte',
];
$expectedErrors[] = [
'Only booleans are allowed in an if condition, Nette\Utils\Html|string|null given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
12,
'default.latte',
];
$expectedErrors[] = [
'Only booleans are allowed in an if condition, Nette\Utils\Html|string|null given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
54,
'default.latte',
];
}
$this->analyse([__DIR__ . '/Fixtures/FormsPresenter.php'], $expectedErrors);
}

public function testFilters(): void
Expand Down Expand Up @@ -1100,6 +1139,11 @@ public function testLinks(): void
98,
'default.latte',
],
[
'Only booleans are allowed in a ternary operator condition, int|false given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
102,
'default.latte',
],
[
'Undefined variable: $title',
7,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function testLinks(): void
98,
'default.latte',
],
[
'Only booleans are allowed in a ternary operator condition, int|false given.', // Should be removed after issue https://github.com/efabrica-team/phpstan-latte/issues/444 is resolved
102,
'default.latte',
],
[
'Parameter #1 $destination of method Nette\Application\UI\Component::link() expects string, Latte\Runtime\Html|string|false given.',
103,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
latte:
features:
phpstanCommand: "vendor/bin/phpstan analyse {dir} --level 8 --no-progress"
phpstanCommand: "vendor/bin/phpstan analyse {dir} --level 8 --no-progress -c tests/strict-rules.neon"
1 change: 1 addition & 0 deletions tests/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rules:
includes:
- %rootDir%/../../../vendor/phpstan/phpstan-nette/extension.neon
- %rootDir%/../../../vendor/phpstan/phpstan-nette/rules.neon
- strict-rules.neon
5 changes: 5 additions & 0 deletions tests/strict-rules.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
reportMaybesInMethodSignatures: false

includes:
- %rootDir%/../../../vendor/phpstan/phpstan-strict-rules/rules.neon

0 comments on commit eb0d99d

Please sign in to comment.