-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathflymake-phpstan.el
121 lines (106 loc) · 4.87 KB
/
flymake-phpstan.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
;;; flymake-phpstan.el --- Flymake backend for PHP using PHPStan -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Friends of Emacs-PHP development
;; Author: USAMI Kenta <[email protected]>
;; Created: 31 Mar 2020
;; Version: 0.8.2
;; Keywords: tools, php
;; Homepage: https://github.com/emacs-php/phpstan.el
;; Package-Requires: ((emacs "26.1") (phpstan "0.8.2"))
;; License: GPL-3.0-or-later
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Flymake backend for PHP using PHPStan (PHP Static Analysis Tool).
;;
;; Put the following into your .emacs file (~/.emacs.d/init.el)
;;
;; (add-hook 'php-mode-hook #'flymake-phpstan-turn-on)
;;
;; For Lisp maintainers: see [GNU Flymake manual - 2.2.2 An annotated example backend]
;; https://www.gnu.org/software/emacs/manual/html_node/flymake/An-annotated-example-backend.html
;;; Code:
(require 'cl-lib)
(require 'php-project)
(require 'flymake)
(require 'phpstan)
(eval-when-compile
(require 'pcase))
(defgroup flymake-phpstan nil
"Flymake backend for PHP using PHPStan."
:group 'flymake
:group 'phpstan)
(defcustom flymake-phpstan-disable-c-mode-hooks t
"When T, disable `flymake-diagnostic-functions' for `c-mode'."
:type 'boolean
:group 'flymake-phpstan)
(defvar-local flymake-phpstan--proc nil)
(defun flymake-phpstan-make-process (root command-args report-fn source)
"Make PHPStan process by ROOT, COMMAND-ARGS, REPORT-FN and SOURCE."
(let ((default-directory root))
(make-process
:name "flymake-phpstan" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *Flymake-PHPStan*")
:command command-args
:sentinel
(lambda (proc _event)
(pcase (process-status proc)
(`exit
(unwind-protect
(when (with-current-buffer source (eq proc flymake-phpstan--proc))
(with-current-buffer (process-buffer proc)
(goto-char (point-min))
(cl-loop
while (search-forward-regexp
(eval-when-compile
(rx line-start (1+ (not (any ":"))) ":"
(group-n 1 (one-or-more (not (any ":")))) ":"
(group-n 2 (one-or-more not-newline)) line-end))
nil t)
for msg = (match-string 2)
for (beg . end) = (flymake-diag-region
source
(string-to-number (match-string 1)))
for type = :warning
collect (flymake-make-diagnostic source beg end type msg)
into diags
finally (funcall report-fn diags)))
(flymake-log :warning "Canceling obsolete check %s" proc))
(kill-buffer (process-buffer proc))))
(code (user-error "PHPStan error (exit status: %s)" code)))))))
(defun flymake-phpstan (report-fn &rest _ignored-args)
"Flymake backend for PHPStan report using REPORT-FN."
(let ((command-args (phpstan-get-command-args :include-executable t)))
(unless (car command-args)
(user-error "Cannot find a phpstan executable command"))
(when (process-live-p flymake-phpstan--proc)
(kill-process flymake-phpstan--proc))
(let ((source (current-buffer))
(target-path (if (or (buffer-modified-p) (not buffer-file-name))
(phpstan-normalize-path
(flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace))
buffer-file-name)))
(save-restriction
(widen)
(setq flymake-phpstan--proc (flymake-phpstan-make-process (php-project-get-root-dir) (append command-args (list "--" target-path)) report-fn source))
(process-send-region flymake-phpstan--proc (point-min) (point-max))
(process-send-eof flymake-phpstan--proc)))))
;;;###autoload
(defun flymake-phpstan-turn-on ()
"Enable `flymake-phpstan' as buffer-local Flymake backend."
(interactive)
(let ((enabled (phpstan-enabled)))
(when enabled
(flymake-mode 1)
(when flymake-phpstan-disable-c-mode-hooks
(remove-hook 'flymake-diagnostic-functions #'flymake-cc t))
(add-hook 'flymake-diagnostic-functions #'flymake-phpstan nil t))))
(provide 'flymake-phpstan)
;;; flymake-phpstan.el ends here