Search
Blade Parser

Blade Parser

The Validate Command

The Blade Parser library provides a blade:validate Artisan command that checks all the Blade template files within the current project.

#Configuring the Validate Command

There are multiple configuration options available for the validate command. To customize the options you will need to publish the configuration file using the following Artisan command:

1php artisan vendor:publish --tag=blade

Once published, you will find a new configuration file at config/blade/validation.php. This configuration file contains the following entries:

  • core_validators: A list of the core validators that will be included when the validate:blade command is invoked. You may comment out/remove entries from this list to disable individual validators.

  • phpstan: This option allows you to disable the phpstan integration. Set this value to false to not run PHPStan or Larastan on the compiled output when validate:blade is invoked.

  • ignore_directives: Some core validators all you to exclude specific directive names from being validated. You can add directive names (without the leading @) to this list to have them ignored by all core validators that supported it.

  • custom_directives: Some validators, such as the debug directive validator, require a known list of validators that meet specific criteria to function. You can add directives to this list (without the leading @) to include them in all core validators that support this configuration item.

  • options: This entry contains a mapping of all core validators and the available options. Consult the specific configuration section for each core validator to learn more.

#PHPStan/Larastan Integration

If you'd like to automatically run Larastan results on the compiled output of Blade templates when running the blade:validate command, you simply need to install it via Composer and ensure the blade.validation.phpstan configuration option is set to true:

1composer require larastan/larastan:^2.7 --dev

#Running the Validate Command

To perform Blade validation on your Laravel project, issue the following command at the root of your project:

1php artisan blade:validate

#Core Validators

The Blade Parser library provides many validation rules out of the box. You can learn more about each of them in the following sections.

#Directive Argument Spacing

Class: DirectiveArgumentSpacingValidator

The directive argument spacing validator will check that all directives containing arguments contain a certain amount of spaces between the directive name and its arguments.

The following template:

1@if ($something)
2 
3@endif

will cause the following validation message when configured with an expected spacing of 3:

1Expected 3 spaces after [@if], but found 2

#Unpaired Conditions

Class: UnpairedConditionValidator

The unpaired condition validator will emit validation errors if any condition directives (if, elseif, else, etc.) are not paired with another directive.

The following template:

1@if ($this)
2 
3@elseif ($that)

will emit the following validation messages:

1Unpaired condition [@if]
2Unpaired condition [@elseif]

#Empty Condition Expressions

Class: EmptyConditionValidator

The empty condition expressions validator emits a validation error when conditional directives do not contain an expression.

The following template:

1@if
2 
3@else
4 
5@endif

will emit the following validation message:

1Invalid empty expression for [@if]

#Duplicate Condition Expressions

Class: DuplicateConditionExpressionsValidator

The duplicate condition expressions validator may be used to detect trivial identical expressions.

The following template:

1@if ($this == 'that')
2 
3@elseif ($this == 'that')
4 
5@else
6 
7@endif

will produce the following validation message:

1Duplicate expression [$this == 'that'] in [@elseif]

#No Directive Arguments

Class: NoArgumentsValidator

The no directive arguments validator emits a validation error when directive's that should not contain arguments have arguments in the template.

The following template:

1@endphp ($args)

will emit the following validation message:

1@endphp should not have any arguments

#Required Directive Arguments

Class: RequiredArgumentsValidator

The required directive arguments validator emits a validation error when a directive that should contain arguments does not have any arguments in the template.

The following template:

1@extends

will emit the following validation message:

1Required arguments missing for [@extends]

#Inconsistent Directive Casing

Class: InconsistentDirectiveCasingValidator

The inconsistent directive casing validator emits a validation error when a directive's casing within the template does not match the directive's definition.

The following template:

1@ENDPHP

will emit the following validation message:

1Inconsistent casing for [@ENDPHP]; expecting [@endphp]

#Requires Open Directive

Class: RequiresOpenValidator

The requires open directive validator emits a validation error when a closing directive is not paired with an opening directive within the template.

The following template:

1@endfor

emits the following error message:

1Missing required open directive for [@endfor]

#Directive Spacing

Class: DirectiveSpacingValidator

