【问题标题】:PHPCS / PHPMD : Is there a PHP Code Sniffer / Mess Detector way to ensure there are docblocks? [duplicate]PHPCS / PHPMD:是否有 PHP Code Sniffer / Mess Detector 方法来确保有 docblocks? [复制]
【发布时间】:2017-03-06 18:37:20
【问题描述】:

有没有办法可以使用 PHP Code Sniffer 和/或 PHP Mess Detector 来检测我的类/属性/方法是否有正确的文档块?例如:

class Foo
{
    protected $bar;

    public function doStuff(){
        // ...
    }
}

上面的例子应该引起危险。但是,以下示例应该通过:

/**
 * Class Foo
 * @package Vendor\Module
 */
class Foo
{
    /**
     * @var Vendor\Module\Model\Bar
     */
    protected $bar;

    /**
     * This method does stuff
     * @return bool
     */
    public function doStuff(){
        // ...
    }
}

如果文档块正确(如果返回类型与返回的内容匹配),我对每个定义不感兴趣,我的意思是:如果它也这样做会很好,但我要采取的第一步是确保文档块存在。

【问题讨论】:

  • @LukasHajdu:我看了那个答案,但如果我没弄错的话,这个问题更多的是关于验证 docblock 中的内容是否有效,而不是检查 docblock 是否存在。如果我错了,请纠正我。
  • 我在 2017 年为重复的问题添加了 2 个新选项。检查它:stackoverflow.com/a/43722973/1348344

标签: php phpmd phpcs


【解决方案1】:

duplicated answer 的解决方案也适用于 docblock 存在性检查。

这是我的 Bar 类,它有 cmets:

<?php

namespace PhpCSTesting;

use stdClass;

/**
 * Class Bar
 */
class Bar
{
    /**
     * @var stdClass
     */
    protected $bar;

    /**
     * This method does stuff
     *
     * @return boolean
     */
    public function doStuff()
    {
        return true;
    }
}

当我运行嗅探器时,我没有收到任何错误:

bin/phpcs Bar.php --standard=Squiz --sniffs=Squiz.Commenting.FunctionComment,Squiz.Commenting.FunctionCommentThrowTag,Squiz.Commenting.ClassComment,Squiz.Commenting.VariableComment

这是我的 Foo 类,它没有 cmets:

<?php

namespace PhpCSTesting;

class Foo
{
    protected $bar;

    public function doStuff()
    {
        return true;
    }
}

但是,当我为这个类运行嗅探器时,我得到了错误:

bin/phpcs Foo.php --standard=Squiz --sniffs=Squiz.Commenting.FunctionComment,Squiz.Commenting.FunctionCommentThrowTag,Squiz.Commenting.ClassComment,Squiz.Commenting.VariableComment

FILE: /Users/lukas/workspace/Foo.php
----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
 5 | ERROR | Missing class doc comment
 7 | ERROR | Missing member variable doc comment
 9 | ERROR | Missing function doc comment
----------------------------------------------------------------------

您可以根据需要更新和修改规则集。

【讨论】:

    猜你喜欢
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2013-11-01
    • 2013-06-18
    • 1970-01-01
    • 2017-01-10
    • 2017-10-20
    相关资源
    最近更新 更多