Skip to content

Commit 9d3feaf

Browse files
committed
Update
1 parent ef47b1d commit 9d3feaf

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
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-3726-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3727-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)
@@ -726,7 +726,7 @@ The string key of an array or generator must be trimmed and have no double space
726726

727727
#### TypedClassConstantFixer
728728
Class constants must have a type.
729-
*Risky: when constant is not of single type.*
729+
*Risky: when constant can be of different types.*
730730
```diff
731731
<?php
732732
class Foo {

src/Fixer/TypedClassConstantFixer.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121

2222
final class TypedClassConstantFixer extends AbstractFixer
2323
{
24+
private const TOKEN_TO_TYPE_MAP = [
25+
\T_DNUMBER => 'float',
26+
\T_LNUMBER => 'int',
27+
\T_CONSTANT_ENCAPSED_STRING => 'string',
28+
CT::T_ARRAY_SQUARE_BRACE_CLOSE => 'array',
29+
];
30+
2431
public function getDefinition(): FixerDefinitionInterface
2532
{
2633
return new FixerDefinition(
@@ -39,7 +46,7 @@ class Foo {
3946
),
4047
],
4148
'',
42-
'when constant is not of single type',
49+
'when constant can be of different types',
4350
);
4451
}
4552

@@ -91,55 +98,50 @@ private static function fixClass(Tokens $tokens, int $openParenthesisIndex, int
9198
continue;
9299
}
93100

101+
$type = self::getTypeOfExpression($tokens, $assignmentIndex);
102+
94103
$tokens->insertAt(
95104
$constantNameIndex,
96105
[
97-
self::getTypeOfExpression($tokens, $assignmentIndex),
106+
new Token([$type === 'array' ? CT::T_ARRAY_TYPEHINT : \T_STRING, $type]),
98107
new Token([\T_WHITESPACE, ' ']),
99108
],
100109
);
101110
}
102111
}
103112

104-
private static function getTypeOfExpression(Tokens $tokens, int $assignmentIndex): Token
113+
private static function getTypeOfExpression(Tokens $tokens, int $assignmentIndex): string
105114
{
106115
$semicolonIndex = $tokens->getNextTokenOfKind($assignmentIndex, [';']);
107116
\assert(\is_int($semicolonIndex));
108117

109118
$beforeSemicolonIndex = $tokens->getPrevMeaningfulToken($semicolonIndex);
110119
\assert(\is_int($beforeSemicolonIndex));
111120

112-
$map = [
113-
\T_DNUMBER => [\T_STRING, 'float'],
114-
\T_LNUMBER => [\T_STRING, 'int'],
115-
\T_CONSTANT_ENCAPSED_STRING => [\T_STRING, 'string'],
116-
CT::T_ARRAY_SQUARE_BRACE_CLOSE => [CT::T_ARRAY_TYPEHINT, 'array'],
117-
];
118-
119121
$tokenId = $tokens[$beforeSemicolonIndex]->getId();
120122

123+
if (isset(self::TOKEN_TO_TYPE_MAP[$tokenId])) {
124+
return self::TOKEN_TO_TYPE_MAP[$tokenId];
125+
}
126+
121127
if ($tokens[$beforeSemicolonIndex]->isGivenKind(\T_STRING)) {
122128
$lowercasedContent = \strtolower($tokens[$beforeSemicolonIndex]->getContent());
123129
if (\in_array($lowercasedContent, ['false', 'true', 'null'], true)) {
124-
return new Token([\T_STRING, $lowercasedContent]);
130+
return $lowercasedContent;
125131
}
126132
}
127133

128-
if ($tokenId === null && $tokens[$beforeSemicolonIndex]->equals(')')) {
134+
if ($tokens[$beforeSemicolonIndex]->equals(')')) {
129135
$openParenthesisIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $beforeSemicolonIndex);
130136

131137
$arrayIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex);
132138
\assert(\is_int($arrayIndex));
133139

134140
if ($tokens[$arrayIndex]->isGivenKind(\T_ARRAY)) {
135-
$tokenId = CT::T_ARRAY_SQUARE_BRACE_CLOSE;
141+
return 'array';
136142
}
137143
}
138144

139-
if (isset($map[$tokenId])) {
140-
return new Token($map[$tokenId]);
141-
}
142-
143-
return new Token([\T_STRING, 'mixed']);
145+
return 'mixed';
144146
}
145147
}

tests/Fixer/TypedClassConstantFixerTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,35 @@ public static function provideFixCases(): iterable
105105
<?php
106106
class Foo {
107107
public const int ONE = 1;
108-
public const bool ALREADY_TYPED = true;
109-
public const string NAME = 'name';
108+
protected const bool|float ALREADY_TYPED = true;
109+
private const string NAME = 'name';
110110
}
111111
const NOT_CLASS = true;
112112
class Bar {
113-
public const array ARRAY_LONG_SYNTAX = array();
114-
public const array ARRAY_SHORT_SYNTAX = [];
115-
public const string lowercased_name = 'lowercased_name';
113+
const array ARRAY_LONG_SYNTAX = array();
114+
const array ARRAY_SHORT_SYNTAX = [];
115+
const string lowercased_name = 'lowercased_name';
116116
}
117117
PHP,
118118
<<<'PHP'
119119
<?php
120120
class Foo {
121121
public const ONE = 1;
122-
public const bool ALREADY_TYPED = true;
123-
public const NAME = 'name';
122+
protected const bool|float ALREADY_TYPED = true;
123+
private const NAME = 'name';
124124
}
125125
const NOT_CLASS = true;
126126
class Bar {
127-
public const ARRAY_LONG_SYNTAX = array();
128-
public const ARRAY_SHORT_SYNTAX = [];
129-
public const lowercased_name = 'lowercased_name';
127+
const ARRAY_LONG_SYNTAX = array();
128+
const ARRAY_SHORT_SYNTAX = [];
129+
const lowercased_name = 'lowercased_name';
130130
}
131131
PHP,
132132
];
133+
134+
yield 'constant that can be of different types' => [
135+
'<?php class Foo { public const string BAR = SHOULD_BE_INT ? 1 : "one"; }',
136+
'<?php class Foo { public const BAR = SHOULD_BE_INT ? 1 : "one"; }',
137+
];
133138
}
134139
}

0 commit comments

Comments
 (0)