Skip to content

Commit 1a380cb

Browse files
authored
PromotedConstructorPropertyFixer - fix for variadic parameters (#768)
1 parent 69072e9 commit 1a380cb

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![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)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3410-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3413-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

src/Analyzer/Analysis/ConstructorAnalysis.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getConstructorPromotableParameters(): array
7474

7575
$typeIndex = $this->tokens->getPrevMeaningfulToken($index);
7676
\assert(\is_int($typeIndex));
77-
if ($this->tokens[$typeIndex]->equalsAny(['(', ',', [\T_CALLABLE]])) {
77+
if ($this->tokens[$typeIndex]->equalsAny(['(', ',', [\T_CALLABLE], [\T_ELLIPSIS]])) {
7878
continue;
7979
}
8080

tests/Analyzer/Analysis/ConstructorAnalysisTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,20 @@ public function __construct($noType1, $noType2, int $i, $noType3) {}
105105
}',
106106
];
107107

108-
yield 'callable is not supported' => [
108+
yield 'callable is not promotable' => [
109109
[15 => '$a', 20 => '$b', 35 => '$i'],
110110
'<?php class Foo {
111111
public function __construct(array $a, bool $b, callable $c1, CALLABLE $c1, int $i) {}
112112
}',
113113
];
114114

115+
yield 'variadic parameter is not promotable' => [
116+
[15 => '$x', 20 => '$y'],
117+
'<?php class Foo {
118+
public function __construct(int $x, int $y, int ...$z) {}
119+
}',
120+
];
121+
115122
if (\PHP_VERSION_ID >= 80000) {
116123
yield 'some already promoted' => [
117124
[22 => '$b', 39 => '$s'],

tests/Fixer/PromotedConstructorPropertyFixerTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ public function __construct(int $x);
7272
',
7373
];
7474

75+
yield 'do not promote with callable type' => [
76+
'<?php class Foo {
77+
public function __construct(callable $x) {
78+
$this->x = $x;
79+
}
80+
}
81+
',
82+
];
83+
84+
yield 'do not promote variadic parameter' => [
85+
'<?php class Foo {
86+
public function __construct(int ...$x) {
87+
$this->x = $x;
88+
}
89+
}
90+
',
91+
];
92+
7593
yield 'do not promote when types of property and parameter are different' => [
7694
'<?php class Foo {
7795
private int $x;
@@ -690,23 +708,23 @@ public function __construct(string $y) {
690708

691709
yield 'do not promote with different name when new one is already used' => [
692710
'<?php class Foo {
693-
private string $x;
711+
private int $x;
694712
public function __construct(
695-
callable $x,
696-
string $y,
697-
private string $z,
713+
int $x,
714+
int $y,
715+
private int $z,
698716
) {
699717
$this->x = $y;
700718
}
701719
}
702720
',
703721
'<?php class Foo {
704-
private string $x;
705-
private string $z;
722+
private int $x;
723+
private int $z;
706724
public function __construct(
707-
callable $x,
708-
string $y,
709-
string $z,
725+
int $x,
726+
int $y,
727+
int $z,
710728
) {
711729
$this->x = $y;
712730
$this->z = $z;

0 commit comments

Comments
 (0)