-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP83] Implements a rule to add types to class constants (#5290)
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
- Loading branch information
1 parent
ea5b331
commit 52b6654
Showing
23 changed files
with
560 additions
and
4 deletions.
There are no files selected for viewing
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
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
28 changes: 28 additions & 0 deletions
28
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/AddTypeToConstRectorTest.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\Php83\Rector\ClassConst\AddTypeToConstRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddTypeToConstRectorTest 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'; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...lassConst/AddTypeToConstRector/Fixture/apply_type_to_class_const_when_class_final.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,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
final class ApplyTypesWhenClassFinal | ||
{ | ||
public const STRING = 'some_type'; | ||
|
||
public const INT = 1; | ||
|
||
public const FLOAT = 1.0; | ||
|
||
public const BOOL = true; | ||
|
||
public const NULL = null; | ||
|
||
public const ARRAY = []; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
final class ApplyTypesWhenClassFinal | ||
{ | ||
public const string STRING = 'some_type'; | ||
|
||
public const int INT = 1; | ||
|
||
public const float FLOAT = 1.0; | ||
|
||
public const bool BOOL = true; | ||
|
||
public const null NULL = null; | ||
|
||
public const array ARRAY = []; | ||
} | ||
|
||
?> |
41 changes: 41 additions & 0 deletions
41
...Const/AddTypeToConstRector/Fixture/apply_type_to_class_const_when_const_protected.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,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
class ApplyTypesToPrivateConsts | ||
{ | ||
private const STRING = 'some_type'; | ||
|
||
private const INT = 1; | ||
|
||
private const FLOAT = 1.0; | ||
|
||
private const BOOL = true; | ||
|
||
private const NULL = null; | ||
|
||
private const ARRAY = []; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
class ApplyTypesToPrivateConsts | ||
{ | ||
private const string STRING = 'some_type'; | ||
|
||
private const int INT = 1; | ||
|
||
private const float FLOAT = 1.0; | ||
|
||
private const bool BOOL = true; | ||
|
||
private const null NULL = null; | ||
|
||
private const array ARRAY = []; | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...sts/Php83/Rector/ClassConst/AddTypeToConstRector/Fixture/skip_when_abstract_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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
abstract class SkipAbstractClass | ||
{ | ||
public const STRING = 'some_type'; | ||
} | ||
|
||
?> | ||
|
11 changes: 11 additions & 0 deletions
11
...or/ClassConst/AddTypeToConstRector/Fixture/skip_when_implementable_not_autoloaded.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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
final class SkipExtendsNotAutoloadedInterfaceClass implements NotAutoloadedInterface | ||
{ | ||
public const STRING = 'some_type'; | ||
} | ||
|
||
?> | ||
|
13 changes: 13 additions & 0 deletions
13
.../Rector/ClassConst/AddTypeToConstRector/Fixture/skip_when_interface_defines_const.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
use Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source\SomeInterface; | ||
|
||
final class SkipOnInterfaceImplements implements SomeInterface | ||
{ | ||
public const STRING = 'some_type'; | ||
} | ||
|
||
?> | ||
|
16 changes: 16 additions & 0 deletions
16
...ts/Php83/Rector/ClassConst/AddTypeToConstRector/Fixture/skip_when_operations_used.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public const string A = 'A'; | ||
|
||
public const B = self::A . 'b'; | ||
|
||
public const int INT = 1; | ||
|
||
public const INT2 = 1 + self::INT; | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...ClassConst/AddTypeToConstRector/Fixture/skip_when_parent_class_const_defines_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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
use Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source\ParentClass; | ||
|
||
final class SomeClass extends ParentClass | ||
{ | ||
public const STRING_OR_INT = 'some_type'; | ||
} | ||
|
||
?> | ||
|
11 changes: 11 additions & 0 deletions
11
...83/Rector/ClassConst/AddTypeToConstRector/Fixture/skip_when_parent_not_autoloaded.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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
final class SkipExtendsNotAutoloadedClass extends NotAutoloadedClass | ||
{ | ||
public const STRING = 'some_type'; | ||
} | ||
|
||
?> | ||
|
12 changes: 12 additions & 0 deletions
12
...ector/ClassConst/AddTypeToConstRector/Fixture/skip_when_referencing_another_const.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,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
use Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source\ParentClass; | ||
|
||
final class SomeClass | ||
{ | ||
public const STRING = ParentClass::STRING_OR_INT; | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...hp83/Rector/ClassConst/AddTypeToConstRector/Fixture/skip_when_trait_defines_const.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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
use Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source\SomeTrait; | ||
|
||
final class SkipWhenTraitDefinesInterface | ||
{ | ||
use SomeTrait; | ||
|
||
public const STRING = 'something'; | ||
} | ||
|
||
?> | ||
|
13 changes: 13 additions & 0 deletions
13
...p83/Rector/ClassConst/AddTypeToConstRector/Fixture/skip_when_trait_not_autoloaded.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
final class SkipExtendsNotAutoloadedClass | ||
{ | ||
use NotAutoloadedTrait; | ||
|
||
public const STRING = 'some_type'; | ||
} | ||
|
||
?> | ||
|
10 changes: 10 additions & 0 deletions
10
...s/Php83/Rector/ClassConst/AddTypeToConstRector/Fixture/skip_when_type_already_set.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public const string|int string = 'A'; | ||
} | ||
|
||
?> |
8 changes: 8 additions & 0 deletions
8
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/Source/ParentClass.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,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source; | ||
|
||
class ParentClass | ||
{ | ||
public const STRING_OR_INT = 'string'; | ||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/Source/SomeInterface.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,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source; | ||
|
||
interface SomeInterface | ||
{ | ||
public const STRING = 'something'; | ||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/Source/SomeOtherInterface.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,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source; | ||
|
||
interface SomeOtherInterface | ||
{ | ||
public const STRING_ANOTHER = 'something'; | ||
} |
8 changes: 8 additions & 0 deletions
8
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/Source/SomeTrait.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,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Source; | ||
|
||
trait SomeTrait | ||
{ | ||
public const STRING = 'something'; | ||
} |
12 changes: 12 additions & 0 deletions
12
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Core\ValueObject\PhpVersion; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(\Rector\Php83\Rector\ClassConst\AddTypeToConstRector::class); | ||
|
||
$rectorConfig->phpVersion(PhpVersion::PHP_83); | ||
}; |
Oops, something went wrong.