Skip to content

Commit dac996f

Browse files
committed
refactor: make DeclareStrictTypesRector safe and add to ruleset
1 parent 13f7abd commit dac996f

File tree

46 files changed

+920
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+920
-58
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"tests/debug_functions.php",
100100
"rules-tests/Transform/Rector/FuncCall/FuncCallToMethodCallRector/Source/some_view_function.php",
101101
"rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Source/FunctionTyped.php",
102+
"rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Source/functions.php",
102103
"rules-tests/Php70/Rector/ClassMethod/Php4ConstructorRector/Source/ParentClass.php",
103104
"rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Source/functions.php"
104105
]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
class Test
46
{
57
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
4+
5+
$arrow = fn (): int => 42;
6+
7+
$arrowWithParam = fn (int $x): int => $x + 1;
8+
9+
$arrowString = fn (): string => 'hello';
10+
11+
?>
12+
-----
13+
<?php
14+
15+
declare(strict_types=1);
16+
17+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
18+
19+
$arrow = fn (): int => 42;
20+
21+
$arrowWithParam = fn (int $x): int => $x + 1;
22+
23+
$arrowString = fn (): string => 'hello';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
4+
5+
$closure = function (): int {
6+
return 42;
7+
};
8+
9+
$closureWithParam = function (int $x): int {
10+
return $x + 1;
11+
};
12+
13+
?>
14+
-----
15+
<?php
16+
17+
declare(strict_types=1);
18+
19+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
20+
21+
$closure = function (): int {
22+
return 42;
23+
};
24+
25+
$closureWithParam = function (int $x): int {
26+
return $x + 1;
27+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\TypedClass;
6+
7+
new TypedClass('John', 25);
8+
9+
?>
10+
-----
11+
<?php
12+
13+
declare(strict_types=1);
14+
15+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
16+
17+
use Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\TypedClass;
18+
19+
new TypedClass('John', 25);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
4+
5+
use function Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\acceptsInt;
6+
7+
function callWithCast(): void
8+
{
9+
acceptsInt((int) '123');
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
declare(strict_types=1);
17+
18+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
19+
20+
use function Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\acceptsInt;
21+
22+
function callWithCast(): void
23+
{
24+
acceptsInt((int) '123');
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
4+
5+
$fn = strlen(...);
6+
7+
$upperFn = strtoupper(...);
8+
9+
?>
10+
-----
11+
<?php
12+
13+
declare(strict_types=1);
14+
15+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
16+
17+
$fn = strlen(...);
18+
19+
$upperFn = strtoupper(...);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
4+
5+
use function Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\acceptsFloat;
6+
7+
acceptsFloat(123);
8+
9+
?>
10+
-----
11+
<?php
12+
13+
declare(strict_types=1);
14+
15+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
16+
17+
use function Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\acceptsFloat;
18+
19+
acceptsFloat(123);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\TypedClass;
6+
7+
function useMethodCalls(TypedClass $obj): void
8+
{
9+
$obj->add(1, 2);
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
declare(strict_types=1);
17+
18+
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
19+
20+
use Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Source\TypedClass;
21+
22+
function useMethodCalls(TypedClass $obj): void
23+
{
24+
$obj->add(1, 2);
25+
}

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/multiple_declare.php.inc

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
44

5-
$count = 0;
6-
function example() {
7-
global $count;
8-
$count++;
9-
echo "$count instructions executed<br>";
10-
}
11-
12-
register_tick_function('example');
13-
145
declare(ticks=1) {
156
$cars = ["Ford", "Volvo", "BMW"];
167
foreach($cars as $car) {
@@ -26,15 +17,6 @@ declare(strict_types=1);
2617

2718
namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\Fixture;
2819

29-
$count = 0;
30-
function example() {
31-
global $count;
32-
$count++;
33-
echo "$count instructions executed<br>";
34-
}
35-
36-
register_tick_function('example');
37-
3820
declare(ticks=1) {
3921
$cars = ["Ford", "Volvo", "BMW"];
4022
foreach($cars as $car) {

0 commit comments

Comments
 (0)