diff --git a/src/FileReportGenerator.php b/src/FileReportGenerator.php index f208674..619876d 100644 --- a/src/FileReportGenerator.php +++ b/src/FileReportGenerator.php @@ -27,6 +27,12 @@ class FileReportGenerator */ private $option; + private $baseLookup = [ + 2 => 'b', + 8 => 'o', + 16 => 'x', + ]; + public function __construct(SplFileInfo $file, Option $option) { $this->file = $file; @@ -59,6 +65,21 @@ public function detect(Node $node): iterable $extension->setOption($this->option); if ($extension->extend($node)) { + if (( + $scalar instanceof LNumber && + $scalar->getAttribute('kind') !== LNumber::KIND_DEC + )) { + $scalar->value = sprintf( + '0%s%d', + $this->baseLookup[$scalar->getAttribute('kind')], + base_convert( + (string) $scalar->value, + LNumber::KIND_DEC, + $scalar->getAttribute('kind') + ) + ); + } + yield new DetectionResult($this->file, $scalar->getLine(), $scalar->value); } } diff --git a/tests/DetectorTest.php b/tests/DetectorTest.php index 8d5f567..8b8df38 100644 --- a/tests/DetectorTest.php +++ b/tests/DetectorTest.php @@ -73,7 +73,7 @@ public function testDetectDefault(): void ], [ 'line' => 15, - 'value' => 15, + 'value' => '0o15', ], [ 'line' => 18, @@ -445,4 +445,28 @@ static function (DetectionResult $detectionResult) { iterator_to_array($result, false) ); } + + public function testDetectDifferentBase(): void + { + + $this->option->setExtensions([new AssignExtension()]); + + $result = $this->detector->detect($this->createSplFileInfo(self::FIXTURES_DIR . '/test_different_base.php')); + $numbers = $this->getActualResult($result); + $this->assertContains( + [ + 'line' => 5, + 'value' => '0x25', + ], + $numbers + ); + + $this->assertContains( + [ + 'line' => 6, + 'value' => '0b1111', + ], + $numbers + ); + } } diff --git a/tests/Fixtures/Files/test_1.php b/tests/Fixtures/Files/test_1.php index 69930d6..8115a24 100644 --- a/tests/Fixtures/Files/test_1.php +++ b/tests/Fixtures/Files/test_1.php @@ -12,7 +12,7 @@ class TEST_1 public function test($input = 4) { if ($input > 2) { - return 15; + return 015; } for ($i = 3; $i <= 10; $i++) { diff --git a/tests/Fixtures/Files/test_different_base.php b/tests/Fixtures/Files/test_different_base.php new file mode 100644 index 0000000..92d74ee --- /dev/null +++ b/tests/Fixtures/Files/test_different_base.php @@ -0,0 +1,9 @@ +