识别PHP未使用的变量(在Emacs中)? [英] Identifying PHP unused variables (in Emacs)?

查看:222
本文介绍了识别PHP未使用的变量(在Emacs中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



与其他语言一样,这可以通过使用诸如 的工具来实现> flymake 即可。
我已经启用了Flymake,即时显示我的PHP文件的语法错误,但仍然令人沮丧的是,PHP逻辑错误有时是由于以下情况:

 <?php 
$ foo = whatever();
$ bar = something($ fo);
...

请注意$ foo 这将有助于开发人员的头痛和过度使用咖啡。



更新:



在Pascal和Gabor提示后,我设置了我的php.ini:

  error_reporting = E_ALL | E_STRICT 

当我从命令行运行php时,我现在可以看到关于未定义的通知变量(有或没有-l选项):

 > php -r'$ foo = 3; echo $ fo;'
PHP注意:未定义的变量:fo在第1行的命令行代码中
> php -r'$ foo = 3; echo $ fo;'-l
PHP注意:未定义的变量:第一行的命令行代码中的fo

这是我目前在我的.emacs中使用的。尽管如此,我仍然无法在通知中进行匹配:(



;; FlyMake for Php
(require 'flymake)

 (defun flymake-php-init()
使用php检查当前的语法文件
(let *((temp(flymake-init-create-temp-buffer-copy'flymake-create-temp-inplace))
(local(file-relative-name temp -name))))$)
(列表php(列表-flocal-l))))

(add-列表'flymake-err-line-patterns
'(\\(Parse\\ | Fatal\\)error:+ \\(。*?\\)in \\(。*?\\)在线\\([0-9] + \\)$3 4 nil 2))

(add-在线\\\(。* \\)中列出'flymake-err-line-patterns
'(注意:\\(。* \\) \\([0-9] + \\)2 3 nil 1))

(add-to-list'flymake-allowed-file -name-masks'(\\.php $flymake-php-init))

我也尝试过Gabor的配置,同样的结果,错误,错误的通知。



请注意,从命令行,解析错误如下所示:

 > php -r'$ fo o = 3; echo $ fo;'-l 
PHP解析错误:语法错误,意外的T_STRING在第1行的命令行代码

我不明白为什么通告不匹配。我已经单独尝试了正则表达式,似乎正确匹配:

 (search-forward-regexp注意:\ \(。* \\)在\\(。* \\)行\\([0-9] + \\))

PHP注意:未定义的变量:在第1行的命令行代码中的fo

  PHP注意:未定义的变量:第1行命令行代码中的fo 
PHP堆栈跟踪:
PHP 1. {main}()命令行代码:0

所以,我想我应该稍微更改正则表达式以匹配多行错误。任何关于这个的提示?

解决方案

由于Flymake使用 php 检查选项( -l <​​/ code>)用于突出显示解析错误,没有明显的方法来捕获通知和其他错误,而不运行或词法解析代码。如果不仅仅是lint而是执行你的脚本不是问题,那么你可以执行以下操作。



不幸的是, flymake-php 将错误行模式定义为常数(至少在Emacs入门工具包附带的包中),甚至flymake命令都是硬编码的。有几种方法来实现我们的目标,每一种都是一种痛苦。可以根据原来的方式定义我们的 flymake-php-init 函数,这是一个很快而不脏的解决方案。

 (defun my-flymake-php-init()
;;添加一个新的错误模式来捕获通知
(add-to-list'flymake- \\(。* \\)中的err-line-patterns
'(\\(Notice\\))\\(。* \\)行\\([0-9] + \\)
3 4 nil 2))
(let *((temp-file(flymake-init-create-temp-buffer -copy
'flymake-create-temp-inplace))
(本地文件(文件相对名称)
临时文件
(文件名目录缓冲区文件-name))
;;这里我们删除了-l开关
(列表php(列表-f本地文件)))

然后自定义 flymake-allowed-php-file-name-masks my-flymake-php-init 函数初始化flymake-php而不是原始的。所以它是有效的:




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?

解决方案

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:

这篇关于识别PHP未使用的变量(在Emacs中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