Skip to content
Permalink
Browse files

Fix #1899 - detect dead code after array assignment

  • Loading branch information...
muglug committed Jul 4, 2019
1 parent 8020167 commit d0f6f85dba20023664f2de0659457e49a76398fb
@@ -496,11 +496,7 @@ public static function updateArrayType(
}
}
} elseif ($root_var_id) {
if ($context->hasVariable($root_var_id, $statements_analyzer)) {
$context->vars_in_scope[$root_var_id] = $root_type;
} else {
$context->vars_in_scope[$root_var_id] = $root_type;
}
$context->vars_in_scope[$root_var_id] = $root_type;
}
return null;
@@ -147,7 +147,7 @@ public static function analyze(
$var_name = '$' . $stmt->name;
if (!$context->hasVariable($var_name, $statements_analyzer)) {
if (!$context->hasVariable($var_name, !$array_assignment ? $statements_analyzer : null)) {
if (!isset($context->vars_possibly_in_scope[$var_name]) ||
!$statements_analyzer->getFirstAppearance($var_name)
) {
@@ -504,7 +504,36 @@ function f(string $type) {
f(A::class);
f(B::class);',
],
],
'compareToExactClassString' => [
'<?php
/**
* @template T as object
*/
class Type
{
/** @var class-string<T> */
private $typeName;
/**
* @param class-string<T> $typeName
*/
public function __construct(string $typeName) {
$this->typeName = $typeName;
}
/**
* @param mixed $value
* @return T
*/
public function cast($value) {
if (is_object($value) && get_class($value) === $this->typeName) {
return $value;
}
throw new RuntimeException();
}
}'
],
];
}
@@ -1630,6 +1630,14 @@ function bar() : void {
}',
'error_message' => 'UnusedVariable',
],
'detectUselessArrayAssignment' => [
'<?php
function foo() : void {
$a = [];
$a[0] = 1;
}',
'error_message' => 'UnusedVariable',
],
];
}
}

0 comments on commit d0f6f85

Please sign in to comment.
You can’t perform that action at this time.