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

[CodeQuality] Skip call parent::__construct() indirect parent on ConstructClassMethodToSetUpTestCaseRector #251

Merged
merged 20 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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 phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<directory>rules-tests</directory>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba here rules-tests need to be registered to show the PHPUnit error, it was never executed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, that's a big miss from my side 🤦 😅

<exclude>tests/Rector/Class_/AddSeeTestAnnotationRector/Source</exclude>
<exclude>tests/Rector/ClassMethod/DependsAnnotationWithValueToAttributeRector/Source</exclude>
</testsuite>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector\Fixture;

use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector\Source\TestResponse;

final class SkipCallParentConstructExists extends TestResponse
{
private $someValue;

public function __construct()
{
$this->someValue = 1000;

parent::__construct();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipParameterUsed extends TestCase
{
public function __construct($param)
{
$this->initEarly($param);
}

private function initEarly($param)
{
echo 'init';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector\Source;

use PHPUnit\Framework\TestCase;

abstract class TestResponse extends TestCase
{
public function __construct()
{
echo "init";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -66,7 +67,7 @@ public function refactor(Node $node): ?int
return null;
}

if (! $this->isName($node->name, 'test*')) {
if (! fnmatch('test*', $node->name->toString(), FNM_NOESCAPE)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->isName($node->name, 'test*')) {
if (fnmatch('test*', $node->name->toString(), FNM_NOESCAPE)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba @staabm using fnmatch() directly seems the solution

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->isName($node->name, 'assert*')) {
if (! fnmatch('assert*', $methodName, FNM_NOESCAPE)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder;
use Rector\PHPUnit\PhpDoc\DataProviderMethodRenamer;
Expand Down Expand Up @@ -96,7 +97,7 @@ public function refactor(Node $node): ?Node

$dataProviderClassMethods = $this->dataProviderClassMethodFinder->find($node);
foreach ($dataProviderClassMethods as $dataProviderClassMethod) {
if (! $this->isName($dataProviderClassMethod, 'test*')) {
if (! fnmatch('test*', $dataProviderClassMethod->name->toString(), FNM_NOESCAPE)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NodeAnalyzer/TestsNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function isTestClassMethod(ClassMethod $classMethod): bool
return false;
}

if ($this->nodeNameResolver->isName($classMethod, 'test*')) {
if (fnmatch('test*', $classMethod->name->toString(), FNM_NOESCAPE)) {
return true;
}

Expand Down
Loading