Skip to content

[New Rule] Function/method call in for loop declaration MUST be avoided #28

Closed
@lenaorobei

Description

@lenaorobei

Rule

Function/method call in for loop declaration MUST be avoided.

Reason

PHP will call the method/function on each iteration of the for loop.

Bad Example:

for ($i = 0; $i < count($array); $i++){
    //code goes here
}

Good example:

$count = count($array);
for ($i = 0; $i < $count; $i++){
    //code goes here
}

Implementation

  • Subscribe to the T_FOR token.
  • Check if condition contains function call (T_STRING with followed by open parentheses).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions