daa / project-tools

项目开发工具

1.0.3 2015-03-08 13:34 UTC

This package is not auto-updated.

Last update: 2024-09-25 13:54:07 UTC


README

基于Composer的项目工具,用于自动化以下操作:

  • 检查PHP文件是否存在语法错误
  • 检查PHP文件是否符合PSR编码标准
  • 检查PHP文件是否存在可能的错误、未使用参数、代码不优化等问题
  • 确保所有项目测试都通过
  • 检查资源(js、css等)

安装

只需将daa/project-tools添加为Composer依赖项。

# composer.json

    "require-dev": {
        ...
        "daa/project-tools": "~1.0"
    }

用法

如果您使用git进行项目开发,请使用提供的脚本来配置自动检查。

# composer.json

    "scripts": {
        "post-update-cmd": "Project\\Script\\GitHooks::setup",
        "post-install-cmd": "Project\\Script\\GitHooks::setup"
    }

在执行composer update --dev之后,每次您进行git提交时,它将确保在处理提交之前没有错误、编码标准问题或失败的测试。

注意:在某些IDE中可能无法使用(例如,在Netbeans中忽略git钩子)。我倾向于使用命令行。

您可以通过修改.git/hooks/pre-commit中的$conf数组来配置预提交规则,默认值是

$conf = array(
    'excludeTests' => false,
    'codingStandard' => 'PSR2',
    'messRules' => 'controversial',
    ''
);

更高级的配置可以是

$conf = array(
    'excludeTests' => true,
    'codingStandard' => array('PSR2', 'symfony2'),
    'messRules' => array('controversial', 'codesize', 'unusedcode'),
    'customChecks' => array(
        array('cmd' => 'scss-lint', 'ext' => 'css'),
        array('cmd' => 'jscs --preset=jquery', 'ext' => 'js')
    )
);

如您所见,在这个例子中,我们为我们的资源添加了linters,但这些额外的检查可以是任何东西。

替代用法

如果您不使用git或者不想使用自动化钩子进行检查,您可以使用这些工具手动操作。

use Project\Tool\CodeQualityTool;

// check an entire composer project
$tool = new CodeQualityTool();
$tool->run();

// check an entire composer project but without executing tests
$tool = new CodeQualityTool();
$tool->excludeTests();
$tool->run();

// check a set of files
$files = array('file1.php', 'file2.php');
$tool = new CodeQualityTool($files);
$tool->run();

// check a set of files without executing tests
$files = array('file1.php', 'file2.php');
$tool = new CodeQualityTool($files, true);
$tool->run();

您还可以使用单个模块

use Project\Tool\Checker\SyntaxErrorChecker;
use Project\Tool\Checker\CodingStandardsChecker;

// example of how to use a checker to check whole project
$checker = new SyntaxErrorChecker($projectDir);
if (!$checker->check()) {
    throw new \Exception('There are syntax errors!');
}

// example of how to use a checker to check a set of files
$files = array('file1.php', 'file2.php');
$checker = new SyntaxErrorChecker($projectDir);
if (!$checker->check($files)) {
    throw new \Exception('There are syntax errors!');
}

有关更多信息,请参阅Hooks/git/pre-commit和Tools/CodeQualityTool.php。