diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c9d053..d6ec178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,3 +7,11 @@ All notable changes of the phpactor.el are documented in this file using the [Ke ### Added * Add `phpstan-analyze-this-file` command +* Add `phpstan.dist.neon` to `phpstan-get-config-file` as PHPStan config files. +* Add `phpstan-pro` command to launch [PHPStan Pro]. + +[PHPStan Pro]: https://phpstan.org/blog/introducing-phpstan-pro + +### Changed + +* Make flycheck analyze the original file directly instead of temporary files when there are no changes to the file. diff --git a/flycheck-phpstan.el b/flycheck-phpstan.el index 35af909..a4256b1 100644 --- a/flycheck-phpstan.el +++ b/flycheck-phpstan.el @@ -63,9 +63,10 @@ (flycheck-define-checker phpstan "PHP static analyzer based on PHPStan." :command ("php" (eval (phpstan-get-command-args)) - (eval (phpstan-normalize-path - (flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace) - (flycheck-save-buffer-to-temp #'flycheck-temp-file-system)))) + (eval (if (or (buffer-modified-p) (not buffer-file-name)) + (phpstan-normalize-path + (flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace)) + buffer-file-name))) :working-directory (lambda (_) (phpstan-get-working-dir)) :enabled (lambda () (flycheck-phpstan--enabled-and-set-variable)) :error-patterns diff --git a/phpstan.el b/phpstan.el index 143ab94..3185f0c 100644 --- a/phpstan.el +++ b/phpstan.el @@ -56,11 +56,12 @@ (require 'cl-lib) (require 'php-project) +(eval-when-compile + (require 'php)) ;; Variables: - (defgroup phpstan nil - "Interaface to PHPStan" + "Interaface to PHPStan." :tag "PHPStan" :prefix "phpstan-" :group 'tools @@ -229,7 +230,7 @@ NIL (php-project-get-root-dir))))) (defun phpstan-get-config-file () - "Return path to phpstan configure file or `NIL'." + "Return path to phpstan configure file or NIL." (if phpstan-config-file (if (and (consp phpstan-config-file) (eq 'root (car phpstan-config-file))) @@ -238,8 +239,8 @@ NIL phpstan-config-file) (let ((working-directory (phpstan-get-working-dir))) (when working-directory - (cl-loop for name in '("phpstan.neon" "phpstan.neon.dist") - for dir = (locate-dominating-file working-directory name) + (cl-loop for name in '("phpstan.neon" "phpstan.neon.dist" "phpstan.dist.neon") + for dir = (locate-dominating-file working-directory name) if dir return (expand-file-name name dir)))))) @@ -252,7 +253,7 @@ NIL phpstan-autoload-file))) (defun phpstan-normalize-path (source-original &optional source) - "Return normalized source file path to pass by `SOURCE-ORIGINAL' OR `SOURCE'. + "Return normalized source file path to pass by SOURCE-ORIGINAL or SOURCE. If neither `phpstan-replace-path-prefix' nor executable docker is set, it returns the value of `SOURCE' as it is." @@ -272,7 +273,7 @@ it returns the value of `SOURCE' as it is." (or source source-original)))) (defun phpstan-get-level () - "Return path to phpstan configure file or `NIL'." + "Return path to phpstan configure file or NIL." (cond ((null phpstan-level) nil) ((integerp phpstan-level) (int-to-string phpstan-level)) @@ -283,17 +284,30 @@ it returns the value of `SOURCE' as it is." "Return --memory-limit value." phpstan-memory-limit) +;;;###autoload (defun phpstan-analyze-this-file () "Analyze current buffer-file using PHPStan." (interactive) (let ((file (expand-file-name (or buffer-file-name (read-file-name "Choose a PHP script: "))))) - (compile (mapconcat #'shell-quote-argument (append (phpstan-get-command-args t) (list file)) " ")))) + (compile (mapconcat #'shell-quote-argument + (append (phpstan-get-command-args :include-executable t) (list file)) " ")))) +;;;###autoload (defun phpstan-analyze-file (file) "Analyze a PHP script FILE using PHPStan." (interactive (list (expand-file-name (read-file-name "Choose a PHP script: ")))) - (compile (mapconcat #'shell-quote-argument (append (phpstan-get-command-args t) (list file)) " "))) + (compile (mapconcat #'shell-quote-argument + (append (phpstan-get-command-args :include-executable t) (list file)) " "))) + +;;;###autoload +(defun phpstan-pro () + "Analyze current PHP project using PHPStan Pro." + (interactive) + (let ((compilation-buffer-name-function (lambda (_) "*PHPStan Pro*")) + (command (mapconcat #'shell-quote-argument + (phpstan-get-command-args :include-executable t :use-pro t) " "))) + (compile command t))) (defun phpstan-get-executable-and-args () "Return PHPStan excutable file and arguments." @@ -332,7 +346,7 @@ it returns the value of `SOURCE' as it is." ((executable-find "phpstan") (list (executable-find "phpstan"))) (t (error "PHPStan executable not found"))))))) -(defun phpstan-get-command-args (&optional include-executable) +(cl-defun phpstan-get-command-args (&key include-executable use-pro) "Return command line argument for PHPStan." (let ((executable-and-args (phpstan-get-executable-and-args)) (path (phpstan-normalize-path (phpstan-get-config-file))) @@ -342,6 +356,7 @@ it returns the value of `SOURCE' as it is." (append (if include-executable (list (car executable-and-args)) nil) (cdr executable-and-args) (list "analyze" "--error-format=raw" "--no-progress" "--no-interaction") + (and use-pro (list "--pro" "--no-ansi")) (and path (list "-c" path)) (and autoload (list "-a" autoload)) (and memory-limit (list "--memory-limit" memory-limit))