Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13809.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@ function bar(array $list): void
assertType("list<'foo'>", $list);
}

/**
* @param list<mixed> $list
*/
function bar2(array $list): void
{
foreach ($list as $key => &$value) {
if (rand(0, 1)) {
$value = 'foo';
}
$key = 'bar';
}

assertType("list<mixed>", $list);
}

/**
* @param list<mixed> $list
*/
function bar3(array $list): void
{
foreach ($list as &$value) {
if (rand(0, 1)) {
$value = 'foo';
} else {
$value = 'maybe';
}
}

assertType("list<mixed>", $list); // could be list<'foo'|'maybe'>
}

/**
* @param list<string> $list
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13851.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Bug13851;

use function PHPStan\Testing\assertType;

class Transaction
{
public function __construct(public int $value)
{
}

public function withValue(int $value): self
{
return new self($value);
}
}

/**
* @return array<string, non-empty-list<Transaction>>
*/
function getPositions(): array
{
return ['AAPL' => [new Transaction(50)], 'TSLA' => [new Transaction(50), new Transaction(100)]];
}

$positions = getPositions();

foreach ($positions as $symbol => &$transactions) {
foreach ($transactions as &$transaction) {
$transaction = $transaction->withValue(60);
}
}

assertType('array<string, non-empty-list<Bug13851\Transaction>>', $positions);
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14083.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Bug14083;

use function PHPStan\Testing\assertType;

/**
* @param list<string> $convert
*/
function example(array $convert): void {
foreach ($convert as &$item) {
$item = strtoupper($item);
}
assertType('list<string>', $convert);
}

/**
* @param list<string> $convert
*/
function example2(array $convert): void {
foreach ($convert as $key => $item) {
$convert[$key] = strtoupper($item);
}
assertType('list<uppercase-string>', $convert);
}
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14084.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Bug14084;

use function PHPStan\Testing\assertType;

/**
* @param array<string, list<string>> $convert
*/
function example(array $convert): void
{
foreach ($convert as &$inner) {
foreach ($inner as &$val) {
$val = strtoupper($val); // https://github.com/phpstan/phpstan/issues/14083
}
}
assertType('array<string, list<string>>', $convert);
}

/**
* @param array<string, list<string>> $convert
*/
function example2(array $convert): void
{
foreach ($convert as &$inner) {
foreach ($inner as $key => $val) {
$inner[$key] = strtoupper($val);
}
}
assertType('array<string, list<string>>', $convert);
}

/**
* @param array<string, list<string>> $convert
*/
function example3(array &$convert): void
{
foreach ($convert as $outerKey => $inner) {
foreach ($inner as $key => $val) {
$convert[$outerKey][$key] = strtoupper($val);
}
}
assertType('array<string, list<string>>', $convert);
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,4 +1305,9 @@ public function testBug5946(): void
]);
}

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

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9669.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bug9669;

class HelloWorld
{
/**
* @param array<int, array{a: string}> $sets
*
* @return array<int, array{a: string, b: bool}>
*/
public function sayHello(array $sets): array
{
foreach ($sets as &$set) {
$set['b'] = false;
}

return $sets;
}
}
Loading