Skip to content

Commit 1eb53e7

Browse files
committed
Update
1 parent 6980f4b commit 1eb53e7

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
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-3722-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3724-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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private static function getTypeOfExpression(Tokens $tokens, int $assignmentIndex
107107
\assert(\is_int($semicolonIndex));
108108

109109
$beforeSemicolonIndex = $tokens->getPrevMeaningfulToken($semicolonIndex);
110+
\assert(\is_int($beforeSemicolonIndex));
110111

111112
$map = [
112113
\T_DNUMBER => [\T_STRING, 'float'],
@@ -118,11 +119,21 @@ private static function getTypeOfExpression(Tokens $tokens, int $assignmentIndex
118119
$tokenId = $tokens[$beforeSemicolonIndex]->getId();
119120

120121
if ($tokens[$beforeSemicolonIndex]->isGivenKind(\T_STRING)) {
121-
return new Token([\T_STRING, $tokens[$beforeSemicolonIndex]->getContent()]);
122+
$lowercasedContent = \strtolower($tokens[$beforeSemicolonIndex]->getContent());
123+
if (\in_array($lowercasedContent, ['false', 'true', 'null'], true)) {
124+
return new Token([\T_STRING, $tokens[$beforeSemicolonIndex]->getContent()]);
125+
}
122126
}
123127

124128
if ($tokenId === null && $tokens[$beforeSemicolonIndex]->equals(')')) {
125-
$tokenId = CT::T_ARRAY_SQUARE_BRACE_CLOSE;
129+
$openParenthesisIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $beforeSemicolonIndex);
130+
131+
$arrayIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex);
132+
\assert(\is_int($arrayIndex));
133+
134+
if ($tokens[$arrayIndex]->isGivenKind(\T_ARRAY)) {
135+
$tokenId = CT::T_ARRAY_SQUARE_BRACE_CLOSE;
136+
}
126137
}
127138

128139
if (isset($map[$tokenId])) {

tests/Fixer/TypedClassConstantFixerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,15 @@ public static function provideFixCases(): iterable
8484
"<?php class Foo { public const string BAR = 'John Doe'; }",
8585
"<?php class Foo { public const BAR = 'John Doe'; }",
8686
];
87+
88+
yield 'unknown other constant' => [
89+
'<?php class Foo { public const mixed BAR = CONSTANT_FROM_FAR_AWAY; }',
90+
'<?php class Foo { public const BAR = CONSTANT_FROM_FAR_AWAY; }',
91+
];
92+
93+
yield 'expression of unknown type' => [
94+
'<?php class Foo { public const mixed BAR = 10 * (FLOAT_OR_INTEGER + 7); }',
95+
'<?php class Foo { public const BAR = 10 * (FLOAT_OR_INTEGER + 7); }',
96+
];
8797
}
8898
}

0 commit comments

Comments
 (0)