Skip to content

Commit a92a801

Browse files
committed
Support more complex concatenations
1 parent 1606892 commit a92a801

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
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-3737-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3738-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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class TypedClassConstantFixer extends AbstractFixer
2323
{
2424
private const INTEGER_KINDS = [\T_LNUMBER, '+', '-', '*', '(', ')', \T_POW, \T_SL, \T_SR];
2525
private const FLOAT_KINDS = [\T_DNUMBER, ...self::INTEGER_KINDS, '/'];
26-
private const STRING_KINDS = [\T_CONSTANT_ENCAPSED_STRING, '.', \T_LNUMBER, \T_DNUMBER];
26+
private const STRING_KINDS = [\T_CONSTANT_ENCAPSED_STRING, \T_LNUMBER, \T_DNUMBER];
2727

2828
public function getDefinition(): FixerDefinitionInterface
2929
{
@@ -144,33 +144,36 @@ private static function getTypeOfExpression(Tokens $tokens, int $index): string
144144
*/
145145
private static function getTypeOfExpressionForTokenKinds(array $tokenKinds): string
146146
{
147-
if (self::hasExclusivelyKinds($tokenKinds, self::INTEGER_KINDS)) {
147+
if (self::isOfTypeBasedOnKinds($tokenKinds, self::INTEGER_KINDS)) {
148148
return 'int';
149149
}
150150

151-
if (self::hasExclusivelyKinds($tokenKinds, self::FLOAT_KINDS)) {
151+
if (self::isOfTypeBasedOnKinds($tokenKinds, self::FLOAT_KINDS)) {
152152
return 'float';
153153
}
154154

155-
if (self::hasExclusivelyKinds($tokenKinds, self::STRING_KINDS)) {
155+
if (self::isOfTypeBasedOnKinds($tokenKinds, self::STRING_KINDS, '.')) {
156156
return 'string';
157157
}
158158

159159
return 'mixed';
160160
}
161161

162162
/**
163-
* @param list<int|string> $tokenKinds
163+
* @param list<int|string> $expressionTokenKinds
164164
* @param list<int|string> $expectedKinds
165165
*/
166-
private static function hasExclusivelyKinds(array $tokenKinds, array $expectedKinds): bool
166+
private static function isOfTypeBasedOnKinds(array $expressionTokenKinds, array $expectedKinds, ?string $instantWinner = null): bool
167167
{
168-
foreach ($tokenKinds as $index => $tokenKind) {
169-
if (\in_array($tokenKind, $expectedKinds, true)) {
170-
unset($tokenKinds[$index]);
168+
foreach ($expressionTokenKinds as $index => $expressionTokenKind) {
169+
if ($expressionTokenKind === $instantWinner) {
170+
return true;
171+
}
172+
if (\in_array($expressionTokenKind, $expectedKinds, true)) {
173+
unset($expressionTokenKinds[$index]);
171174
}
172175
}
173176

174-
return $tokenKinds === [];
177+
return $expressionTokenKinds === [];
175178
}
176179
}

tests/Fixer/TypedClassConstantFixerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,16 @@ public static function provideFixCases(): iterable
125125
"<?php class Foo { public const BAR = 'John Doe'; }",
126126
];
127127

128-
yield 'string as result of concatenation' => [
128+
yield 'string as result of concatenations' => [
129129
'<?php class Foo { public const string BAR = "A" . 1 . "B" . 0.25 . "C"; }',
130130
'<?php class Foo { public const BAR = "A" . 1 . "B" . 0.25 . "C"; }',
131131
];
132132

133+
yield 'string as result of concatenation with other constant' => [
134+
'<?php class Foo { public const string BAR = "A" . Baz::C; }',
135+
'<?php class Foo { public const BAR = "A" . Baz::C; }',
136+
];
137+
133138
yield 'multiple constants' => [
134139
<<<'PHP'
135140
<?php

0 commit comments

Comments
 (0)