Skip to content
Prev Previous commit
Next Next commit
add new tests. Can't run them locally, will check on CI
  • Loading branch information
orklah committed Jan 3, 2020
commit 1ad0eb02b7f99f2abc5e122db7c6783112819ba5
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9773,6 +9773,11 @@ public function dataBug2648(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2648.php');
}

public function dataListType(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/list-type.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand All @@ -9791,6 +9796,7 @@ public function dataBug2648(): array
* @dataProvider dataPsalmPrefixedTagsWithUnresolvableTypes
* @dataProvider dataComplexGenericsExample
* @dataProvider dataBug2648
* @dataProvider dataListType
* @param ConstantStringType $expectedType
* @param Type $actualType
*/
Expand Down
110 changes: 110 additions & 0 deletions tests/PHPStan/Analyser/data/list-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace ListType;

use function PHPStan\Analyser\assertType;

class Foo
{
public function withoutGenerics(): void
{
/** @var list $list */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should at least once typehint the type as method parameter and assert it right at the beginning of the body. If you assert the variable only after some changes (array appending), you have no guarantees about the state right after $list = [];.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I added some test cases with direct assertions. It should be ok

$list = [];
$list[] = '1';
$list[] = true;
$list[] = new \stdClass();
assertType('array<int, mixed>', $list);
}


public function withMixedType(): void
{
/** @var list<mixed> $list */
$list = [];
$list[] = '1';
$list[] = true;
$list[] = new \stdClass();
assertType('array<int, mixed>', $list);
}

public function withObjectType(): void
{
/** @var list<\DateTime> $list */
$list = [];
$list[] = new \DateTime();
assertType('array<int, \DateTime>', $list);
}

public function withObjectTypeWrongContent(): void
{
/** @var list<\DateTime> $list */
$list = [];
$list[] = new \DateTime();
$list[] = false;
assertType('*error*', $list);//this should fail. Fix type
}

public function withScalarTypeWrongContent(): void
{
/** @var list<scalar> $list */
$list = [];
$list[] = '1';
$list[] = true;
$list[] = new \DateTime();
assertType('*error*', $list);//this should fail. Fix type
}

/** @return list<scalar> */
public function withScalarGoodContent(): void
{
/** @var list<scalar> $list */
$list = [];
$list[] = '1';
$list[] = true;
assertType('array<int, scalar>', $list);
}

public function withStringKey(): void
{
/** @var list $list */
$list = [];
$list[] = '1';
$list['a'] = true;
assertType('*error*', $list);//this should fail. Fix type
}

public function withNumericKey(): void
{
/** @var list $list */
$list = [];
$list[] = '1';
$list['1'] = true;
assertType('array<int, scalar>', $list);
}

public function withWrongGenericsNumber(): void
{
/** @var list<int, mixed> $list */
$list = [];
$list[] = '1';
$list[] = true;
assertType('*error*', $list);//this should fail. Fix type
}

public function withFullListFunctionality(): void
{
// These won't output errors for now but should when list type will be fully implemented
/** @var list $list */
$list = [];
$list[] = '1';
$list[] = '2';
unset($list[0]);//break list behaviour
assertType('array<int, mixed>', $list);

/** @var list $list2 */
$list2 = [];
$list2[2] = '1';//Most likely to create a gap in indexes
assertType('array<int, scalar>', $list2);
}

}