使用PHP静态代码分析工具控制你的代码
How many times have you seen a code that seemed to be written in a rush? Multiple times, I believe. In some cases, it’s written by someone else, but pretty often your own creations, given enough time, make you think: “who the hell wrote this crap”? Keeping code clean ain’t easy and that’s why I’ve decided to present you with some tools, which can help you keep your PHP code clean. The time has come to brush up on static code analysis in PHP!
你有多少次看到一个似乎是匆忙写成的代码?多次,我相信。在某些情况下,它是由别人写的,但往往是你自己的创作,只要有足够的时间,就会让你觉得:"这到底是谁写的垃圾"?保持代码的清洁并不容易,这就是为什么我决定向你介绍一些工具,它们可以帮助你保持你的PHP代码清洁。现在是学习PHP静态代码分析的时候了。
Keeping code under control is one thing, but even worse is the fact that there are no universal standards of coding. Huge, open-source projects, frameworks and libraries apply their own programming standards. The problem has been noticed some time ago, and it led to the origins of PHP Standards Recommendations (PSR). It’s a document made jointly by the authors of the most popular PHP projects.
保持代码受控是一回事,但更糟糕的是没有通用的编码标准。巨大的开源项目、框架和库都应用他们自己的编程标准。这个问题在一段时间前就被注意到了,它导致了PHP标准建议(PSR)的产生。这是一份由最流行的PHP项目的作者共同编写的文件。
Good code vs bad code
好的代码与坏的代码
Even though the key recommendations (PSR-0 to PSR-4) have been acknowledged by the majority of the PHP community as a de facto standard, later attempts at unifying more common application elements often failed and so many people have left the project since, including the authors of such famous projects as Symfony, Laravel, Doctrine or Guzzle.
尽管关键的建议(PSR-0到PSR-4)已经被大多数的PHP社区承认为事实上的标准,但后来统一更多通用的应用元素的尝试常常失败,所以后来有很多人离开了这个项目,包括Symfony、Laravel、Doctrine或Guzzle等著名项目的作者。
The competing standards
相互竞争的标准
Developers also vary their standards of code quality and cleanliness. The lack of coding standards sanitization at the project level can lead to needless disputes during the Code Review.
开发人员对代码质量和清洁度的标准也各不相同。在项目层面上缺乏编码标准的净化,会导致在代码审查中出现不必要的纠纷。
Two types of people
两种类型的人
Maintaining a fairly good code quality isn’t an easy task. Below, I’ll try to show you a few static code analysis tools, which might hel...