trovit/php-code-validator-bundle

此包已被弃用且不再维护。未建议替换包。

提供组织并执行PHP代码验证器的基本系统

v1.0.2 2017-01-25 18:22 UTC

This package is not auto-updated.

Last update: 2022-04-02 08:55:03 UTC


README

Build Status

Symfony 包,提供组织并执行PHP代码验证器的基本系统。

安装

步骤 1: 使用composer要求包

$ composer require trovit/php-validator-bundle "^1.0"

步骤 2: 启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Trovit\PhpCodeValidatorBundle\TrovitPhpCodeValidatorBundle(),
        // ...
    );
}

步骤 3: 配置包

目前只有2个参数可用

  • temporary_path (必需,字符串):临时文件应该创建的临时路径。这对于那些仅与文件系统一起工作的验证器库是必要的。

  • validator_services (字符串数组):每个字符串代表一个验证器服务的引用名称

  • php_cs_config (字符串数组):每个字符串代表PHP Code Sniffer中可用的配置之一。

示例(《除 temporary_path 外,所有默认设置》)

# app/config.yml
trovit_php_code_validator:
    temporary_path: "%kernel.cache_dir%/tmp/"
    validator_services:
      - 'trovit.php_code_validator.validators.parallel_lint_validator'
    php_cs_config:
        reports:
            json: ~
        verbosity: 0
        showProgress: false
        interactive: false
        cache: false
        showSources: true

使用

在您想要调用具有坏代码(例如语法错误)的 execute 方法的任何位置获取管理器服务。它将以 PhpCodeValidatorResult 对象返回错误。

使用php lint的示例(《PHP Parallel Lint》)

// src/AppBundle/Controller/DefaultController.php

$code = '<?php echo "hola ?>'; //missing "

// This will return a PhpCodeValidatorResult object wich contains an array of detected problems
$result = $this->get('trovit.php_code_validator.managers.validator_manager')->execute($code);

$result->hasErrors(); // will return true (hasWarnings() is also available if needed)

$result->getErrors(); //will return an array of PhpCodeValidatorProblem:

/*
    new PhpCodeValidatorProblem()
    ->setMessage('Unexpected end of file, expecting variable '.
        '(T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)')
    ->setErrorType(PhpCodeValidatorProblem::ERROR_TYPE)
    ->setLineNum(1)
    ->setColumnNum(null)
    ->setErrorName('Parallel Lint Error');
*/

有关更多信息,请参阅组件仓库