|
43 | 43 |
|
44 | 44 | ;; Usually it is defined dynamically by flycheck
|
45 | 45 | (defvar flycheck-phpstan-executable)
|
| 46 | +(defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*") |
46 | 47 |
|
47 | 48 | (defun flycheck-phpstan--enabled-and-set-variable ()
|
48 | 49 | "Return path to phpstan configure file, and set buffer execute in side effect."
|
|
60 | 61 | (null phpstan-executable)))
|
61 | 62 | (setq-local flycheck-phpstan-executable (car (phpstan-get-executable-and-args)))))))
|
62 | 63 |
|
| 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 | + |
63 | 96 | (flycheck-define-checker phpstan
|
64 | 97 | "PHP static analyzer based on PHPStan."
|
65 |
| - :command ("php" (eval (phpstan-get-command-args)) |
| 98 | + :command ("php" (eval (phpstan-get-command-args :format "json")) |
66 | 99 | (eval (if (or (buffer-modified-p) (not buffer-file-name))
|
67 | 100 | (phpstan-normalize-path
|
68 | 101 | (flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace))
|
69 | 102 | buffer-file-name)))
|
70 | 103 | :working-directory (lambda (_) (phpstan-get-working-dir))
|
71 | 104 | :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 |
74 | 106 | :modes (php-mode phps-mode))
|
75 | 107 |
|
76 | 108 | (add-to-list 'flycheck-checkers 'phpstan t)
|
|
0 commit comments