Skip to content

Commit e300872

Browse files
authored
Fix the behavior of ClassPropertyAssignToConstructorPromotionRector when types do not match (#7972)
* Fix the behavior of `ClassPropertyAssignToConstructorPromotionRector` when types do not match * CR fix * CR fix * remove unnecessary changes * CR fix * add tests * add tests * add tests * naming * naming * separate tests * add fixtures * add fixtures * CR fix
1 parent 05371b7 commit e300872

13 files changed

Lines changed: 417 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationBasic
9+
{
10+
private SomeInterface $x;
11+
12+
public function __construct(
13+
SomeInterfaceImplementation $x,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationBasic
29+
{
30+
public function __construct(private SomeInterfaceImplementation $x)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationDefaultValueInParam
9+
{
10+
private SomeInterface $x;
11+
12+
public function __construct(
13+
SomeInterfaceImplementation $x = new SomeInterfaceImplementation(),
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationDefaultValueInParam
29+
{
30+
public function __construct(private SomeInterfaceImplementation $x = new SomeInterfaceImplementation())
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationNonNullableParamWithNullDefault
9+
{
10+
private ?SomeInterface $x;
11+
12+
public function __construct(
13+
SomeInterfaceImplementation $x = null,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationNonNullableParamWithNullDefault
29+
{
30+
public function __construct(private ?\Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation $x = null)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationNonNullableParamWithNullDefaultAndNonNullableProperty
9+
{
10+
private SomeInterface $x;
11+
12+
public function __construct(
13+
SomeInterfaceImplementation $x = null,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationNonNullableParamWithNullDefaultAndNonNullableProperty
29+
{
30+
public function __construct(private ?\Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation $x = null)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationNullableParam
9+
{
10+
private ?SomeInterface $x;
11+
12+
public function __construct(
13+
?SomeInterfaceImplementation $x,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationNullableParam
29+
{
30+
public function __construct(private ?SomeInterfaceImplementation $x)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationNullableParamAndDefaultValue
9+
{
10+
private ?SomeInterface $x;
11+
12+
public function __construct(
13+
?SomeInterfaceImplementation $x = null,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationNullableParamAndDefaultValue
29+
{
30+
public function __construct(private ?SomeInterfaceImplementation $x = null)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationNullableParamAndNonNullableProperty
9+
{
10+
private SomeInterface $x;
11+
12+
public function __construct(
13+
?SomeInterfaceImplementation $x,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationNullableParamAndNonNullableProperty
29+
{
30+
public function __construct(private \Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation $x)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationUnionWithNullInParam
9+
{
10+
private ?SomeInterface $x;
11+
12+
public function __construct(
13+
null|SomeInterfaceImplementation $x,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationUnionWithNullInParam
29+
{
30+
public function __construct(private null|SomeInterfaceImplementation $x)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationUnionWithNullInParamAndDefaultValue
9+
{
10+
private ?SomeInterface $x;
11+
12+
public function __construct(
13+
null|SomeInterfaceImplementation $x = null,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationUnionWithNullInParamAndDefaultValue
29+
{
30+
public function __construct(private null|SomeInterfaceImplementation $x = null)
31+
{
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
6+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
7+
8+
class WithInterfaceAndItsImplementationUnionWithNullInParamAndNonNullableProperty
9+
{
10+
private SomeInterface $x;
11+
12+
public function __construct(
13+
null|SomeInterfaceImplementation $x,
14+
) {
15+
$this->x = $x;
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;
24+
25+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterface;
26+
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation;
27+
28+
class WithInterfaceAndItsImplementationUnionWithNullInParamAndNonNullableProperty
29+
{
30+
public function __construct(private \Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SomeInterfaceImplementation $x)
31+
{
32+
}
33+
}
34+
35+
?>

0 commit comments

Comments
 (0)