Skip to content

Integrate commands into Emacs

USAMI Kenta edited this page Nov 11, 2022 · 1 revision

This page describes how to execute PHP related commands from Emacs.

PATH environment variable

An environment variable called PATH is important to make the command executable available. UNIX systems (including many Linux, BSD, macOS) can add PATH in the user's shell configuration file.

In many cases, the PHP runtime can be installed system-wide, but in order to use globally installed tools by Composer, it is necessary to set the PATH.

The following code is an example to add ~/local/bin in your home directory as the installation directory for executable files.

PATH=$HOME/local/bin:$PATH

Adding this setting is just called "Add (a directory) to path".

For example files .profile, .bash_profile, .zshenv, etc. are known as files that set environment variables, but which file to set depends on the shell you are using and how you are using it. In most cases both .bashrc and .bash_profile will work if you're using Bash, please follow your consistency.

  • If you start Emacs from shell in terminal, all environment variables are automatically taken over.
  • If you start GUI Emacs without a shell, consider exec-path-from-shell.
  • If you're using Doom Emacs you don't even need anything in the GUI as it's integrated with the ability to automatically inherit PATH from the shell.

PHP

PHP Mode does not depends on php commands, but having PHP installed on your system enables many useful features. PHP can be easily installed by many OS package managers (apt-get, yum, brew, pacman, etc.).

If you want to explicitly use the execution other than PHP installed system-wide, add that directory to PATH.

If you want to explicitly specify the PHP executable regardless of $PATH, add the following settings to init.el.

(custom-set-variable '(php-executable "/path/to/php"))

Note that this variable is not referenced by all Emacs functions.

Package manager

Composer

Composer is a dependency manager for PHP. This is the de facto standard installer for installing modern PHP toolsets and frameworks. Composer supports two types of package installation: per project and in a location called "global".

The composer command can be installed from your system's package manager or by following Download Composer method.

Packages installed by composer global are actually located under your home directory, not system-wide as default.

You can check in which directory the composer global installed command is located with the following command:

composer global -q config bin-dir --absolute

Add to PATH typically either in UNIX systems:

PATH=$HOME/.composer/vendor/bin
PATH=$HOME/.config/composer/vendor/bin

Static Analyzer

PHPStan

PHPStan is a static analysis tool for PHP.

# Install PHPStan into your PHP project
cd /path/to/your/project
composer require --dev phpstan/phpstan

# Install PHPStan globally (user-wide)
composer global require phpstan/phpstan

PHPStan works fine with both project and global installations, but best practice is to add it to your project as require --dev.

phpstan.el integrates PHPStan into Emacs. Add flymake-phpstan or flycheck-phpstan package for on-the-fly checking while editing PHP codes. phpstan.el can detect system and global installed PHPStan just fine.

Psalm

Psalm is another PHP static analysis tool. Psalm is a powerful analyzer that is as good as PHPStan.

psalm.el integrates Psalm into Emacs.

REPL

PsySH

PsySH is a PHP interactive shell (also called REPL).

# Install PsySH into your PHP project
cd /path/to/your/project
composer require --dev psy/psysh

# Install PsySH globally (user-wide)
composer global require psy/psysh

psysh.el integrates PsySH into Emacs.

Language Server

Phpactor

Phpactor is a PHP language server with completion, refactoring and introspection functionality. This server features the LSP protocol and a proprietary (legacy) RPC protocol.

phpactor.el contains Phpactor installer/updater and the RPC protocol client. This package alone provides IDE-like functionality, but it will be discontinued in the future, so it is recommended to use it in combination with other LSP Clients.

See also LSP Support.

Testing

PHPUnit

PHPUnit is the most popular xUnit testing framework for PHP. This is commonly installed by composer require --dev in the project.

phpunit.el integrates PHPUnit into Emacs. It's very easy to run PHPUnit inside an Emacs buffer.

Clone this wiki locally