-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add DynamicDocBlockPropertyToNativePropertyRector (#5691)
* [CodeQuality] Kick off DynamicDocBlockPropertyToNativePropertyRector * add nullable type with default * skip __get() and __set() * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
- Loading branch information
1 parent
6f863d1
commit d7bfc24
Showing
13 changed files
with
594 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...BlockPropertyToNativePropertyRector/DynamicDocBlockPropertyToNativePropertyRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class DynamicDocBlockPropertyToNativePropertyRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ty/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/bare_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class BareClass | ||
{ | ||
public function run(): void | ||
{ | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
final class BareClass | ||
{ | ||
private ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; | ||
public function run(): void | ||
{ | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> |
42 changes: 42 additions & 0 deletions
42
...or/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/existing_property.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class ExistingProperty extends TestCase | ||
{ | ||
private SomeDependency $someDependency; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
final class ExistingProperty extends TestCase | ||
{ | ||
private SomeDependency $someDependency; | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> |
42 changes: 42 additions & 0 deletions
42
...amicDocBlockPropertyToNativePropertyRector/Fixture/improve_existing_property_type.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class ImproveExistingPropertyType extends TestCase | ||
{ | ||
private $someDependency; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
final class ImproveExistingPropertyType extends TestCase | ||
{ | ||
private \Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
.../Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/include_null.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class IncludeNull | ||
{ | ||
protected $someDependency = null; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
final class IncludeNull | ||
{ | ||
protected ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency = null; | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
.../DynamicDocBlockPropertyToNativePropertyRector/Fixture/promoted_property_priority.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class PromotedPropertyPriority | ||
{ | ||
public function __construct( | ||
protected SomeDependency $someDependency | ||
) { | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
final class PromotedPropertyPriority | ||
{ | ||
public function __construct( | ||
protected SomeDependency $someDependency | ||
) { | ||
} | ||
} | ||
|
||
?> |
21 changes: 21 additions & 0 deletions
21
...ector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/skip_get_magic.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class SkipGetMagic | ||
{ | ||
public function run(): void | ||
{ | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/skip_set_magic.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class SkipSetMagic | ||
{ | ||
public function run(): void | ||
{ | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
|
||
public function __set($name, $value) | ||
{ | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ity/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/some_test.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
/** | ||
* @property SomeDependency $someDependency | ||
*/ | ||
#[\AllowDynamicProperties] | ||
final class SomeTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency; | ||
|
||
final class SomeTest extends TestCase | ||
{ | ||
private ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->someDependency = new SomeDependency(); | ||
} | ||
} | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...ity/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Source/SomeDependency.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source; | ||
|
||
class SomeDependency | ||
{ | ||
} |
13 changes: 13 additions & 0 deletions
13
...ty/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector; | ||
use Rector\Config\RectorConfig; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->phpVersion(PhpVersionFeature::DEPRECATE_DYNAMIC_PROPERTIES); | ||
|
||
$rectorConfig->rule(DynamicDocBlockPropertyToNativePropertyRector::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\NodeFactory; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\ComplexType; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\NullableType; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\Property; | ||
use PhpParser\Node\Stmt\PropertyProperty; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode; | ||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
use Rector\StaticTypeMapper\StaticTypeMapper; | ||
|
||
final readonly class TypedPropertyFactory | ||
{ | ||
public function __construct( | ||
private StaticTypeMapper $staticTypeMapper, | ||
) { | ||
} | ||
|
||
public function createFromPropertyTagValueNode( | ||
PropertyTagValueNode $propertyTagValueNode, | ||
Class_ $class, | ||
string $propertyName | ||
): Property { | ||
$propertyProperty = new PropertyProperty($propertyName); | ||
$propertyTypeNode = $this->createPropertyTypeNode($propertyTagValueNode, $class); | ||
|
||
return new Property(Class_::MODIFIER_PRIVATE, [$propertyProperty], [], $propertyTypeNode); | ||
} | ||
|
||
public function createPropertyTypeNode( | ||
PropertyTagValueNode $propertyTagValueNode, | ||
Class_ $class, | ||
bool $isNullable = true | ||
): Name|ComplexType|Identifier|null { | ||
$propertyType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType( | ||
$propertyTagValueNode->type, | ||
$class | ||
); | ||
|
||
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY); | ||
|
||
if ($isNullable && ! $typeNode instanceof NullableType && ! $typeNode instanceof ComplexType && $typeNode instanceof Node) { | ||
return new NullableType($typeNode); | ||
} | ||
|
||
return $typeNode; | ||
} | ||
} |
Oops, something went wrong.