php - 允许 SVN 提交现有的 PreCommit Hook 警告

标签 php svn coding-style phpunit pre-commit-hook

我正在使用 SVN 预提交 Hook 在能够提交之前验证我的代码标准 (PSR2)。这非常有效,只有一个异常(exception)。我的单元测试 (PHPUnit) 文件存在于我的引导类中,存在于所有静态单元测试函数中,但也在引导类定义之上启用错误消息。

PSR2 标准会在尝试提交时发出警告,因为您不能在包含类定义的文件中拥有不在类或函数中的代码。

有没有人有办法在我的代码嗅探器中排除这个错误,或者有办法使我的代码有效(无需将代码放入 Bootstrap 类的每个静态函数中以启用我的错误消息)?

这是文件:

<?php
namespace AlbumTest;

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);

class Bootstrap
{
    protected static $serviceManager;
    protected static $config;
    protected static $bootstrap;

    public static function init()
    {
        // Load the user-defined test configuration file, if it exists; otherwise, load
        if (is_readable(__DIR__ . '/TestConfig.php')) {
            $testConfig = include __DIR__ . '/TestConfig.php';
        } else {
            $testConfig = include __DIR__ . '/TestConfig.php.dist';
        }

        $zf2ModulePaths = array();

        if (isset($testConfig['module_listener_options']['module_paths'])) {
            $modulePaths = $testConfig['module_listener_options']['module_paths'];
            foreach ($modulePaths as $modulePath) {
                if (($path = static::findParentPath($modulePath)) ) {
                    $zf2ModulePaths[] = $path;
                }
            }
        }

        $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
        $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS')
                                                            ? ZF2_MODULES_TEST_PATHS : '');

        static::initAutoloader();

        // use ModuleManager to load this module and it's dependencies
        $baseConfig = array(
            'module_listener_options' => array(
                'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
            ),
        );

        $config = ArrayUtils::merge($baseConfig, $testConfig);

        $serviceManager = new ServiceManager(new ServiceManagerConfig());
        $serviceManager->setService('ApplicationConfig', $config);
        $serviceManager->get('ModuleManager')->loadModules();

        static::$serviceManager = $serviceManager;
        static::$config = $config;
    }

    public static function getServiceManager()
    {
        return static::$serviceManager;
    }

    public static function getConfig()
    {
        return static::$config;
    }

    protected static function initAutoloader()
    {
        $vendorPath = static::findParentPath('vendor');

        if (is_readable($vendorPath . '/autoload.php')) {
            $loader = include $vendorPath . '/autoload.php';
        } else {
            $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH')
                                          ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library')
                                          ? $vendorPath . '/ZF2/library' : false));

            if (!$zf2Path) {
                throw new RuntimeException(
                    'Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'
                );
            }

            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';

        }

        AutoloaderFactory::factory(
            array(
                'Zend\Loader\StandardAutoloader' => array(
                    'autoregister_zf' => true,
                    'namespaces' => array(
                        __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
                    ),
                ),
            )
        );
    }

    protected static function findParentPath($path)
    {
        $dir = __DIR__;
        $previousDir = '.';
        while (!is_dir($dir . '/' . $path)) {
            $dir = dirname($dir);
            if ($previousDir === $dir) {
                return false;
            }
            $previousDir = $dir;
        }
        return $dir . '/' . $path;
    }
}

Bootstrap::init();

最佳答案

你的 precommit-hook 是什么样子的?在发送到 Codesniffer 之前,您可以动态删除前几行吗? 另一种解决方案是在 Bootstrap 的初始化中设置 error_reporting

关于php - 允许 SVN 提交现有的 PreCommit Hook 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624539/

相关文章:

php - 避免重复预订/重复消费

svn - Visual SVN - 获取所有存储库的总大小

android - 动态改变 Activity 主题

javascript - 在 Javascript 中反对前导逗号的客观原因

php - 样式表未链接

php - 阈值在php代码中是什么意思

svn - "No changes found"在使用 hgsubversion 通过 TortoiseHg 推送到 SVN 服务器时发生

svn - 从 SVN 更新时是否可以始终覆盖本地更改?

php - 渐进式(增量)检查 PHP 样式

php - cakephp 2.0 中的内连接?