Skip to content

Commit

Permalink
AddMethodCallBasedStrictParamTypeRector: Support changing protected m…
Browse files Browse the repository at this point in the history
…ethods in final classes (#4611)

* AddMethodCallBasedStrictParamTypeRector: Support changing protected methods in final classes

* Update final_class_without_extends.php.inc

* Create skip_final_class_with_extends.php.inc

* Update skip_final_class_with_extends.php.inc

* Create skip_non_final_class_without_extends.php.inc

* fix per review

* Support final methods without base-types
  • Loading branch information
staabm committed Jul 27, 2023
1 parent 3a95b14 commit 0b96adb
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class ProtectedMethodInFinalClassWithoutBaseClass
{
public function runFirst(string $data)
{
$this->process($data);
}

protected function process($data)
{
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class ProtectedMethodInFinalClassWithoutBaseClass
{
public function runFirst(string $data)
{
$this->process($data);
}

protected function process(string $data)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

class FinalProtectedMethodInClassWithoutBaseClass
{
public function runFirst(string $data)
{
$this->process($data);
}

final protected function process($data)
{
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

class FinalProtectedMethodInClassWithoutBaseClass
{
public function runFirst(string $data)
{
$this->process($data);
}

final protected function process(string $data)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class SkipProtectedMethodInFinalClassWithBaseClass extends SomeClass
{
public function runFirst(string $data)
{
$this->process($data);
}

protected function process($data)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class ProtectedMethodInFinalClassImplementingInterface implements SomeUnknownInterface
{
public function runFirst(string $data)
{
$this->process($data);
}

protected function process($data)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

class SkipProtectedMethodInNonFinalClassWithoutBaseClass
{
public function runFirst(string $data)
{
$this->process($data);
}

protected function process($data)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\PhpParser\NodeFinder\LocalMethodCallFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\TypeDeclaration\NodeAnalyzer\CallTypesResolver;
use Rector\TypeDeclaration\NodeAnalyzer\ClassMethodParamTypeCompleter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -88,7 +89,18 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $method->isPrivate()) {
$isPrivate =
($node->isFinal()
&& $node->extends === null
&& $node->implements === []
&& $method->isProtected())
|| ($method->isFinal()
&& $node->extends === null
&& $node->implements === []
)
|| $method->isPrivate();

if (!$isPrivate) {
continue;
}

Expand Down

0 comments on commit 0b96adb

Please sign in to comment.