Skip to content

Commit 5691ed5

Browse files
authored
Merge pull request #5 from emacs-php/split/phpstan-flycheck.el
Split phpstan-flycheck
2 parents b0665de + 04697b4 commit 5691ed5

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

Cask

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(source "melpa" "https://melpa.org/packages/")
33

44
(package-file "phpstan.el")
5+
(package-file "phpstan-flycheck.el")
56
(development
67
(depends-on "flycheck")
78
(depends-on "php-mode"))

phpstan-flycheck.el

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
;;; phpstan-flycheck.el --- Flycheck integration for PHPStan -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2018 Friends of Emacs-PHP development
4+
5+
;; Author: USAMI Kenta <tadsan@zonu.me>
6+
;; Created: 15 Mar 2018
7+
;; Version: 0.1.0
8+
;; Keywords: convenience, php
9+
;; Homepage: https://github.com/emacs-php/phpstan.el
10+
;; Package-Requires: ((emacs "24") (flycheck "26"))
11+
12+
;; This program is free software; you can redistribute it and/or modify
13+
;; it under the terms of the GNU General Public License as published by
14+
;; the Free Software Foundation, either version 3 of the License, or
15+
;; (at your option) any later version.
16+
17+
;; This program is distributed in the hope that it will be useful,
18+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
;; GNU General Public License for more details.
21+
22+
;; You should have received a copy of the GNU General Public License
23+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
24+
25+
;;; Commentary:
26+
27+
;; Flycheck integration for PHPStan.
28+
29+
;;; Code:
30+
(require 'flycheck)
31+
(require 'phpstan)
32+
33+
;; Usually it is defined dynamically by flycheck
34+
(defvar flycheck-phpstan-executable)
35+
36+
(defun phpstan-flycheck--enabled-and-set-variable ()
37+
"Return path to phpstan configure file, and set buffer execute in side effect."
38+
(let ((enabled (not (null (or phpstan-working-dir (phpstan-get-config-file))))))
39+
(prog1 enabled
40+
(when (and phpstan-flycheck-auto-set-executable
41+
(not (and (boundp 'flycheck-phpstan-executable)
42+
(symbol-value 'flycheck-phpstan-executable)))
43+
(or (eq 'docker phpstan-executable)
44+
(and (consp phpstan-executable)
45+
(stringp (car phpstan-executable))
46+
(listp (cdr phpstan-executable)))))
47+
(set (make-local-variable 'flycheck-phpstan-executable)
48+
(if (eq 'docker phpstan-executable)
49+
phpstan-docker-executable
50+
(car phpstan-executable)))))))
51+
52+
;;;###autoload
53+
(flycheck-define-checker phpstan
54+
"PHP static analyzer based on PHPStan."
55+
:command ("php" (eval (phpstan-get-command-args))
56+
(eval (phpstan-normalize-path
57+
(flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace)
58+
(flycheck-save-buffer-to-temp #'flycheck-temp-file-system))))
59+
:working-directory (lambda (_) (phpstan-get-working-dir))
60+
:enabled (lambda () (phpstan-flycheck--enabled-and-set-variable))
61+
:error-patterns
62+
((error line-start (1+ (not (any ":"))) ":" line ":" (message) line-end))
63+
:modes (php-mode)
64+
:next-checkers (php))
65+
66+
(provide 'phpstan-flycheck)
67+
;;; phpstan-flycheck.el ends here

phpstan.el

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
;;; Code:
3131
(require 'php-project)
32-
(require 'flycheck nil)
3332

3433

3534
;; Variables:
@@ -116,9 +115,6 @@ max
116115

117116
(defconst phpstan-docker-executable "docker")
118117

119-
;; Usually it is defined dynamically by flycheck
120-
(defvar flycheck-phpstan-executable)
121-
122118
;;;###autoload
123119
(progn
124120
(defvar phpstan-executable nil
@@ -166,22 +162,6 @@ NIL
166162
if dir
167163
return (expand-file-name name dir)))))
168164

169-
(defun phpstan-enabled-and-set-flycheck-variable ()
170-
"Return path to phpstan configure file, and set buffer execute in side effect."
171-
(let ((enabled (not (null (or phpstan-working-dir (phpstan-get-config-file))))))
172-
(prog1 enabled
173-
(when (and phpstan-flycheck-auto-set-executable
174-
(not (and (boundp 'flycheck-phpstan-executable)
175-
(symbol-value 'flycheck-phpstan-executable)))
176-
(or (eq 'docker phpstan-executable)
177-
(and (consp phpstan-executable)
178-
(stringp (car phpstan-executable))
179-
(listp (cdr phpstan-executable)))))
180-
(set (make-local-variable 'flycheck-phpstan-executable)
181-
(if (eq 'docker phpstan-executable)
182-
phpstan-docker-executable
183-
(car phpstan-executable)))))))
184-
185165
(defun phpstan-normalize-path (source-original &optional source)
186166
"Return normalized source file path to pass by `SOURCE-ORIGINAL' OR `SOURCE'.
187167
@@ -245,20 +225,5 @@ it returns the value of `SOURCE' as it is."
245225
(setq args (append args (list "-l" level))))
246226
(append executable args)))
247227

248-
;;;###autoload
249-
(when (featurep 'flycheck)
250-
(flycheck-define-checker phpstan
251-
"PHP static analyzer based on PHPStan."
252-
:command ("php" (eval (phpstan-get-command-args))
253-
(eval (phpstan-normalize-path
254-
(flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace)
255-
(flycheck-save-buffer-to-temp #'flycheck-temp-file-system))))
256-
:working-directory (lambda (_) (phpstan-get-working-dir))
257-
:enabled (lambda () (phpstan-enabled-and-set-flycheck-variable))
258-
:error-patterns
259-
((error line-start (1+ (not (any ":"))) ":" line ":" (message) line-end))
260-
:modes (php-mode)
261-
:next-checkers (php)))
262-
263228
(provide 'phpstan)
264229
;;; phpstan.el ends here

0 commit comments

Comments
 (0)