Skip to content
Open
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
56 changes: 49 additions & 7 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3072,15 +3072,57 @@

if ($originalExprType->equals($nativeType)) {
$newType = TypeCombinator::intersect($type, $originalExprType);
return $this->specifyExpressionType($expr, $newType, $newType, TrinaryLogic::createYes());
$scope = $this->specifyExpressionType($expr, $newType, $newType, TrinaryLogic::createYes());
} else {
$newType = TypeCombinator::intersect($type, $originalExprType);
$scope = $this->specifyExpressionType(
$expr,
$newType,
TypeCombinator::intersect($type, $nativeType),
TrinaryLogic::createYes(),
);
}

return $this->specifyExpressionType(
$expr,
TypeCombinator::intersect($type, $originalExprType),
TypeCombinator::intersect($type, $nativeType),
TrinaryLogic::createYes(),
);
if ($originalExprType instanceof MixedType && $newType->isArray()->yes()) {

Check warning on line 3086 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ ); } - if ($originalExprType instanceof MixedType && $newType->isArray()->yes()) { + if ($originalExprType instanceof MixedType && !$newType->isArray()->no()) { $exprKey = $this->getNodeKey($expr); foreach ($this->expressionTypes as $holder) { $holderExpr = $holder->getExpr();

Check warning on line 3086 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ ); } - if ($originalExprType instanceof MixedType && $newType->isArray()->yes()) { + if ($originalExprType instanceof MixedType && !$newType->isArray()->no()) { $exprKey = $this->getNodeKey($expr); foreach ($this->expressionTypes as $holder) { $holderExpr = $holder->getExpr();
$exprKey = $this->getNodeKey($expr);
foreach ($this->expressionTypes as $holder) {
$holderExpr = $holder->getExpr();
if (!$holderExpr instanceof Node\Expr\ArrayDimFetch || $holderExpr->dim === null) {
continue;
}
if ($this->getNodeKey($holderExpr->var) !== $exprKey) {
continue;
}
if (!$holder->getCertainty()->yes()) {

Check warning on line 3096 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($this->getNodeKey($holderExpr->var) !== $exprKey) { continue; } - if (!$holder->getCertainty()->yes()) { + if ($holder->getCertainty()->no()) { continue; } $dimType = $scope->getType($holderExpr->dim)->toArrayKey();

Check warning on line 3096 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($this->getNodeKey($holderExpr->var) !== $exprKey) { continue; } - if (!$holder->getCertainty()->yes()) { + if ($holder->getCertainty()->no()) { continue; } $dimType = $scope->getType($holderExpr->dim)->toArrayKey();
continue;
}
$dimType = $scope->getType($holderExpr->dim)->toArrayKey();
$constantStrings = $dimType->getConstantStrings();
$offsetKey = null;
if (count($constantStrings) === 1) {
$offsetKey = $constantStrings[0];
} elseif ($dimType instanceof ConstantIntegerType) {
$offsetKey = $dimType;
}
if ($offsetKey === null) {
continue;
}

$currentType = $scope->getType($expr);
$enrichedType = TypeCombinator::intersect(
$currentType,
new HasOffsetValueType($offsetKey, $holder->getType()),
);
$scope = $scope->specifyExpressionType(
$expr,
$enrichedType,
$scope->getNativeType($expr),
TrinaryLogic::createYes(),
);
}
}

return $scope;
}

public function removeTypeFromExpression(Expr $expr, Type $typeToRemove): self
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4560.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug4560;

use function PHPStan\Testing\assertType;

final class ResetPasswordForm
{
/**
* @param array{token: string, password: string, email: string} $data
*/
public static function fromArray(array $data): self
{
return new self();
}
}

class HelloWorld
{
public function sayHello(): void
{
if (!empty($_POST['resetPassword'])) {
$data = $_POST['resetPassword'];
$data['token'] = $_POST['token'];

assert(array_key_exists('password', $data));
assert(array_key_exists('email', $data));

assertType("non-empty-array&hasOffset('email')&hasOffset('password')&hasOffsetValue('token', mixed)", $data);
}
}
}
Loading