Skip to content

Commit 2414947

Browse files
authored
Merge pull request #30 from emacs-php/feature/auto-activate
Auto activation
2 parents a2bbd6a + 273f89e commit 2414947

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

flycheck-phpstan.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
(defun flycheck-phpstan--enabled-and-set-variable ()
4848
"Return path to phpstan configure file, and set buffer execute in side effect."
49-
(let ((enabled (or phpstan-working-dir (phpstan-get-config-file))))
49+
(let ((enabled (phpstan-enabled)))
5050
(prog1 enabled
5151
(when (and phpstan-flycheck-auto-set-executable
5252
(not (and (boundp 'flycheck-phpstan-executable)

flymake-phpstan.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
(defun flymake-phpstan-turn-on ()
105105
"Enable flymake-phpstan as buffer-local Flymake backend."
106106
(interactive)
107-
(let ((enabled (or phpstan-working-dir (phpstan-get-config-file))))
107+
(let ((enabled (phpstan-enabled)))
108108
(when enabled
109109
(flymake-mode 1)
110110
(when flymake-phpstan-disable-c-mode-hooks

phpstan.el

+45-6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
:type 'boolean
7373
:group 'phpstan)
7474

75+
(defcustom phpstan-enable-on-no-config-file t
76+
"If T, activate configuration from composer even when `phpstan.neon' is not found."
77+
:type 'boolean
78+
:group 'phpstan)
79+
7580
;;;###autoload
7681
(progn
7782
(defvar phpstan-working-dir nil
@@ -112,6 +117,24 @@ NIL
112117
(and (eq 'root (car v)) (stringp (cdr v)))
113118
(null v) (stringp v)))))
114119

120+
;;;###autoload
121+
(progn
122+
(defvar-local phpstan-autoload-file nil
123+
"Path to autoload file for PHPStan.
124+
125+
STRING
126+
Path to `phpstan' autoload file.
127+
128+
`(root . STRING)'
129+
Relative path to `phpstan' configuration file from project root directory.
130+
131+
NIL
132+
If `phpstan-enable-on-no-config-file', search \"vendor/autoload.php\" in (phpstan-get-working-dir).")
133+
(put 'phpstan-autoload-file 'safe-local-variable
134+
#'(lambda (v) (if (consp v)
135+
(and (eq 'root (car v)) (stringp (cdr v)))
136+
(null v) (stringp v)))))
137+
115138
;;;###autoload
116139
(progn
117140
(defvar-local phpstan-level nil
@@ -177,6 +200,13 @@ NIL
177200
((stringp phpstan-working-dir) phpstan-working-dir)
178201
(t (php-project-get-root-dir))))
179202

203+
(defun phpstan-enabled ()
204+
"Return non-NIL if PHPStan configured or Composer detected."
205+
(or (phpstan-get-config-file)
206+
(phpstan-get-autoload-file)
207+
(and phpstan-enable-on-no-config-file
208+
(php-project-get-root-dir))))
209+
180210
(defun phpstan-get-config-file ()
181211
"Return path to phpstan configure file or `NIL'."
182212
(if phpstan-config-file
@@ -192,6 +222,14 @@ NIL
192222
if dir
193223
return (expand-file-name name dir))))))
194224

225+
(defun phpstan-get-autoload-file ()
226+
"Return path to autoload file or NIL."
227+
(when phpstan-autoload-file
228+
(if (and (consp phpstan-autoload-file)
229+
(eq 'root (car phpstan-autoload-file)))
230+
(expand-file-name (cdr phpstan-autoload-file) (php-project-get-root-dir))
231+
phpstan-autoload-file)))
232+
195233
(defun phpstan-normalize-path (source-original &optional source)
196234
"Return normalized source file path to pass by `SOURCE-ORIGINAL' OR `SOURCE'.
197235
@@ -249,14 +287,15 @@ it returns the value of `SOURCE' as it is."
249287
(defun phpstan-get-command-args ()
250288
"Return command line argument for PHPStan."
251289
(let ((executable (phpstan-get-executable))
252-
(args (list "analyze" "--error-format=raw" "--no-progress" "--no-interaction"))
253290
(path (phpstan-normalize-path (phpstan-get-config-file)))
291+
(autoload (phpstan-get-autoload-file))
254292
(level (phpstan-get-level)))
255-
(when path
256-
(setq args (append args (list "-c" path))))
257-
(when level
258-
(setq args (append args (list "-l" level))))
259-
(append executable args)))
293+
(append executable
294+
(list "analyze" "--error-format=raw" "--no-progress" "--no-interaction")
295+
(and path (list "-c" path))
296+
(and autoload (list "-a" autoload))
297+
(and level (list "-l" level))
298+
(list "--"))))
260299

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

0 commit comments

Comments
 (0)