Skip to content

Commit 0ca2298

Browse files
committed
Refactor flycheck-phpstan-parse-json
1 parent ea44313 commit 0ca2298

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

flycheck-phpstan.el

+29-35
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
(defvar flycheck-phpstan-executable)
4646
(defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*")
4747

48-
4948
(defcustom flycheck-phpstan-ignore-metadata-list nil
5049
"Set of metadata items to ignore in PHPStan messages for Flycheck."
5150
:type '(set (const identifier)
@@ -76,45 +75,40 @@
7675

7776
(defun flycheck-phpstan-parse-output (output &optional _checker _buffer)
7877
"Parse PHPStan errors from OUTPUT."
79-
(with-current-buffer (flycheck-phpstan--temp-buffer)
80-
(erase-buffer)
81-
(insert output))
82-
(flycheck-phpstan-parse-json (flycheck-phpstan--temp-buffer)))
78+
(let* ((json-buffer (with-current-buffer (flycheck-phpstan--temp-buffer)
79+
(erase-buffer)
80+
(insert output)
81+
(current-buffer)))
82+
(data (phpstan--parse-json json-buffer))
83+
(errors (phpstan--plist-to-alist (plist-get data :files))))
84+
(flycheck-phpstan--build-errors errors)))
8385

8486
(defun flycheck-phpstan--temp-buffer ()
8587
"Return a temporary buffer for decode JSON."
8688
(get-buffer-create flycheck-phpstan--temp-buffer-name))
8789

88-
(defun flycheck-phpstan-parse-json (json-buffer)
89-
"Parse PHPStan errors from JSON-BUFFER."
90-
(let ((data (phpstan--parse-json json-buffer)))
91-
(cl-loop for (file . entry) in (flycheck-phpstan--plist-to-alist (plist-get data :files))
92-
append (cl-loop for messages in (plist-get entry :messages)
93-
for text = (let* ((msg (plist-get messages :message))
94-
(ignorable (plist-get messages :ignorable))
95-
(identifier (unless (memq 'identifier flycheck-phpstan-ignore-metadata-list)
96-
(plist-get messages :identifier)))
97-
(tip (unless (memq 'tip flycheck-phpstan-ignore-metadata-list)
98-
(plist-get messages :tip)))
99-
(lines (list (when (and identifier ignorable)
100-
(concat phpstan-identifier-prefix identifier))
101-
(when tip
102-
(concat phpstan-tip-message-prefix tip))))
103-
(lines (cl-remove-if #'null lines)))
104-
(if (null lines)
105-
msg
106-
(concat msg flycheck-phpstan-metadata-separator
107-
(mapconcat #'identity lines "\n"))))
108-
collect (flycheck-error-new-at (plist-get messages :line)
109-
nil 'error text
110-
:filename file)))))
111-
112-
(defun flycheck-phpstan--plist-to-alist (plist)
113-
"Convert PLIST to association list."
114-
(let (alist)
115-
(while plist
116-
(push (cons (substring-no-properties (symbol-name (pop plist)) 1) (pop plist)) alist))
117-
(nreverse alist)))
90+
(defun flycheck-phpstan--build-errors (errors)
91+
"Build Flycheck errors from PHPStan ERRORS."
92+
(cl-loop for (file . entry) in errors
93+
append (cl-loop for messages in (plist-get entry :messages)
94+
for text = (let* ((msg (plist-get messages :message))
95+
(ignorable (plist-get messages :ignorable))
96+
(identifier (unless (memq 'identifier flycheck-phpstan-ignore-metadata-list)
97+
(plist-get messages :identifier)))
98+
(tip (unless (memq 'tip flycheck-phpstan-ignore-metadata-list)
99+
(plist-get messages :tip)))
100+
(lines (list (when (and identifier ignorable)
101+
(concat phpstan-identifier-prefix identifier))
102+
(when tip
103+
(concat phpstan-tip-message-prefix tip))))
104+
(lines (cl-remove-if #'null lines)))
105+
(if (null lines)
106+
msg
107+
(concat msg flycheck-phpstan-metadata-separator
108+
(mapconcat #'identity lines "\n"))))
109+
collect (flycheck-error-new-at (plist-get messages :line)
110+
nil 'error text
111+
:filename file))))
118112

119113
(flycheck-define-checker phpstan
120114
"PHP static analyzer based on PHPStan."

phpstan.el

+8
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ NIL
271271
(and (stringp (car v)) (listp (cdr v))))
272272
(or (eq 'docker v) (null v) (stringp v))))))
273273

274+
;; Utilities:
275+
(defun phpstan--plist-to-alist (plist)
276+
"Convert PLIST to association list."
277+
(let (alist)
278+
(while plist
279+
(push (cons (substring-no-properties (symbol-name (pop plist)) 1) (pop plist)) alist))
280+
(nreverse alist)))
281+
274282
;; Functions:
275283
(defun phpstan-get-working-dir ()
276284
"Return path to working directory of PHPStan."

0 commit comments

Comments
 (0)