-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable configure file #4
Changes from 9 commits
7bf0f23
ba7dfc8
526f0b7
968b8fe
c48251b
a5f1711
45ba779
bc56031
0825ac8
6fa077b
6840f6c
a198897
63fd9eb
4555592
d559ee1
05c9f76
70cb256
23d1860
dffcad7
3748533
4d53e60
56355b0
21cf970
8d63798
0508372
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/.cask | ||
/composer.lock | ||
/vendor |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "emacs-php/phpstan.el", | ||
"description": "Emacs interface to PHPStan", | ||
"license": "GPL-3.0-or-later", | ||
"require-dev": { | ||
"phpstan/phpstan": "dev-master" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,19 +28,85 @@ | |
;; https://github.com/phpstan/phpstan | ||
|
||
;;; Code: | ||
(require 'php-project) | ||
(require 'flycheck nil) | ||
|
||
|
||
;; Variables: | ||
|
||
;;;###autoload | ||
(progn | ||
(defvar phpstan-config-file nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flycheck also provides the |
||
(make-variable-buffer-local 'phpstan-config-file) | ||
(put 'phpstan-config-file 'safe-local-variable | ||
#'(lambda (v) (if (consp v) | ||
(and (eq 'root (car v)) (stringp (cdr v))) | ||
(null v) (stringp v))))) | ||
|
||
;;;###autoload | ||
(progn | ||
(defvar phpstan-level "0") | ||
(make-variable-buffer-local 'phpstan-level) | ||
(put 'phpstan-level 'safe-local-variable | ||
#'(lambda (v) (or (null v) | ||
(integerp v) | ||
(and (stringp v) | ||
(string-match-p "\\`[1-9][0-9]*\\'" v)))))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not match There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. It fixed. |
||
|
||
;;;###autoload | ||
(progn | ||
(defvar phpstan-executable nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flycheck defines a variable for this automatically. I guess the question is the same as for the variable above. |
||
(make-variable-buffer-local 'phpstan-executable) | ||
(put 'phpstan-executable 'safe-local-variable | ||
#'(lambda (v) (if (consp v) | ||
(and (eq 'root (car v)) (stringp (cdr v))) | ||
(null v) (stringp v))))) | ||
|
||
;; Functions: | ||
(defun phpstan-get-config-file () | ||
"Return path to phpstan configure file or `NIL'." | ||
(if phpstan-config-file | ||
(if (and (consp phpstan-config-file) | ||
(eq 'root (car phpstan-config-file))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not clear what the structure of |
||
(expand-file-name (cdr phpstan-config-file) (php-project-get-root-dir)) | ||
phpstan-config-file) | ||
(cl-loop for name in '("phpstan.neon" "phpstan.neon.dist") | ||
for file = nil | ||
for dir = (locate-dominating-file default-directory name) | ||
if dir | ||
return (expand-file-name name dir)))) | ||
|
||
(defun phpstan-get-level () | ||
"Return path to phpstan configure file or `NIL'." | ||
(cond | ||
((null phpstan-level) "0") | ||
((integerp phpstan-level) (int-to-string phpstan-level)) | ||
(t phpstan-level))) | ||
|
||
(defun phpstan-get-executable () | ||
"Return PHPStan excutable file." | ||
(let ((executable (or phpstan-executable '(root . "vendor/bin/phpstan")))) | ||
(when (and (consp executable) | ||
(eq 'root (car executable))) | ||
(setq executable | ||
(expand-file-name (cdr executable) (php-project-get-root-dir)))) | ||
(if (file-exists-p executable) | ||
executable | ||
(if (executable-find "phpstan") | ||
"phpstan" | ||
(error "PHPStan executable not found"))))) | ||
|
||
;;;###autoload | ||
(when (featurep 'flycheck) | ||
(flycheck-define-checker phpstan-checker | ||
(flycheck-define-checker phpstan | ||
"PHP static analyzer based on PHPStan." | ||
:command ("phpstan" | ||
"analyze" | ||
"--no-progress" | ||
"--errorFormat=raw" | ||
:command ("php" (eval (phpstan-get-executable)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a feature though? I run phpstan through docker so this would not work for me for example. And there are many people who do so as well: https://github.com/phpstan/docker-image There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. This problem was solved with the hack of |
||
"analyze" "--errorFormat=raw" "--no-progress" "--no-interaction" | ||
"-c" (eval (phpstan-get-config-file)) | ||
"-l" (eval (phpstan-get-level)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if these options eval to |
||
source) | ||
:working-directory (lambda (_) (php-project-get-root-dir)) | ||
:enabled (lambda () (locate-dominating-file "phpstan.neon" default-directory)) | ||
:enabled (lambda () (phpstan-get-config-file)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this still doesn't work if I don't use any configuration file at all. |
||
:error-patterns | ||
((error line-start (1+ (not (any ":"))) ":" line ":" (message) line-end)) | ||
:modes (php-mode) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# dummy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
foo(); | ||
f(); | ||
foo(); | ||
|
||
echo Fooo; | ||
echo FOO; | ||
|
||
// Local Variables: | ||
// phpstan-config-file: (root . "tests/phpstan.neon") | ||
// phpstan-level: 7 | ||
// End: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is not automated, but we can check flycheck's behavior manually. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
const FOO = 'Foo'; | ||
|
||
function foo() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parameters: | ||
bootstrap: %rootDir%/../../../tests/bootstrap.php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason of having these as variables instead of flycheck options? The flycheck macro expands to a defcustom so we can still change it manually just as a defvar but it probides good documentation and listing of options for users.