PEAR PHP_CodeSniffer: End of line character is invalid

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

PEAR PHP_CodeSniffer: End of line character is invalid

Post 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
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post 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".
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post by jason »

File > Convert Line Delimeiers To > Unix
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post by jeff00seattle »

jason wrote:File > Convert Line Delimeiers To > Unix
That works! PHP_CodeSniffer likes it!

Thanks
treterpeter
Forum Newbie
Posts: 2
Joined: Tue Oct 30, 2012 8:33 am

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post by treterpeter »

hm,
my codesniffer would still not like it...
treterpeter
Forum Newbie
Posts: 2
Joined: Tue Oct 30, 2012 8:33 am

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post 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 :-)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post by Weirdan »

CodeSniffer checks the code according to the specified standard, so it's the standard you use that dictates the line endings.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post 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.
owenmck
Forum Newbie
Posts: 1
Joined: Wed Jun 17, 2015 11:31 pm

Re: PEAR PHP_CodeSniffer: End of line character is invalid

Post 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]
Post Reply