Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive PhanUndeclaredVariable for too many params #3246

Merged
merged 1 commit into from
Sep 9, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features(Analysis):
+ Disable `simplify_ast` by default.
Phan's analysis of compound conditions and assignments/negations in conditions has improved enough that it should no longer be necessary.
+ Import more specific phpdoc/real array return types for internal global functions from opcache.
+ Emit `PhanUndeclaredVariable` and other warnings about arguments when there are too many parameters for methods. (#3245)

Maintenance:
+ Make `\Phan\Library\None` a singleton in internal uses.
Expand Down
7 changes: 7 additions & 0 deletions src/Phan/AST/ArrowFunc.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ private function buildUses(Node $n) : void
}
}
return;
case ast\AST_CLASS:
foreach ($n->children['args']->children ?? [] as $child_node) {
if ($child_node instanceof Node) {
$this->buildUses($child_node);
}
}
return;
}
foreach ($n->children as $child_node) {
if ($child_node instanceof Node) {
Expand Down
29 changes: 28 additions & 1 deletion src/Phan/AST/UnionTypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,34 @@ public function visitConst(Node $node) : UnionType
return UnionType::empty();
}

/**
* @throws UnanalyzableException
*/
public function visitClass(Node $node) : UnionType
{
if ($node->flags & ast\flags\CLASS_ANONYMOUS) {
$class_name =
(new ContextNode(
$this->code_base,
$this->context,
$node
))->getUnqualifiedNameForAnonymousClass();
} else {
$class_name = (string)$node->children['name'];
}

if (!$class_name) {
// Should only occur with --use-fallback-parser
throw new UnanalyzableException($node, "Class name cannot be empty");
}

// @phan-suppress-next-line PhanThrowTypeMismatchForCall
return FullyQualifiedClassName::fromStringInContext(
$class_name,
$this->context
)->asType()->asRealUnionType();
}

/**
* Visit a node with kind `\ast\AST_CLASS_CONST`
*
Expand Down Expand Up @@ -2706,7 +2734,6 @@ public static function unionTypeFromClassNode(
Context $context,
$node
) : UnionType {

// If this is a list, build a union type by
// recursively visiting the child nodes
if ($node instanceof Node
Expand Down
6 changes: 6 additions & 0 deletions src/Phan/Analysis/ArgumentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,12 @@ private static function analyzeParameterList(

// This issue should be caught elsewhere
if (!$parameter) {
$argument_type = UnionTypeVisitor::unionTypeFromNode(
$code_base,
$context,
$argument,
true
);
continue;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/php74_files/expected/020_class.php.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%s:2 PhanPluginUseReturnValueNoopVoid The function/method \accepts_one() is declared to return void and it has no side effects
%s:2 PhanUnusedGlobalFunctionParameter Parameter $x is never used
%s:7 PhanParamTooMany Call with 2 arg(s) to \accepts_one($x) which only takes 1 arg(s) defined at %s:2
%s:7 PhanUndeclaredVariable Variable $a is undeclared
%s:8 PhanParamTooManyInternal Call with 2 arg(s) to \strlen(string $string) which only takes 1 arg(s)
%s:8 PhanUndeclaredVariable Variable $other is undeclared
%s:10 PhanParamTooMany Call with 1 arg(s) to \anonymous_class_%s::__construct() which only takes 0 arg(s) defined at %s:10
%s:10 PhanUndeclaredVariable Variable $invalid is undeclared
%s:10 PhanUnusedVariable Unused definition of variable $x
%s:13 PhanUndeclaredVariable Variable $z is undeclared
%s:14 PhanParamTooMany Call with 2 arg(s) to \anonymous_class_%s::__construct($first) which only takes 1 arg(s) defined at %s:14
%s:14 PhanUndeclaredVariable Variable $a is undeclared
%s:14 PhanUndeclaredVariable Variable $b is undeclared
%s:14 PhanUnusedPublicNoOverrideMethodParameter Parameter $first is never used
%s:15 PhanParamTooMany Call with 2 arg(s) to \Test::no_args() which only takes 0 arg(s) defined at %s:4
%s:16 PhanUndeclaredVariable Variable $c is undeclared
%s:17 PhanUndeclaredVariable Variable $d is undeclared
22 changes: 22 additions & 0 deletions tests/php74_files/src/020_class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
function accepts_one($x) {}
class Test {
public static function no_args() {}
}
call_user_func(function () {
accepts_one(1, $a);
echo strlen('x', $other);
// Should warn about missing $invalid
$x = new class ($invalid) {};
// Should warn about missing $a and $b (and PhanParamTooMany), but not $x
$fn = fn() => [
$z,
new class ($a, $b) { public function __construct($first) {} public static function test(int $x) { return $x;}},
Test::no_args(
$c,
$d
),

];
return $fn;
});