Skip to content

Commit

Permalink
[TypeDeclaration] optionally only add types for hard coded return val…
Browse files Browse the repository at this point in the history
…ues in `ReturnTypeFromStrictScalarReturnExprRector` (#5364)
  • Loading branch information
RobertMe committed Dec 30, 2023
1 parent e8684d4 commit e044d50
Show file tree
Hide file tree
Showing 19 changed files with 466 additions and 13 deletions.
29 changes: 25 additions & 4 deletions build/target-repository/docs/rector_rules_overview.md
@@ -1,4 +1,4 @@
# 354 Rules Overview
# 355 Rules Overview

<br>

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

- [Php82](#php82) (4)

- [Php83](#php83) (2)
- [Php83](#php83) (3)

- [Privatization](#privatization) (4)

Expand Down Expand Up @@ -5312,6 +5312,19 @@ Add const to type

<br>

### CombineHostPortLdapUriRector

Combine separated host and port on `ldap_connect()` args

- class: [`Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector`](../rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php)

```diff
-ldap_connect('ldap://ldap.example.com', 389);
+ldap_connect('ldap://ldap.example.com:389');
```

<br>

## Privatization

### FinalizeClassesWithoutChildrenRector
Expand Down Expand Up @@ -6850,20 +6863,28 @@ Add return type based on strict parameter type

Change return type based on strict scalar returns - string, int, float or bool

:wrench: **configure it!**

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

```diff
final class SomeClass
{
- public function run($value)
+ public function run($value): string
- public function foo($value)
+ public function foo($value): string
{
if ($value) {
return 'yes';
}

return 'no';
}

- public function bar(string $value)
+ public function bar(string $value): int
{
return strlen($value);
}
}
```

Expand Down
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class Bool
{
public function test($value)
{
if ($value) {
return false;
}

return true;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class Bool
{
public function test($value): bool
{
if ($value) {
return false;
}

return true;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class UntypedBaseClass
{
public function test()
{
return 0;
}
}

class SomeChild2 extends UntypedBaseClass
{
final public function test()
{
return 'abc';
}

}

class SomeChild2Child extends SomeChild2 {}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class UntypedBaseClass
{
public function test()
{
return 0;
}
}

class SomeChild2 extends UntypedBaseClass
{
final public function test(): string
{
return 'abc';
}

}

class SomeChild2Child extends SomeChild2 {}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class Float
{
public function test()
{
return 10.4;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class Float
{
public function test(): float
{
return 10.4;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class FloatNegative
{
public function test()
{
return -23.1;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class FloatNegative
{
public function test(): float
{
return -23.1;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class FloatPositive
{
public function test()
{
return +33.9;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class FloatPositive
{
public function test(): float
{
return +33.9;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class Int
{
public function test()
{
return 1;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class Int
{
public function test(): int
{
return 1;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class IntNegative
{
public function test()
{
return -3;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class IntNegative
{
public function test(): int
{
return -3;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class IntPositive
{
public function test()
{
return +1;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class IntPositive
{
public function test(): int
{
return +1;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class SkipCalc
{
public function test()
{
return 1 + 2;
}
}
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class SkipCall
{
public function test($value)
{
return strlen($value);
}
}
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class SkipConcat
{
public function test()
{
return 'hello' . 'world';
}
}
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class SkipTernary
{
public function test($value)
{
return $value ? 10 : 0;
}
}
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\FixtureHardCodedOnly;

class SkipVariable
{
public function test()
{
$value = 'test';
return $value;
}
}

0 comments on commit e044d50

Please sign in to comment.