1.2.280. Could Make A Function

When a function is called across the code with the same arguments often enough, it should be turned into a local API.

This approach is similar to turning literals into constants : it centralize the value, it helps refactoring by updating it. It also makes the code more readable. Moreover, it often highlight common grounds between remote code locations.

The analysis looks for functions calls, and checks the arguments. When the calls occurs more than 4 times, it is reported.

<?php

// str_replace is used to clean '&' from strings.
// It should be upgraded to a central function
function foo($arg ) {
    $arg = str_replace('&', '', $arg);
    // do something with $arg
}

class y {
    function bar($database ) {
        $value = $database->queryName();
        $value = str_replace('&', '', $value);
        // $value = removeAmpersand($value);
        // do something with $arg2
    }
}

// helper function
function removeAmpersand($string) {
    return str_replace('&', '', $string);
}

?>

Name

Default

Type

Description

centralizeThreshold

8

integer

Minimal number of calls of the function with one common argument.

See also Don’t repeat yourself (DRY).

1.2.280.1. Suggestions

  • Create a constant for common pieces of data

  • Create a function based on context-free repeated elements

  • Create a class based on repeated elements with dependent values

1.2.280.2. Specs

Short name

Functions/CouldCentralize

Rulesets

All, Analyze, Changed Behavior, Suggestions

Exakat since

0.11.6

PHP Version

All

Severity

Minor

Time To Fix

Slow (1 hour)

Precision

Very high

Features

class

Available in

Entreprise Edition, Exakat Cloud