Skip to content

Commit 526f0b7

Browse files
committed
Add file local variables and getter functions
1 parent ba7dfc8 commit 526f0b7

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

phpstan.el

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,72 @@
2828
;; https://github.com/phpstan/phpstan
2929

3030
;;; Code:
31+
(require 'php-project)
3132
(require 'flycheck nil)
3233

34+
35+
;; Variables:
36+
37+
;;;###autoload
38+
(progn
39+
(defvar phpstan-configure-file nil)
40+
(make-variable-buffer-local 'phpstan-configure-file)
41+
(put 'phpstan-configure-file 'safe-local-variable
42+
#'(lambda (v) (if (consp v)
43+
(and (eq 'root (car v)) (stringp (cdr v)))
44+
(null v) (stringp v)))))
45+
46+
;;;###autoload
47+
(progn
48+
(defvar phpstan-level "0")
49+
(make-variable-buffer-local 'phpstan-level)
50+
(put 'phpstan-level 'safe-local-variable
51+
#'(lambda (v) (or (null v)
52+
(integerp v)
53+
(and (stringp v)
54+
(string-match-p "\\`[1-9][0-9]*\\'" v))))))
55+
56+
;;;###autoload
57+
(progn
58+
(defvar phpstan-executable nil)
59+
(make-variable-buffer-local 'phpstan-executable)
60+
(put 'phpstan-executable 'safe-local-variable
61+
#'(lambda (v) (if (consp v)
62+
(and (eq 'root (car v)) (stringp (cdr v)))
63+
(null v) (stringp v)))))
64+
65+
;; Functions:
66+
(defun phpstan-get-configure-file ()
67+
"Return path to phpstan configure file or `NIL'."
68+
(if phpstan-configure-file
69+
(if (and (consp phpstan-configure-file)
70+
(eq 'root (car phpstan-configure-file)))
71+
(expand-file-name (cdr phpstan-configure-file) (php-project-get-root-dir))
72+
phpstan-configure-file)
73+
(let ((dir (locate-dominating-file "phpstan.neon" default-directory)))
74+
(when dir
75+
(expand-file-name "phpstan.neon" dir)))))
76+
77+
(defun phpstan-get-level ()
78+
"Return path to phpstan configure file or `NIL'."
79+
(cond
80+
((null phpstan-level) "0")
81+
((integerp phpstan-level) (int-to-string phpstan-level))
82+
(t phpstan-level)))
83+
84+
(defun phpstan-get-executable ()
85+
"Return PHPStan excutable file."
86+
(let ((executable (or phpstan-executable '(root . "vendor/bin/phpstan"))))
87+
(when (and (consp executable)
88+
(eq 'root (car executable)))
89+
(setq executable
90+
(expand-file-name (cdr executable) (php-project-get-root-dir))))
91+
(if (file-exists-p executable)
92+
executable
93+
(if (executable-find "phpstan")
94+
"phpstan"
95+
(error "PHPStan executable not found")))))
96+
3397
;;;###autoload
3498
(when (featurep 'flycheck)
3599
(flycheck-define-checker phpstan-checker

0 commit comments

Comments
 (0)