From fe84a138edde2980f047f58d5972d762c26e6549 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 19 Dec 2024 01:51:44 +0700 Subject: [PATCH] [Php80] Keep numeric string, string "true", "false" as is on StringAnnotationToAttributeMapper --- .../StringAnnotationToAttributeMapper.php | 20 ------------ .../Fixture/keep_string_as_is.php.inc | 31 +++++++++++++++++++ .../TestWithAttributeTest.php | 28 +++++++++++++++++ .../config/configured_rule.php | 10 ++++++ .../AnnotationToAttributeMapperTest.php | 5 +-- 5 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 tests/Issues/TestWithAttribute/Fixture/keep_string_as_is.php.inc create mode 100644 tests/Issues/TestWithAttribute/TestWithAttributeTest.php create mode 100644 tests/Issues/TestWithAttribute/config/configured_rule.php diff --git a/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php b/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php index 400fd658da..fa3a48149a 100644 --- a/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php +++ b/src/PhpAttribute/AnnotationToAttributeMapper/StringAnnotationToAttributeMapper.php @@ -5,9 +5,6 @@ namespace Rector\PhpAttribute\AnnotationToAttributeMapper; use PhpParser\Node\Expr; -use PhpParser\Node\Expr\ConstFetch; -use PhpParser\Node\Name; -use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Scalar\String_; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface; @@ -27,23 +24,6 @@ public function isCandidate(mixed $value): bool */ public function map($value): Expr { - if (strtolower($value) === 'true') { - return new ConstFetch(new Name('true')); - } - - if (strtolower($value) === 'false') { - return new ConstFetch(new Name('false')); - } - - if (strtolower($value) === 'null') { - return new ConstFetch(new Name('null')); - } - - // number as string to number - if (is_numeric($value) && strlen((string) (int) $value) === strlen($value)) { - return Int_::fromString($value); - } - if (str_contains($value, "'") && ! str_contains($value, "\n")) { $kind = String_::KIND_DOUBLE_QUOTED; } else { diff --git a/tests/Issues/TestWithAttribute/Fixture/keep_string_as_is.php.inc b/tests/Issues/TestWithAttribute/Fixture/keep_string_as_is.php.inc new file mode 100644 index 0000000000..fd238ca262 --- /dev/null +++ b/tests/Issues/TestWithAttribute/Fixture/keep_string_as_is.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/tests/Issues/TestWithAttribute/TestWithAttributeTest.php b/tests/Issues/TestWithAttribute/TestWithAttributeTest.php new file mode 100644 index 0000000000..35ad441ae7 --- /dev/null +++ b/tests/Issues/TestWithAttribute/TestWithAttributeTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/TestWithAttribute/config/configured_rule.php b/tests/Issues/TestWithAttribute/config/configured_rule.php new file mode 100644 index 0000000000..4d4acca6e8 --- /dev/null +++ b/tests/Issues/TestWithAttribute/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(TestWithAnnotationToAttributeRector::class); +}; diff --git a/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php b/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php index b047a2059d..423e79b41b 100644 --- a/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php +++ b/tests/PhpAttribute/AnnotationToAttributeMapper/AnnotationToAttributeMapperTest.php @@ -43,9 +43,10 @@ public function test(mixed $input, string $expectedTypeClass): void public static function provideData(): Iterator { yield [false, ConstFetch::class]; - yield ['false', ConstFetch::class]; - yield ['100', Int_::class]; + yield ['false', String_::class]; + yield ['100', String_::class]; yield ['hey', String_::class]; yield [['hey'], Array_::class]; + yield [100, Int_::class]; } }