emacs-php / phpstan.el

Interface to PHPStan (PHP static analyzer)
GNU General Public License v3.0
25 stars 16 forks source link
emacs flycheck php static-analysis

(add-hook 'php-mode-hook 'my-php-mode-setup)

+END_SRC

*** For Flymake user

+BEGIN_SRC emacs-lisp

(add-hook 'php-mode-hook #'flymake-phpstan-turn-on)

+END_SRC

*** Using Docker (phpstan/docker-image) Install [[https://www.docker.com/get-started][Docker]] and [[https://github.com/phpstan/phpstan/pkgs/container/phpstan][phpstan/phpstan image]].

If you always use Docker for PHPStan, add the following into your ~.emacs~ file (~~/.emacs.d/init.el~)

+BEGIN_SRC emacs-lisp

(setq-default phpstan-executable 'docker)

+END_SRC

Put the following into ~.dir-locals.el~ files on the root directory of project.

+BEGIN_SRC emacs-lisp

((nil . ((php-project-root . git) (phpstan-executable . docker) (phpstan-working-dir . (root . "path/to/dir")) (phpstan-config-file . (root . "path/to/dir/phpstan-docker.neon")) (phpstan-memory-limit . "1G") (phpstan-level . 7))))

+END_SRC

*** Using composer (project specific) If your project Composer relies on phpstan, you do not need to set anything.

+BEGIN_SRC emacs-lisp

((nil . ((php-project-root . git) (phpstan-executable . docker) (phpstan-working-dir . (root . "path/to/dir")) (phpstan-config-file . (root . "path/to/dir/phpstan-docker.neon")) (phpstan-level . 7))))

+END_SRC

*** Using Composer Please install [[https://packagist.org/packages/phpstan/phpstan][phpstan/phpstan]] package for each user environment or project by using [[https://getcomposer.org/download/][Composer]].

If you are unfamiliar with resolving dependencies, the following shell commands are recommended.

+BEGIN_SRC shell

$ composer global require phpstan/phpstan

+END_SRC

NOTICE: [[https://packagist.org/packages/phpstan/phpstan-shim][phpstan/phpstan-shim]] is deprecated. Please read [[https://medium.com/@ondrejmirtes/phpstan-0-12-released-f1a88036535d][PHPStan 0.12 Released!]].

* Using PHAR archive NOTICE: ~phpstan.el~ requires PHPStan 0.10+*. We strongly* recommend using the latest PHPStan.

Please download ~phpstan.phar~ from [[https://github.com/phpstan/phpstan/releases][Releases · phpstan/phpstan]]. ** Settings Variables for phpstan are mainly controlled by [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html][directory variables]] (~.dir-locals.el~).

Frequently ~(root. "path/to/file")~ notation appears in these variables. It is relative to the top level directory of the project. In general, the directory containing one of ~.projectile~, ~composer.json~, ~.git~ file (or directory) is at the top level.

Please be aware that the root directory of the PHP project may NOT match either of PHPStan's ~%rootDir%~ and/or ~%currentWorkingDirectory%~.

Typically, you would set the following ~.dir-locals.el~.

+BEGIN_SRC emacs-lisp

((nil . ((php-project-root . auto) (phpstan-executable . docker) (phpstan-working-dir . (root . "path/to/dir/")) (phpstan-config-file . (root . "path/to/dir/phpstan-custom.neon")) (phpstan-level . max))))

+END_SRC

If there is a ~phpstan.neon~ file in the root directory of the project, you do not need to set both ~phpstan-working-dir~ and ~phpstan-config-file~. ** Commands This package provides convenient commands for using PHPStan from Emacs. *** Command ~phpstan-insert-dumptype~ Add ~\PHPStan\dumpType(...);~ to your PHP code and analyze it to make PHPStan display the type of the expression.

+BEGIN_SRC

(define-key php-mode-map (kbd "C-c ^") #'phpstan-insert-dumptype)

+END_SRC

By default, if you press ~C-u~ before invoking the command, ~\PHPStan\dumpPhpDocType()~ will be inserted.

This feature was added in PHPStan 1.12.7 and will dump types compatible with the ~@param~ and ~@return~ PHPDoc tags.

** API Most variables defined in this package are buffer local. If you want to set it for multiple projects, use [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Default-Value.html][setq-default]].

*** Local variable ~phpstan-working-dir~ Path to working directory of PHPStan.

*** Local variable ~phpstan-config-file~ Path to project specific configuration file of PHPStan.

*** Local variable ~phpstan-level~ Rule level of PHPStan analysis. Please see [[https://github.com/phpstan/phpstan/blob/master/README.md#rule-levels][README #Rule levels of PHPStan]]. ~0~ is the loosest and you can also use ~max~ as an alias for the highest level. Default level is ~0~.

*** Local variable ~phpstan-executable~

*** Custom variable ~phpstan-flycheck-auto-set-executable~ Set flycheck phpstan-executable automatically when non-NIL.

*** Custom variable ~phpstan-memory-limit~ Use phpstan memory limit option when non-NIL.

*** Custom variable ~phpstan-docker-image~ Docker image URL or Docker Hub image name or NIL. Default as ~"ghcr.io/phpstan/phpstan"~. See [[https://phpstan.org/user-guide/docker][Docker - PHPStan Documentation]] and [[https://github.com/orgs/phpstan/packages/container/package/phpstan][GitHub Container Registory - Package phpstan]].