Skip to content

Commit

Permalink
[Php81] Skip override __construct from interface on NewInInitializerR…
Browse files Browse the repository at this point in the history
…ector (#1913)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user authored Mar 7, 2022
1 parent c149cb4 commit dbdcaa1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function hasParentClassMethod(ClassReflection $classReflection, string $m
return $this->resolveParentClassMethods($classReflection, $methodName) !== [];
}

/**
* Look both parent class and interface, yes, all PHP interface methods are abstract
*/
public function hasAbstractParentClassMethod(ClassReflection $classReflection, string $methodName): bool
{
$parentClassMethods = $this->resolveParentClassMethods($classReflection, $methodName);
Expand Down Expand Up @@ -87,18 +90,19 @@ public function resolveParentClassMethodReturnType(ClassReflection $classReflect
private function resolveParentClassMethods(ClassReflection $classReflection, string $methodName): array
{
$parentClassMethods = [];
foreach ($classReflection->getParents() as $parentClassReflections) {
if (! $parentClassReflections->hasMethod($methodName)) {
$parents = array_merge($classReflection->getParents(), $classReflection->getInterfaces());
foreach ($parents as $parent) {
if (! $parent->hasMethod($methodName)) {
continue;
}

$methodReflection = $parentClassReflections->getNativeMethod($methodName);
$methodReflection = $parent->getNativeMethod($methodName);
if (! $methodReflection instanceof PhpMethodReflection) {
continue;
}

$methodDeclaringMethodClass = $methodReflection->getDeclaringClass();
if ($methodDeclaringMethodClass->getName() === $parentClassReflections->getName()) {
if ($methodDeclaringMethodClass->getName() === $parent->getName()) {
$parentClassMethods[] = $methodReflection;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;

use DateTime;
use Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source\OverrideInterfaceMethod;

class SkipOverrideInterfaceMethod implements OverrideInterfaceMethod
{
private DateTime $dateTime;

public function __construct(
?DateTime $dateTime = null
) {
$this->dateTime = $dateTime ?? new DateTime('now');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source;

use DateTime;

interface OverrideInterfaceMethod
{
public function __construct(
?DateTime $dateTime = null
);
}

0 comments on commit dbdcaa1

Please sign in to comment.