PHP code example of deinte / laravel-blade-validator

1. Go to this page and download the library: Download deinte/laravel-blade-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

deinte / laravel-blade-validator example snippets


return [
    // Default paths to validate
    'paths' => [
        resource_path('views'),
    ],

    // Patterns to ignore
    'ignore' => [
        '**/vendor/**',
        '**/node_modules/**',
    ],

    // Enable/disable rules
    'rules' => [
        'directive-in-component-attribute' => true,
        'use-statement-in-php-block' => true,
        'raw-output-usage' => true,
        'unclosed-directive' => true,
        'inline-javascript' => true,
        'sensitive-data-exposure' => true,
        'deprecated-syntax' => true,
        'legacy-php-tags' => true,
    ],

    // Raw output settings
    'raw_output' => [
        'severity' => 'warning',
        'allowed_patterns' => [],
    ],

    // Sensitive data patterns
    'sensitive_patterns' => [
        'password', 'secret', 'token', 'key', 'credential',
        'bearerToken', 'api_key', 'apiKey', 'private',
    ],
];

use Deinte\BladeValidator\Facades\BladeValidator;

// Validate a single file
$result = BladeValidator::validateFile('/path/to/template.blade.php');

// Validate multiple files
$result = BladeValidator::validateFiles([
    '/path/to/template1.blade.php',
    '/path/to/template2.blade.php',
]);

// Validate a directory
$result = BladeValidator::validateDirectory(resource_path('views'));

// Check results
if (!$result->valid) {
    foreach ($result->errors as $error) {
        echo "{$error->file}:{$error->line} - [{$error->rule}] {$error->message}\n";
    }
}

use Deinte\BladeValidator\BladeValidator;

class MyController
{
    public function validateViews(BladeValidator $validator)
    {
        $result = $validator->validateDirectory(resource_path('views'));

        return response()->json($result->toArray());
    }
}



namespace App\BladeRules;

use Deinte\BladeValidator\Data\BladeValidationError;
use Deinte\BladeValidator\Rules\BladeRuleInterface;

class NoTodoComments implements BladeRuleInterface
{
    public function getName(): string
    {
        return 'no-todo-comments';
    }

    public function getDescription(): string
    {
        return 'Detects TODO comments in Blade templates.';
    }

    public function getDefaultSeverity(): string
    {
        return 'warning';
    }

    public function validate(string $content, string $filePath): array
    {
        $errors = [];

        if (preg_match_all('/\{\{--.*TODO.*--\}\}/i', $content, $matches, PREG_OFFSET_CAPTURE)) {
            foreach ($matches[0] as $match) {
                $line = substr_count(substr($content, 0, $match[1]), "\n") + 1;

                $errors[] = new BladeValidationError(
                    file: $filePath,
                    line: $line,
                    rule: $this->getName(),
                    message: 'TODO comment found in template.',
                    severity: $this->getDefaultSeverity(),
                );
            }
        }

        return $errors;
    }
}

use Deinte\BladeValidator\BladeValidator;
use App\BladeRules\NoTodoComments;

public function boot(BladeValidator $validator)
{
    $validator->addRule(new NoTodoComments());
}
bash
php artisan vendor:publish --tag="blade-validator-config"
bash
php artisan blade:validate
bash
php artisan blade:validate resources/views/components resources/views/layouts
bash
# JSON output for CI/CD
php artisan blade:validate --format=json

# GitHub Actions annotations
php artisan blade:validate --format=github

# Ignore patterns
php artisan blade:validate --ignore="**/cache/**" --ignore="**/vendor/**"

# Run only specific rules
php artisan blade:validate --rules=raw-output-usage,sensitive-data-exposure

# Exclude specific rules
php artisan blade:validate --exclude-rules=deprecated-syntax

# Show warnings and errors
php artisan blade:validate --severity=warning
blade
{{-- Flagged --}}
{{{ $variable }}}           {{-- Use {{ }} instead --}}
{{ e($variable) }}          {{-- Double escaping --}}
@else if($condition)        {{-- Use @elseif --}}
{{ str_limit($text, 100) }} {{-- Use Str::limit() --}}

{{-- Valid --}}
{{ $variable }}
@elseif($condition)
{{ Str::limit($text, 100) }}
bash
php artisan vendor:publish --tag="blade-validator-config"
yaml
blade-validation:
  script:
    - composer install
    - php artisan blade:validate --format=json
  only:
    changes:
      - "resources/views/**/*.blade.php"