Skip to content

Commit

Permalink
Fix float parsing for number starting with 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 25, 2023
1 parent a44ec55 commit c316a3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8216,8 +8216,8 @@ private function getNumberFromImage($numberRepresentation)
return bindec(substr($numberRepresentation, 2));

default:
if (substr($numberRepresentation, 0, 1) === '0') {
return octdec(preg_replace('/^0+(oO)?/', '', $numberRepresentation));
if (preg_match('/^0+[oO]?(\d+)$/', $numberRepresentation, $match)) {
return octdec($match[1]);

Check warning on line 8220 in src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php

View check run for this annotation

Codecov / codecov/patch

src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php#L8220

Added line #L8220 was not covered by tests
}

return $numberRepresentation;
Expand Down
17 changes: 17 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTLiteralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ public function testLiteralWithNonZeroBinaryIntegerValue()
$this->assertEquals('0b100100100110', $literal->getImage());
}

/**
* testLiteralWithZeroFloatValue
*
* @return void
* @since 2.16.0
* @covers \PDepend\Source\Language\PHP\AbstractPHPParser
*/
public function testLiteralWithZeroFloatValue()
{
$class = $this->getFirstClassForTestCase();
$properties = $class->getProperties();
/** @var ASTProperty $property */
$property = $properties[0];

$this->assertSame(0.0, $property->getDefaultValue());
}

/**
* testLiteralWithCurlyBraceFollowedByCompoundExpression
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class testLiteralWithZeroFloatValue
{
private float $amount = 0.00;
}

0 comments on commit c316a3c

Please sign in to comment.