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,27 @@
<?php

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

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\Email;

final class EmailFactory
{
private function createEmail(): Email
{
return new class extends Email {
private int $id = 0;

public function getId(): int
{
return $this->id;
}

public function setId(int $id): Email
{
$this->id = $id;

return $this;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

class Email
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
namespace Rector\Privatization\TypeManipulator;

use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VerbosityLevel;

/**
* Made with GPT-5
Expand All @@ -37,45 +35,6 @@ public function sharedArrayStructure(Type ...$types): Type
}
}

// If all are ConstantArrayType and have the *same* ordered key list -> preserve shape.
$allConstantArrayTypes = array_reduce($types, fn ($c, $t): bool => $c && $t instanceof ConstantArrayType, true);
if ($allConstantArrayTypes) {
/** @var ConstantArrayType[] $consts */
$consts = $types;

// Compare key sets (by stringified key types)
$firstKeys = array_map(
fn (Type $type): string => $type->describe(VerbosityLevel::typeOnly()),
$consts[0]->getKeyTypes()
);
foreach ($consts as $c) {
$keys = array_map(
fn (Type $type): string => $type->describe(VerbosityLevel::typeOnly()),
$c->getKeyTypes()
);
if ($keys !== $firstKeys) {
$allConstantArrayTypes = false;
break;
}
}

if ($allConstantArrayTypes) {
$resultKeyTypes = $consts[0]->getKeyTypes();
$valueColumns = [];
foreach ($consts as $const) {
$valueColumns[] = $const->getValueTypes();
}

$resultValueTypes = [];
foreach (array_keys($resultKeyTypes) as $i) {
$col = array_column($valueColumns, $i);
$resultValueTypes[] = $this->sharedArrayStructure(...$col);
}

return new ConstantArrayType($resultKeyTypes, $resultValueTypes);
}
}

// Generic ArrayType path: reconcile key type + recurse into item types
/** @var ArrayType[] $types */
/** @var ArrayType[] $arrayTypes */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ private function getActualReturnedClass(ClassMethod $classMethod): ?string

$className = $classNames[0];

// skip anonymous classes, they have no usable name
if ($this->reflectionProvider->hasClass($className)) {
$returnedClassReflection = $this->reflectionProvider->getClass($className);
if ($returnedClassReflection->isAnonymous()) {
return null;
}
}

if ($returnedClass === null) {
$returnedClass = $className;
} elseif ($returnedClass !== $className) {
Expand Down
Loading