Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added strict rules to tests #449

Merged
merged 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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