Skip to content

Commit

Permalink
Merge pull request #51 from emacs-php/feature/error-identifier
Browse files Browse the repository at this point in the history
[Flycheck] Support Error Identifier added in PHPStan 1.11
  • Loading branch information
zonuexe authored May 27, 2024
2 parents 44924b6 + 3c489cf commit 6f1c7bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
33 changes: 28 additions & 5 deletions flycheck-phpstan.el
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
(defvar flycheck-phpstan-executable)
(defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*")


(defcustom flycheck-phpstan-ignore-metadata-list nil
"Set of metadata items to ignore in PHPStan messages for Flycheck."
:type '(set (const identifier)
(const tip))
:group 'phpstan)

(defcustom flycheck-phpstan-metadata-separator "\n"
"Separator of PHPStan message and metadata."
:type 'string
:safe #'stringp
:group 'phpstan)

(defun flycheck-phpstan--enabled-and-set-variable ()
"Return path to phpstan configure file, and set buffer execute in side effect."
(let ((enabled (phpstan-enabled)))
Expand Down Expand Up @@ -77,11 +90,21 @@
(let ((data (phpstan--parse-json json-buffer)))
(cl-loop for (file . entry) in (flycheck-phpstan--plist-to-alist (plist-get data :files))
append (cl-loop for messages in (plist-get entry :messages)
for text = (let ((msg (plist-get messages :message))
(tip (plist-get messages :tip)))
(if tip
(concat msg "\n" phpstan-tip-message-prefix tip)
msg))
for text = (let* ((msg (plist-get messages :message))
(ignorable (plist-get messages :ignorable))
(identifier (unless (memq 'identifier flycheck-phpstan-ignore-metadata-list)
(plist-get messages :identifier)))
(tip (unless (memq 'tip flycheck-phpstan-ignore-metadata-list)
(plist-get messages :tip)))
(lines (list (when (and identifier ignorable)
(concat phpstan-identifier-prefix identifier))
(when tip
(concat phpstan-tip-message-prefix tip))))
(lines (cl-remove-if #'null lines)))
(if (null lines)
msg
(concat msg flycheck-phpstan-metadata-separator
(mapconcat #'identity lines "\n"))))
collect (flycheck-error-new-at (plist-get messages :line)
nil 'error text
:filename file)))))
Expand Down
18 changes: 15 additions & 3 deletions phpstan.el
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
:safe #'stringp
:group 'phpstan)

(defcustom phpstan-identifier-prefix "🪪 "
"Prefix of PHPStan error identifier."
:type 'string
:safe #'stringp
:group 'phpstan)

(defcustom phpstan-enable-remote-experimental nil
"Enable PHPStan analysis remotely by TRAMP.
Expand Down Expand Up @@ -359,14 +365,14 @@ it returns the value of `SOURCE' as it is."
(let ((file (phpstan--expand-file-name (or buffer-file-name
(read-file-name "Choose a PHP script: ")))))
(compile (mapconcat #'shell-quote-argument
(phpstan-get-command-args :include-executable t :args (list file)) " "))))
(phpstan-get-command-args :include-executable t :args (list file) :verbose 1) " "))))

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

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

(cl-defun phpstan-get-command-args (&key include-executable use-pro args format options config)
(cl-defun phpstan-get-command-args (&key include-executable use-pro args format options config verbose)
"Return command line argument for PHPStan."
(let ((executable-and-args (phpstan-get-executable-and-args))
(config (or config (phpstan-normalize-path (phpstan-get-config-file))))
Expand All @@ -457,6 +463,12 @@ it returns the value of `SOURCE' as it is."
(and autoload (list "-a" autoload))
(and memory-limit (list "--memory-limit" memory-limit))
(and level (list "-l" level))
(cond
((null verbose) nil)
((memq verbose '(1 t)) (list "-v"))
((eq verbose 2) (list "-vv"))
((eq verbose 3) (list "-vvv"))
(error ":verbose option should be 1, 2, 3 or `t'"))
(cond
(phpstan--use-xdebug-option (list phpstan--use-xdebug-option))
((eq phpstan-use-xdebug-option 'auto)
Expand Down

0 comments on commit 6f1c7bb

Please sign in to comment.