Skip to content

Commit

Permalink
Fixed constant types handling in TypedPropertyFromStrictConstructorRe…
Browse files Browse the repository at this point in the history
…ctor (#4588)

* Fixed constant-literal handling in TypedPropertyFromStrictConstructorRector

* fix

* add analog test to TypedPropertyFromStrictConstructorReadonlyClassRector
  • Loading branch information
staabm committed Jul 24, 2023
1 parent 578d637 commit e9e6a42
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector\Fixture;

final class SkipLiteralIncompatibleType
{
/**
* @var string
*/
public $due_date;

public function __construct()
{
$this->due_date = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector\Fixture;

final class SkipLiteralStringType
{
/**
* @var string
*/
public $due_date;

public function __construct()
{
$this->due_date = '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

final class SkipLiteralIncompatibleType
{
/**
* @var string
*/
public $due_date;

public function __construct()
{
$this->due_date = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

final class SkipLiteralStringType
{
/**
* @var string
*/
public $due_date;

public function __construct()
{
$this->due_date = '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public function refactor(Node $node): ?Node

// public property can be anything
if ($property->isPublic()) {
if (!$phpDocInfo->getVarType() instanceof MixedType) {
continue;
}


$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $propertyType);
$hasChanged = true;
continue;
Expand Down

0 comments on commit e9e6a42

Please sign in to comment.