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 @@ -3,15 +3,28 @@
namespace Rector\TypeDeclaration\PropertyTypeInferer;

use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\TypeDeclaration\Contract\PropertyTypeInfererInterface;

final class GetterOrSetterPropertyTypeInferer extends AbstractPropertyTypeInferer implements PropertyTypeInfererInterface
{
/**
* @var BetterStandardPrinter
*/
private $betterStandardPrinter;

public function __construct(BetterStandardPrinter $betterStandardPrinter)
{
$this->betterStandardPrinter = $betterStandardPrinter;
}

/**
* @return string[]
*/
Expand All @@ -24,41 +37,79 @@ public function inferProperty(Property $property): array
$propertyName = $this->nameResolver->resolve($property);

foreach ($class->getMethods() as $classMethod) {
if (count((array) $classMethod->stmts) !== 1) {
if (! $this->hasClassMethodOnlyStatementReturnOfPropertyFetch($classMethod, $propertyName)) {
continue;
}

$onlyClassMethodStmt = $classMethod->stmts[0];
if (! $onlyClassMethodStmt instanceof Return_) {
continue;
$returnTypes = $this->resolveClassMethodReturnTypes($classMethod);
if ($returnTypes !== []) {
return $returnTypes;
}

if (! $onlyClassMethodStmt->expr instanceof PropertyFetch) {
continue;
}
throw new ShouldNotHappenException(sprintf(
'"%s" for "%s" type',
__METHOD__,
$this->betterStandardPrinter->print($classMethod->returnType)
));
}

if (! $this->nameResolver->isName($onlyClassMethodStmt->expr, $propertyName)) {
continue;
}
return [];
}

// @todo resolve from doc?
if (! $classMethod->returnType) {
continue;
}
public function getPriority(): int
{
return 600;
}

$result = $this->nameResolver->resolve($classMethod->returnType);
if ($result !== null) {
return [$result];
}
private function hasClassMethodOnlyStatementReturnOfPropertyFetch(
ClassMethod $classMethod,
string $propertyName
): bool {
if (count((array) $classMethod->stmts) !== 1) {
return false;
}

throw new ShouldNotHappenException(__METHOD__);
$onlyClassMethodStmt = $classMethod->stmts[0];
if (! $onlyClassMethodStmt instanceof Return_) {
return false;
}

return [];
/** @var Return_ $return */
$return = $onlyClassMethodStmt;

if (! $return->expr instanceof PropertyFetch) {
return false;
}

return $this->nameResolver->isName($return->expr, $propertyName);
}

public function getPriority(): int
/**
* @return string[]
*/
private function resolveClassMethodReturnTypes(ClassMethod $classMethod): array
{
return 600;
// @todo resolve from doc?
if (! $classMethod->returnType) {
return [];
}

if ($classMethod->returnType instanceof NullableType) {
$type = $classMethod->returnType->type;
} else {
$type = $classMethod->returnType;
}

$result = $this->nameResolver->resolve($type);
if ($result !== null) {
$types = [$result];

if ($classMethod->returnType instanceof NullableType) {
$types[] = 'null';
}

return $types;
}

return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\TypeDeclaration\Tests\Rector\FunctionLike\PropertyTypeDeclarationRector\Fixture;

use Symfony\Component\HttpFoundation\File\File;

final class FileUpload
{
private $file;

public function getFile(): ?File
{
return $this->file;
}

public function setFile(File $file): void
{
$this->file = $file;
}
}

?>
-----
<?php

namespace Rector\TypeDeclaration\Tests\Rector\FunctionLike\PropertyTypeDeclarationRector\Fixture;

use Symfony\Component\HttpFoundation\File\File;

final class FileUpload
{
/**
* @var \Symfony\Component\HttpFoundation\File\File|null
*/
private $file;

public function getFile(): ?File
{
return $this->file;
}

public function setFile(File $file): void
{
$this->file = $file;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function test(): void
__DIR__ . '/Fixture/doctrine_relation.php.inc',

__DIR__ . '/Fixture/complex.php.inc',

__DIR__ . '/Fixture/single_nullable_return.php.inc',
// skip
__DIR__ . '/Fixture/skip_multi_vars.php.inc',
]);
Expand Down