Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Aug 30, 2022
1 parent 5a52370 commit 41be802
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,14 @@ public function testSpecifyExistentOffsetWhenEnteringForeach(): void
$this->analyse([__DIR__ . '/data/specify-existent-offset-when-entering-foreach.php'], []);
}

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

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

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

namespace Bug3872;

class Demo
{
public function analyze($json): void
{
foreach (json_decode($json, true) as $item) {
$item += ['value' => '', 'operator' => ''];
if ($item['value']) {
$item['value'] = strtotime($item['value']);
if ($item['operator'] === 'eq') {
echo 'test';
}
}
}
}
}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-6783.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bug6783;

/**
* @return array<string, mixed>
*/
function foo(): array
{
// something from elsewhere (not in the scope of PHPStan)
return [
// removing or keeping those lines does/should not change the reporting
'foo' => [
'bar' => true,
]
];
}

function bar() {
$data = foo();
$data = $data['foo'] ?? []; // <<< removing this line suppress the error
$data += [
'default' => true,
];
foreach (['formatted'] as $field) {
$data[$field] = empty($data[$field]) ? false : true;
}

$bar = $data['bar'];
}

0 comments on commit 41be802

Please sign in to comment.