Skip to content

Commit

Permalink
AtomicStaticCallAnalyzer: clear tmp var from context (fix #7556)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Feb 2, 2022
1 parent ff2636e commit 3c3e692
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,15 @@ private static function handleNamedCall(
$class_storage->final
);

$context->vars_in_scope['$tmp_mixin_var'] = $new_lhs_type;
$mixin_context = clone $context;
$mixin_context->vars_in_scope['$__tmp_mixin_var__'] = $new_lhs_type;

return self::forwardCallToInstanceMethod(
$statements_analyzer,
$stmt,
$stmt_name,
$context,
'tmp_mixin_var',
$mixin_context,
'__tmp_mixin_var__',
true
);
}
Expand Down Expand Up @@ -694,18 +695,21 @@ function (PhpParser\Node\Arg $arg): PhpParser\Node\Expr\ArrayItem {
// with nonexistent method, we try to forward to instance method call for resolve pseudo method.

// Use parent type as static type for the method call
$context->vars_in_scope['$tmp_parent_var'] = new Union([$lhs_type_part]);
$tmp_context = clone $context;
$tmp_context->vars_in_scope['$__tmp_parent_var__'] = new Union([$lhs_type_part]);

if (self::forwardCallToInstanceMethod(
$statements_analyzer,
$stmt,
$stmt_name,
$context,
'tmp_parent_var'
$tmp_context,
'__tmp_parent_var__'
) === false) {
return false;
}

unset($tmp_context);

// Resolve actual static return type according to caller (i.e. $this) static type
if (isset($context->vars_in_scope['$this'])
&& $method_call_type = $statements_analyzer->node_data->getType($stmt)
Expand Down
23 changes: 23 additions & 0 deletions tests/MagicMethodAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,29 @@ class B extends A {}
class C {}',
'error_message' => 'InvalidDocblock',
],
'magicParentCallShouldNotPolluteContext' => [
'<?php
/**
* @method baz(): Foo
*/
class Foo
{
public function __call()
{
return new self();
}
}
class Bar extends Foo
{
public function baz(): Foo
{
parent::baz();
return $__tmp_parent_var__;
}
}',
'error_message' => 'UndefinedVariable',
]
];
}

Expand Down
24 changes: 24 additions & 0 deletions tests/MixinAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,30 @@ function test() : FooGrandChild {
}',
'error_message' => 'LessSpecificReturnStatement'
],
'mixinStaticCallShouldNotPolluteContext' => [
'<?php
/**
* @template T
*/
class Foo
{
public function foobar(): void {}
}
/**
* @template T
* @mixin Foo<T>
*/
class Bar
{
public function baz(): self
{
self::foobar();
return $__tmp_mixin_var__;
}
}',
'error_message' => 'UndefinedVariable'
],
];
}
}

0 comments on commit 3c3e692

Please sign in to comment.