Format PHP with PHP-CS-Fixer in Zed Editor

Format PHP with PHP-CS-Fixer in Zed Editor

The new Code Editor Zed is balzing fast and has language support for PHP, so why not try it out?

Roman Zipp, October 2nd, 2024

I recently tried out the new Zed code editor, which also has first party support for PHP Language Servers phpactor or intelliphense!

One issue I ran into is auto formatting code. I'm a fan of auto formatting since this preserves a cohesive code style over many projects and helps to reduce merge issues.

External formatters in Zed

Formatters in Zed work a bit different than some implementations in VSCode or similar editors.

You can configure an external formatter by specifying an external key with a command and arguments values.

Once you run the editor: format action or save the file (with format_on_save enabled), you can provide the formatter with the current file in two ways

  1. Set a {buffer_path} placeholder variable

  2. Append a - to pipe in the file contents to the command

We will use the second way since you could also call commands inside docker containers. When mounting your project root inside a container, the absolute path placed inside {buffer_path} will mismatch and you will need to do some magic to convert it into a relative path.

Format using PHP-CS-Fixer

I've created a bin script in my PHP-CS-Fixer Config project that takes in the STDIN, calls PHP-CS-Fixer to format the file contents and returns the result as STDOUT.

{
  "languages": {
    "PHP": {
      "language_servers": ["intelephense", "!phpactor"],
      "format_on_save": "on",
      "formatter": {
        "external": {
          "command": "vendor/bin/php-cs-fixer-stdin",
          "arguments": ["-"]
        }
      }
    }
  }
}

The bin script is pretty opinionated and only uses the fix command of PHPCS, feel free to fork it, contribute or do whatever!

Install

You could just install the PHP-CS-Fixer-Config dependency or copy the script below. Make sure to alter the command path if you wish to use the manual way.

composer require romanzipp/php-cs-fixer-config --dev

php-cs-fixer-stdin.php

// Inspired from https://gist.github.com/vuon9/be16429f751e12f72e220c18777d9bc7
//
// This script will
//   1. Read the file contents provided by STDIN
//   2. Create a temporary file (tries multiple directories)
//   3. Call PHP-CS-Fixer's "fix" command with path to the temp file
//   4. provides the fixed file contents as STDOUT

function error_exit(string $message, int $code = 1): void
{
    fwrite(STDERR, $message . PHP_EOL);
    exit($code);
}

// Read file contents from STDIN

$fileContents = file_get_contents('php://stdin');
$fileContents = trim($fileContents);

// Create temp file and save STDIN contents

$tryTmpDirs = [
    sys_get_temp_dir(),
    '.tmp',
];

$createdTempFile = false;

foreach ($tryTmpDirs as $tmpDir) {
    $ok = is_dir($tmpDir) || mkdir($tmpDir, 0777, true);
    if (false === $ok) {
        continue;
    }

    $tmpFile = tempnam($tmpDir, 'fix_');
    if (false === $tmpFile) {
        continue;
    }

    $ok = file_put_contents($tmpFile, $fileContents);
    if (false === $ok) {
        continue;
    }

    $createdTempFile = true;
}

if ( ! $createdTempFile) {
    error_exit('could not save STDIN to temp file');
}

// Check if PHP-CS-Fixer is installed

$whichBinary = exec('which php-cs-fixer');
if ('' === $whichBinary) {
    error_exit('php-cs-fixer binary not found in $PATH folders');
}

$cmd = sprintf('php-cs-fixer --quiet fix %s', $tmpFile);

// Run the command

$output = [];
$returnCode = 0;

exec($cmd, $output, $returnCode);

if ($returnCode > 0) {
    error_exit(implode(' ', $output), $returnCode);
}

// Return new contents to STDOUT

$newContents = file_get_contents($tmpFile);

if (false === $newContents) {
    error_exit('couldnt read from temp file after fixing');
}

if ( ! empty($newContents)) {
    fwrite(STDOUT, trim($newContents) . PHP_EOL);
}

if (false === @unlink($tmpFile)) {
    error_exit('couldnt delete temp file');
}