资讯专栏INFORMATION COLUMN

Laravel学习笔记之Code Style

Sourcelink / 1100人阅读

摘要:在合作开发时需要统一下,如给写时一些如得按照字母顺序来写,等等。推荐一个,该是作者写的,质量有保证。每一个的定义可以参考的主页。

在合作开发时需要统一下code style,如给method写annotation时一些tag如@params ,@throw,@return得按照字母顺序来写,等等。推荐一个package:friendsofphp/php-cs-fixer,该package是Symfony作者写的,质量有保证。
安装下该package:

composer require friendsofphp/php-cs-fixer --dev

然后项目根目录创建一个可执行文件如.cs文件:

#!/bin/bash

vendor/bin/php-cs-fixer fix

以后在项目根目录只需执行./cs就可以自动修复不复合规定code style的代码,code rules的定义在该package packgist的网站上有说明。vendor/bin/php-cs-fixer fix会读取.php_cs文件返回的code rules,所以还得定义哪些code rules,同样在项目根目录中新建文件.php_cs,然后加上code rules,如:

exclude("bootstrap")
            ->exclude("database")
            ->exclude("public")
            ->exclude("resources")
            ->exclude("storage")
            ->exclude("vendor")
            ->notPath(".phpstorm.meta.php")
            ->notPath("_ide_helper.php")
            ->notPath("server.php")
            ->in(__DIR__);

return SymfonyCSConfig::create()
    ->level(SymfonyCSFixerInterface::PSR2_LEVEL)
    ->fixers([
    // Use all symfony fixers but the following "-"."fixer".

        // Exclude psr-0
        "-psr0",

        // Concatenation should be used without spaces.
        "-concat_without_spaces",

        // A return statement wishing to return nothing should be simply "return".
        "-empty_return",

        // Remove useless semicolon statements.
        "-no_empty_comment",

        // Binary operators should be surrounded by at least one space.
        "-operators_spaces",

        // Phpdocs annotation descriptions should not end with a full stop.
        "-phpdoc_annotation_without_dot",

        // @return void and @return null annotations should be omitted from phpdocs.
        "-phpdoc_no_empty_return",

        // @package and @subpackage annotations should be omitted from phpdocs.
        "-phpdoc_no_package",

        // All items of the @param, @throws, @return, @var, and @type phpdoc tags must be aligned vertically.
        "-phpdoc_params",

        // Annotations in phpdocs should be grouped together so that annotations of the same type immediately follow each other,
        // and annotations of a different type are separated by a single blank line.
        "-phpdoc_separation",

        // Phpdocs short descriptions should end in either a full stop, exclamation mark, or question mark.
        "-phpdoc_short_description",

        // Docblocks should only be used on structural elements.
        "-phpdoc_to_comment",

        // Pre incrementation/decrementation should be used if possible.
        "-pre_increment",

        // Unalign double arrow symbols.
        "-unalign_double_arrow",

        // Unalign equals symbols.
        "-unalign_equals",

    // Use all the following fixers.
        // Align double arrow symbols in consecutive lines.
        "align_double_arrow",

        // Align equals symbols in consecutive lines.
        "align_equals",

        // Concatenation should be used with at least one whitespace around.
        "concat_with_spaces",

        // Replace deprecated ereg regular expression functions with preg.
        "ereg_to_preg",

        // Add, replace or remove header comment.
        // "header_comment",

        // Multi-line whitespace before closing semicolon are prohibited.
        "multiline_spaces_before_semicolon",

        // Ensure there is no code on the same line as the PHP open tag.
        "newline_after_open_tag",

        // There should not be an empty return statement at the end of a function.
        "no_useless_return",

        // Ordering use statements.
        "ordered_use",

        // Convert PHP4-style constructors to __construct.
        "php4_constructor",

        // PHPUnit assertion method calls like "->assertSame(true, $foo)" should be written with dedicated method like "->assertTrue($foo)".
        "php_unit_construct",

        // PHPUnit methods like "assertSame" should be used instead of "assertEquals".
        "php_unit_strict",

        // Annotations in phpdocs should be ordered so that param annotations come first,
        // then throws annotations, then return annotations.
        "phpdoc_order",

        // PHP arrays should use the PHP 5.4 short-syntax.
        "short_array_syntax",

        // Replace short-echo finder($finder)
    ->setUsingCache(true);

code rules可以添加或删除,需要团队统一,一般至少尽量符合PSR-2的大部分标准。每一个code rule的定义可以参考package packgist的主页。

这样,团队里以后每次push code前先./cs下:

当然,还应当在PHPStorm IDE里的Preference->Editor->Code Style->PHP里也设置同样的code rules,然后导出一个xml文件,方便在团队里共享并导入到每个开发者的PHPStorm,这样保证团队的每一个Code Style保持相同。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22113.html

相关文章

  • Laravel学习笔记一-开发环境搭建

    摘要:配置需要一个来用于与虚拟机进行连接,默认假定这个密钥会被放在文件夹下。三使用管理项目版本使用可以对我们的代码进行版本控制,如果万一误删了代码想回到之前的情况,则可以通过版本控制进行回滚。配置选项代表对进行全局设置。 laravel学习笔记,重新梳理知识点。 一、环境配置 1、编辑器选用 Atom PHPStorm SublimeText Atom 是由 GitHub 官方在 201...

    Galence 评论0 收藏0
  • Laravel学习笔记Errors Tracking神器——Sentry

    摘要:中异常处理类主要包含两个方法和,其中就是主要用来向第三方发送异常报告,这里选择向这个神器发送异常报告,并使用通知开发人员。通过也能发现的执行流程。 说明:Laravel学习笔记之bootstrap源码解析中聊异常处理时提到过Sentry这个神器,并打算以后聊聊这款神器,本文主要就介绍这款Errors Tracking神器Sentry,Sentry官网有一句话个人觉得帅呆了: Stop ...

    xiguadada 评论0 收藏0
  • Laravel学习笔记三-前端工作流

    摘要:本节将学习是如何利用形成一套完整的前端工作流模式的。你也可以使用下面命令来强制安装所有模块,不管该模块之前是否安装过由于国内墙的原因,使用安装会非常缓慢,慢到想切,不过还好,我们可以使用淘宝提供的国内镜像进行下载。 本节将学习 Laravel 是如何利用 Sass, NPM, Gulp形成一套完整的前端工作流模式的。 一、句法强大的样式表Sass Sass 是一种可用于编写CSS的语言...

    liuchengxu 评论0 收藏0
  • Laravel学习笔记bootstrap源码解析

    摘要:总结本文主要学习了启动时做的七步准备工作环境检测配置加载日志配置异常处理注册注册启动。 说明:Laravel在把Request通过管道Pipeline送入中间件Middleware和路由Router之前,还做了程序的启动Bootstrap工作,本文主要学习相关源码,看看Laravel启动程序做了哪些具体工作,并将个人的研究心得分享出来,希望对别人有所帮助。Laravel在入口index...

    xiaoxiaozi 评论0 收藏0
  • Laravel 学习笔记

    摘要:根据我自己的理解,适当的调整了顺序,对一些比较常用的功能做一些说明,可结合文档学习。 根据我自己的理解,适当的调整了顺序,对一些比较常用的功能做一些说明,可结合文档学习。Learning laravel: 准备Learning laravel: 创建项目Learning laravel: 路由Learning laravel: URLLearning laravel: 控制器Learn...

    Jeff 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<