Skip to content

Commit 39d48e6

Browse files
committed
Support values with a leading backslash
1 parent 21151fa commit 39d48e6

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
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-3748-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3750-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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ private static function fixClass(Tokens $tokens, int $openParenthesisIndex, int
9696
continue;
9797
}
9898

99-
$type = self::getTypeOfExpression($tokens, $assignmentIndex);
99+
$expressionStartIndex = $tokens->getNextMeaningfulToken($assignmentIndex);
100+
\assert(\is_int($expressionStartIndex));
101+
102+
if ($tokens[$expressionStartIndex]->isGivenKind(\T_NS_SEPARATOR)) {
103+
$expressionStartIndex = $tokens->getNextMeaningfulToken($expressionStartIndex);
104+
\assert(\is_int($expressionStartIndex));
105+
}
106+
107+
$type = self::getTypeOfExpression($tokens, $expressionStartIndex);
100108

101109
$tokens->insertAt(
102110
$constantNameIndex,
@@ -118,9 +126,6 @@ private static function getTypeOfExpression(Tokens $tokens, int $index): string
118126

119127
$foundKinds = [];
120128

121-
$index = $tokens->getNextMeaningfulToken($index);
122-
\assert(\is_int($index));
123-
124129
$questionMarkCount = 0;
125130
do {
126131
if ($questionMarkCount > 1) {

tests/Fixer/TypedClassConstantFixerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ public static function provideFixCases(): iterable
125125
'<?php class Foo { public const BAR = NULL; }',
126126
];
127127

128+
yield 'values with a leading backslash' => [
129+
<<<'PHP'
130+
<?php class Foo {
131+
public const false C_FALSE = \false;
132+
public const true C_TRUE = \true;
133+
public const null C_NULL = \null;
134+
}
135+
PHP,
136+
<<<'PHP'
137+
<?php class Foo {
138+
public const C_FALSE = \false;
139+
public const C_TRUE = \true;
140+
public const C_NULL = \null;
141+
}
142+
PHP,
143+
];
144+
128145
yield 'string with double quotes' => [
129146
'<?php class Foo { public const string BAR = "Jane Doe"; }',
130147
'<?php class Foo { public const BAR = "Jane Doe"; }',
@@ -135,6 +152,11 @@ public static function provideFixCases(): iterable
135152
"<?php class Foo { public const BAR = 'John Doe'; }",
136153
];
137154

155+
yield 'binary string' => [
156+
'<?php class Foo { public const string BAR = b"Jane Doe"; }',
157+
'<?php class Foo { public const BAR = b"Jane Doe"; }',
158+
];
159+
138160
yield 'string with heredoc syntax' => [
139161
<<<'PHP'
140162
<?php class Foo {

tests/PriorityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @coversNothing
3030
*
31-
* @requires PHP >= 8.1
31+
* @requires PHP >= 8.3
3232
*/
3333
final class PriorityTest extends TestCase
3434
{

0 commit comments

Comments
 (0)