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

[php2cpg] Resolve static method call name when called by self #3736

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -967,10 +967,13 @@ class AstCreator(filename: String, phpAst: PhpFile)(implicit withSchemaValidatio

val fullName = call.target match {
// Static method call with a known class name
case Some(nameExpr: PhpNameExpr) if call.isStatic =>
s"${nameExpr.name}${StaticMethodDelimiter}$name"
case Some(nameExpr: PhpNameExpr) if call.isStatic => {
if (nameExpr.name == "self") composeMethodFullName(name, call.isStatic)
else s"${nameExpr.name}${StaticMethodDelimiter}$name"
}

case Some(expr) =>
// composeMethodFullName(name, call.isStatic)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want to leave in commented-out code, then please supply a note

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, and thanks for the catch - this comment has been removed.

s"$UnresolvedNamespace\\$codePrefix"

case None if PhpBuiltins.FuncNames.contains(name) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ class CallTests extends PhpCode2CpgFixture {
}
}

/* This possibly should exist in NamespaceTests.scala */
"static method calls that refer to self" should {
val cpg = code("""
|<?php
|class ClassA {
| function foo($x) {
| return self::bar($x);
| }
| static function bar($param) {
| return 0;
| }
|}
|""".stripMargin)

"resolve the correct method full name" in {
val List(barCall) = cpg.call("bar").take(1).l
barCall.name shouldBe "bar"
barCall.methodFullName shouldBe s"ClassA::bar"
barCall.receiver.isEmpty shouldBe true
barCall.dispatchType shouldBe DispatchTypes.STATIC_DISPATCH
barCall.code shouldBe "self::bar($x)"
}
}

"method calls with simple names should be correct" in {
val cpg = code("<?php\n$f->foo($x);")

Expand Down
Loading