diff --git a/Cask b/Cask index 3c10a5e..d5a854a 100644 --- a/Cask +++ b/Cask @@ -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")) diff --git a/phpstan-flycheck.el b/phpstan-flycheck.el new file mode 100644 index 0000000..9c1ea87 --- /dev/null +++ b/phpstan-flycheck.el @@ -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 +;; 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 . + +;;; 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 diff --git a/phpstan.el b/phpstan.el index de7986e..a16110e 100644 --- a/phpstan.el +++ b/phpstan.el @@ -29,7 +29,6 @@ ;;; Code: (require 'php-project) -(require 'flycheck nil) ;; Variables: @@ -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 @@ -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'. @@ -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