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
17 changes: 11 additions & 6 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4310,24 +4310,29 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
$scope = $this;
if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) {
$dimType = $scope->getType($expr->dim)->toArrayKey();
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
if ($dimType->isInteger()->yes() || $dimType->isString()->yes()) {
$exprVarType = $scope->getType($expr->var);
if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) {
$types = [
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(ArrayAccess::class),
new NullType(),
];
if ($dimType instanceof ConstantIntegerType) {
if ($dimType->isInteger()->yes()) {
$types[] = new StringType();
}
$offsetValueType = TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types));

if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
$offsetValueType = TypeCombinator::intersect(
$offsetValueType,
new HasOffsetValueType($dimType, $type),
);
}

$scope = $scope->specifyExpressionType(
$expr->var,
TypeCombinator::intersect(
TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)),
new HasOffsetValueType($dimType, $type),
),
$offsetValueType,
$scope->getNativeType($expr->var),
$certainty,
);
Expand Down
16 changes: 6 additions & 10 deletions src/Rules/UnusedFunctionParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use function array_combine;
use function array_map;
use function array_merge;
use function in_array;
use function is_array;
Expand Down Expand Up @@ -43,20 +41,18 @@ public function getUnusedParameters(
string $identifier,
): array
{
$parameterNames = array_map(static function (Variable $variable): string {
$unusedParameters = [];
foreach ($parameterVars as $variable) {
if (!is_string($variable->name)) {
throw new ShouldNotHappenException();
}
return $variable->name;
}, $parameterVars);
$unusedParameters = array_combine($parameterNames, $parameterVars);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rewrite/simplifed this code, as it relied on array_combine which can return false on PHP7.4, which got caught by the MutatingScope improvement.

Argument of an invalid type array<string, PhpParser\Node\Expr\Variable>|false supplied for foreach, only iterables are supported.

rewrote it to get rid of the PHP 7.4 specific behaviour so I don't need to add 7.4 only ignores or similar hacks.

foreach ($this->getUsedVariables($scope, $statements) as $variableName) {
if (!isset($unusedParameters[$variableName])) {
continue;
}

$unusedParameters[$variable->name] = $variable;
}
foreach ($this->getUsedVariables($scope, $statements) as $variableName) {
unset($unusedParameters[$variableName]);
}

$errors = [];
foreach ($unusedParameters as $name => $variable) {
$errorBuilder = RuleErrorBuilder::message(sprintf($unusedParameterMessage, $name))->identifier($identifier);
Expand Down
Loading