Skip to content

Add phpstan-copy-dumped-type command #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

All notable changes of the `phpstan.el` are documented in this file using the [Keep a Changelog](https://keepachangelog.com/) principles.

<!-- ## Unreleased -->
## Unreleased

### Added

* Add `phpstan-copy-dumped-type` command to copy the nearest dumped type from `PHPStan\dumpType()` or `PHPStan\dumpPhpDocType()` messages.

## [0.8.2]

Expand Down
8 changes: 8 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ Insert a ~@phpstan-ignore~ tag to suppress any PHPStan errors on the current lin
By default it inserts the tag on the previous line, but if there is already a tag at the end of the current line or on the previous line, the identifiers will be appended there.

If there is no existing tag and ~C-u~ is pressed before the command, it will be inserted at the end of the line.
*** Command ~phpstan-copy-dumped-type~
Copy the nearest dumped type message from PHPStan's output.

This command looks for messages like ~Dumped type: int|string|null~ reported by ~PHPStan\dumpType()~ or ~PHPStan\dumpPhpDocType()~, and copies the type string to the kill ring.

If there are multiple dumped types in the buffer, it selects the one closest to the current line.

If no dumped type messages are found, the command signals an error.
** API
Most variables defined in this package are buffer local. If you want to set it for multiple projects, use [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Default-Value.html][setq-default]].

Expand Down
1 change: 1 addition & 0 deletions flycheck-phpstan.el
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
(errors (phpstan--plist-to-alist (plist-get data :files))))
(unless phpstan-disable-buffer-errors
(phpstan-update-ignorebale-errors-from-json-buffer errors))
(phpstan-update-dumped-types errors)
(flycheck-phpstan--build-errors errors)))

(defun flycheck-phpstan--temp-buffer ()
Expand Down
25 changes: 25 additions & 0 deletions phpstan.el
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ have unexpected behaviors or performance implications."
(defvar-local phpstan--use-xdebug-option nil)

(defvar-local phpstan--ignorable-errors '())
(defvar-local phpstan--dumped-types '())

;;;###autoload
(progn
Expand Down Expand Up @@ -523,6 +524,17 @@ it returns the value of `SOURCE' as it is."
(setq phpstan--ignorable-errors
(mapcar (lambda (v) (cons (car v) (mapcar #'cdr (cdr v)))) (seq-group-by #'car identifiers)))))

(defun phpstan-update-dumped-types (errors)
"Update `phpstan--dumped-types' variable by ERRORS."
(save-match-data
(setq phpstan--dumped-types
(cl-loop for (_ . entry) in errors
append (cl-loop for message in (plist-get entry :messages)
for msg = (plist-get message :message)
if (string-match (eval-when-compile (rx bos "Dumped type: ")) msg)
collect (cons (plist-get message :line)
(substring-no-properties msg (match-end 0))))))))

(defconst phpstan--re-ignore-tag
(eval-when-compile
(rx (* (syntax whitespace)) "//" (* (syntax whitespace))
Expand Down Expand Up @@ -590,6 +602,19 @@ POSITION determines where to insert the comment and can be either `this-line' or
(if new-position (if append ", " " ") "// @phpstan-ignore ")
(string-join identifiers ", ")))))))

;;;###autoload
(defun phpstan-copy-dumped-type ()
"Copy a dumped PHPStan type."
(interactive)
(if phpstan--dumped-types
(let ((type (if (eq 1 (length phpstan--dumped-types))
(cdar phpstan--dumped-types)
(let ((linum (line-number-at-pos)))
(cdar (seq-sort-by (lambda (elm) (abs (- linum (car elm)))) #'< phpstan--dumped-types))))))
(kill-new type)
(message "Copied %s" type))
(user-error "No dumped PHPStan types")))

;;;###autoload
(defun phpstan-insert-dumptype (&optional expression prefix-num)
"Insert PHPStan\\dumpType() expression-statement by EXPRESSION and PREFIX-NUM."
Expand Down