Intelephense PHP Language Server (CLI)

1.16.5 · active · verified Sun Apr 19

Intelephense is a high-performance PHP language server that provides robust IDE features such as code completion, parameter hints, diagnostics, formatting, and symbol navigation. It supports PHP 7 and 8, offering comprehensive static analysis and accurate type inference for modern PHP projects. While widely recognized as a VS Code extension, the `intelephense` npm package specifically distributes the command-line interface (CLI) of the language server. This CLI enables integration with various Language Server Protocol (LSP) clients beyond VS Code or facilitates its use in automated workflows like CI/CD pipelines for linting and diagnostics. Currently at version 1.16.5 on npm, the project is actively developed, ensuring regular updates to support new PHP features and maintain compatibility with evolving editor integrations. Its primary differentiators include its speed, detailed static analysis tailored for PHP, and a rich feature set that significantly enhances PHP development experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install Intelephense locally, create a sample PHP file, and then programmatically run diagnostics on it using Node.js's `child_process` module to capture and parse the output.

import { execSync } from 'node:child_process';
import { writeFileSync, existsSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';

const projectRoot = './tmp_php_project';
const phpFilePath = join(projectRoot, 'index.php');

if (!existsSync(projectRoot)) {
  mkdirSync(projectRoot);
}

// Create a sample PHP file for diagnostics
const phpContent = `<?php

class MyClass {
    public function greet(string $name): string {
        return 'Hello, ' . $name;
    }
}

$obj = new MyClass();
// Missing semicolon here intentionally for diagnostics
echo $obj->greet('World')

// Undefined variable to demonstrate diagnostics
$undefinedVar = $nonExistentVar;
`;

writeFileSync(phpFilePath, phpContent);

console.log('Running Intelephense diagnostics on the sample PHP file...');

try {
  // Run diagnostics and capture output
  const output = execSync(`intelephense diagnostics ${projectRoot} --json`, { encoding: 'utf8' });
  const diagnostics = JSON.parse(output);

  console.log('Intelephense Diagnostics Report:');
  if (diagnostics.length > 0) {
    diagnostics.forEach(diag => {
      console.log(`- [${diag.severity}] ${diag.message} at ${diag.uri}:${diag.range.start.line + 1}:${diag.range.start.character + 1}`);
    });
  } else {
    console.log('No diagnostics reported (might indicate no issues or configuration problem).');
  }
} catch (error) {
  console.error('Failed to run Intelephense diagnostics:', error.message);
  if (error.stderr) console.error(error.stderr);
  console.error('Make sure PHP is installed and in your PATH, and intelephense is installed via `npm i -g intelephense` or `npm i intelephense`.');
}

// Clean up (optional)
// require('node:fs').rmSync(projectRoot, { recursive: true, force: true });

view raw JSON →