为什么根据CodeSniffer我的行尾仍然错误? [英] Why are my line-endings still wrong according to CodeSniffer?

查看:168
本文介绍了为什么根据CodeSniffer我的行尾仍然错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个git钩子,在提交之前检查我的代码样式.它将暂存的文件传递给CodeSniffer.我使用PSR-2代码样式,这意味着即使在Windows上,换行符也应为\n.但是,即使更改了PhpStorm设置和git设置,它仍然给我错误,换行符为\r\n.为什么会这样?

I made a git hook that checks my code style before commit. It passes the staged files to CodeSniffer. I use the PSR-2 code style which means newlines should be \n even on Windows. However even after changing PhpStorm settings and git settings it still gives me the error that the newlines are \r\n. Why does this happen?

PhpStorm
使用正则表达式搜索\r\n不会返回实例,因此我认为问题在于git.

PhpStorm
Searching for \r\n with regex on does not return instances, so I believe the problem to lie with git.

.gitconfig

[core]
    autocrlf = false
    editor = \"C:/Program Files (x86)/GitExtensions/GitExtensions.exe\" fileeditor
    eol = lf
[user]
    name = Thomas Moors
    email = thomas.moors@*****.nl
[merge]
    tool = kdiff3
[mergetool "kdiff3"]
    path = C:/Program Files (x86)/KDiff3/kdiff3.exe
[diff]
    guitool = kdiff3
[difftool "kdiff3"]
    path = C:/Program Files (x86)/KDiff3/kdiff3.exe

错误

转录:

FILE: ...\Thomas\Documents\example-live\laravel\app\Models\DoMdokUser.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 1 | ERROR | [x] End of line character is invalid; expected "\n" but
   |       |     found "\r\n"
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 105ms; Memory: 4Mb

Fix the error before commit please

编辑:使用其他编辑器(n ++),问题确实似乎与phpstorm有关.替换\ r \ n可解决此问题.那么为什么PHPStorm无法正常工作?

edit: using another editor (n++) the problem does seem to lie with phpstorm. Replacing \r\n fixes the problem. So why does PHPStorm not work properly?

推荐答案

使用regex搜索\r\n不会返回实例,因此我认为问题在于git.

Searching for \r\n with regex on does not return instances, so I believe the problem to lie with git.

不一定是git.

IDE(PhpStorm)使用标准化的行分隔符(\n/LF)将所有行存储在内存中,当您保存文件时,它将用文件打开时检测到的实际行分隔符替换它们.这也意味着,如果您碰巧在一个文件中有混合的行尾(例如CRLFLF)..保存后,它将仅使用一种样式(例如仅LF).

IDE (PhpStorm) stores all lines in memory with normalized line separator (which is \n / LF) and when you saving the file it replaces them with actual line separator symbol detected on file opening. This also means that if you happen to have mixed line endings in one file (e.g. both CRLF and LF) .. after save it will use only one style (e.g. only LF).

归一化的行尾允许您以更简单的方式执行正则表达式搜索/替换-无需担心实际符号是什么.

Normalized line ending allows you to execute your regex searches/replaces in a bit more simpler way -- no need to worry about what the actual symbol is.

现在-Code Style设置页面-如您在该字段下方的提示中看到的那样,它说:应用于新文件".这意味着此设置不会以任何方式影响现有文件.

Now -- the Code Style settings page -- as you can see in that hint below the field, it says: "Applied to new files". This means that this setting does NOT affect existing files in any way.

要更改特定现有文件的行尾::打开文件,然后通过状态栏中的相应部分..或通过File | Line Separators

To change line ending for a particular existing file: open the file and either change it via appropriate section in Status Bar .. or via File | Line Separators

那为什么PhpStorm无法正常工作?

So why does PhpStorm not work properly?

它正常工作-您碰巧不知道如何更改行分隔符样式.

It works properly -- you just happen to not know how to change the line separator style.

这篇关于为什么根据CodeSniffer我的行尾仍然错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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