Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP83] Implements a rule to add types to class constants #5290

Merged
merged 8 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
# 352 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 @@ -5288,6 +5288,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