Page 1 of 1

PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Thu Jan 07, 2010 6:53 pm
by jeff00seattle
Hi

I have another ERROR discovered by PEAR PHP_CodeSniffer, http://pear.php.net/package/PHP_CodeSniffer:
End of line character is invalid; expected "\n" but found "\r\n"

And this ERROR is only occurring at the very first line, at <?php, in my PHP code.

Code: Select all

<?php
/**
 * foo.php
 *
 */
 
?>
 
 
1. This ERROR appears only after validation is complete and there are no other ERROR or WARNING.
2. In editor viewing all hidden characters, there are no unusual end of line characters after the first line, [CR][LF], which is the same for all lines within my PHP code.

Anybody with an idea what this is about?

Thanks

Jeff in Seattle

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Thu Jan 07, 2010 7:10 pm
by flying_circus
I'm not sure why that would be an error, but maybe this is an opportunity for me to learn something as well?

Unix based OS's start a new line by \n while Microsoft Windows uses \r\n.

You can use the "find and replace" function of your editor to replace \r\n with \n, but make sure you check the check box for "regular expressions".

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Thu Jan 07, 2010 8:08 pm
by jeff00seattle
I am running PHP_CodeSniffer within the Windows environment, and it is expected for editor within the Windows environment to use for the end of line with "\r\n".

So, I am thinking this an error within PHP_CodeSniffer because it is expecting to validate PHP code within a UNIX environment, where the default end of line is "\n" .

So, within my IDE, Eclipse PDT, I cannot remove "\r\n" from my code, thereby I have no way of avoiding this error in Windows:
End of line character is invalid; expected "\n" but found "\r\n"

What do you think?

Jeff in Seattle

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Fri Jan 08, 2010 7:18 am
by jason
File > Convert Line Delimeiers To > Unix

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Fri Jan 08, 2010 8:36 am
by jeff00seattle
jason wrote:File > Convert Line Delimeiers To > Unix
That works! PHP_CodeSniffer likes it!

Thanks

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Tue Oct 30, 2012 8:36 am
by treterpeter
hm,
my codesniffer would still not like it...

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Tue Oct 30, 2012 9:08 am
by treterpeter
now I got it:
You need to *select* the line you want to convert to unix before you press "convert to unix".

My codesniffer is happy again :-)

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Tue Oct 30, 2012 9:15 am
by Weirdan
CodeSniffer checks the code according to the specified standard, so it's the standard you use that dictates the line endings.

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Tue Oct 30, 2012 2:43 pm
by requinix
Weirdan wrote:CodeSniffer checks the code according to the specified standard, so it's the standard you use that dictates the line endings.
...which is defined in the ruleset XML. Their example has

Code: Select all

 <!--
    Another useful example of changing sniff settings is
    to specify the end of line character that your standard
    should check for.
 -->
 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\r\n"/>
  </properties>
 </rule>
If you don't want to enforce that, and since CodeSniffer is really just a collection of rules to validate code against, then remove the rule definition.

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Posted: Wed Jun 17, 2015 11:35 pm
by owenmck
I found a remark in our corporate wiki which might help:
One method to get rid of this is to convert the file to *nix style line breaks. One implementation of this method uses awk:

[text]awk '{ sub("\r$", ""); print }' input.txt > output.txt

cp output.txt input.txt[/text]