Skip to content

Commit 05c9f76

Browse files
committed
Add phpstan-normalize-path and support Docker
1 parent d559ee1 commit 05c9f76

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

phpstan.el

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@
6767
(and (stringp v)
6868
(string-match-p "\\`[0-9]\\'" v))))))
6969

70+
;;;###autoload
71+
(progn
72+
(defvar phpstan-replace-path-prefix)
73+
(make-variable-buffer-local 'phpstan-replace-path-prefix)
74+
(put 'phpstan-replace-path-prefix 'safe-local-variable
75+
#'(lambda (v) (or (null v) (stringp v)))))
76+
7077
;; Usually it is defined dynamically by flycheck
7178
(defvar flycheck-phpstan-executable)
7279

@@ -117,6 +124,25 @@ NIL
117124
(set (make-local-variable 'flycheck-phpstan-executable)
118125
(car phpstan-executable)))))
119126

127+
(defun phpstan-normalize-path (source-original source)
128+
"Return normalized source file path to pass by `SOURCE-ORIGINAL' OR `SOURCE'.
129+
130+
If neither `phpstan-replace-path-prefix' nor executable docker is set,
131+
it returns the value of `SOURCE' as it is."
132+
(let ((working-directory (expand-file-name (php-project-get-root-dir)))
133+
(prefix
134+
(cond
135+
((not (null phpstan-replace-path-prefix)) phpstan-replace-path-prefix)
136+
((and (consp phpstan-executable)
137+
(string= "docker" (car phpstan-executable))) "/app"))))
138+
(if prefix
139+
(expand-file-name
140+
(replace-regexp-in-string (concat "\\`" (regexp-quote working-directory))
141+
""
142+
source-original t t)
143+
prefix)
144+
source)))
145+
120146
(defun phpstan-get-level ()
121147
"Return path to phpstan configure file or `NIL'."
122148
(cond
@@ -151,7 +177,9 @@ NIL
151177
"analyze" "--errorFormat=raw" "--no-progress" "--no-interaction"
152178
"-c" (eval (phpstan-get-config-file))
153179
"-l" (eval (phpstan-get-level))
154-
source)
180+
(eval (phpstan-normalize-path
181+
(flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace)
182+
(flycheck-save-buffer-to-temp #'flycheck-temp-file-system))))
155183
:working-directory (lambda (_) (php-project-get-root-dir))
156184
:enabled (lambda () (phpstan-get-config-file-and-set-flycheck-variable))
157185
:error-patterns

test-docker.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
foo();
4+
f();
5+
foo();
6+
7+
echo Fooo;
8+
echo FOO;
9+
10+
// Local Variables:
11+
// phpstan-executable: ("docker" "run" "--rm" "-v" "/Users/megurine/repo/emacs/phpstan.el/:/app" "phpstan/phpstan")
12+
// phpstan-config-file: "/app/tests/phpstan-docker.neon"
13+
// phpstan-level: 7
14+
// End:

tests/phpstan-docker.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parameters:
2+
bootstrap: %rootDir%/../../../../app/tests/bootstrap.php

0 commit comments

Comments
 (0)