Skip to content

Commit 864e8fa

Browse files
committed
Make flycheck-phpstan use --error-format=json instead of "raw"
1 parent e245f87 commit 864e8fa

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

flycheck-phpstan.el

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
;; Usually it is defined dynamically by flycheck
4545
(defvar flycheck-phpstan-executable)
46+
(defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*")
4647

4748
(defun flycheck-phpstan--enabled-and-set-variable ()
4849
"Return path to phpstan configure file, and set buffer execute in side effect."
@@ -60,17 +61,48 @@
6061
(null phpstan-executable)))
6162
(setq-local flycheck-phpstan-executable (car (phpstan-get-executable-and-args)))))))
6263

64+
(defun flycheck-phpstan-parse-output (output &optional _checker _buffer)
65+
"Parse PHPStan errors from OUTPUT."
66+
(with-current-buffer (flycheck-phpstan--temp-buffer)
67+
(erase-buffer)
68+
(insert output))
69+
(flycheck-phpstan-parse-json (flycheck-phpstan--temp-buffer)))
70+
71+
(defun flycheck-phpstan--temp-buffer ()
72+
"Return a temporary buffer for decode JSON."
73+
(get-buffer-create flycheck-phpstan--temp-buffer-name))
74+
75+
(defun flycheck-phpstan-parse-json (json-buffer)
76+
"Parse PHPStan errors from JSON-BUFFER."
77+
(let ((data (phpstan--parse-json json-buffer)))
78+
(cl-loop for (file . entry) in (flycheck-phpstan--plist-to-alist (plist-get data :files))
79+
append (cl-loop for messages in (plist-get entry :messages)
80+
for text = (let ((msg (plist-get messages :message))
81+
(tip (plist-get messages :tip)))
82+
(if tip
83+
(concat msg "\n" phpstan-tip-message-prefix tip)
84+
msg))
85+
collect (flycheck-error-new-at (plist-get messages :line)
86+
nil 'error text
87+
:filename file)))))
88+
89+
(defun flycheck-phpstan--plist-to-alist (plist)
90+
"Convert PLIST to association list."
91+
(let (alist)
92+
(while plist
93+
(push (cons (substring-no-properties (symbol-name (pop plist)) 1) (pop plist)) alist))
94+
(nreverse alist)))
95+
6396
(flycheck-define-checker phpstan
6497
"PHP static analyzer based on PHPStan."
65-
:command ("php" (eval (phpstan-get-command-args))
98+
:command ("php" (eval (phpstan-get-command-args :format "json"))
6699
(eval (if (or (buffer-modified-p) (not buffer-file-name))
67100
(phpstan-normalize-path
68101
(flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace))
69102
buffer-file-name)))
70103
:working-directory (lambda (_) (phpstan-get-working-dir))
71104
:enabled (lambda () (flycheck-phpstan--enabled-and-set-variable))
72-
:error-patterns
73-
((error line-start (1+ (not (any ":"))) ":" line ":" (message) line-end))
105+
:error-parser flycheck-phpstan-parse-output
74106
:modes (php-mode phps-mode))
75107

76108
(add-to-list 'flycheck-checkers 'phpstan t)

0 commit comments

Comments
 (0)