trovit / php-code-validator-bundle
Provides a basic system to organize and execute php code validators
Installs: 81
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 9
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- symfony/framework-bundle: ^2.8|^3.1
- trovit/php-code-validator: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.12
- matthiasnoback/symfony-config-test: ^2.0
- phpro/grumphp: ^0.9.5
- phpunit/phpunit: 5.7.*
- sebastian/phpcpd: ^2.0
- sensiolabs/security-checker: ^3.0
This package is not auto-updated.
Last update: 2022-04-02 08:55:03 UTC
README
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