Skip to content

Commit

Permalink
Set php static init offsets to whole file (#4975)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannescoetzee authored Sep 30, 2024
1 parent f5dda21 commit 7c417d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,14 @@ class AstCreator(relativeFileName: String, fileName: String, phpAst: PhpFile, di
val fullName = composeMethodFullName(StaticInitMethodName, isStatic = true)
val ast =
staticInitMethodAst(inits, fullName, Option(signature), TypeConstants.Void, fileName = Some(relativeFileName))

for {
method <- ast.root.collect { case method: NewMethod => method }
content <- fileContent
} {
method.offset(0)
method.offsetEnd(content.length)
}
Option(ast)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.joern.php2cpg.querying

import io.joern.php2cpg.Config
import io.joern.php2cpg.parser.Domain
import io.joern.php2cpg.testfixtures.PhpCode2CpgFixture
import io.joern.x2cpg.Defines
Expand All @@ -10,12 +11,14 @@ import io.shiftleft.semanticcpg.language.*
class MemberTests extends PhpCode2CpgFixture {

"class constants" should {
val cpg = code("""<?php
val source = """<?php
|class Foo {
| const A = 'A', B = 'B';
| public const C = 'C';
|}
|""".stripMargin)
|""".stripMargin

val cpg = code(source, "foo.php").withConfig(Config().withDisableFileContent(false))

"have member nodes representing them" in {
inside(cpg.typeDecl.name("Foo").member.sortBy(_.name).toList) { case List(aMember, bMember, cMember) =>
Expand Down Expand Up @@ -46,6 +49,14 @@ class MemberTests extends PhpCode2CpgFixture {
checkConstAssign(bAssign, "B")
checkConstAssign(cAssign, "C")
}
clinitMethod.isExternal shouldBe false
clinitMethod.offset shouldBe Some(0)
clinitMethod.offsetEnd shouldBe Some(source.length)
cpg.file
.name("foo.php")
.content
.map(_.substring(clinitMethod.offset.get, clinitMethod.offsetEnd.get))
.l shouldBe List(source)
}
}
}
Expand Down

0 comments on commit 7c417d2

Please sign in to comment.