cubesolutions/phpconsistent

该软件包已被放弃,不再维护。未建议替代软件包。

PHPConsistent 是一个动态和静态代码分析工具,用于验证代码中函数/方法调用与被调用函数/方法的内联文档(docblock)之间的一致性。

dev-master 2014-08-04 14:32 UTC

This package is not auto-updated.

Last update: 2022-10-15 05:44:29 UTC


README

免责声明:这是第一个版本。我知道代码可以大幅改进,但这正是pull requests的作用;-) - 欢迎任何反馈!

PHPConsistent 是一个动态和静态代码分析工具,用于验证代码中函数/方法调用与被调用函数/方法的内联文档(docblock)之间的一致性。目标是提高您代码以及您使用的库的代码质量。

  • 验证您的代码是否使用正确的参数和参数类型进行调用
  • 验证被调用函数/方法的内联文档(docblock)是否准确

它将比较

  • docblock中指定的参数类型 <-> 调用函数/方法时传递的参数类型
  • docblock中指定的参数数量 <-> 函数/方法定义中实际存在的参数数量
  • docblock中指定的参数名称 <-> 函数/方法定义中实际存在的参数名称

它将输出到

  • 文件
  • FirePHP(Firebug的插件)
  • PHPUnit,当作为PHPUnit的TestListener运行时(目前正在开发中)

示例输出

Invalid type calling SomeClass->GiveMeAnArray : parameter 3 ($somearray) should be of type array but got boolean instead : library/App.php (line 5)
Parameter names in function definition and docblock don't match when calling JustAnotherFunction : parameter 2 ($inputFilename) should be called $inputFile according to docblock : application/Bootstrap.php (line 214)
Parameter count in function definition and docblock don't match when calling OneMoreFunction : function has 6 but should be 5 according to docblock : application/Bootstrap.php (line 215)

这是否强制执行静态类型?

它不会在您的代码中强制执行静态类型,它只强制使用函数/方法开发者定义的类型调用函数/方法,这是常识。

要求

  • PHP 5.3 或更高版本
  • Xdebug 2.2.4 或更高版本
  • FirePHP Core 0.4.0 或更高版本(如果您想使用FirePHP报告)
  • PHPUnit 3.8 或更高版本(如果您想从单元测试中运行它)

性能

由于PHPConsistent需要Xdebug生成代码的完整跟踪,因此它创建了一个相当大的文件。然后它分析这个大文件。换句话说:它将您的代码速度降低5-20倍,因此绝对不应在生产环境中使用。如果您想使用PHPUnit集成,并且有很多单元测试,建议不要在本地机器上运行,而是在测试环境或持续集成平台上运行。

安装

通过Composer

{
    "require": {
        "cubesolutions/phpconsistent": "dev-master"
    }
}

##在您的引导文件中使用PHPConsistent 更多文档将在这里添加,但实际上只需要include_once('Main.php'),然后

$phpconsistent = new PHPConsistent_Main(
    null,    // Full path of the trace file - PHPConsistent normally takes care of this
    10,      // Maximum code depth to search - default = 10 calls deep
    false,    // Ignore null values as parameters - default = false
    <Reporting Type>,     // See Reporting section below
    <Reporting Location>, // See Reporting section below
    array(                // Array of file name strings to ignore (useful if you want to ignore an entire framework)
    ),
    array(                // Array of class name strings to ignore methods calls to
    ),
    array(                // Array of function strings to ignore calls to
    )
);
$phpconsistent->start();

示例:忽略null值,通过FirePHP报告10层深度,并忽略所有来自Zend Framework文件的调用

$phpconsistent = new PHPConsistent_Main(
    null,
    20,
    true,
    PHPConsistent_Main::LOG_TO_FIREPHP,
    null,
    array('Zend'),
    array(),
    array()
);
$phpconsistent->start();

##使用自动预置使用PHPConsistent

如果您不想接触任何现有的bootstrap代码,但仍然想使用PHPConsistent,您可以在php.ini(不推荐)中设置auto_prepend_file参数,或者您的Apache虚拟主机块或.htaccess文件。

要实现这一点,您需要在项目文件夹外创建一个文件(最好是放在某个集中位置,这样您可以使用PHPConsistent进行多个项目,同时只需安装一次)。此文件应按照上述说明加载PHPConsistent。然后,将auto_prepend_file参数指向您创建的文件。确保Apache有足够的权限访问此文件。

##重要提示 - 当FirePHP似乎无法正常工作时

如果您的代码使用ob_flush(),请注意PHPConsistent在启用FirePHP报告功能时使用ob_start()来启用输出缓冲。使用ob_flush()可能会将头部信息发送到浏览器,这意味着PHPConsistent不能再向FirePHP发送头部信息。如果您已开启display_errors,将会显示一个错误信息,提示头部信息已经被发送。如果关闭display_errors,则不会显示任何信息,FirePHP将不会报告任何内容。

##报告

  1. 到文件
  • 将'log'配置参数设置为PHPConsistent_Main::LOG_TO_FILE
  • 将'log_location'配置参数设置为要附加的文件路径
  1. 到FirePHP
  • 在Firefox中安装Firebug和FirePHP
  • 将'log'配置参数设置为PHPConsistent_Main::LOG_TO_FIREPHP
  1. 从/到PHPUnit(目前正在开发中)
  • 为您的单元测试设置PHPUnit
  • 添加到您的phpunit.xml中
   <listeners>
   
     <listener class="PHPConsistentTestListener" file="/optional/path/to/PHPConsistentTestListener.php">
     
       <arguments>
       
         <array>
         
           <element key="depth">
           
             <integer>10</integer>
             
           </element>
           
           <element key="ignorenull">
           
             <boolean>false</boolean>
             
           </element>
           
         </array>
         
       </arguments>
       
     </listener>
     
   </listeners>