Skip to content

Commit 540c379

Browse files
authored
Merge pull request #56 from emacs-php/fix/suppress-flycheck-errors-on-editing
Prevent Flycheck from showing errors due to deleted temp files during editing
2 parents f289114 + 3b0a83a commit 540c379

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

flycheck-phpstan.el

+24
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
;; Usually it is defined dynamically by flycheck
4545
(defvar flycheck-phpstan-executable)
4646
(defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*")
47+
(defvar flycheck-phpstan--output-filter-added nil)
48+
(defconst flycheck-phpstan--nofiles-message (eval-when-compile (regexp-quote "[ERROR] No files found to analyse.")))
4749

4850
(defcustom flycheck-phpstan-ignore-metadata-list nil
4951
"Set of metadata items to ignore in PHPStan messages for Flycheck."
@@ -57,6 +59,24 @@
5759
:safe #'stringp
5860
:group 'phpstan)
5961

62+
(defun flycheck-phpstan--suppress-no-files-error (next checker exit-status files output callback cwd)
63+
"Suppress Flycheck errors if PHPStan reports no files in a modified buffer.
64+
65+
This function is intended to be used as an :around advice for
66+
`flycheck-finish-checker-process'.
67+
68+
It prevents Flycheck from displaying an error when:
69+
- CHECKER is `phpstan',
70+
- the current buffer is modified,
71+
- and OUTPUT contains the message `flycheck-phpstan--nofiles-message'.
72+
73+
NEXT, EXIT-STATUS, FILES, OUTPUT, CALLBACK, and CWD are the original arguments
74+
passed to `flycheck-finish-checker-process'."
75+
(unless (and (eq checker 'phpstan)
76+
(buffer-modified-p)
77+
(string-match-p flycheck-phpstan--nofiles-message output))
78+
(funcall next checker exit-status files output callback cwd)))
79+
6080
(defun flycheck-phpstan--enabled-and-set-variable ()
6181
"Return path to phpstan configure file, and set buffer execute in side effect."
6282
(let ((enabled (phpstan-enabled)))
@@ -71,6 +91,10 @@
7191
(and (stringp (car-safe phpstan-executable))
7292
(listp (cdr-safe phpstan-executable)))
7393
(null phpstan-executable)))
94+
(unless flycheck-phpstan--output-filter-added
95+
(advice-add 'flycheck-finish-checker-process
96+
:around #'flycheck-phpstan--suppress-no-files-error)
97+
(setq flycheck-phpstan--output-filter-added t))
7498
(setq-local flycheck-phpstan-executable (car (phpstan-get-executable-and-args)))))))
7599

76100
(defun flycheck-phpstan-parse-output (output &optional _checker _buffer)

0 commit comments

Comments
 (0)