Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/FileReportGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
26 changes: 25 additions & 1 deletion tests/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testDetectDefault(): void
],
[
'line' => 15,
'value' => 15,
'value' => '0o15',
],
[
'line' => 18,
Expand Down Expand Up @@ -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
);
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/Files/test_1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/Files/test_different_base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
class test
{
public function base() {
$hex = 0x25;
$binary = 0b1111;
}
}