Skip to content
Closed
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
69 changes: 69 additions & 0 deletions src/Rules/Functions/UnsetRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Unset_>
*/
class UnsetRule implements \PHPStan\Rules\Rule
{

public function getNodeType(): string
{
return Node\Stmt\Unset_::class;
}

public function processNode(Node $node, Scope $scope): array
{
$functionArguments = $node->vars;
$messages = [];

foreach ($functionArguments as $argument) {
$message = $this->canBeUnset($argument, $scope);

if (!$message) {
continue;
}

$messages[] = $message;
}

return $messages;
}

private function canBeUnset(Node $node, Scope $scope): ?string
{
if ($node instanceof Node\Expr\Variable && is_string($node->name)) {
$scopeHasVariable = $scope->hasVariableType($node->name);

if ($scopeHasVariable->no()) {
return RuleErrorBuilder::message(
sprintf('Call to function unset() contains undefined variable $%s.', $node->name)
)->line($node->getLine())->build()->getMessage();
}
} elseif ($node instanceof Node\Expr\ArrayDimFetch && $node->dim !== null) {
$type = $scope->getType($node->var);
$dimType = $scope->getType($node->dim);

$isOffsetAccessible = !$type->isOffsetAccessible()->no() && $type->getIterableKeyType()->isSuperTypeOf($dimType)->no();

if ($isOffsetAccessible || $type->hasOffsetValueType($dimType)->no()) {
return RuleErrorBuilder::message(
sprintf(
'Cannot unset offset %s on %s.',
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value())
)
)->line($node->getLine())->build()->getMessage();
}
}

return null;
}

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

namespace PHPStan\Rules\Functions;

/**
* @extends \PHPStan\Testing\RuleTestCase<UnsetRule>
*/
class UnsetRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
return new UnsetRule();
}

public function testUnsetRule(): void
{
require_once __DIR__ . '/data/unset.php';
$this->analyse([__DIR__ . '/data/unset.php'], [
[
'Call to function unset() contains undefined variable $notSetVariable.',
6,
],
[
'Cannot unset offset \'a\' on 3.',
10,
],
[
'Cannot unset offset \'b\' on 1.',
14,
],
[
'Cannot unset offset \'c\' on 1.',
18,
],
[
'Cannot unset offset \'b\' on 1.',
18,
],
[
'Cannot unset offset \'string\' on iterable<int, int>.',
31,
],
]);
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Functions/data/unset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

function unsetInaccessible ()
{

unset($notSetVariable);

$scalar = 3;

unset($scalar['a']);

$singleDimArray = ['a' => 1];

unset($singleDimArray['a']['b']);

$multiDimArray = ['a' => ['b' => 1]];

unset($multiDimArray['a']['b']['c'], $scalar, $singleDimArray['a']['b']);

}

/** @param iterable<int> $iterable */
function unsetOnMaybeIterable(iterable $iterable)
{
unset($iterable['string']);
}

/** @param iterable<int, int> $iterable */
function unsetOnYesIterable(iterable $iterable)
{
unset($iterable['string']);
}