PHP Codesniffer: LF or CRLF?

Asked

Viewed 216 times

0

Hello

I’ve seen a question here about the end-of-line problem that PHP Codesniffer displays:

End of line character is invalid; expected "\n" but found "\r\n"

However, my doubt is a little different...

I use Windows, and VS Code leaves as default CRLF in all the files I create, and I know that to solve this problem that PHP Codesniffer informs, just switch to LF. And the big question is, does the fact that I’m switching to LF affect my codes in any way? taking into consideration that I use Windows and not Linux.

1 answer

2


It doesn’t hurt anything in Linux because the PHP interpreter knows what it needs, however I must point out that depending on how you set up GIT, it will sync according to the operating system, even if you manually switch all CRLF to LF, so to facilitate and avoid seeing such messages you could standardize using the file .editorconfig for all (modern) editors to use the same standard

Inside the document .editorconfig should look something like:

root = true

[*.{js,css,php,html}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

And for git create a file called .gitattributes and put something like:

* text=auto
* text eol=lf

In this case the texts will use as LF, because PHP runs as LF on Windows and Linux without problem, as I said the interpreter understands, on .editorconfig I put an adjustment to the files .md because markdown sometimes needs spaces until line breaks in some cases, configure that part as you wish:

charset = utf-8             # charset padrão que irá usar em seus arquivos
end_of_line = lf            # quebra de linha padrão
insert_final_newline = true # adicionar ou não linha vazia no final do documento
indent_style = space        # trocar tabs por espaços
indent_size = 4             # indentação com TAB se torna em 4 espaços (depende de indent_style)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.