Skip to content

Commit

Permalink
Improve ReturnTypeWillChangeRector to handle any method of defined ty…
Browse files Browse the repository at this point in the history
…pe; move PhpDocFromTypeDeclarationDecorator to Downgrade rules (#2754)
  • Loading branch information
TomasVotruba committed Aug 11, 2022
1 parent c0070b1 commit 2667f35
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 296 deletions.
8 changes: 5 additions & 3 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -8696,11 +8696,13 @@ Add #[\ReturnTypeWillChange] attribute to configured instanceof class with metho
```php
use Rector\Config\RectorConfig;
use Rector\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector;
use Rector\Transform\ValueObject\ClassMethodReference;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ReturnTypeWillChangeRector::class, [
ArrayAccess::class => ['offsetGet'],
]);
$rectorConfig->ruleWithConfiguration(
ReturnTypeWillChangeRector::class,
[new ClassMethodReference('ArrayAccess', 'offsetGet')]
);
};
```

Expand Down

This file was deleted.

8 changes: 7 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,10 @@ parameters:
- rules/TypeDeclaration/Rector/ClassMethod/AddArrayParamDocTypeRector.php
- packages/BetterPhpDocParser/PhpDocParser/PhpDocFromTypeDeclarationDecorator.php

- '#Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with (.*?) and Rector\\Renaming\\Rector\\Name\\RenameClassRector will always evaluate to true#'
-
message: '#Call to method PHPUnit\\Framework\\Assert\:\:(.*?)\(\) with (.*?) and (.*?) will always evaluate to (.*?)#'
path: *Rector.php

-
message: '#Method call return value that should be used, but is not#'
path: src/PhpParser/Node/NodeFactory.php
Expand All @@ -706,3 +709,6 @@ parameters:
-
message: '#This call has duplicate argument#'
path: rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php

# tests
- '#Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with (.*?) and Rector\\Renaming\\Rector\\Name\\RenameClassRector will always evaluate to true#'
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class MyTestCase3 extends ParentTestCase
{
public function dataProvider(): array
{
return [[__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Correct/correct.php.inc']];
return [[__DIR__ . '/Fixture/class_extending_array_access.php.inc', __DIR__ . '/Correct/correct.php.inc']];
}
}

Expand All @@ -24,7 +24,7 @@ final class MyTestCase3 extends ParentTestCase
{
public function dataProvider(): \Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Fixture/class_extending_array_access.php.inc', __DIR__ . '/Correct/correct.php.inc'];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\F

use Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source\Options;

class SomeClass extends Options
final class ClassExtendingArrayAccess extends Options
{
public function offsetGet($offset)
{
Expand All @@ -19,7 +19,7 @@ namespace Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\F

use Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source\Options;

class SomeClass extends Options
final class ClassExtendingArrayAccess extends Options
{
#[\ReturnTypeWillChange]
public function offsetGet($offset)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Fixture;

final class SkipExtraPublicMethod extends \ArrayObject
{
public function extraMethod()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

namespace Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\FixtureCustomConfig;

use Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source\Options;
use Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source\ClassThatWillChangeReturnType;

class SomeClass extends Options
final class SomeClass extends ClassThatWillChangeReturnType
{
public function offsetGet($offset)
{
}

public function offsetExists($offset)
public function changeMyReturn()
{
}
}
Expand All @@ -21,17 +17,12 @@ class SomeClass extends Options

namespace Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\FixtureCustomConfig;

use Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source\Options;
use Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source\ClassThatWillChangeReturnType;

class SomeClass extends Options
final class SomeClass extends ClassThatWillChangeReturnType
{
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
}

#[\ReturnTypeWillChange]
public function offsetExists($offset)
public function changeMyReturn()
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source;

abstract class ClassThatWillChangeReturnType
{
public function changeMyReturn()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Tests\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector\Source\ClassThatWillChangeReturnType;
use Rector\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector;
use Rector\Transform\ValueObject\ClassMethodReference;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(ReturnTypeWillChangeRector::class, [
'ArrayAccess' => ['offsetExists'],
]);
$rectorConfig->ruleWithConfiguration(ReturnTypeWillChangeRector::class, [
new ClassMethodReference(ClassThatWillChangeReturnType::class, 'changeMyReturn'),
]);
};
15 changes: 15 additions & 0 deletions rules/Php81/Enum/AttributeName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Php81\Enum;

final class AttributeName
{
/**
* Available since PHP 8.1
* @see https://php.watch/versions/8.1/ReturnTypeWillChange
* @var string
*/
public const RETURN_TYPE_WILL_CHANGE = 'ReturnTypeWillChange';
}

0 comments on commit 2667f35

Please sign in to comment.