mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-15 21:38:24 +01:00
skip var type in anonymous class for PropertyTypeDeclarationRector
This commit is contained in:
parent
01b4c78e47
commit
058dd5aeb5
@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
|
||||
|
||||
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;
|
||||
@ -24,8 +25,12 @@ final class AllAssignNodePropertyTypeInferer extends AbstractTypeInferer impleme
|
||||
|
||||
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);
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -45,8 +45,12 @@ final class GetterPropertyTypeInferer extends AbstractTypeInferer implements Pro
|
||||
|
||||
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);
|
||||
|
@ -29,8 +29,12 @@ final class GetterTypeDeclarationPropertyTypeInferer extends AbstractTypeInferer
|
||||
|
||||
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);
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -35,6 +35,7 @@ final class PropertyTypeDeclarationRectorTest extends AbstractRectorTestCase
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user