Identify unused PHP variables (in Emacs)?

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?

+4
source share
4 answers

Because Flymake uses the php ( -l ) binary syntax check option to highlight parsing errors, there is no obvious way to catch notifications and other errors without running or lexical code analysis. If it is not a problem not only to delay, but also to execute your script, then you can do the following.

Unfortunately, flymake-php defines error line patterns as persistent (at least in the kit that comes with the Emacs Starter Kit), and even the flymake command is hard-coded. There are several ways to achieve our goal, and each is pain. Maybe this is a quick and not very dirty solution for defining 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 configure flymake-allowed-php-file-name-masks to use the my-flymake-php-init function to initialize flymake-php instead of the original one. And so it works:


(source: flickr.com )

+3
source

This is not the answer to your question, since it does not work in emacs, but PHP can generate notifications when you try to read from a variable that has not been initialized.

For more information see:

  • error_reporting , which should include E_NOTICE
  • display_errors to display these notifications (and other errors)
    • which is useful in developing
    • but should not be enabled on the production server.


For example, with the error_reporting message to tell E_NOTICE (and others) the following code:

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

Calls this notification:

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

It's not as simple as getting into your editor, I admit, but it will still help you figure out why something is not working; -)

+5
source

A bit of a late answer, but I detailed how to connect Emacs Flymake using PHP_CodeSniffer and the PHP_CodeSniffer-VariableAnalysis plugin, which adds warnings about unused variables (and undefined variable warnings) to 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, I am a little inclined to think that it works well, but I regularly use it for work, and it does the trick.

This method works by static analysis of the source, and not to run the program, which is useful if you want to avoid unintended side effects ...

+1
source

A decent IDE will give you variable names in the current scope when you enter them through Intellisense.

This should seriously reduce the number of times you missed the variable name. It also allows your variable names to be more descriptive than $foo


In addition, you should always consider the warnings of undefined variables, since they immediately tell you when you made a spelling mistake.

-4
source

Source: https://habr.com/ru/post/1305377/


All Articles