Snyk PHP CLI Plugin

1.12.1 · active · verified Wed Apr 22

The `snyk-php-plugin` is a specialized plugin designed to extend the Snyk Command Line Interface (CLI) functionality for PHP Composer projects. Its primary role is to provide dependency metadata from `composer.json` and `composer.lock` files, enabling Snyk to identify, fix, and monitor known security vulnerabilities in PHP applications. The current stable version is 1.12.1, with recent releases indicating an active development cadence, including features like improved Composer command handling, Prettier integration, and enhanced security scanning capabilities. As a plugin, it is not intended for standalone programmatic use but integrates directly with the Snyk CLI to provide comprehensive security analysis for PHP dependencies, distinguishing it from general-purpose static analysis tools or package managers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use the `getDepsFromProject` function from the snyk-php-plugin. It simulates a basic PHP Composer project structure and then invokes the plugin's core logic to extract dependency information, printing the detected packages and their versions. While the primary use is via the Snyk CLI, this example illustrates direct interaction with the plugin's internal functions.

import { writeFileSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
import { getDepsFromProject } from 'snyk-php-plugin';

// Create a dummy PHP project directory for demonstration
const projectDir = join(process.cwd(), 'temp-php-project');
mkdirSync(projectDir, { recursive: true });

// Simulate composer.json and composer.lock files
writeFileSync(join(projectDir, 'composer.json'), JSON.stringify({
  "name": "vendor/package",
  "description": "A dummy PHP package",
  "require": {
    "monolog/monolog": "^2.0",
    "php": ">=7.4"
  },
  "require-dev": {
    "phpunit/phpunit": "^9.5"
  }
}, null, 2));

writeFileSync(join(projectDir, 'composer.lock'), `{ "packages": [ { "name": "monolog/monolog", "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", "reference": "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0" }, "require": { "php": ">=7.2" } } ], "packages-dev": [ { "name": "phpunit/phpunit", "version": "9.5.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", "reference": "b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0" }, "require": { "php": ">=7.3" } } ] }`);

async function scanPhpProject() {
  console.log(`Scanning PHP project at: ${projectDir}`);
  try {
    // In a real scenario, Snyk CLI would call this plugin internally.
    // Here, we demonstrate direct programmatic access to getDepsFromProject.
    const result = await getDepsFromProject(projectDir, { args: [], path: [] });
    console.log('Detected PHP dependencies:');
    result.plugin.package.dependencies.forEach(dep => {
      console.log(`- ${dep.name}@${dep.version}`);
    });
  } catch (error) {
    console.error('Error during dependency scan:', error.message);
  }
}

scanPhpProject();

view raw JSON →