Skip to content

Commit

Permalink
[Php80] Fix add default nullable type on ClassPropertyAssignToConstru…
Browse files Browse the repository at this point in the history
…ctorPromotionRector (#4091)
  • Loading branch information
samsonasik committed Jun 6, 2023
1 parent c567d5d commit ff21394
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 20 deletions.
14 changes: 12 additions & 2 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType as NodeUnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\ClassAutoloadingException;
use PHPStan\Reflection\ClassReflection;
Expand Down Expand Up @@ -112,7 +112,7 @@ public function isObjectType(Node $node, ObjectType $requiredObjectType): bool

public function getType(Node $node): Type
{
if ($node instanceof Property && $node->type instanceof NullableType) {
if ($node instanceof Property && $node->type instanceof Node) {
return $this->getType($node->type);
}

Expand Down Expand Up @@ -171,6 +171,16 @@ public function getType(Node $node): Type
return new MixedType();
}

if ($node instanceof NodeUnionType) {
$types = [];

foreach ($node->types as $type) {
$types[] = $this->getType($type);
}

return new UnionType($types);
}

if (! $node instanceof Expr) {
return new MixedType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
Expand All @@ -29,23 +34,49 @@ public function getNodeClasses(): array

/**
* @param Identifier $node
* @return StringType|BooleanType|IntegerType|FloatType|MixedType
* @return StringType|BooleanType|ConstantBooleanType|NullType|ObjectWithoutClassType|ArrayType|IterableType|IntegerType|FloatType|MixedType
*/
public function resolve(Node $node): Type
{
if ($node->toLowerString() === 'string') {
$lowerString = $node->toLowerString();

if ($lowerString === 'string') {
return new StringType();
}

if ($node->toLowerString() === 'bool') {
if ($lowerString === 'bool') {
return new BooleanType();
}

if ($node->toLowerString() === 'int') {
if ($lowerString === 'false') {
return new ConstantBooleanType(false);
}

if ($lowerString === 'true') {
return new ConstantBooleanType(true);
}

if ($lowerString === 'null') {
return new NullType();
}

if ($lowerString === 'object') {
return new ObjectWithoutClassType();
}

if ($lowerString === 'array') {
return new ArrayType(new MixedType(), new MixedType());
}

if ($lowerString === 'int') {
return new IntegerType();
}

if ($node->toLowerString() === 'float') {
if ($lowerString === 'iterable') {
return new IterableType(new MixedType(), new MixedType());
}

if ($lowerString === 'float') {
return new FloatType();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultFalse
{
public $name;

public function __construct(string $name = false)
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultFalse
{
public function __construct(public string|false $name = false)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo

final class AddDefaultNullable
{
public function __construct(public $name = null)
public function __construct(public ?string $name = null)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable2
{
public $name;

public function __construct(string|false $name = null)
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable2
{
public function __construct(public string|false|null $name = null)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable3
{
public $name;

public function __construct(object|false $name = null)
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable3
{
public function __construct(public object|false|null $name = null)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable4
{
public $name;

public function __construct(array|false $name = null)
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable4
{
public function __construct(public array|false|null $name = null)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable5
{
public $name;

public function __construct(iterable|false $name = null)
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable5
{
public function __construct(public iterable|false|null $name = null)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable6
{
public $name;

public function __construct(self|false $name = null)
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class AddDefaultNullable6
{
public function __construct(public \Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture\AddDefaultNullable6|false|null $name = null)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use DateTimeInterface;

class NullableType
{
public function __construct(private ?\DateTimeInterface $time = null)
public function __construct(private ?DateTimeInterface $time = null)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class StringDefaultString
{
public $name;

public function __construct(string $name = '')
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class StringDefaultString
{
public function __construct(public string $name = '')
{
}
}

?>
Loading

0 comments on commit ff21394

Please sign in to comment.