Skip to content

Generic/ForLoopWithTestFunctionCall: sniff doesn't handle calling a function using a variable variable properly #297

@rodrigoprimo

Description

@rodrigoprimo

Describe the bug

During the review of #235, @jrfnl noticed that the Generic.CodeAnalysis.ForLoopWithTestFunctionCall sniff doesn't trigger a warning when a function is called in the test part of the for loop using a variable variable. This is a valid way to call a function, so the warning should be triggered.

Code sample

<?php

for ($i = 0; $i < ${$functionName}(); $i++) {}

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above.
  2. Run phpcs --standard=Generic --sniffs=Generic.CodeAnalysis.ForLoopWithTestFunctionCall test.php
  3. PHPCS doesn't report any warnings.
$ bin/phpcs --standard=Generic --sniffs=Generic.CodeAnalysis.ForLoopWithTestFunctionCall test.php
$

Expected behavior

The sniff triggers the Avoid function calls in a FOR loop test part warning.

Versions (please complete the following information)

Operating System Ubuntu 23.04
PHP version 8.3
PHP_CodeSniffer version master
Standard Generic
Install type git clone

Please confirm:

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions