Skip to content

Commit d0dbbfd

Browse files
authored
Merge pull request #40 from emacs-php/fix/phpstan-executable
Fix PHPStan executable
2 parents 834f4bf + f9c0813 commit d0dbbfd

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

flycheck-phpstan.el

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"Return path to phpstan configure file, and set buffer execute in side effect."
4949
(let ((enabled (phpstan-enabled)))
5050
(prog1 enabled
51-
(when (and phpstan-flycheck-auto-set-executable
51+
(when (and enabled
52+
phpstan-flycheck-auto-set-executable
5253
(null (bound-and-true-p flycheck-phpstan-executable))
5354
(or (stringp phpstan-executable)
5455
(eq 'docker phpstan-executable)
@@ -57,11 +58,7 @@
5758
(and (stringp (car-safe phpstan-executable))
5859
(listp (cdr-safe phpstan-executable)))
5960
(null phpstan-executable)))
60-
(set (make-local-variable 'flycheck-phpstan-executable)
61-
(cond
62-
((eq 'docker phpstan-executable) phpstan-docker-executable)
63-
((stringp phpstan-executable) phpstan-executable)
64-
(t (car phpstan-executable))))))))
61+
(setq-local flycheck-phpstan-executable (car (phpstan-get-executable-and-options)))))))
6562

6663
(flycheck-define-checker phpstan
6764
"PHP static analyzer based on PHPStan."

flymake-phpstan.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
(defun flymake-phpstan (report-fn &rest _ignored-args)
9292
"Flymake backend for PHPStan report using REPORT-FN."
93-
(let ((command-args (phpstan-get-command-args)))
93+
(let ((command-args (phpstan-get-command-args t)))
9494
(unless (car command-args)
9595
(user-error "Cannot find a phpstan executable command"))
9696
(when (process-live-p flymake-phpstan--proc)

phpstan.el

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,22 +285,30 @@ it returns the value of `SOURCE' as it is."
285285

286286
(defun phpstan-analyze-file (file)
287287
"Analyze a PHPScript FILE using PHPStan."
288-
(interactive "fChoose a PHP script: ")
289-
(compile (mapconcat #'shell-quote-argument (append (phpstan-get-command-args) (list file)) " ")))
288+
(interactive (list (expand-file-name (read-file-name "Choose a PHP script: "))))
289+
(compile (mapconcat #'shell-quote-argument (append (phpstan-get-command-args t) (list file)) " ")))
290290

291-
(defun phpstan-get-executable ()
291+
(defun phpstan-get-executable-and-args ()
292292
"Return PHPStan excutable file and arguments."
293293
(cond
294294
((eq 'docker phpstan-executable)
295-
(list "run" "--rm" "-v"
295+
(list phpstan-docker-executable "run" "--rm" "-v"
296296
(concat (expand-file-name (php-project-get-root-dir)) ":/app")
297297
phpstan-docker-image))
298298
((and (consp phpstan-executable)
299299
(eq 'root (car phpstan-executable)))
300-
(list
301-
(expand-file-name (cdr phpstan-executable) (php-project-get-root-dir))))
302-
((and (stringp phpstan-executable) (file-exists-p phpstan-executable))
303-
(list phpstan-executable))
300+
(let ((phpstan (expand-file-name (cdr phpstan-executable) (php-project-get-root-dir))))
301+
(if (file-executable-p phpstan)
302+
(list phpstan)
303+
(list php-executable phpstan))))
304+
((and (stringp phpstan-executable))
305+
(unless (file-exists-p phpstan-executable)
306+
(user-error "File %s is not exists. Please check `phpstan-executable' variable" phpstan-executable))
307+
(when (file-directory-p phpstan-executable)
308+
(user-error "Path %s is a directory. Please check `phpstan-executable' variable" phpstan-executable))
309+
(if (file-executable-p phpstan-executable)
310+
(list phpstan-executable)
311+
(list php-executable phpstan-executable)))
304312
((and phpstan-flycheck-auto-set-executable
305313
(listp phpstan-executable)
306314
(stringp (car phpstan-executable))
@@ -310,18 +318,22 @@ it returns the value of `SOURCE' as it is."
310318
(let ((vendor-phpstan (expand-file-name "vendor/bin/phpstan"
311319
(php-project-get-root-dir))))
312320
(cond
313-
((file-exists-p vendor-phpstan) (list vendor-phpstan))
321+
((file-exists-p vendor-phpstan)
322+
(if (file-executable-p vendor-phpstan)
323+
(list vendor-phpstan)
324+
(list php-executable vendor-phpstan)))
314325
((executable-find "phpstan") (list (executable-find "phpstan")))
315326
(t (error "PHPStan executable not found")))))))
316327

317-
(defun phpstan-get-command-args ()
328+
(defun phpstan-get-command-args (&optional include-executable)
318329
"Return command line argument for PHPStan."
319-
(let ((executable (phpstan-get-executable))
330+
(let ((executable-and-args (phpstan-get-executable-and-args))
320331
(path (phpstan-normalize-path (phpstan-get-config-file)))
321332
(autoload (phpstan-get-autoload-file))
322333
(memory-limit (phpstan-get-memory-limit))
323334
(level (phpstan-get-level)))
324-
(append executable
335+
(append (if include-executable (list (car executable-and-args)) nil)
336+
(cdr executable-and-options)
325337
(list "analyze" "--error-format=raw" "--no-progress" "--no-interaction")
326338
(and path (list "-c" path))
327339
(and autoload (list "-a" autoload))

0 commit comments

Comments
 (0)