1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Add suppport for float enums

Fixes #1099
This commit is contained in:
Matthew Brown 2019-01-27 18:34:13 -05:00
parent 0574e06a42
commit 89999fd554
3 changed files with 34 additions and 6 deletions

View File

@ -500,6 +500,10 @@ abstract class Type
return new Atomic\TScalarClassConstant($fq_classlike_name, $const_name);
}
if (preg_match('/^\-?(0|[1-9][0-9]*)(\.[0-9]{1,})$/', $parse_tree->value)) {
return new TLiteralFloat((float) $parse_tree->value);
}
if (preg_match('/^\-?(0|[1-9][0-9]*)$/', $parse_tree->value)) {
return new TLiteralInt((int) $parse_tree->value);
}
@ -701,6 +705,17 @@ abstract class Type
}
if ($char === '.') {
if ($i + 1 < $c
&& is_numeric($chars[$i + 1])
&& $i > 0
&& is_numeric($chars[$i - 1])
) {
$type_tokens[$rtc] .= $char;
$was_char = false;
continue;
}
if ($i + 2 > $c || $chars[$i + 1] !== '.' || $chars[$i + 2] !== '.') {
throw new TypeParseTreeException('Unexpected token ' . $char);
}

View File

@ -31,13 +31,15 @@ class EnumTest extends TestCase
'<?php
namespace Ns;
/** @psalm-param "foo\"with"|"bar"|1|2|3 $s */
/** @psalm-param "foo\"with"|"bar"|1|2|3|4.0|4.1 $s */
function foo($s) : void {}
foo("foo\"with");
foo("bar");
foo(1);
foo(2);
foo(3);',
foo(3);
foo(4.0);
foo(4.1);',
],
'noRedundantConditionWithSwitch' => [
'<?php
@ -138,6 +140,15 @@ class EnumTest extends TestCase
foo(4);',
'error_message' => 'InvalidArgument',
],
'enumWrongFloat' => [
'<?php
namespace Ns;
/** @psalm-param 1.2|3.4|5.6 $s */
function foo($s) : void {}
foo(7.8);',
'error_message' => 'InvalidArgument',
],
'classConstantIncorrect' => [
'<?php
namespace Ns;

View File

@ -657,7 +657,7 @@ class TypeParseTest extends TestCase
*/
public function testEnum()
{
$docblock_type = Type::parseString('( \'foo\\\'with\' | "bar\"bar" | "baz" | "bat\\\\" | \'bang bang\' | 1 | 2 | 3)');
$docblock_type = Type::parseString('( \'foo\\\'with\' | "bar\"bar" | "baz" | "bat\\\\" | \'bang bang\' | 1 | 2 | 3 | 4.5)');
$resolved_type = new Type\Union([
new Type\Atomic\TLiteralString('foo\'with'),
@ -667,7 +667,8 @@ class TypeParseTest extends TestCase
new Type\Atomic\TLiteralString('bang bang'),
new Type\Atomic\TLiteralInt(1),
new Type\Atomic\TLiteralInt(2),
new Type\Atomic\TLiteralInt(3)
new Type\Atomic\TLiteralInt(3),
new Type\Atomic\TLiteralFloat(4.5)
]);
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
@ -678,7 +679,7 @@ class TypeParseTest extends TestCase
*/
public function testEnumWithoutSpaces()
{
$docblock_type = Type::parseString('\'foo\\\'with\'|"bar\"bar"|"baz"|"bat\\\\"|\'bang bang\'|1|2|3');
$docblock_type = Type::parseString('\'foo\\\'with\'|"bar\"bar"|"baz"|"bat\\\\"|\'bang bang\'|1|2|3|4.5');
$resolved_type = new Type\Union([
new Type\Atomic\TLiteralString('foo\'with'),
@ -688,7 +689,8 @@ class TypeParseTest extends TestCase
new Type\Atomic\TLiteralString('bang bang'),
new Type\Atomic\TLiteralInt(1),
new Type\Atomic\TLiteralInt(2),
new Type\Atomic\TLiteralInt(3)
new Type\Atomic\TLiteralInt(3),
new Type\Atomic\TLiteralFloat(4.5)
]);
$this->assertSame($resolved_type->getId(), $docblock_type->getId());