Git Product home page Git Product logo

php-code-validator-bundle's Introduction

Php Code Validator Bundle

Build Status

Symfony bundle which provides a basic system to organize and execute php code validators.

Installation

Step 1: Require bundle using composer

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

Step 2: Enable the bundle

<?php
// app/AppKernel.php

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

Step 3: Configure the bundle

There are only 2 parameters available at the moment:

  • temporary_path (required, string): temporary path where the temporary files should be created. This is necessary for those validator libraries that only works with filesystem.

  • validator_services (string[]): each string represents the reference name of a validator service

  • php_cs_config (string[]): each string represents one of the configurations available in PHP Code Sniffer.

Example (all defaults except 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

Usage

Get the manager service wherever you want to call the method execute with the bad code (syntax errors for example) as a parameter. It will return the errors in a PhpCodeValidatorResult object.

Example with a 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');
*/

For more information please see the component repo

php-code-validator-bundle's People

Contributors

poteirard avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.