Skip to content

Commit

Permalink
[Php80] Keep numeric string, string "true", "false" as is on StringAn…
Browse files Browse the repository at this point in the history
…notationToAttributeMapper
  • Loading branch information
samsonasik committed Dec 18, 2024
1 parent c5ac495 commit fe84a13
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
31 changes: 31 additions & 0 deletions tests/Issues/TestWithAttribute/Fixture/keep_string_as_is.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Issues\DoubleRun\TestWithAttribute;

class KeepStringAsIs extends \PHPUnit\Framework\TestCase
{
/**
* @testWith ["2", 2, "null", "true", "false", true, false]
* ["[email protected]"]
*/
public function testSomething(string $userId): void
{
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\DoubleRun\TestWithAttribute;

class KeepStringAsIs extends \PHPUnit\Framework\TestCase
{
#[\PHPUnit\Framework\Attributes\TestWith(['2', 2, 'null', 'true', 'false', true, false])]
#[\PHPUnit\Framework\Attributes\TestWith(['[email protected]'])]
public function testSomething(string $userId): void
{
}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/TestWithAttribute/TestWithAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\TestWithAttribute;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class TestWithAttributeTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
10 changes: 10 additions & 0 deletions tests/Issues/TestWithAttribute/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(TestWithAnnotationToAttributeRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

0 comments on commit fe84a13

Please sign in to comment.