Skip to content

Commit 62d048c

Browse files
committed
Support complex arrays
1 parent a92a801 commit 62d048c

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
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-3738-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3740-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/TypedClassConstantFixer.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,19 @@ private static function getTypeOfExpression(Tokens $tokens, int $index): string
144144
*/
145145
private static function getTypeOfExpressionForTokenKinds(array $tokenKinds): string
146146
{
147-
if (self::isOfTypeBasedOnKinds($tokenKinds, self::INTEGER_KINDS)) {
147+
if (self::isOfTypeBasedOnKinds($tokenKinds, [], [\T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
148+
return 'array';
149+
}
150+
151+
if (self::isOfTypeBasedOnKinds($tokenKinds, self::INTEGER_KINDS, [])) {
148152
return 'int';
149153
}
150154

151-
if (self::isOfTypeBasedOnKinds($tokenKinds, self::FLOAT_KINDS)) {
155+
if (self::isOfTypeBasedOnKinds($tokenKinds, self::FLOAT_KINDS, [])) {
152156
return 'float';
153157
}
154158

155-
if (self::isOfTypeBasedOnKinds($tokenKinds, self::STRING_KINDS, '.')) {
159+
if (self::isOfTypeBasedOnKinds($tokenKinds, self::STRING_KINDS, ['.'])) {
156160
return 'string';
157161
}
158162

@@ -162,11 +166,18 @@ private static function getTypeOfExpressionForTokenKinds(array $tokenKinds): str
162166
/**
163167
* @param list<int|string> $expressionTokenKinds
164168
* @param list<int|string> $expectedKinds
169+
* @param list<int|string> $instantWinners
165170
*/
166-
private static function isOfTypeBasedOnKinds(array $expressionTokenKinds, array $expectedKinds, ?string $instantWinner = null): bool
167-
{
171+
private static function isOfTypeBasedOnKinds(
172+
array $expressionTokenKinds,
173+
array $expectedKinds,
174+
array $instantWinners,
175+
): bool {
168176
foreach ($expressionTokenKinds as $index => $expressionTokenKind) {
169-
if ($expressionTokenKind === $instantWinner) {
177+
if ($expressionTokenKind === '?') {
178+
return false;
179+
}
180+
if (\in_array($expressionTokenKind, $instantWinners, true)) {
170181
return true;
171182
}
172183
if (\in_array($expressionTokenKind, $expectedKinds, true)) {

tests/Fixer/TypedClassConstantFixerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public static function provideFixCases(): iterable
5555
'<?php class Foo { public const BAR = [1 => 2] + array(3 => 4) + [5 => 6]; }',
5656
];
5757

58+
yield 'array as sum of long syntax array and constant' => [
59+
'<?php class Foo { public const array BAR = Baz::C + array(1); }',
60+
'<?php class Foo { public const BAR = Baz::C + array(1); }',
61+
];
62+
63+
yield 'array as sum of short syntax array and constant' => [
64+
'<?php class Foo { public const array BAR = Baz::C1 + [2] + Baz::C2; }',
65+
'<?php class Foo { public const BAR = Baz::C1 + [2] + Baz::C2; }',
66+
];
67+
5868
yield 'false' => [
5969
'<?php class Foo { public const false BAR = false; }',
6070
'<?php class Foo { public const BAR = false; }',

0 commit comments

Comments
 (0)