Skip to content

Commit 09a2956

Browse files
authored
PromotedConstructorPropertyFixer - do not promote when type of property is not nullable and type of parameter is nullable (#1042)
1 parent 4f3cdbd commit 09a2956

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
66
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
77
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
8-
![Tests](https://img.shields.io/badge/tests-3795-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3796-brightgreen.svg)
99
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
1010

1111
[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)

src/Fixer/PromotedConstructorPropertyFixer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,18 @@ private function typesAllowPromoting(string $propertyType, string $parameterType
275275
return true;
276276
}
277277

278-
if (\substr($propertyType, 0, 1) === '?') {
278+
$isPropertyNullable = \substr($propertyType, 0, 1) === '?';
279+
$isParameterNullable = \substr($parameterType, 0, 1) === '?';
280+
281+
if ($isParameterNullable && !$isPropertyNullable) {
282+
return false;
283+
}
284+
285+
if ($isPropertyNullable) {
279286
$propertyType = \substr($propertyType, 1);
280287
}
281288

282-
if (\substr($parameterType, 0, 1) === '?') {
289+
if ($isParameterNullable) {
283290
$parameterType = \substr($parameterType, 1);
284291
}
285292

tests/Fixer/PromotedConstructorPropertyFixerTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ public function __construct(string $x) {
100100
',
101101
];
102102

103+
yield 'do not promote when type of property is not nullable and type of parameter is nullable' => [
104+
'<?php class Foo {
105+
private int $x;
106+
public function __construct(?int $x) {
107+
$this->x = $x;
108+
}
109+
}
110+
',
111+
];
112+
103113
yield 'do not promote when not simple assignment' => [
104114
'<?php class Foo {
105115
private int $x;
@@ -919,7 +929,8 @@ public function __construct(int $x, int $y, int $z, bool $condition)
919929

920930
yield 'promote nullable types' => [
921931
'<?php class Foo {
922-
public function __construct(private ?int $x, private ?int $y, private ?int $z)
932+
private int $z;
933+
public function __construct(private ?int $x, private ?int $y, ?int $z)
923934
{
924935
}
925936
}
@@ -932,7 +943,6 @@ public function __construct(int $x, ?int $y, ?int $z)
932943
{
933944
$this->x = $x;
934945
$this->y = $y;
935-
$this->z = $z;
936946
}
937947
}
938948
',

0 commit comments

Comments
 (0)