Skip to content

Commit

Permalink
[Php83] Adds rule for adding Override attribute (#5170)
Browse files Browse the repository at this point in the history
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
peterfox and samsonasik committed Nov 3, 2023
1 parent cc97bec commit 2ad03db
Show file tree
Hide file tree
Showing 18 changed files with 400 additions and 1 deletion.
31 changes: 30 additions & 1 deletion build/target-repository/docs/rector_rules_overview.md
@@ -1,4 +1,4 @@
# 355 Rules Overview
# 356 Rules Overview

<br>

Expand Down Expand Up @@ -44,6 +44,8 @@

- [Php82](#php82) (4)

- [Php83](#php83) (1)

- [Privatization](#privatization) (4)

- [Removing](#removing) (5)
Expand Down Expand Up @@ -5314,6 +5316,33 @@ Change deprecated utf8_decode and utf8_encode to mb_convert_encoding

<br>

## Php83

### AddOverrideAttributeToOverriddenMethodsRector

Add override attribute to overridden methods

- class: [`Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector`](../rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php)

```diff
class ParentClass
{
public function foo()
{
}
}

class ChildClass extends ParentClass
{
+ #[\Override]
public function foo()
{
}
}
```

<br>

## Privatization

### FinalizeClassesWithoutChildrenRector
Expand Down
15 changes: 15 additions & 0 deletions config/set/level/up-to-php83.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SetList::PHP_83, LevelSetList::UP_TO_PHP_82]);

// parameter must be defined after import, to override imported param version
$rectorConfig->phpVersion(PhpVersion::PHP_83);
};
10 changes: 10 additions & 0 deletions config/set/php83.php
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([AddOverrideAttributeToOverriddenMethodsRector::class]);
};
5 changes: 5 additions & 0 deletions packages/Set/ValueObject/LevelSetList.php
Expand Up @@ -11,6 +11,11 @@
*/
final class LevelSetList implements SetListInterface
{
/**
* @var string
*/
public const UP_TO_PHP_83 = __DIR__ . '/../../../config/set/level/up-to-php83.php';

/**
* @var string
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/Set/ValueObject/SetList.php
Expand Up @@ -106,6 +106,11 @@ final class SetList implements SetListInterface
*/
public const PHP_82 = __DIR__ . '/../../../config/set/php82.php';

/**
* @var string
*/
public const PHP_83 = __DIR__ . '/../../../config/set/php83.php';

/**
* @var string
*/
Expand Down
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddOverrideAttributeToOverriddenMethodsRectorTest 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';
}
}
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleParentClass;

class ChildClass extends ExampleParentClass
{
public function foo()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleParentClass;

class ChildClass extends ExampleParentClass
{
#[\Override]
public function foo()
{
}
}

?>
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleChildClass;

class ChildClass extends ExampleChildClass
{
public function foo()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleChildClass;

class ChildClass extends ExampleChildClass
{
#[\Override]
public function foo()
{
}
}

?>
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleParentClass;

class ChildClass extends ExampleParentClass
{
public function bar()
{
}
}

?>
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleParentClass;

class ChildClass extends ExampleParentClass
{
public function __construct()
{
}
}

?>
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleParentClass;

class SkipNonInheritingClass
{
}

?>
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleParentClass;

class ChildClass extends ExampleParentClass
{
#[\Override]
public function foo()
{
}
}

?>
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source;

class ExampleChildClass extends ExampleParentClass
{
}
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source;

class ExampleConstructParentClass
{
public function __construct()
{
}
}
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source;

class ExampleParentClass
{
public function foo()
{
}

private function bar()
{
}
}
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddOverrideAttributeToOverriddenMethodsRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_83);
};

0 comments on commit 2ad03db

Please sign in to comment.