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

[TypeDeclaration] Add ReturnTypeFromStrictNewArrayRector #2572

Merged
merged 3 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 23 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 @@
# 519 Rules Overview
# 520 Rules Overview

<br>

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

- [Transform](#transform) (36)

- [TypeDeclaration](#typedeclaration) (28)
- [TypeDeclaration](#typedeclaration) (29)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -11908,6 +11908,27 @@ Add strict return type based native function return

<br>

### ReturnTypeFromStrictNewArrayRector

Add strict return array type based on created empty array and returned

- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php)

```diff
final class SomeClass
{
- public function run()
+ public function run(): array
{
$values = [];

return $values;
}
}
```

<br>

### ReturnTypeFromStrictTypedCallRector

Add return type from strict return type of call
Expand Down
2 changes: 2 additions & 0 deletions config/set/type-declaration-strict.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
Expand All @@ -32,4 +33,5 @@
ReturnTypeFromStrictBoolReturnExprRector::class,
]);
$rectorConfig->rule(ReturnTypeFromStrictNativeFuncCallRector::class);
$rectorConfig->rule(ReturnTypeFromStrictNewArrayRector::class);
};
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$rectorConfig->ruleWithConfiguration(
PreferThisOrSelfMethodCallRector::class,
[
'PHPUnit\Framework\TestCase' => PreferenceSelfThis::PREFER_THIS(),
'PHPUnit\Framework\TestCase' => PreferenceSelfThis::PREFER_THIS,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(PreferThisOrSelfMethodCallRector::class, [
SomeAbstractTestCase::class => PreferenceSelfThis::PREFER_SELF(),
BeLocalClass::class => PreferenceSelfThis::PREFER_THIS(),
TestCase::class => PreferenceSelfThis::PREFER_SELF(),
SomeAbstractTestCase::class => PreferenceSelfThis::PREFER_SELF,
BeLocalClass::class => PreferenceSelfThis::PREFER_THIS,
TestCase::class => PreferenceSelfThis::PREFER_SELF,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class ReturnOverrideWithArray
{
public function run()
{
$values = [];

$values = [1000];

return $values;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class ReturnOverrideWithArray
{
public function run(): array
{
$values = [];

$values = [1000];

return $values;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class SkipOverride
{
public function run()
{
$values = [];

$values = 100;

return $values;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class SkipTwoReturns
{
public function run()
{
$values = [];

if (mt_rand(1, 2)) {
return $values;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class SomeClass
{
public function run()
{
$values = [];

return $values;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class SomeClass
{
public function run(): array
{
$values = [];

return $values;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ReturnTypeFromStrictNewArrayRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->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,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;

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