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
26 changes: 23 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5628,10 +5628,9 @@ private function processAssignVar(
$varNativeType = $scope->getNativeType($var);

// 4. compose types
if ($varType instanceof ErrorType) {
$isImplicitArrayCreation = $this->isImplicitArrayCreation($dimFetchStack, $scope);
if ($isImplicitArrayCreation->yes()) {
$varType = new ConstantArrayType([], []);
}
if ($varNativeType instanceof ErrorType) {
$varNativeType = new ConstantArrayType([], []);
}
$offsetValueType = $varType;
Expand Down Expand Up @@ -6018,6 +6017,27 @@ static function (): void {
return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
}

/**
* @param list<ArrayDimFetch> $dimFetchStack
*/
private function isImplicitArrayCreation(array $dimFetchStack, Scope $scope): TrinaryLogic
{
if (count($dimFetchStack) === 0) {
return TrinaryLogic::createNo();
}

$varNode = $dimFetchStack[0]->var;
if (!$varNode instanceof Variable) {
return TrinaryLogic::createNo();
}

if (!is_string($varNode->name)) {
return TrinaryLogic::createNo();
}

return $scope->hasVariableType($varNode->name)->negate();
}

/**
* @param list<ArrayDimFetch> $dimFetchStack
* @param list<Type|null> $offsetTypes
Expand Down
25 changes: 25 additions & 0 deletions src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Rules\Arrays;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
Expand All @@ -11,6 +13,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
Expand Down Expand Up @@ -42,6 +45,10 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if ($this->isImplicitArrayCreation($node, $scope)->yes()) {
return [];
}

if ($node->dim !== null) {
$dimType = $scope->getType($node->dim);
$unknownClassPattern = sprintf('Access to offset %s on an unknown class %%s.', SprintfHelper::escapeFormatString($dimType->describe(VerbosityLevel::value())));
Expand Down Expand Up @@ -153,4 +160,22 @@ public function processNode(Node $node, Scope $scope): array
);
}

private function isImplicitArrayCreation(Node\Expr\ArrayDimFetch $node, Scope $scope): TrinaryLogic
{
$varNode = $node->var;
while ($varNode instanceof ArrayDimFetch) {
$varNode = $varNode->var;
}

if (!$varNode instanceof Variable) {
return TrinaryLogic::createNo();
}

if (!is_string($varNode->name)) {
return TrinaryLogic::createNo();
}

return $scope->hasVariableType($varNode->name)->negate();
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12447-bis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace Bug12447Bis;

use function PHPStan\Testing\assertType;

interface Foo
{

}

class HelloWorld
{

public function __invoke(Foo $foo): void
{
$a = $foo->doFoo();
assertType('*ERROR*', $a);
$a[] = 5;
assertType('mixed', $a);
}
}
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12447.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Bug12447;

use function PHPStan\Testing\assertType;

assertType('mixed', $data);
$data[] = 1;
assertType('mixed', $data);

function (): void {
assertType('*ERROR*', $data);
$data[] = 1;
assertType('array{1}', $data);
};
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,17 @@ public function testBug3747(): void
$this->analyse([__DIR__ . '/data/bug-3747.php'], []);
}

public function testBug12447(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;

$this->analyse([__DIR__ . '/data/bug-12447.php'], [
[
'Cannot access an offset on mixed.',
5,
],
]);
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-12447.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);

namespace Bug12447;

$data[] = 1;

function (): void {
$data[] = 1;
};
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,11 @@ public function testBug12748(): void
$this->analyse([__DIR__ . '/data/bug-12748.php'], []);
}

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

public function testBug11019(): void
{
$this->analyse([__DIR__ . '/data/bug-11019.php'], []);
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-3803.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug3803;

/** @param array<string> $chars */
function fun(array $chars) : void{
$string = "";
foreach($chars as $k => $v){
$string[$k] = $v;
}
if($string === "wheee"){
var_dump("yes");
}
}

fun(["w", "h", "e", "e", "e"]);
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Variables/UnsetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function testBug4289(): void
$this->analyse([__DIR__ . '/data/bug-4289.php'], []);
}

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

public function testBug5223(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-5223.php'], [
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-4204.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug4204;

interface Block
{
public function getSettings(): mixed;
}

class HelloWorld
{
/**
* @param array<int, object> $blocks
*/
public function sayHello(array $blocks): void
{
foreach ($blocks as $block) {
$settings = $block->getSettings();

if (isset($settings['name'])) {
// switch name with code key
$settings['code'] = $settings['name'];
unset($settings['name']);
}
}
}
}
Loading