Skip to content

Commit 00bb0b3

Browse files
authored
Merge pull request #52 from emacs-php/feature/phpstan-insert-dumptype
Add phpstan-insert-dumptype command
2 parents 83bae2c + 9323e06 commit 00bb0b3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.org

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ Typically, you would set the following ~.dir-locals.el~.
8888
#+END_SRC
8989

9090
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~.
91+
** Commands
92+
This package provides convenient commands for using PHPStan from Emacs.
93+
*** Command ~phpstan-insert-dumptype~
94+
Add ~\PHPStan\dumpType(...);~ to your PHP code and analyze it to make PHPStan display the type of the expression.
95+
#+BEGIN_SRC
96+
(define-key php-mode-map (kbd "C-c ^") #'phpstan-insert-dumptype)
97+
#+END_SRC
98+
99+
By default, if you press ~C-u~ before invoking the command, ~\PHPStan\dumpPhpDocType()~ will be inserted.
100+
101+
This feature was added in *PHPStan 1.12.7* and will dump types compatible with the ~@param~ and ~@return~ PHPDoc tags.
91102

92103
** API
93104
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]].

phpstan.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ have unexpected behaviors or performance implications."
145145
:safe #'booleanp
146146
:group 'phpstan)
147147

148+
(defconst phpstan-template-dump-type "\\PHPStan\\dumpType();")
149+
(defconst phpstan-template-dump-phpdoc-type "\\PHPStan\\dumpPhpDocType();")
150+
151+
(defcustom phpstan-intert-dump-type-templates (cons phpstan-template-dump-type
152+
phpstan-template-dump-phpdoc-type)
153+
"Default template of PHPStan dumpType insertion."
154+
:type '(cons string string)
155+
:group 'phpstan)
156+
148157
(defvar-local phpstan--use-xdebug-option nil)
149158

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

492+
;;;###autoload
493+
(defun phpstan-insert-dumptype (&optional expression prefix-num)
494+
"Insert PHPStan\\dumpType() expression-statement by EXPRESSION and PREFIX-NUM."
495+
(interactive
496+
(list
497+
(if (region-active-p)
498+
(buffer-substring-no-properties (region-beginning) (region-end))
499+
(or (thing-at-point 'symbol t) ""))
500+
current-prefix-arg))
501+
(let ((template (if current-prefix-arg
502+
(cdr phpstan-intert-dump-type-templates)
503+
(car phpstan-intert-dump-type-templates))))
504+
(move-end-of-line 1)
505+
(newline-and-indent)
506+
(insert template)
507+
(search-backward "(" (line-beginning-position) t)
508+
(forward-char)
509+
(insert expression)))
510+
483511
(provide 'phpstan)
484512
;;; phpstan.el ends here

0 commit comments

Comments
 (0)