Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![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)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3795-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-3796-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![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)
Expand Down
11 changes: 9 additions & 2 deletions src/Fixer/PromotedConstructorPropertyFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,18 @@ private function typesAllowPromoting(string $propertyType, string $parameterType
return true;
}

if (\substr($propertyType, 0, 1) === '?') {
$isPropertyNullable = \substr($propertyType, 0, 1) === '?';
$isParameterNullable = \substr($parameterType, 0, 1) === '?';

if ($isParameterNullable && !$isPropertyNullable) {
return false;
}

if ($isPropertyNullable) {
$propertyType = \substr($propertyType, 1);
}

if (\substr($parameterType, 0, 1) === '?') {
if ($isParameterNullable) {
$parameterType = \substr($parameterType, 1);
}

Expand Down
14 changes: 12 additions & 2 deletions tests/Fixer/PromotedConstructorPropertyFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public function __construct(string $x) {
',
];

yield 'do not promote when type of property is not nullable and type of parameter is nullable' => [
'<?php class Foo {
private int $x;
public function __construct(?int $x) {
$this->x = $x;
}
}
',
];

yield 'do not promote when not simple assignment' => [
'<?php class Foo {
private int $x;
Expand Down Expand Up @@ -919,7 +929,8 @@ public function __construct(int $x, int $y, int $z, bool $condition)

yield 'promote nullable types' => [
'<?php class Foo {
public function __construct(private ?int $x, private ?int $y, private ?int $z)
private int $z;
public function __construct(private ?int $x, private ?int $y, ?int $z)
{
}
}
Expand All @@ -932,7 +943,6 @@ public function __construct(int $x, ?int $y, ?int $z)
{
$this->x = $x;
$this->y = $y;
$this->z = $z;
}
}
',
Expand Down