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
Expand Up @@ -4,6 +4,7 @@

namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
/**
* @param HasOffsetType $type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): never
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
{
throw new ShouldNotHappenException();
}
Expand Down
15 changes: 10 additions & 5 deletions packages/VendorLocker/ParentClassMethodTypeOverrideGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\Core\PhpParser\AstResolver;
use Rector\NodeNameResolver\NodeNameResolver;
Expand Down Expand Up @@ -35,6 +38,11 @@ public function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
return true;
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($parentClassMethodReflection->getVariants());
if ($parametersAcceptor instanceof FunctionVariantWithPhpDocs && ! $parametersAcceptor->getNativeReturnType() instanceof MixedType) {
return false;
}

$classReflection = $parentClassMethodReflection->getDeclaringClass();
$fileName = $classReflection->getFileName();

Expand Down Expand Up @@ -95,11 +103,8 @@ private function getParentClassMethod(ClassMethod $classMethod): ?MethodReflecti
return null;
}

foreach ($classReflection->getAncestors() as $parentClassReflection) {
if ($classReflection === $parentClassReflection) {
continue;
}

$parentClassReflections = array_merge($classReflection->getParents(), $classReflection->getInterfaces());
foreach ($parentClassReflections as $parentClassReflection) {
if (! $parentClassReflection->hasNativeMethod($methodName)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

use PhpParser\Node;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Source\RunnableInterface;

class SkipImplementsInterfaceMethod implements RunnableInterface
{
public function run(): ?Node
{
throw new ShouldNotHappenException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Source;

use PhpParser\Node;

interface RunnableInterface
{
public function run(): ?Node;
}