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 @@

use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\Contract\TypeInferer\PropertyTypeInfererInterface;
Expand All @@ -24,8 +25,12 @@ public function __construct(AssignToPropertyTypeInferer $assignToPropertyTypeInf

public function inferProperty(Property $property): Type
{
/** @var ClassLike $class */
/** @var ClassLike|null $class */
$class = $property->getAttribute(AttributeKey::CLASS_NODE);
if ($class === null) {
// anonymous class
return new MixedType();
}

$propertyName = $this->nameResolver->getName($property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ final class ConstructorPropertyTypeInferer extends AbstractTypeInferer implement
{
public function inferProperty(Property $property): Type
{
/** @var Class_ $class */
/** @var Class_|null $class */
$class = $property->getAttribute(AttributeKey::CLASS_NODE);
if ($class === null) {
// anonymous class
return new MixedType();
}

$classMethod = $class->getMethod('__construct');
if ($classMethod === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ public function __construct(

public function inferProperty(Property $property): Type
{
/** @var Class_ $class */
/** @var Class_|null $class */
$class = $property->getAttribute(AttributeKey::CLASS_NODE);
if ($class === null) {
// anonymous class
return new MixedType();
}

/** @var string $propertyName */
$propertyName = $this->nameResolver->getName($property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public function __construct(TypeDeclarationToStringConverter $typeDeclarationToS

public function inferProperty(Property $property): Type
{
/** @var Class_ $class */
/** @var Class_|null $class */
$class = $property->getAttribute(AttributeKey::CLASS_NODE);
if ($class === null) {
// anonymous class
return new MixedType();
}

/** @var string $propertyName */
$propertyName = $this->nameResolver->getName($property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ final class SingleMethodAssignedNodePropertyTypeInferer extends AbstractTypeInfe
{
public function inferProperty(Property $property): Type
{
/** @var Class_ $class */
/** @var Class_|null $class */
$class = $property->getAttribute(AttributeKey::CLASS_NODE);
if ($class === null) {
// anonymous class
return new MixedType();
}

$classMethod = $class->getMethod('__construct');
if ($classMethod === null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

class SkipAnonymousClass
{
public function makeClass(int $bar = 1)
{
$class = new class() {
public $bar;
};

$class->bar = $bar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function provideDataForTest(): Iterator
yield [__DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc'];
yield [__DIR__ . '/Fixture/setter_type.php.inc'];
yield [__DIR__ . '/Fixture/skip_multi_vars.php.inc'];
yield [__DIR__ . '/Fixture/skip_anonymous_class.php.inc'];
}

protected function getRectorClass(): string
Expand Down