Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassLike\AddVoidReturnTypeWhereNoReturnRector\Fixture;

class FinalEmptyMethodInNonFinalClass
{
final public function run()
{
}

final public function throwOnly()
{
throw new \RuntimeException();
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassLike\AddVoidReturnTypeWhereNoReturnRector\Fixture;

class FinalEmptyMethodInNonFinalClass
{
final public function run(): void
{
}

final public function throwOnly(): void
{
throw new \RuntimeException();
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassLike\AddVoidReturnTypeWhereNoReturnRector\Fixture;

abstract class FinalMethodsInAbstractClass
{
final public function run()
{
echo 1;
}

final protected function prot()
{
echo 1;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassLike\AddVoidReturnTypeWhereNoReturnRector\Fixture;

abstract class FinalMethodsInAbstractClass
{
final public function run(): void
{
echo 1;
}

final protected function prot(): void
{
echo 1;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassLike\AddVoidReturnTypeWhereNoReturnRector\Fixture;

class FinalProtectedMethodInNonFinalClass
{
final protected function run()
{
echo 1;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassLike\AddVoidReturnTypeWhereNoReturnRector\Fixture;

class FinalProtectedMethodInNonFinalClass
{
final protected function run(): void
{
echo 1;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,24 @@ private function shouldSkipClassMethod(
return true;
}

// final method cannot be overridden by child
$canBeOverridden = ! $isFinalClass && ! $classMethod->isFinal();

// is not final and has only exception? possibly implemented by child
if (! $isFinalClass && $this->hasExceptionOnly($classMethod)) {
if ($canBeOverridden && $this->hasExceptionOnly($classMethod)) {
return true;
}

// possibly required by child implementation
if (! $isFinalClass && $classMethod->stmts === []) {
if ($canBeOverridden && $classMethod->stmts === []) {
return true;
}

if ($classMethod->isProtected()) {
return ! $isFinalClass;
return $canBeOverridden;
}

return $classReflection->isAbstract();
return ! $classMethod->isFinal() && $classReflection->isAbstract();
}

private function hasExceptionOnly(ClassMethod $classMethod): bool
Expand Down
Loading