Skip to content

Commit

Permalink
[php] allow custom file extensions in frontend
Browse files Browse the repository at this point in the history
 * add option in frontend to associate custom file extensions to PHP
 * add tests
  • Loading branch information
tuxology committed Jul 5, 2024
1 parent 7b6210e commit b561d58
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scopt.OParser
final case class Config(
phpIni: Option[String] = None,
phpParserBin: Option[String] = None,
phpExtensions: Set[String] = Set(),
downloadDependencies: Boolean = false
) extends X2CpgConfig[Config]
with TypeRecoveryParserConfig[Config]
Expand All @@ -24,6 +25,10 @@ final case class Config(
copy(phpParserBin = Some(phpParserBin)).withInheritedFields(this)
}

def withExtensions(phpExtensions: Set[String]): Config = {
copy(phpExtensions = phpExtensions).withInheritedFields(this)
}

override def withDownloadDependencies(downloadDependencies: Boolean): Config = {
copy(downloadDependencies = downloadDependencies).withInheritedFields(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AstCreationPass(config: Config, cpg: Cpg, parser: PhpParser)(implicit with

private val logger = LoggerFactory.getLogger(this.getClass)

val PhpSourceFileExtensions: Set[String] = Set(".php")
var PhpSourceFileExtensions: Set[String] = if config.phpExtensions.nonEmpty then config.phpExtensions else Set(".php")

override def generateParts(): Array[String] = SourceFiles
.determine(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.joern.php2cpg.passes

import io.joern.php2cpg.Config
import io.joern.php2cpg.parser.Domain.PhpOperators
import io.joern.php2cpg.testfixtures.PhpCode2CpgFixture
import io.shiftleft.semanticcpg.language.*

class PhpFileExtensionsTest extends PhpCode2CpgFixture() {

"two files with two new extensions associated with PHP" should {

val cpg = code(
"""
|require 'vendor/autoload.php';
|
|$s3 = new S3Client([
| 'version' => 'latest',
| 'region' => 'us-east-1',
| 'credentials' => [
| 'key' => 'YOUR_AWS_ACCESS_KEY_ID',
| 'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
| ]
|]);
|""".stripMargin,
"sample.php"
)
.moreCode(
"""<?php
|require 'vendor/autoload.php';
|
|$s3 = new S3Client([
| 'version' => 'latest',
| 'region' => 'us-east-1',
| 'credentials' => [
| 'key' => 'YOUR_AWS_ACCESS_KEY_ID',
| 'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
| ]
|]);
|
|""".stripMargin,
"something.cls"
)
.withConfig(Config().withExtensions(Set(".php", ".cls")))

"be scanned and present in cpg" in {
cpg.file.name.l shouldBe List("sample.php", "something.cls", "<unknown>")
cpg.file.name.l.size shouldBe 3
}

}

"two files with only default (.php) extension associated with PHP" should {

val cpg = code(
"""
|require 'vendor/autoload.php';
|
|$s3 = new S3Client([
| 'version' => 'latest',
| 'region' => 'us-east-1',
| 'credentials' => [
| 'key' => 'YOUR_AWS_ACCESS_KEY_ID',
| 'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
| ]
|]);
|""".stripMargin,
"sample.php"
)
.moreCode(
"""<?php
|require 'vendor/autoload.php';
|
|$s3 = new S3Client([
| 'version' => 'latest',
| 'region' => 'us-east-1',
| 'credentials' => [
| 'key' => 'YOUR_AWS_ACCESS_KEY_ID',
| 'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
| ]
|]);
|
|""".stripMargin,
"something.cls"
)

"be scanned and one default extension present in cpg" in {
cpg.file.name.l shouldBe List("sample.php", "<unknown>")
cpg.file.name.l.size shouldBe 2
}
}

}

0 comments on commit b561d58

Please sign in to comment.