Is it somehow possible to identify unused variables in a PHP file in Emacs?
With other languages, this is possible by using tools such as flymake. I've already enabled Flymake to show syntax errors for my PHP files on the fly, but still it's frustrating that PHP logic errors are sometimes due to situations like:
<?php
$foo = whatever();
$bar = something($fo);
...
Note the typo on $foo
that will contribute to the developer's headache and to his exorbitant use of coffee.
UPDATE:
After the hints by Pascal and Gabor, I set in my php.ini:
error_reporting = E_ALL | E_STRICT
When I run php from command line, I'm now able to see the notice about 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'm currently using in my .emacs. It works perfectly fine with parse errors, but I'm still not able to match on the notices, though :(
;; FlyMake for Php (require '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've also tried Gabor's configuration. Same result. Fine with errors, bad with notices.
Please note that from command line, parse errors look like:
> php -r '$fo o = 3; echo $fo;' -l
PHP Parse error: syntax error, unexpected T_STRING in Command line code on line 1
I don't get why Notices are not matched. I've tried the regular expression separately and it seems to match correctly:
(search-forward-regexp "Notice: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)")
PHP Notice: Undefined variable: fo in Command line code on line 1
(C-x C-e
will jump to the end of the lines).
Finally, I disabled XDebug for now, since the notices 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 guess I should slightly change the regexp to match the multiline errors. Any hint about this?
Bit of a late answer, but I've detailed how to hook up Emacs Flymake with PHP_CodeSniffer and the PHP_CodeSniffer-VariableAnalysis plugin which adds unused variable warnings (and undefined variable warnings too) in this blog:
http://www.illusori.co.uk/perl/2011/07/25/perl_php_static_analysis_with_emacs_flymake.html
Being the author of various bits that glue it all together means I'm a little biased in thinking it works well, but I use it regularly for work and it does the trick.
This method works by static analysis of the source rather than running the program, which is useful if you want to avoid unintended side effects...
Since Flymake uses the
php
binary's syntax check option (-l
) for highlighting parse errors, there is no obvious way to catch notices and other errors without running or lexical parsing the code. If it's not a problem to not only lint but execute your script, then you can do the following.Unfortunately,
flymake-php
defines error line patterns as constant (at least in the bundle shipped with Emacs Starter Kit), and even the flymake command is hard-coded. There is a few ways to achieve our goal and each is a pain. May be it's a quick and not so dirty solution to define ourflymake-php-init
function based on the original one.Then customize
flymake-allowed-php-file-name-masks
to usemy-flymake-php-init
function for initializing flymake-php instead of the original one. And so it works:(source: flickr.com)
Decent IDE's will give you the names of the variables in the current scope when you are typing them via Intellisense.
This should severely cut down on the number of times you misspell a variable name. It also allows your variable names to be more descriptive than
$foo
Furthermore, you should always pay attention to undefined variable warnings, as they immediately tell you when you have made a misspelling.
It's not really an answer to your question, as it's not working in emacs, but PHP can raise notices, when you are trying to read from a variable that's not been initialized.
For more informations, see :
error_reporting
, which should includeE_NOTICE
display_errors
to have those notices (and other errors) displayedFor example, with error_reporting set to report E_NOTICE (and others), the following portion of code :
Raises this notice :
It's not as simple as getting in your editor, I admit -- but will still help finding out why something doesn't work ;-)