tags:

views:

359

answers:

3

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?

A: 

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.

Chacha102
$foo was obviously just an example. A couple of days ago it took me around 10 minutes to understand I was using $submision_id instead of $submission_id. I'll ignore the "decent" provocation, just saying that autocompletion is available in Emacs too and that Emacs is free and works on Linux, while Visual Studio does not. BTW, shouldn't this be a comment rather than an answer? :)
Roberto Aloi
I've just seen your edit. http://xdebug.org/ is very cool to trace and debug function, too.I mean, there's nothing wrong with using Visual Studio. Just, I don't see the point of answering a question like "how do I do this with the X text editor" with an answer like: "Don't use the X editor. Y is better.". We all have our favourite IDE or text editor. The best is the one you're more productive with. Thanks anyway.
Roberto Aloi
+5  A: 

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 include E_NOTICE
  • display_errors to have those notices (and other errors) displayed
    • which is useful when developping
    • but should not be enabled on a production server.


For example, with error_reporting set to report E_NOTICE (and others), the following portion of code :

$aa = 'glop';
echo strlen($a);

Raises this notice :

Notice: Undefined variable: a in /.../temp/temp.php on line 5

It's not as simple as getting in your editor, I admit -- but will still help finding out why something doesn't work ;-)

Pascal MARTIN
I'll try it on monday and I'll let you know. It shouldn't be too complicated to add this to my flymake config. Thanks a lot.
Roberto Aloi
+2  A: 

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 our flymake-php-init function based on the original one.

(defun my-flymake-php-init ()
  ;; add a new error pattern to catch notices
  (add-to-list 'flymake-err-line-patterns
               '("\\(Notice\\): \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)"
                 3 4 nil 2))
  (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
         (local-file  (file-relative-name
                       temp-file
                       (file-name-directory buffer-file-name))))
    ;; here we removed the "-l" switch
    (list "php" (list "-f" local-file))))

Then customize flymake-allowed-php-file-name-masks to use my-flymake-php-init function for initializing flymake-php instead of the original one. And so it works:

Török Gábor
First of all, thanks a lot for this. I updated my question. Could you have a look to it when you have a sec?
Roberto Aloi