Is it possible to identify unused variables in a PHP file in Emacs?
With other languages, this is possible with tools such as flymake . I already turned on Flymake to display syntax errors for my PHP files on the fly, but still it upsets that PHP logical errors sometimes occur due to situations such as:
<?php $foo = whatever(); $bar = something($fo); ...
Pay attention to the typo on $ foo , which will contribute to the headache of the developer and his excessive use of coffee.
UPDATE:
After the prompts of Pascal and Gabor, I installed my php.ini:
error_reporting = E_ALL | E_STRICT
When I run php from the command line, now I can see the notification of the undefined variable (with or without the -l option):
> php -r '$foo = 3; echo $fo;' PHP Notice: Undefined variable: fo in Command line code on line 1 > php -r '$foo = 3; echo $fo;' -l PHP Notice: Undefined variable: fo in Command line code on line 1
This is what I am currently using in my .emacs. It works great with parsing errors, but I still can't match notifications :(
;; FlyMake for Php (requires "flymake")
(defun flymake-php-init () "Use php to check the syntax of the current file." (let* ((temp (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace)) (local (file-relative-name temp (file-name-directory buffer-file-name)))) (list "php" (list "-f" local "-l")))) (add-to-list 'flymake-err-line-patterns '("\\(Parse\\|Fatal\\) error: +\\(.*?\\) in \\(.*?\\) on line \\([0-9]+\\)$" 3 4 nil 2)) (add-to-list 'flymake-err-line-patterns '("Notice: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)" 2 3 nil 1)) (add-to-list 'flymake-allowed-file-name-masks '("\\.php$" flymake-php-init))
I also tried setting up Gabor. The same result. Perfect with bugs, bad with notifications.
Note that from the command line, analysis errors look like this:
> php -r '$fo o = 3; echo $fo;' -l PHP Parse error: syntax error, unexpected T_STRING in Command line code on line 1
I do not understand why the notifications do not match. I tried the regex separately and seemed to answer correctly:
(search-forward-regexp "Notice: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)") PHP Notice: Undefined variable: fo in Command line code on line 1
( Cx Ce will skip to the end of lines).
Finally, now I have disabled XDebug since notifications were originally reported as:
PHP Notice: Undefined variable: fo in Command line code on line 1 PHP Stack trace: PHP 1. {main}() Command line code:0
So, I think I need to slightly modify the regex to match multi-line errors. Any hint of this?