Skip to content

Commit 02c8b1d

Browse files
Add impure annotation in some Collection methods
1 parent 442bb84 commit 02c8b1d

File tree

3 files changed

+68
-41
lines changed

3 files changed

+68
-41
lines changed

stubs/Collections/Collection.stub

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,31 @@ use IteratorAggregate;
1616
interface Collection extends Countable, IteratorAggregate, ArrayAccess, ReadableCollection
1717
{
1818

19+
/**
20+
* @phpstan-impure
21+
*
22+
* @param T $element
23+
*
24+
* @return true
25+
*/
26+
public function add($element) {}
27+
28+
/**
29+
* @phpstan-impure
30+
*
31+
* @param TKey $key
32+
*
33+
* @return T|null
34+
*/
35+
public function remove($key) {}
36+
37+
/**
38+
* @phpstan-impure
39+
*
40+
* @param T $element
41+
*
42+
* @return bool
43+
*/
44+
public function removeElement($element) {}
45+
1946
}

tests/Type/Doctrine/Collection/IsEmptyTypeSpecifyingExtensionTest.php

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,28 @@
22

33
namespace PHPStan\Type\Doctrine\Collection;
44

5-
use PHPStan\Rules\Rule;
6-
use PHPStan\Testing\RuleTestCase;
5+
use PHPStan\Testing\TypeInferenceTestCase;
76

8-
/**
9-
* @extends RuleTestCase<VariableTypeReportingRule>
10-
*/
11-
class IsEmptyTypeSpecifyingExtensionTest extends RuleTestCase
7+
class IsEmptyTypeSpecifyingExtensionTest extends TypeInferenceTestCase
128
{
139

14-
protected function getRule(): Rule
10+
/** @return iterable<mixed> */
11+
public function dataFileAsserts(): iterable
1512
{
16-
return new VariableTypeReportingRule();
13+
yield from $this->gatherAssertTypes(__DIR__ . '/data/collection.php');
1714
}
1815

19-
public function testExtension(): void
16+
/**
17+
* @dataProvider dataFileAsserts
18+
* @param mixed ...$args
19+
*/
20+
public function testFileAsserts(
21+
string $assertType,
22+
string $file,
23+
...$args
24+
): void
2025
{
21-
$this->analyse([__DIR__ . '/data/collection.php'], [
22-
[
23-
'Variable $entityOrFalse1 is: MyEntity|false',
24-
18,
25-
],
26-
[
27-
'Variable $entityOrFalse2 is: MyEntity|false',
28-
21,
29-
],
30-
[
31-
'Variable $false1 is: false',
32-
25,
33-
],
34-
[
35-
'Variable $false2 is: false',
36-
28,
37-
],
38-
[
39-
'Variable $entity1 is: MyEntity',
40-
33,
41-
],
42-
[
43-
'Variable $entity2 is: MyEntity',
44-
36,
45-
],
46-
]);
26+
$this->assertFileAsserts($assertType, $file, ...$args);
4727
}
4828

4929
public static function getAdditionalConfigFiles(): array

tests/Type/Doctrine/Collection/data/collection.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use Doctrine\Common\Collections\ArrayCollection;
44

5+
use function PHPStan\Testing\assertType;
6+
57
class MyEntity
68
{
79

@@ -15,23 +17,41 @@ class MyEntity
1517
$collection = new ArrayCollection();
1618

1719
$entityOrFalse1 = $collection->first();
18-
$entityOrFalse1;
20+
assertType('MyEntity|false', $entityOrFalse1);
1921

2022
$entityOrFalse2 = $collection->last();
21-
$entityOrFalse2;
23+
assertType('MyEntity|false', $entityOrFalse2);
2224

2325
if ($collection->isEmpty()) {
2426
$false1 = $collection->first();
25-
$false1;
27+
assertType('false', $false1);
2628

2729
$false2 = $collection->last();
28-
$false2;
30+
assertType('false', $false2);
2931
}
3032

3133
if (!$collection->isEmpty()) {
3234
$entity1 = $collection->first();
33-
$entity1;
35+
assertType('MyEntity', $entity1);
3436

3537
$entity2 = $collection->last();
36-
$entity2;
38+
assertType('MyEntity', $entity2);
39+
}
40+
41+
if ($collection->isEmpty()) {
42+
$collection->add($new);
43+
$result1 = $collection->first();
44+
assertType('MyEntity|false', $result1);
45+
46+
$result2 = $collection->last();
47+
assertType('MyEntity|false', $result2);
48+
}
49+
50+
if (!$collection->isEmpty()) {
51+
$collection->removeElement($new);
52+
$result3 = $collection->first();
53+
assertType('MyEntity|false', $result3);
54+
55+
$result4 = $collection->last();
56+
assertType('MyEntity|false', $result4);
3757
}

0 commit comments

Comments
 (0)