Skip to content

Commit a2a594a

Browse files
committed
Support shift operators
1 parent be50d63 commit a2a594a

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
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-3734-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3736-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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
final class TypedClassConstantFixer extends AbstractFixer
2323
{
24+
private const INTEGER_KINDS = [\T_LNUMBER, '+', '-', '*', '(', ')', \T_SL, \T_SR];
25+
private const FLOAT_KINDS = [\T_DNUMBER, ...self::INTEGER_KINDS, '/'];
26+
private const STRING_KINDS = [\T_CONSTANT_ENCAPSED_STRING, '.', \T_LNUMBER, \T_DNUMBER];
27+
2428
public function getDefinition(): FixerDefinitionInterface
2529
{
2630
return new FixerDefinition(
@@ -140,15 +144,15 @@ private static function getTypeOfExpression(Tokens $tokens, int $index): string
140144
*/
141145
private static function getTypeOfExpressionForTokenKinds(array $tokenKinds): string
142146
{
143-
if (self::hasExclusivelyKinds($tokenKinds, [\T_LNUMBER, '+', '-', '*', '(', ')'])) {
147+
if (self::hasExclusivelyKinds($tokenKinds, self::INTEGER_KINDS)) {
144148
return 'int';
145149
}
146150

147-
if (self::hasExclusivelyKinds($tokenKinds, [\T_DNUMBER, \T_LNUMBER, '+', '-', '*', '/', '(', ')'])) {
151+
if (self::hasExclusivelyKinds($tokenKinds, self::FLOAT_KINDS)) {
148152
return 'float';
149153
}
150154

151-
if (self::hasExclusivelyKinds($tokenKinds, [\T_CONSTANT_ENCAPSED_STRING, '.', \T_LNUMBER, \T_DNUMBER])) {
155+
if (self::hasExclusivelyKinds($tokenKinds, self::STRING_KINDS)) {
152156
return 'string';
153157
}
154158

tests/Fixer/TypedClassConstantFixerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public static function provideFixCases(): iterable
8080
'<?php class Foo { public const BAR = 1000 * (701 + 22); }',
8181
];
8282

83+
yield 'integer shift left operator' => [
84+
'<?php class Foo { public const int BAR = 1 << 16; }',
85+
'<?php class Foo { public const BAR = 1 << 16; }',
86+
];
87+
88+
yield 'integer shift right operator' => [
89+
'<?php class Foo { public const int BAR = 1024 >> 1; }',
90+
'<?php class Foo { public const BAR = 1024 >> 1; }',
91+
];
92+
8393
yield 'float' => [
8494
'<?php class Foo { public const float BAR = 2.5; }',
8595
'<?php class Foo { public const BAR = 2.5; }',

0 commit comments

Comments
 (0)