PHP标准化之路(一):使用 EditorConfig 实现语法统一

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP标准化之路(一):使用 EditorConfig 实现语法统一脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

EditorConfig 有助于为跨越各种编辑器和 IDE 的同一项目的多个开发人员维护一致的编码样式。 EdITorconfig 项目由用于定义编码样式的文件格式和一组文本编辑器插件组成,这些插件使编辑器能够读取文件格式并遵循定义的样式。 EditorConfig 文件易于阅读并且可以与版本控制系统配合使用。

因此在团队项目里,我们可以用 EditorConfig 实现编码风格统一。除了 WordPress、Laravel、Symfony 等大型框架有自成一套的编码规范以外,我建议所有的 PHP 项目都应该遵循 PSR PHP 标准规范。目前 PSR 中与编码规范有关的项目有已定稿的 PSR-1 和 PSR-2,将来还会有正在草稿阶段的 PSR-12。现在我们只需要按照 PSR-2 来执行就可以了。在项目里我们可以在根目录建立一个 .editorconfig 文件,内容如下:

# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# PHP PSR-2 Coding Standards
# http://www.php-fig.org/psr/psr-2/

root = true

[*.php]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

然后到 EditorConfig 官网现在对应的 IDE 插件即可。如果你开发的是类似 Laravel 的包含多种语言的项目,可以参考下面的设置,对各种类型的文件单独设置编码规则。

# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# All PHP files MUST use the Unix LF (linefeed) line ending.
# Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.
# All PHP files MUST end with a single blank line.
# There MUST NOT be trailing whitespace at the end of non-blank lines.
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space

# PHP-Files, Composer.json, MD-Files
[{*.php,composer.json,*.md}]
indent_size = 4

# HTML-Files LESS-Files SASS-Files CSS-Files JS-Files JSON-Files
[{*.html,*.less,*.sass,*.css,*.js,*.json}]
indent_size = 4

[*.vue]


# Gitlab-CI, Travis-CI
[*.yml]
indent_style = space
indent_size = 2

脚本宝典总结

以上是脚本宝典为你收集整理的PHP标准化之路(一):使用 EditorConfig 实现语法统一全部内容,希望文章能够帮你解决PHP标准化之路(一):使用 EditorConfig 实现语法统一所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。