Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cask
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(source "melpa" "https://melpa.org/packages/")

(package-file "phpstan.el")
(package-file "phpstan-flycheck.el")
(development
(depends-on "flycheck")
(depends-on "php-mode"))
67 changes: 67 additions & 0 deletions phpstan-flycheck.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
;;; phpstan-flycheck.el --- Flycheck integration for PHPStan -*- lexical-binding: t; -*-

;; Copyright (C) 2018 Friends of Emacs-PHP development

;; Author: USAMI Kenta <tadsan@zonu.me>
;; Created: 15 Mar 2018
;; Version: 0.1.0
;; Keywords: convenience, php
;; Homepage: https://github.com/emacs-php/phpstan.el
;; Package-Requires: ((emacs "24") (flycheck "26"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Flycheck integration for PHPStan.

;;; Code:
(require 'flycheck)
(require 'phpstan)

;; Usually it is defined dynamically by flycheck
(defvar flycheck-phpstan-executable)

(defun phpstan-flycheck--enabled-and-set-variable ()
"Return path to phpstan configure file, and set buffer execute in side effect."
(let ((enabled (not (null (or phpstan-working-dir (phpstan-get-config-file))))))
(prog1 enabled
(when (and phpstan-flycheck-auto-set-executable
(not (and (boundp 'flycheck-phpstan-executable)
(symbol-value 'flycheck-phpstan-executable)))
(or (eq 'docker phpstan-executable)
(and (consp phpstan-executable)
(stringp (car phpstan-executable))
(listp (cdr phpstan-executable)))))
(set (make-local-variable 'flycheck-phpstan-executable)
(if (eq 'docker phpstan-executable)
phpstan-docker-executable
(car phpstan-executable)))))))

;;;###autoload
(flycheck-define-checker phpstan
"PHP static analyzer based on PHPStan."
:command ("php" (eval (phpstan-get-command-args))
(eval (phpstan-normalize-path
(flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace)
(flycheck-save-buffer-to-temp #'flycheck-temp-file-system))))
:working-directory (lambda (_) (phpstan-get-working-dir))
:enabled (lambda () (phpstan-flycheck--enabled-and-set-variable))
:error-patterns
((error line-start (1+ (not (any ":"))) ":" line ":" (message) line-end))
:modes (php-mode)
:next-checkers (php))

(provide 'phpstan-flycheck)
;;; phpstan-flycheck.el ends here
35 changes: 0 additions & 35 deletions phpstan.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

;;; Code:
(require 'php-project)
(require 'flycheck nil)


;; Variables:
Expand Down Expand Up @@ -116,9 +115,6 @@ max

(defconst phpstan-docker-executable "docker")

;; Usually it is defined dynamically by flycheck
(defvar flycheck-phpstan-executable)

;;;###autoload
(progn
(defvar phpstan-executable nil
Expand Down Expand Up @@ -166,22 +162,6 @@ NIL
if dir
return (expand-file-name name dir)))))

(defun phpstan-enabled-and-set-flycheck-variable ()
"Return path to phpstan configure file, and set buffer execute in side effect."
(let ((enabled (not (null (or phpstan-working-dir (phpstan-get-config-file))))))
(prog1 enabled
(when (and phpstan-flycheck-auto-set-executable
(not (and (boundp 'flycheck-phpstan-executable)
(symbol-value 'flycheck-phpstan-executable)))
(or (eq 'docker phpstan-executable)
(and (consp phpstan-executable)
(stringp (car phpstan-executable))
(listp (cdr phpstan-executable)))))
(set (make-local-variable 'flycheck-phpstan-executable)
(if (eq 'docker phpstan-executable)
phpstan-docker-executable
(car phpstan-executable)))))))

(defun phpstan-normalize-path (source-original &optional source)
"Return normalized source file path to pass by `SOURCE-ORIGINAL' OR `SOURCE'.

Expand Down Expand Up @@ -245,20 +225,5 @@ it returns the value of `SOURCE' as it is."
(setq args (append args (list "-l" level))))
(append executable args)))

;;;###autoload
(when (featurep 'flycheck)
(flycheck-define-checker phpstan
"PHP static analyzer based on PHPStan."
:command ("php" (eval (phpstan-get-command-args))
(eval (phpstan-normalize-path
(flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace)
(flycheck-save-buffer-to-temp #'flycheck-temp-file-system))))
:working-directory (lambda (_) (phpstan-get-working-dir))
:enabled (lambda () (phpstan-enabled-and-set-flycheck-variable))
:error-patterns
((error line-start (1+ (not (any ":"))) ":" line ":" (message) line-end))
:modes (php-mode)
:next-checkers (php)))

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