Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 11, 2022
1 parent f206515 commit c728038
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Expand Up @@ -570,4 +570,9 @@ public function testBug5979(): void
$this->analyse([__DIR__ . '/data/bug-5979.php'], []);
}

public function testBug4165(): void
{
$this->analyse([__DIR__ . '/data/bug-4165.php'], []);
}

}
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4165.php
@@ -0,0 +1,36 @@
<?php

namespace Bug4165;

interface Client
{
/**
* @return 'int'|'stg'|'prd'
*/
public function env(): string;
}

final class ComparisonKeysBuilder
{
private Client $client;
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* @phpstan-return array<'int'|'stg'|'prd', int>
*/
public function __invoke(): array
{
$result = [
'int' => 3,
'stg' => 4,
'prd' => 5
];

$result[$this->client->env()] = 42;

return $result;
}
}
Expand Up @@ -344,4 +344,32 @@ public function testBug6286(): void
]);
}

public function testBug4906(): void
{
$this->analyse([__DIR__ . '/data/bug-4906.php'], []);
}

public function testBug4910(): void
{
$this->analyse([__DIR__ . '/data/bug-4910.php'], []);
}

public function testBug3703(): void
{
$this->analyse([__DIR__ . '/data/bug-3703.php'], [
[
'Property Bug3703\Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, array<string, array<int, string>>>.',
15,
],
[
'Property Bug3703\Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, array<string, int>>.',
18,
],
[
'Property Bug3703\Foo::$bar (array<string, array<string, array<int>>>) does not accept array<string, string>.',
21,
],
]);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-3703.php
@@ -0,0 +1,24 @@
<?php

namespace Bug3703;

class Foo {
/**
* @var array<string, array<string, int[]>>
*/
public $bar;

public function doFoo()
{
$foo = new self();
// Should not be allowed (missing string key)
$foo->bar['foo']['bar'][] = 'ok';

// Should not be allowed (value should be array)
$foo->bar['foo']['bar'] = 1;

// Should not be allowed
$foo->bar['ok'] = 'ok';
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-4906.php
@@ -0,0 +1,32 @@
<?php

namespace Bug4906;

class Connection
{
/**
* @var array<string,mixed>
* @phpstan-var array<string,mixed>
* @psalm-var Params
*/
private $params;
}

class HelloWorld
{
/**
* @var array<string,mixed>
*/
private $connectionParameters;

private function overrideConnectionParameters(): void
{
$overrideConnectionParameters = \Closure::bind(function (array $connectionParameters) {
foreach ($connectionParameters as $parameterKey => $parameterValue) {
$this->params[$parameterKey] = $parameterValue;
}
}, $this, Connection::class);
$overrideConnectionParameters($this->connectionParameters);
}

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-4910.php
@@ -0,0 +1,39 @@
<?php

namespace Bug4910;

class Facing{
public const NORTH = 2;
public const SOUTH = 3;
public const WEST = 4;
public const EAST = 5;
}

/**
* @phpstan-type VineAcceptedFaces Facing::NORTH|Facing::EAST|Facing::SOUTH|Facing::WEST
*/
class Vine{
/**
* @var int[]
* @phpstan-var array<VineAcceptedFaces, VineAcceptedFaces>
*/
protected $faces = [];

/**
* @param int[] $faces
* @phpstan-param list<VineAcceptedFaces> $faces
* @return $this
*/
public function setFaces(array $faces) : self{
$uniqueFaces = [];
foreach($faces as $face){
if($face !== Facing::NORTH && $face !== Facing::SOUTH && $face !== Facing::WEST && $face !== Facing::EAST){
throw new \InvalidArgumentException("Facing can only be north, east, south or west");
}
$uniqueFaces[$face] = $face;
}

$this->faces = $uniqueFaces;
return $this;
}
}

0 comments on commit c728038

Please sign in to comment.