|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer: custom fixers. |
| 5 | + * |
| 6 | + * (c) 2018 Kuba Werłos |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Tests\Fixer; |
| 13 | + |
| 14 | +/** |
| 15 | + * @internal |
| 16 | + * |
| 17 | + * @covers \PhpCsFixerCustomFixers\Fixer\TypedClassConstantFixer |
| 18 | + */ |
| 19 | +final class TypedClassConstantFixerTest extends AbstractFixerTestCase |
| 20 | +{ |
| 21 | + public function testIsRisky(): void |
| 22 | + { |
| 23 | + self::assertRiskiness(true); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @dataProvider provideFixCases |
| 28 | + * |
| 29 | + * @requires PHP >= 8.3 |
| 30 | + */ |
| 31 | + public function testFix(string $expected, ?string $input = null): void |
| 32 | + { |
| 33 | + $this->doTest($expected, $input); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return iterable<array{0: string, 1?: string}> |
| 38 | + */ |
| 39 | + public static function provideFixCases(): iterable |
| 40 | + { |
| 41 | + yield 'non-class constants are ignored' => ['<?php const FOO = 1;']; |
| 42 | + |
| 43 | + yield 'array with long syntax' => [ |
| 44 | + '<?php class Foo { public const array BAR = array(true, 1, "foo"); }', |
| 45 | + '<?php class Foo { public const BAR = array(true, 1, "foo"); }', |
| 46 | + ]; |
| 47 | + |
| 48 | + yield 'array with short syntax' => [ |
| 49 | + '<?php class Foo { public const array BAR = [true, 1, "foo"]; }', |
| 50 | + '<?php class Foo { public const BAR = [true, 1, "foo"]; }', |
| 51 | + ]; |
| 52 | + |
| 53 | + yield 'false' => [ |
| 54 | + '<?php class Foo { public const false BAR = false; }', |
| 55 | + '<?php class Foo { public const BAR = false; }', |
| 56 | + ]; |
| 57 | + |
| 58 | + yield 'true' => [ |
| 59 | + '<?php class Foo { public const true BAR = true; }', |
| 60 | + '<?php class Foo { public const BAR = true; }', |
| 61 | + ]; |
| 62 | + |
| 63 | + yield 'float' => [ |
| 64 | + '<?php class Foo { public const float BAR = 2.5; }', |
| 65 | + '<?php class Foo { public const BAR = 2.5; }', |
| 66 | + ]; |
| 67 | + |
| 68 | + yield 'integer' => [ |
| 69 | + '<?php class Foo { public const int BAR = 123; }', |
| 70 | + '<?php class Foo { public const BAR = 123; }', |
| 71 | + ]; |
| 72 | + |
| 73 | + yield 'integer as result of sum' => [ |
| 74 | + '<?php class Foo { public const int BAR = 1 + 2 + 3; }', |
| 75 | + '<?php class Foo { public const BAR = 1 + 2 + 3; }', |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'null' => [ |
| 79 | + '<?php class Foo { public const null BAR = null; }', |
| 80 | + '<?php class Foo { public const BAR = null; }', |
| 81 | + ]; |
| 82 | + |
| 83 | + yield 'NULL' => [ |
| 84 | + '<?php class Foo { public const null BAR = NULL; }', |
| 85 | + '<?php class Foo { public const BAR = NULL; }', |
| 86 | + ]; |
| 87 | + |
| 88 | + yield 'string with double quotes' => [ |
| 89 | + '<?php class Foo { public const string BAR = "Jane Doe"; }', |
| 90 | + '<?php class Foo { public const BAR = "Jane Doe"; }', |
| 91 | + ]; |
| 92 | + |
| 93 | + yield 'string with single quotes' => [ |
| 94 | + "<?php class Foo { public const string BAR = 'John Doe'; }", |
| 95 | + "<?php class Foo { public const BAR = 'John Doe'; }", |
| 96 | + ]; |
| 97 | + |
| 98 | + yield 'unknown other constant' => [ |
| 99 | + '<?php class Foo { public const mixed BAR = CONSTANT_FROM_FAR_AWAY; }', |
| 100 | + '<?php class Foo { public const BAR = CONSTANT_FROM_FAR_AWAY; }', |
| 101 | + ]; |
| 102 | + |
| 103 | + yield 'expression of unknown type' => [ |
| 104 | + '<?php class Foo { public const mixed BAR = 10 * (FLOAT_OR_INTEGER + 7); }', |
| 105 | + '<?php class Foo { public const BAR = 10 * (FLOAT_OR_INTEGER + 7); }', |
| 106 | + ]; |
| 107 | + |
| 108 | + yield 'multiple constants' => [ |
| 109 | + <<<'PHP' |
| 110 | + <?php |
| 111 | + class Foo { |
| 112 | + public const int ONE = 1; |
| 113 | + protected const bool|float ALREADY_TYPED = true; |
| 114 | + private const string NAME = 'name'; |
| 115 | + } |
| 116 | + const NOT_CLASS = true; |
| 117 | + class Bar { |
| 118 | + const array ARRAY_LONG_SYNTAX = array(); |
| 119 | + const array ARRAY_SHORT_SYNTAX = []; |
| 120 | + const string lowercased_name = 'lowercased_name'; |
| 121 | + } |
| 122 | + PHP, |
| 123 | + <<<'PHP' |
| 124 | + <?php |
| 125 | + class Foo { |
| 126 | + public const ONE = 1; |
| 127 | + protected const bool|float ALREADY_TYPED = true; |
| 128 | + private const NAME = 'name'; |
| 129 | + } |
| 130 | + const NOT_CLASS = true; |
| 131 | + class Bar { |
| 132 | + const ARRAY_LONG_SYNTAX = array(); |
| 133 | + const ARRAY_SHORT_SYNTAX = []; |
| 134 | + const lowercased_name = 'lowercased_name'; |
| 135 | + } |
| 136 | + PHP, |
| 137 | + ]; |
| 138 | + |
| 139 | + // if someone does these, they deserve to have their code broken |
| 140 | + yield 'constant that can be of different types' => [ |
| 141 | + '<?php class Foo { public const string BAR = SHOULD_BE_INT ? 1 : "one"; }', |
| 142 | + '<?php class Foo { public const BAR = SHOULD_BE_INT ? 1 : "one"; }', |
| 143 | + ]; |
| 144 | + |
| 145 | + yield 'constant that can be of different types - more complex case' => [ |
| 146 | + <<<'PHP' |
| 147 | + <?php |
| 148 | + class HellCoreServiceManagerHelper |
| 149 | + { |
| 150 | + const float OPTION_666__YES__1010011010_VALUE_4_1_3 |
| 151 | + = IS_OVERRIDEN_BY_BEELZEBOSS |
| 152 | + ? "Hell yeah" |
| 153 | + : CIRCLES_MANAGER_ACCESS === [0o1232, 'super_manager', false, -66.6] |
| 154 | + ? true |
| 155 | + : HellComponent443556::SHOULDNT_NOT_BE_DIFFERENT_THAN_NULL |
| 156 | + ? null |
| 157 | + : 0.001; |
| 158 | + } |
| 159 | + PHP, |
| 160 | + <<<'PHP' |
| 161 | + <?php |
| 162 | + class HellCoreServiceManagerHelper |
| 163 | + { |
| 164 | + const OPTION_666__YES__1010011010_VALUE_4_1_3 |
| 165 | + = IS_OVERRIDEN_BY_BEELZEBOSS |
| 166 | + ? "Hell yeah" |
| 167 | + : CIRCLES_MANAGER_ACCESS === [0o1232, 'super_manager', false, -66.6] |
| 168 | + ? true |
| 169 | + : HellComponent443556::SHOULDNT_NOT_BE_DIFFERENT_THAN_NULL |
| 170 | + ? null |
| 171 | + : 0.001; |
| 172 | + } |
| 173 | + PHP, |
| 174 | + ]; |
| 175 | + } |
| 176 | +} |
0 commit comments