Skip to content

Commit

Permalink
Add phpstan-insert-dumptype command
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed Oct 28, 2024
1 parent 83bae2c commit 9323e06
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ Typically, you would set the following ~.dir-locals.el~.
#+END_SRC

If there is a ~phpstan.neon~ file in the root directory of the project, you do not need to set both ~phpstan-working-dir~ and ~phpstan-config-file~.
** Commands
This package provides convenient commands for using PHPStan from Emacs.
*** Command ~phpstan-insert-dumptype~
Add ~\PHPStan\dumpType(...);~ to your PHP code and analyze it to make PHPStan display the type of the expression.
#+BEGIN_SRC
(define-key php-mode-map (kbd "C-c ^") #'phpstan-insert-dumptype)
#+END_SRC

By default, if you press ~C-u~ before invoking the command, ~\PHPStan\dumpPhpDocType()~ will be inserted.

This feature was added in *PHPStan 1.12.7* and will dump types compatible with the ~@param~ and ~@return~ PHPDoc tags.

** 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
28 changes: 28 additions & 0 deletions phpstan.el
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ have unexpected behaviors or performance implications."
:safe #'booleanp
:group 'phpstan)

(defconst phpstan-template-dump-type "\\PHPStan\\dumpType();")
(defconst phpstan-template-dump-phpdoc-type "\\PHPStan\\dumpPhpDocType();")

(defcustom phpstan-intert-dump-type-templates (cons phpstan-template-dump-type
phpstan-template-dump-phpdoc-type)
"Default template of PHPStan dumpType insertion."
:type '(cons string string)
:group 'phpstan)

(defvar-local phpstan--use-xdebug-option nil)

;;;###autoload
Expand Down Expand Up @@ -480,5 +489,24 @@ it returns the value of `SOURCE' as it is."
options
(and args (cons "--" args)))))

;;;###autoload
(defun phpstan-insert-dumptype (&optional expression prefix-num)
"Insert PHPStan\\dumpType() expression-statement by EXPRESSION and PREFIX-NUM."
(interactive
(list
(if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(or (thing-at-point 'symbol t) ""))
current-prefix-arg))
(let ((template (if current-prefix-arg
(cdr phpstan-intert-dump-type-templates)
(car phpstan-intert-dump-type-templates))))
(move-end-of-line 1)
(newline-and-indent)
(insert template)
(search-backward "(" (line-beginning-position) t)
(forward-char)
(insert expression)))

(provide 'phpstan)
;;; phpstan.el ends here

0 comments on commit 9323e06

Please sign in to comment.