|
| 1 | +;;; phpstan-flycheck.el --- Flycheck integration for PHPStan -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2018 Friends of Emacs-PHP development |
| 4 | + |
| 5 | +;; Author: USAMI Kenta <tadsan@zonu.me> |
| 6 | +;; Created: 15 Mar 2018 |
| 7 | +;; Version: 0.1.0 |
| 8 | +;; Keywords: convenience, php |
| 9 | +;; Homepage: https://github.com/emacs-php/phpstan.el |
| 10 | +;; Package-Requires: ((emacs "24") (flycheck "26")) |
| 11 | + |
| 12 | +;; This program is free software; you can redistribute it and/or modify |
| 13 | +;; it under the terms of the GNU General Public License as published by |
| 14 | +;; the Free Software Foundation, either version 3 of the License, or |
| 15 | +;; (at your option) any later version. |
| 16 | + |
| 17 | +;; This program is distributed in the hope that it will be useful, |
| 18 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +;; GNU General Public License for more details. |
| 21 | + |
| 22 | +;; You should have received a copy of the GNU General Public License |
| 23 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 24 | + |
| 25 | +;;; Commentary: |
| 26 | + |
| 27 | +;; Flycheck integration for PHPStan. |
| 28 | + |
| 29 | +;;; Code: |
| 30 | +(require 'flycheck) |
| 31 | +(require 'phpstan) |
| 32 | + |
| 33 | +;; Usually it is defined dynamically by flycheck |
| 34 | +(defvar flycheck-phpstan-executable) |
| 35 | + |
| 36 | +(defun phpstan-flycheck--enabled-and-set-variable () |
| 37 | + "Return path to phpstan configure file, and set buffer execute in side effect." |
| 38 | + (let ((enabled (not (null (or phpstan-working-dir (phpstan-get-config-file)))))) |
| 39 | + (prog1 enabled |
| 40 | + (when (and phpstan-flycheck-auto-set-executable |
| 41 | + (not (and (boundp 'flycheck-phpstan-executable) |
| 42 | + (symbol-value 'flycheck-phpstan-executable))) |
| 43 | + (or (eq 'docker phpstan-executable) |
| 44 | + (and (consp phpstan-executable) |
| 45 | + (stringp (car phpstan-executable)) |
| 46 | + (listp (cdr phpstan-executable))))) |
| 47 | + (set (make-local-variable 'flycheck-phpstan-executable) |
| 48 | + (if (eq 'docker phpstan-executable) |
| 49 | + phpstan-docker-executable |
| 50 | + (car phpstan-executable))))))) |
| 51 | + |
| 52 | +;;;###autoload |
| 53 | +(flycheck-define-checker phpstan |
| 54 | + "PHP static analyzer based on PHPStan." |
| 55 | + :command ("php" (eval (phpstan-get-command-args)) |
| 56 | + (eval (phpstan-normalize-path |
| 57 | + (flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace) |
| 58 | + (flycheck-save-buffer-to-temp #'flycheck-temp-file-system)))) |
| 59 | + :working-directory (lambda (_) (phpstan-get-working-dir)) |
| 60 | + :enabled (lambda () (phpstan-flycheck--enabled-and-set-variable)) |
| 61 | + :error-patterns |
| 62 | + ((error line-start (1+ (not (any ":"))) ":" line ":" (message) line-end)) |
| 63 | + :modes (php-mode) |
| 64 | + :next-checkers (php)) |
| 65 | + |
| 66 | +(provide 'phpstan-flycheck) |
| 67 | +;;; phpstan-flycheck.el ends here |
0 commit comments