1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add a bunch of new checks

This commit is contained in:
Matthew Brown 2016-09-21 19:15:09 -04:00
parent abae0a9cae
commit 041f183d49
2 changed files with 31 additions and 8 deletions

View File

@ -115,7 +115,7 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<string,string>', (string) $context->vars_in_scope['foo']);
$this->assertEquals('string', (string) $context->vars_in_scope['foo[\'bar\']']);
}
public function testImplicit2DStringArrayCreation()
@ -132,7 +132,7 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<string,array<string,string>>', (string) $context->vars_in_scope['foo']);
$this->assertEquals('string', (string) $context->vars_in_scope['foo[\'bar\'][\'baz\']']);
}
public function testImplicit3DStringArrayCreation()
@ -146,6 +146,7 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<string,array<string,array<string,string>>>', (string) $context->vars_in_scope['foo']);
$this->assertEquals('string', (string) $context->vars_in_scope['foo[\'bar\'][\'baz\'][\'bat\']']);
}
public function testImplicit4DStringArrayCreation()
@ -159,6 +160,7 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<string,array<string,array<string,array<string,string>>>>', (string) $context->vars_in_scope['foo']);
$this->assertEquals('string', (string) $context->vars_in_scope['foo[\'bar\'][\'baz\'][\'bat\'][\'bap\']']);
}
public function test2Step2DIntArrayCreation()
@ -172,6 +174,7 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<string,array<string,string>>', (string) $context->vars_in_scope['foo']);
$this->assertEquals('string', (string) $context->vars_in_scope['foo[\'bar\'][\'baz\']']);
}
public function test2StepImplicit3DIntArrayCreation()
@ -224,12 +227,16 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
$foo = [];
$foo["a"] = "hello";
$foo["b"][] = "goodbye";
$bar = $foo["a"];
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<string,string|array<int,string>>', (string) $context->vars_in_scope['foo']);
$this->assertEquals('string', (string) $context->vars_in_scope['foo[\'a\']']);
$this->assertEquals('array<int,string>', (string) $context->vars_in_scope['foo[\'b\']']);
$this->assertEquals('string', (string) $context->vars_in_scope['bar']);
}
public function testConflictingTypesWithAssignment3()
@ -245,4 +252,20 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
$file_checker->check(true, true, $context);
$this->assertEquals('array<string,string|array<string,array<string,string>>>', (string) $context->vars_in_scope['foo']);
}
public function testIssetKeyedOffset()
{
$file_checker = new \Psalm\Checker\FileChecker(
'somefile.php',
self::$_parser->parse('<?php
if (!isset($foo["a"])) {
$foo["a"] = "hello";
}
')
);
$context = new Context('somefile.php');
$context->vars_in_scope['foo'] = \Psalm\Type::getArray();
$file_checker->check(true, true, $context);
$this->assertEquals('string', (string) $context->vars_in_scope['foo[\'a\']']);
}
}

View File

@ -35,7 +35,7 @@ class TypeCombinationTest extends PHPUnit_Framework_TestCase
public function testArrayOfIntOrString()
{
$this->assertEquals(
'array<int|string,int|string>',
'array<mixed,int|string>',
(string) Type::combineTypes([
self::getAtomic('array<int>'),
self::getAtomic('array<string>')
@ -46,7 +46,7 @@ class TypeCombinationTest extends PHPUnit_Framework_TestCase
public function testArrayOfIntOrAlsoString()
{
$this->assertEquals(
'array<int|string,int>|string',
'array<mixed,int>|string',
(string) Type::combineTypes([
self::getAtomic('array<int>'),
self::getAtomic('string')
@ -68,7 +68,7 @@ class TypeCombinationTest extends PHPUnit_Framework_TestCase
public function testArrayStringOrEmptyArray()
{
$this->assertEquals(
'array<int|string,string>',
'array<mixed,string>',
(string) Type::combineTypes([
self::getAtomic('array<empty>'),
self::getAtomic('array<string>')
@ -79,7 +79,7 @@ class TypeCombinationTest extends PHPUnit_Framework_TestCase
public function testArrayMixedOrString()
{
$this->assertEquals(
'array<int|string,mixed>',
'array<mixed,mixed>',
(string) Type::combineTypes([
self::getAtomic('array<mixed>'),
self::getAtomic('array<string>')
@ -101,7 +101,7 @@ class TypeCombinationTest extends PHPUnit_Framework_TestCase
public function testArrayMixedOrEmpty()
{
$this->assertEquals(
'array<int|string,mixed>',
'array<mixed,mixed>',
(string) Type::combineTypes([
self::getAtomic('array<empty>'),
self::getAtomic('array<mixed>')
@ -112,7 +112,7 @@ class TypeCombinationTest extends PHPUnit_Framework_TestCase
public function testArrayBigCombination()
{
$this->assertEquals(
'array<int|string,int|float|string>',
'array<mixed,int|float|string>',
(string) Type::combineTypes([
self::getAtomic('array<int|float>'),
self::getAtomic('array<string>')