Skip to content

Commit 6f1c7bb

Browse files
authored
Merge pull request #51 from emacs-php/feature/error-identifier
[Flycheck] Support Error Identifier added in PHPStan 1.11
2 parents 44924b6 + 3c489cf commit 6f1c7bb

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

flycheck-phpstan.el

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@
4545
(defvar flycheck-phpstan-executable)
4646
(defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*")
4747

48+
49+
(defcustom flycheck-phpstan-ignore-metadata-list nil
50+
"Set of metadata items to ignore in PHPStan messages for Flycheck."
51+
:type '(set (const identifier)
52+
(const tip))
53+
:group 'phpstan)
54+
55+
(defcustom flycheck-phpstan-metadata-separator "\n"
56+
"Separator of PHPStan message and metadata."
57+
:type 'string
58+
:safe #'stringp
59+
:group 'phpstan)
60+
4861
(defun flycheck-phpstan--enabled-and-set-variable ()
4962
"Return path to phpstan configure file, and set buffer execute in side effect."
5063
(let ((enabled (phpstan-enabled)))
@@ -77,11 +90,21 @@
7790
(let ((data (phpstan--parse-json json-buffer)))
7891
(cl-loop for (file . entry) in (flycheck-phpstan--plist-to-alist (plist-get data :files))
7992
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))
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"))))
85108
collect (flycheck-error-new-at (plist-get messages :line)
86109
nil 'error text
87110
:filename file)))))

phpstan.el

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@
129129
:safe #'stringp
130130
:group 'phpstan)
131131

132+
(defcustom phpstan-identifier-prefix "🪪 "
133+
"Prefix of PHPStan error identifier."
134+
:type 'string
135+
:safe #'stringp
136+
:group 'phpstan)
137+
132138
(defcustom phpstan-enable-remote-experimental nil
133139
"Enable PHPStan analysis remotely by TRAMP.
134140
@@ -359,14 +365,14 @@ it returns the value of `SOURCE' as it is."
359365
(let ((file (phpstan--expand-file-name (or buffer-file-name
360366
(read-file-name "Choose a PHP script: ")))))
361367
(compile (mapconcat #'shell-quote-argument
362-
(phpstan-get-command-args :include-executable t :args (list file)) " "))))
368+
(phpstan-get-command-args :include-executable t :args (list file) :verbose 1) " "))))
363369

364370
;;;###autoload
365371
(defun phpstan-analyze-file (file)
366372
"Analyze a PHP script FILE using PHPStan."
367373
(interactive (list (phpstan--expand-file-name (read-file-name "Choose a PHP script: "))))
368374
(compile (mapconcat #'shell-quote-argument
369-
(phpstan-get-command-args :include-executable t :args (list file)) " ")))
375+
(phpstan-get-command-args :include-executable t :args (list file) :verbose 1) " ")))
370376

371377
;;;###autoload
372378
(defun phpstan-analyze-project ()
@@ -440,7 +446,7 @@ it returns the value of `SOURCE' as it is."
440446
((executable-find "phpstan") (list (executable-find "phpstan")))
441447
(t (error "PHPStan executable not found")))))))
442448

443-
(cl-defun phpstan-get-command-args (&key include-executable use-pro args format options config)
449+
(cl-defun phpstan-get-command-args (&key include-executable use-pro args format options config verbose)
444450
"Return command line argument for PHPStan."
445451
(let ((executable-and-args (phpstan-get-executable-and-args))
446452
(config (or config (phpstan-normalize-path (phpstan-get-config-file))))
@@ -457,6 +463,12 @@ it returns the value of `SOURCE' as it is."
457463
(and autoload (list "-a" autoload))
458464
(and memory-limit (list "--memory-limit" memory-limit))
459465
(and level (list "-l" level))
466+
(cond
467+
((null verbose) nil)
468+
((memq verbose '(1 t)) (list "-v"))
469+
((eq verbose 2) (list "-vv"))
470+
((eq verbose 3) (list "-vvv"))
471+
(error ":verbose option should be 1, 2, 3 or `t'"))
460472
(cond
461473
(phpstan--use-xdebug-option (list phpstan--use-xdebug-option))
462474
((eq phpstan-use-xdebug-option 'auto)

0 commit comments

Comments
 (0)