1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Add support for PHPDoc array style

This commit is contained in:
Matthew Brown 2016-10-29 20:57:03 -04:00
parent 95be3c4a05
commit 4bf07d0adb
2 changed files with 18 additions and 10 deletions

View File

@ -174,14 +174,14 @@ abstract class Type
public static function convertSquareBrackets($type) public static function convertSquareBrackets($type)
{ {
return preg_replace_callback( return preg_replace_callback(
'/([a-zA-Z\<\>\\\\_]+)((\[\])+)/', '/([a-zA-Z\<\>\\\\_\(\)|]+)((\[\])+)/',
function ($matches) { function ($matches) {
$inner_type = $matches[1]; $inner_type = str_replace(['(', ')'], '', $matches[1]);
$dimensionality = strlen($matches[2]) / 2; $dimensionality = strlen($matches[2]) / 2;
for ($i = 0; $i < $dimensionality; $i++) { for ($i = 0; $i < $dimensionality; $i++) {
$inner_type = 'array<int, ' . $inner_type . '>'; $inner_type = 'array<mixed, ' . $inner_type . '>';
} }
return $inner_type; return $inner_type;

View File

@ -28,10 +28,10 @@ class TypeParseTest extends PHPUnit_Framework_TestCase
public function testArray() public function testArray()
{ {
$this->assertEquals('array<int,int>', (string) Type::parseString('array<int,int>')); $this->assertEquals('array<int,int>', (string) Type::parseString('array<int, int>'));
$this->assertEquals('array<int,string>', (string) Type::parseString('array<int,string>')); $this->assertEquals('array<int,string>', (string) Type::parseString('array<int, string>'));
$this->assertEquals('array<int,static>', (string) Type::parseString('array<int,static>')); $this->assertEquals('array<int,static>', (string) Type::parseString('array<int, static>'));
$this->assertEquals('array<int|string,string>', (string) Type::parseString('array<int|string,string>')); $this->assertEquals('array<int|string,string>', (string) Type::parseString('array<int|string, string>'));
} }
public function testGeneric() public function testGeneric()
@ -39,10 +39,18 @@ class TypeParseTest extends PHPUnit_Framework_TestCase
$this->assertEquals('B<int>', (string) Type::parseString('B<int>')); $this->assertEquals('B<int>', (string) Type::parseString('B<int>'));
} }
public function testPhpDocStyle()
{
$this->assertEquals('array<mixed,A>', (string) Type::parseString('A[]'));
$this->assertEquals('array<mixed,A|B>', (string) Type::parseString('(A|B)[]'));
$this->assertEquals('array<mixed,array<mixed,A>>', (string) Type::parseString('A[][]'));
$this->assertEquals('array<mixed,array<mixed,A|B>>', (string) Type::parseString('(A|B)[][]'));
}
public function testObjectLike() public function testObjectLike()
{ {
$this->assertEquals('array{a:int,b:string}', (string) Type::parseString('array{a:int,b:string}')); $this->assertEquals('array{a:int,b:string}', (string) Type::parseString('array{a:int, b:string}'));
$this->assertEquals('array{a:int|string,b:string}', (string) Type::parseString('array{a:int|string,b:string}')); $this->assertEquals('array{a:int|string,b:string}', (string) Type::parseString('array{a:int|string, b:string}'));
$this->assertEquals('array{a:array<int,string|int>,b:string}', (string) Type::parseString('array{a:array<int,string|int>,b:string}')); $this->assertEquals('array{a:array<int,string|int>,b:string}', (string) Type::parseString('array{a:array<int, string|int>, b:string}'));
} }
} }