Skip to content

Commit

Permalink
[TypeDeclaration] Add BoolReturnTypeFromStrictScalarReturnsRector (#3898
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TomasVotruba committed May 19, 2023
1 parent 11b9411 commit a4d6d41
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 7 deletions.
27 changes: 25 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 @@
# 404 Rules Overview
# 405 Rules Overview

<br>

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

- [Transform](#transform) (29)

- [TypeDeclaration](#typedeclaration) (39)
- [TypeDeclaration](#typedeclaration) (40)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -9084,6 +9084,29 @@ Change && and || between nullable objects to instanceof compares

<br>

### BoolReturnTypeFromStrictScalarReturnsRector

Change return type based on strict returns type operations

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

```diff
class SomeClass
{
- public function resolve($first, $second)
+ public function resolve($first, $second): bool
{
if ($first) {
return false;
}

return $first > $second;
}
}
```

<br>

### DeclareStrictTypesRector

Add declare(strict_types=1) if missing
Expand Down
2 changes: 2 additions & 0 deletions config/set/type-declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamAnnotationIncorrectNullableRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector;
Expand Down Expand Up @@ -72,5 +73,6 @@
EmptyOnNullableObjectToInstanceOfRector::class,
PropertyTypeFromStrictSetterGetterRector::class,
ReturnTypeFromStrictTernaryRector::class,
BoolReturnTypeFromStrictScalarReturnsRector::class,
]);
};
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,6 @@ parameters:
- '#Parameter \#3 \$assign of method Rector\\CodeQuality\\Rector\\FunctionLike\\SimplifyUselessVariableRector\:\:processSimplifyUselessVariable\(\) expects PhpParser\\Node\\Expr\\Assign\|PhpParser\\Node\\Expr\\AssignOp, PhpParser\\Node\\Expr given#'
- '#Anonymous variable in a `\$stmtsAware\->stmts\[\$key \- 1\]\->\.\.\.\(\)` method call can lead to false dead methods\. Make sure the variable type is known#'
- '#Anonymous variable in a `\$parentNode\->stmts\[\$key\]\->\.\.\.\(\)` method call can lead to false dead methods\. Make sure the variable type is known#'
- '#Anonymous variable in a `\$stmtsAware\->stmts\[\$key\]\->\.\.\.\(\)` method call can lead to false dead methods\. Make sure the variable type is known#'

- '#Cognitive complexity for "Rector\\TypeDeclaration\\Rector\\ClassMethod\\BoolReturnTypeFromStrictScalarReturnsRector\:\:hasOnlyBoolScalarReturnExprs\(\)" is 11, keep it under 10#'
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector;

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

final class BoolReturnTypeFromStrictScalarReturnsRectorTest 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,17 @@
<?php

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

final class SkipAllNested
{
public function resolve($first, $second)
{
if ($first) {
return false;
}

if ($second) {
return $first > $second;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

class SomeClass
{
public function resolve($first, $second)
{
if ($first) {
return false;
}

return $first > $second;
}
}

?>
-----
<?php

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

class SomeClass
{
public function resolve($first, $second): bool
{
if ($first) {
return false;
}

return $first > $second;
}
}

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

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

final class WithSimpleFunction
{
public function resolve($first, $second)
{
if ($first) {
return false;
}

if ($second) {
return $first > $second;
}

return is_bool($second);
}
}

?>
-----
<?php

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

final class WithSimpleFunction
{
public function resolve($first, $second): bool
{
if ($first) {
return false;
}

if ($second) {
return $first > $second;
}

return is_bool($second);
}
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector;

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

0 comments on commit a4d6d41

Please sign in to comment.