Skip to content

Commit

Permalink
[PHP83] Implements a rule to add types to class constants (#5290)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
  • Loading branch information
peterfox and TomasVotruba authored Dec 3, 2023
1 parent ea5b331 commit 52b6654
Show file tree
Hide file tree
Showing 23 changed files with 560 additions and 4 deletions.
20 changes: 18 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 351 Rules Overview
# 353 Rules Overview

<br>

Expand Down Expand Up @@ -44,7 +44,7 @@

- [Php82](#php82) (4)

- [Php83](#php83) (1)
- [Php83](#php83) (2)

- [Privatization](#privatization) (4)

Expand Down Expand Up @@ -5263,6 +5263,22 @@ Add override attribute to overridden methods

<br>

### AddTypeToConstRector

Add const to type

- class: [`Rector\Php83\Rector\ClassConst\AddTypeToConstRector`](../rules/Php83/Rector/ClassConst/AddTypeToConstRector.php)

```diff
final class SomeClass
{
- public const TYPE = 'some_type';
+ public const string TYPE = 'some_type';
}
```

<br>

## Privatization

### FinalizeClassesWithoutChildrenRector
Expand Down
6 changes: 4 additions & 2 deletions config/set/php83.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([AddOverrideAttributeToOverriddenMethodsRector::class]);
$rectorConfig->rules([
\Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector::class,
\Rector\Php83\Rector\ClassConst\AddTypeToConstRector::class,
]);
};
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';
}
}
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 = [];
}

?>
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 = [];
}

?>
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';
}

?>

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';
}

?>

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';
}

?>

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;
}

?>
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';
}

?>

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';
}

?>

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;
}

?>
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';
}

?>

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';
}

?>

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';
}

?>
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';
}
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';
}
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';
}
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';
}
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);
};
Loading

0 comments on commit 52b6654

Please sign in to comment.