Skip to content

InfinityLoop ​

error on by default

Group: Probable bugs · PhpStorm name: InfinityLoopInspection

A method whose only statement calls the very same method on $this, self or static recurses unconditionally until the stack overflows — typically a getter/proxy that was meant to call a property, a parent method or a differently named method.

Example ​

php
<?php
class Settings
{
    private $theme;

    public function theme()
    {
        return $this->theme();
    }

    public static function boot($env)
    {
        static::boot($env);
    }

    protected function flush()
    {
        // delegate
        self::flush();
    }

    public function locale()
    {
        return $this->theme;
    }

    public function reload()
    {
        return parent::reload();
    }

    public function save()
    {
        return $this->Save();
    }

    public function sync()
    {
        return ($this->sync());
    }

    public function close($n)
    {
        if ($n > 0) {
            return $this->close($n - 1);
        }
    }
}

Reported:

  • line 8: Method calls itself unconditionally; this recursion never ends.
  • line 13: Method calls itself unconditionally; this recursion never ends.
  • line 19: Method calls itself unconditionally; this recursion never ends.
  • line 34: Method calls itself unconditionally; this recursion never ends.

Configure ​

In custos.json:

json
{
  "rules": {
    "InfinityLoop": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore InfinityLoop

/**
 * @noinspection InfinityLoopInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.