Skip to content
On this page

ForEachLoopDeclaration

Verifies that there is a space between each condition of foreach loops.

Scope

Squiz.ControlStructures.ForEachLoopDeclaration

Description

There should be a space between each element of a foreach loop and the as keyword should be lowercase.

Properties

Property NameTypeDefaultAvailable Since
requiredSpacesAfterOpenint01.5.2
requiredSpacesBeforeCloseint01.5.2

Usage

xml
<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration">
    <properties>
        <property name="requiredSpacesAfterOpen" value="1" />
        <property name="requiredSpacesBeforeClose" value="1" />
    </properties>
</rule>

Examples

Example 1

Valid: Correct spacing used.

php
foreach ($foo as $bar => $baz) {
    echo $baz;
}

Invalid: Invalid spacing used.

php
foreach ( $foo  as  $bar=>$baz ) {
    echo $baz;
}

Example 2

Valid: Lowercase as keyword.

php
foreach ($foo as $bar => $baz) {
    echo $baz;
}

Invalid: Uppercase as keyword.

php
foreach ($foo AS $bar => $baz) {
    echo $baz;
}

Released under the MIT License.