The directive spacing validator emits a validation error when a directive does not have sufficient leading or trailing whitespace.

The following template:

1<span class="@if @endif"></span>

produces the following validation messages:

1Missing space before [@if]
2Missing space after [@endif]

#Directive Arguments Line Span

Class: DirectiveArgumentsSpanningLinesValidator

The line-span validator emits a validation error when the directive's arguments span more than a specified number of lines.

The following template:

1@if ($something
2 == $this &&
3 'this' == 'that')
4 
5@endif

produces the following error message when max_line_span is set to 2:

1Maximum line count exceeded for [@if] arguments; found 3 lines expecting a maximum of 2 lines

#Node Compilation

Class: NodeCompilationValidator

The node compilation validator checks the compiled output of echo statements and PHP blocks for any PHP syntax errors.

The following template:

1{{ $hello++++ }}
2 
3 
4 {{ $world+++ }}

produces these validation messages:

1Anticipated PHP compilation error: [syntax error, unexpected token "++", expecting ")"] near [{{ $hello++++ }}]
2Anticipated PHP compilation error: [syntax error, unexpected token ")"] near [{{ $world+++ }}]

#Debug Directives

Class: DebugDirectiveValidator

The debug directives validator emits a validation error if any debug directives appear within a template.

The following template:

1@dd($args)

would produce the following validation message:

1Debug directive [@dd] detected

#Recursive Includes Validator

Class: RecursiveIncludeValidator

The recursive includes validator will attempt to detect possible recursive @include directives.

The following template (with filename test.blade.php):

1@include('test')

produces the following validation error message:

1Possible infinite recursion detected near [@include('test')]

However, the following template would not produce a validation message:

1@if ($someCondition)
2 @include('test')
3@endif

#Inconsistent Directive Indentation

Class: InconsistentIndentationLevelValidator

The inconsistent directive spacing validator may be used to emit validation errors when a pair's opening and closing directives do not have the same indentation.

The following template:

1@if ($this)
2 
3 @endif

produces the validation message:

1Inconsistent indentation level of 8 for [@endif]; parent [@if] has a level of 0

#For Else Validator

Class: ForElseStructureValidator

The for else validator checks for many different types of issues within @forelse blocks, such as:

  • Too many @empty directives

  • Missing @empty directive

The following template:

1@forelse ($users as $user)
2 
3@empty
4@empty
5 
6@endforelse

produces the error message:

1Too many [@empty] directives inside [@forelse]

The validator can also detect missing @empty directives; the template:

1@forelse ($users as $user)
2 
3@endforelse

produces:

1Missing [@empty] directive inside [@forelse]

#Switch Statement Validator

Class: SwitchValidator

The switch statement validator checks for common issues within Blade switch statements, such as:

  • No @case directives were found within the @switch block

  • A @case statement is missing a @break

  • A @case statement has too many @break statements

  • A @switch block contains too many @default statements

The following templates:

1@switch ($var)
2 
3@endswitch
4 
5@switch ($var)
6 @case (1)
7 Something
8@endswitch
9 
10@switch ($var)
11 @case (1)
12 Something
13 @break
14 @break
15@endswitch
16 
17@switch ($var)
18 @case (1)
19 Something
20 @break
21 @default
22 One
23 @break
24 @default
25 Two
26 @break
27@endswitch

would produce the following validation messages:

1No case statements found in [@switch]
2Missing [@break] statement inside [@case]
3Too many [@break] statements inside [@case]
4Too many [@default] cases in [@switch]

#Component Parameter Spacing

Class: ComponentParameterNameSpacingValidator

The component parameter spacing validator may be used to detect extraneous whitespace between a parameter's name and its value.

The following template:

1<x-alert message = "The message" />

produces the following validation message:

1Invalid spacing between component parameter name/value near [message]

#Component Shorthand Variables

Class: ComponentShorthandVariableParameterValidator

The component shorthand variables validator will check for potential typos or unexpected values when working with shorthand variable syntax.

The following template:

1<x-profile $:message /> <x-profile :$message="message" />

produces the following validation messages:

1Potential typo in shorthand parameter variable [$:message]; did you mean [:$message]
2Unexpected value for shorthand parameter variable near [="message"]

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.