1.2.675. Method Could Be Static

A method that doesn’t make any usage of $this could be turned into a static method.

While static methods are usually harder to handle, recognizing the static status is a first step before turning the method into a standalone function.

<?php

class foo {
    static $property = 1;

    // legit static method
    static function staticMethod() {
        return self::$property;
    }

    // This is not using $this, and could be static
    function nonStaticMethod() {
        return self::$property;
    }

    // This is not using $this nor self, could be a standalone function
    function nonStaticMethod() {
        return self::$property;
    }
}

?>

1.2.675.1. Suggestions

  • Make the method static

  • Make the method a standalone function

  • Make use of $this in the method : may be it was forgotten.

1.2.675.2. Specs

Short name

Classes/CouldBeStatic

Rulesets

All, Analyze, Class Review

Exakat since

1.5.7

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

static

Examples

FuelCMS, ExpressionEngine

Available in

Entreprise Edition, Exakat Cloud