Skip to content

Commit e8beb13

Browse files
committed
Fixed crash when PHPStan output is not JSON
1 parent e224a90 commit e8beb13

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

phpstan.el

+16-11
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,22 @@ it returns the value of `SOURCE' as it is."
372372
"Read JSON string from BUFFER."
373373
(with-current-buffer buffer
374374
(goto-char (point-min))
375-
;; Ignore STDERR
376-
(save-match-data
377-
(when (search-forward-regexp "^{" nil t)
378-
(backward-char 1)
379-
(delete-region (point-min) (point))))
380-
(if (eval-when-compile (and (fboundp 'json-serialize)
381-
(fboundp 'json-parse-buffer)))
382-
(with-no-warnings
383-
(json-parse-buffer :object-type 'plist :array-type 'list))
384-
(let ((json-object-type 'plist) (json-array-type 'list))
385-
(json-read-object)))))
375+
(if (not (looking-at-p "{"))
376+
(let ((msg (string-trim (buffer-substring-no-properties (point-min) (point-max))))
377+
(pattern (eval-when-compile (regexp-quote "No files found to analyse."))))
378+
(prog1 nil
379+
(unless (string-match-p pattern msg)
380+
(message "[PHPStan]: %s" msg))))
381+
;; Ignore STDERR
382+
(save-match-data
383+
(when (search-forward-regexp "^{" nil t)
384+
(backward-char 1)
385+
(delete-region (point-min) (point))))
386+
(static-if (and (fboundp 'json-serialize) (fboundp 'json-parse-buffer))
387+
(with-no-warnings
388+
(json-parse-buffer :object-type 'plist :array-type 'list))
389+
(let ((json-object-type 'plist) (json-array-type 'list))
390+
(json-read-object))))))
386391

387392
(defun phpstan--expand-file-name (name)
388393
"Expand file name by NAME."

0 commit comments

Comments
 (0)