Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php-phpcs throws error on new file creation #907

Closed
shanavas786 opened this issue Mar 13, 2016 · 14 comments
Closed

php-phpcs throws error on new file creation #907

shanavas786 opened this issue Mar 13, 2016 · 14 comments

Comments

@shanavas786
Copy link

In global-flycheck-mode, when a new php file is created, it throws the following error.

Suspicious state from syntax checker php-phpcs: Checker php-phpcs returned non-zero exit code 2, but no errors from output: ERROR: You must supply at least one file or directory to process.

Usage: phpcs [-nwlsaepvi] [-d key[=value]]
    [--report=<report>] [--report-file=<reportfile>] [--report-<report>=<reportfile>] ...

Emacs version 24.4.1
Flycheck version 0.26

@swsnr
Copy link
Contributor

swsnr commented Mar 13, 2016

Would you please tell me your phpcs version?

@shanavas786
Copy link
Author

PHP_CodeSniffer version 1.5.5 (stable) by Squiz (http://www.squiz.net)

@swsnr
Copy link
Contributor

swsnr commented Mar 14, 2016

That is a very old version of phpcs. Please check whether the issue still exists with the latest release of phpcs.

@shanavas786
Copy link
Author

Upgraded to PHP_CodeSniffer version 2.5.1 (stable) by Squiz (http://www.squiz.net). The issue is still remaining.
out

@shanavas786
Copy link
Author

accidentally closed.

@swsnr
Copy link
Contributor

swsnr commented Mar 22, 2016

@shanavas786 Does this also happen when the buffer has contents?

@shanavas786
Copy link
Author

No, works fine with existing files. It is the error thrown by phpcs command when run on a non-existing file. I think flycheck has to ignore the error.

@swsnr
Copy link
Contributor

swsnr commented Mar 23, 2016

Would you mind to run the following commands and paste the entire output here?

$ touch empty.php
$ phpcs --report=checkstyle < empty.php

@shanavas786
Copy link
Author

➜ touch empty.php
➜ phpcs --report=checkstyle < empty.php
ERROR: You must supply at least one file or directory to process.

Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors]
    [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...
    [--report-width=<reportWidth>] [--generator=<generator>] [--tab-width=<tabWidth>]
    [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]
    [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]
    [--standard=<standard>] [--sniffs=<sniffs>] [--encoding=<encoding>]
    [--extensions=<extensions>] [--ignore=<patterns>] [--bootstrap=<bootstrap>] <file> ...
                      Set runtime value (see --config-set) 
        -n            Do not print warnings (shortcut for --warning-severity=0)
.................  

➜ phpcs --report=checkstyle  empty.php 
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="2.5.1">
<file name="/home/shanavas/test/empty.php">
 <error line="1" column="1" severity="warning" message="No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them." source="Internal.NoCodeFound"/>
</file>
</checkstyle>

What I understood is, Emacs doesn't create a new file on find-file newfile. The file is created only when the buffer is saved. So running phpcs on newly created file runs the command on a no-existing file and hence the error.

@swsnr swsnr closed this as completed in ba6ae06 Mar 24, 2016
@swsnr
Copy link
Contributor

swsnr commented Mar 24, 2016

@shanavas786 Flycheck does not give file names to phpcs. As you can see in the syntax checker definition we pass the contents of the buffer to standard input of phpcs. Unfortunately, though, while phpcs seems to work fine on an empty file it appears to choke on empty standard input.

I changed Flycheck to skip phpcs if the buffer is empty to workaround this issue, but I'd argue that it's actually a bug in phpcs.

@twlz0ne
Copy link

twlz0ne commented Feb 28, 2017

Seems issue is back.

(emacs-version) ;; => "GNU Emacs 25.2.1 (x86_64-apple-darwin15.6.0) of 2017-02-27"
(flycheck-version) ;; => "31snapshot (package: 20170223.247)"
(shell-command-to-string "phpcs --version") ;; => "PHP_CodeSniffer version 2.8.0 (stable) by Squiz (http://www.squiz.net)"

@cpitclaudel
Copy link
Member

@twlz0ne Can you provide the same details as above, in a new issue? Thanks!

@twlz0ne
Copy link

twlz0ne commented Mar 5, 2017

@cpitclaudel After confirmation, I found that the problem was caused by org-mode. By pressing <sTABphpC-c ', i got a new buffer which size is 1:

emacs-org-mode_empty-buffer-size-1

Than i create an advice to fix it:

(defun around/flycheck-buffer-empty-p (f &rest ARG)
  (let ((n (apply 'buffer-size ARG)))
    (cond ((= n 0) t)
          ((= n 1)
           (let ((matched (string-match "\\`\\*Org Src .* ]\\*\\'" (apply 'buffer-name ARG))))
             (if (null matched)
                 nil
               (zerop matched))))
          (t nil))))

(advice-add 'flycheck-buffer-empty-p :around #'around/flycheck-buffer-empty-p)

Maybe the better way is:

(devar minimal-size 5)
(defun flycheck-buffer-empty-p (&optional buffer)
  (<= (buffer-size buffer) minimal-size))

Although it is not a flycheck issue, but it would be nice if there were an option to decide when to start check.

@cpitclaudel
Copy link
Member

Interesting, thanks. Does the size matter, or is it just a problem with fully blank buffers?
Also, it seems that org-mode sets buffer-file-name to "[*Org Src <file name here>[ php ]*]" for buffers created with C-'. Isn't that wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants