|
45 | 45 | (defvar flycheck-phpstan-executable) |
46 | 46 | (defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*") |
47 | 47 |
|
48 | | - |
49 | 48 | (defcustom flycheck-phpstan-ignore-metadata-list nil |
50 | 49 | "Set of metadata items to ignore in PHPStan messages for Flycheck." |
51 | 50 | :type '(set (const identifier) |
|
76 | 75 |
|
77 | 76 | (defun flycheck-phpstan-parse-output (output &optional _checker _buffer) |
78 | 77 | "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))) |
83 | 85 |
|
84 | 86 | (defun flycheck-phpstan--temp-buffer () |
85 | 87 | "Return a temporary buffer for decode JSON." |
86 | 88 | (get-buffer-create flycheck-phpstan--temp-buffer-name)) |
87 | 89 |
|
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)))) |
118 | 112 |
|
119 | 113 | (flycheck-define-checker phpstan |
120 | 114 | "PHP static analyzer based on PHPStan." |
|
0 commit comments