Skip to content

Commit

Permalink
ComponentReflection: accepts value '1.0' as float [Closes #200]
Browse files Browse the repository at this point in the history
note: gettype() returns 'double', ReflectionType returns 'float'
  • Loading branch information
dg committed Sep 12, 2018
1 parent 0b30b31 commit b808a66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,13 @@ public static function convertType(&$val, $type, $isClass = false)
return false;

} else {
$old = $tmp = ($val === false ? '0' : (string) $val);
$tmp = ($val === false ? '0' : (string) $val);
if ($type === 'double' || $type === 'float') {
$tmp = preg_replace('#\.0*\z#', '', $tmp);
}
$orig = $tmp;
settype($tmp, $type);
if ($old !== ($tmp === false ? '0' : (string) $tmp)) {
if ($orig !== ($tmp === false ? '0' : (string) $tmp)) {
return false; // data-loss occurs
}
$val = $tmp;
Expand Down
25 changes: 24 additions & 1 deletion tests/UI/ComponentReflection.convertType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ testIt('double', '');
testIt('double', 'a');
testIt('double', '0', 0.0);
testIt('double', '1', 1.0);
testIt('double', '1.0');
testIt('double', '1.', 1.0);
testIt('double', '1.0', 1.0);
testIt('double', '1.00', 1.0);
testIt('double', '1..0');
testIt('double', '1.1', 1.1);
testIt('double', '1a');
testIt('double', true, 1.0);
Expand All @@ -102,6 +105,26 @@ testIt('double', 1, 1.0);
testIt('double', 1.0, 1.0);
testIt('double', 1.2, 1.2);

testIt('float', null);
testIt('float', []);
testIt('float', $obj);
testIt('float', '');
testIt('float', 'a');
testIt('float', '0', 0.0);
testIt('float', '1', 1.0);
testIt('float', '1.', 1.0);
testIt('float', '1.0', 1.0);
testIt('float', '1.00', 1.0);
testIt('float', '1..0');
testIt('float', '1.1', 1.1);
testIt('float', '1a');
testIt('float', true, 1.0);
testIt('float', false, 0.0);
testIt('float', 0, 0.0);
testIt('float', 1, 1.0);
testIt('float', 1.0, 1.0);
testIt('float', 1.2, 1.2);

testIt('bool', null);
testIt('bool', []);
testIt('bool', $obj);
Expand Down

0 comments on commit b808a66

Please sign in to comment.