Skip to content

Commit ede9fee

Browse files
committed
Support class constants
1 parent b900c73 commit ede9fee

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-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-3742-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3743-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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,17 @@ private static function getTypeOfExpression(Tokens $tokens, int $index): string
118118
$index = $tokens->getNextMeaningfulToken($index);
119119
\assert(\is_int($index));
120120

121+
$questionMarkCount = 0;
121122
do {
123+
if ($questionMarkCount > 1) {
124+
return 'mixed';
125+
}
126+
$kind = $tokens[$index]->getId() ?? $tokens[$index]->getContent();
127+
if ($kind === '?') {
128+
$questionMarkCount++;
129+
$foundKinds = [];
130+
continue;
131+
}
122132
$foundKinds[] = $tokens[$index]->getId() ?? $tokens[$index]->getContent();
123133

124134
$index = $tokens->getNextMeaningfulToken($index);
@@ -152,7 +162,7 @@ private static function getTypeOfExpressionForTokenKinds(array $tokenKinds): str
152162
return 'float';
153163
}
154164

155-
if (self::isOfTypeBasedOnKinds($tokenKinds, self::STRING_KINDS, ['.'])) {
165+
if (self::isOfTypeBasedOnKinds($tokenKinds, self::STRING_KINDS, ['.', CT::T_CLASS_CONSTANT])) {
156166
return 'string';
157167
}
158168

tests/Fixer/TypedClassConstantFixerTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,24 @@ public static function provideFixCases(): iterable
169169
PHP,
170170
];
171171

172-
yield 'string as result of concatenations' => [
173-
'<?php class Foo { public const string BAR = "A" . 1 . "B" . 0.25 . "C"; }',
174-
'<?php class Foo { public const BAR = "A" . 1 . "B" . 0.25 . "C"; }',
172+
yield 'string as reference to other class' => [
173+
<<<'PHP'
174+
<?php class Foo {
175+
public const string BAR = FooFoo::class;
176+
public const mixed BAZ = CONFIG_66 ? FooFoo::class : -1;
177+
}
178+
PHP,
179+
<<<'PHP'
180+
<?php class Foo {
181+
public const BAR = FooFoo::class;
182+
public const BAZ = CONFIG_66 ? FooFoo::class : -1;
183+
}
184+
PHP,
185+
];
186+
187+
yield 'string as result of concatenations with parentheses' => [
188+
'<?php class Foo { public const string BAR = "A" . 1 . ("B" . 0.25) . "C"; }',
189+
'<?php class Foo { public const BAR = "A" . 1 . ("B" . 0.25) . "C"; }',
175190
];
176191

177192
yield 'string as result of concatenation with other constant' => [

0 commit comments

Comments
 (0)