4. Cobblers

4.1. Introduction

Cobblers mend PHP code. They apply a transformation to it.

Cobblers are a complement to code analysis : the analysis spot code to be fixed, the cobbler mends the code. Later, the analysis doesn’t find those issues anymore.

4.2. List of Cobblers

4.2.1. Add Brackets To Single Instructions

This cobbler adds curly brackets to single expression, with for(), foreach(), while(); and do…while() instructions.

No brackets are added to instructions that are already bracketed.

4.2.1.1. Before

<?php

if ($a)
     $b = 1;
else {
     $c = ;2
}

?>

4.2.1.2. After

<?php

if ($a) {
     $b = 1;
} else {
     $c = ;2
}

?>

4.2.1.3. Reverse Cobbler

  • No anchor for Structures/RemoveBracketsAroundSingleInstruction.ini

4.2.1.4. Specs

Short Name Structures/AddBracketsToSingleInstructions
Exakat version 2.4.6
Available in Entreprise Edition, Exakat Cloud

4.2.2. Add Final Class

Adds final keyword to classes that can suppport it.

4.2.2.1. Before

<?php

class x {
    // this class is not extended, so it might be final
}

?>

4.2.2.2. After

<?php

final class x {
}

?>

4.2.2.3. Suggested Analysis

4.2.2.5. Reverse Cobbler

4.2.2.6. Specs

Short Name Classes/AddFinalClass
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.3. Add No Scream @

Adds the no scream operator @ to an expression.

4.2.3.1. Before

<?php
    $a;
?>

4.2.3.2. After

<?php
    @$a;
?>

4.2.3.3. Suggested Analysis

  • No anchor for Utils/Selector

4.2.3.4. Reverse Cobbler

4.2.3.5. Specs

Short Name Structures/AddNoScream
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.4. Array To Bracket

This cobbler updates the array() syntax, and changes it to the bracket syntax.

4.2.4.1. Before

<?php
$a = array(1, 2, 3);
?>

4.2.4.2. After

<?php
$a = [1, 2, 3];
?>

4.2.4.3. Specs

Short Name Structures/ArrayToBracket
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.5. Create Phpdoc

Create PHPdoc comments for classes, interfaces, traits, methods and functions.

Parameters and return types are collected, along with the name of the structure.

4.2.5.1. Before

<?php

class y {
    function a1(string $error, R $r = null) : int|string
    {

    }
?>

4.2.5.2. After

<?php

/**
 * Name : y
 */
class y {
   /**
    * Name : a1
    *
    * string $error
    * null|R $r
    * @return int|string
    *
    */
    function a1(string $error, R $r = null) : int|string
    {

    }
?>

4.2.5.3. Reverse Cobbler

  • No anchor for Attributes/RemovePhpdoc

4.2.5.4. Specs

Short Name Attributes/CreatePhpdoc
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.6. Gather Use Expression

Move lone use expression to the beginning of the file.

4.2.6.1. Before

<?php
    use A;
    ++$a;
    use B;
?>

4.2.6.2. After

<?php
    use A;
    use B;
    ++$a;
?>

4.2.6.3. Suggested Analysis

4.2.6.4. Specs

Short Name Namespaces/GatherUse
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.7. Make Static Closures And Arrow Functions

Add the static option to closures and arrow functions. This prevents the defining environment to be included in the closure.

4.2.7.1. Before

<?php
    $a = function () { return 1; };
    $b = fn () => 2;
?>

4.2.7.2. After

<?php
    $a = static function () { return 1; };
    $b = static fn () => 2;
?>

4.2.7.3. Suggested Analysis

4.2.7.4. Reverse Cobbler

  • No anchor for Functions/RemoveStaticFromFunction

4.2.7.5. Specs

Short Name Functions/MakeStaticFunction
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.8. Multiple cobbler

Allows to configure multiple cobbler in one file. The file is a YAML file, and must be located in the project’s folder.

The file containts a root object ‘cobbler’, filled with an array of cobblers, and their related configuration. Cobblers may be repeated as often as necessary.

cobblers: - Functions/RenameParameter:

oldName: $a newName: $b method: foo
  • Functions/RenameParameter:
    oldName: $a2 newName: $b method: foo2

The order of the configuration file is the order of execution. Do not rely on it.

4.2.8.1. Before


4.2.8.2. After


4.2.8.3. Parameters

Name Default Type Description
configFile   string The .yaml file in the project folder.

4.2.8.4. Specs

Short Name Utils/Multi
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.9. Plus One To Pre Plusplus

Transforms a + 1 or - 1 operation into a plus-plus (or minus-minus).

4.2.9.1. Before

<?php
    $a = $a + 1;
?>

4.2.9.2. After

<?php
    ++$a;
?>

4.2.9.3. Specs

Short Name Structures/PlusOneToPre
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.10. Post to Pre Plusplus

Transforms a post plus-plus (or minus-minus) operator, into a pre plus-plus (or minus-minus) operator.

4.2.10.1. Before

<?php
    $a++;
?>

4.2.10.2. After

<?php
    ++$a;
?>

4.2.10.3. Specs

Short Name Structures/PostToPre
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.11. Remove A Method In A Class

This removes a method in a class. The method name is provided with its fully qualified name : Name of the class:: name of the method.

The method’s name is a string.

4.2.11.1. Before

<?php

// removing method \x::method1
class x {
    function method1() {}
    function method2() {}
}

?>

4.2.11.2. After

<?php

// removed method \x::method1
class x {
    function method2() {}
}

?>

4.2.11.3. Parameters

Name Default Type Description
name x::method1 string Fully qualified name of the method to remove. Only one allowed.

4.2.11.4. Specs

Short Name Classes/RemoveMethod
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.12. Remove Abstract

Remove the abstract option, from classes and methods.

4.2.12.1. Before

<?php
abstract class x {
    function foo() {}

    abstract function moo() ;
}
?>

4.2.12.2. After

<?php
class x {
    function foo() {}

    function moo() {}
}
?>

4.2.12.3. Specs

Short Name Classes/RemoveAbstract
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.13. Remove Brackets Around Single Instruction

This cobbler removes brackets when they are not compulsory. This applies to single instruction, on for(), foreach(), while(), do…while() structures.

This also means that any refactoring that grows the instruction again to multiple instructions has to add the brackets again.

There is no gain in speed or code lenght by removing those brackets.

4.2.13.1. Before

<?php
     foreach($i = 0; $i < 10; ++$i) { $total += 1; }
?>

4.2.13.2. After

<?php
     foreach($i = 0; $i < 10; ++$i)  $total += 1;
?>

4.2.13.3. Reverse Cobbler

4.2.13.4. Specs

Short Name Structures/RemoveBracketsAroundSingleInstruction
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.14. Remove Dollar Curly

This cobbler transforms the ```` structure into {$ }. It is assumed that the content of the curly braces are only a variable name.

This update is important for PHP 8.2, where the syntax is deprecated.

4.2.14.1. Before

<?php

$a = ;

?>

4.2.14.2. After

<?php

$a = {$b};

?>

4.2.14.3. Specs

Short Name Structures/RemoveDollarCurly
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.15. Remove Final

This cobbler removes the final keyword on classes and methods.

4.2.15.1. Before

<?php

final class y {
    final function foo() {}
}

?>

4.2.15.2. After

<?php

class y {
    function foo() {}
}

?>

4.2.15.4. Reverse Cobbler

4.2.15.5. Specs

Short Name Classes/RemoveFinal
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.16. Remove Instructions

Removes atomic instructions from the code. The whole expression is removed, and the slot is closed.

This cobbler works with element of a block, and not with part of larger expression (like remove a condition in a if/then, or remove the block expression of a while).

4.2.16.1. Before

<?php
    $a = 1; // Code to be removed
    foo(1);

    do          // can remove the while expression
        ++$a;   // removing the block of the do...wihle will generate an compilation error
    while ($a < 10);

?>

4.2.16.2. After

<?php
    foo(1);
?>

4.2.16.3. Suggested Analysis

4.2.16.4. Specs

Short Name Structures/RemoveCode
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.17. Remove Noscream @

Removes the @ operator.

4.2.17.1. Before

<?php
    @$a;
?>

4.2.17.2. After

<?php
    $a;
?>

4.2.17.3. Suggested Analysis

4.2.17.4. Reverse Cobbler

  • This cobbler is its own reverse.

4.2.17.5. Specs

Short Name Structures/RemoveNoScream
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.18. Remove Parenthesis

Remove useless parenthesis from return expression.

4.2.18.1. Before

<?php
function foo() {
    return (1);
}
?>

4.2.18.2. After

<?php
function foo() {
    return 1;
}
?>

4.2.18.3. Suggested Analysis

4.2.18.4. Specs

Short Name Structures/RemoveParenthesis
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.19. Remove Readonly Option

Readonly is a property and class option. This cobbler removes it from both.

The readonly keyword is removed from property definitions, and from promoted properties.

4.2.19.1. Before

<?php

readonly class x {
    private readonly string $x;
}

?>

4.2.19.2. After

<?php

class x {
    private string $x;
}

?>

4.2.19.3. Suggested Analysis

4.2.19.4. Specs

Short Name Classes/RemoveReadonly
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.20. Remove Static From Closures And Arrow Functions

Removes the static option from closures and arrow functions.

4.2.20.1. Before

<?php
    $a = static function () { return 1; };
    $b = static fn () => 2;
?>

4.2.20.2. After

<?php
    $a = function () { return 1; };
    $b = fn () => 2;
?>

4.2.20.3. Suggested Analysis

4.2.20.4. Reverse Cobbler

4.2.20.5. Specs

Short Name Functions/RemoveStaticFromClosure
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.21. Remove The Attribute

Remove attributes from all supporting structures.

Attributes are located on functions, classes, class constants, properties, methods and arguments.

4.2.21.1. Before

<?php

#[Attribute]
function foo(#[AttributeArgument] $arg) {

}
?>

4.2.21.2. After

<?php


function foo($arg) {

}
?>

4.2.21.3. Specs

Short Name Attributes/RemoveAttribute
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.22. Remove Typehint

This cobbler remove the typehint mentions in the code. This might yield some speed when executing, since those tests will be not conveyed at runtime.

Typehints from arguments, method returns and properties are all removed.

4.2.22.1. Before

<?php

class x {
    private string $p;

    function foo(D\E $arg) : void {

    }
}

?>

4.2.22.2. After

<?php

class x {
    private $p;

    function foo($arg) {

    }
}

?>

4.2.22.3. Parameters

Name Default Type Description
type_to_remove all data A comma separated list of types to remove. For example : never,string,ABC;. Use ‘All’ for everyt type.

4.2.22.4. Suggested Analysis

4.2.22.5. Reverse Cobbler

4.2.22.6. Specs

Short Name Functions/RemoveTypes
Exakat version 2.2.5
Available in Entreprise Edition, Exakat Cloud

4.2.23. Remove Unused Use

Removes the unused use expression from the top of the file. Groupuse are not processed yet.

4.2.23.1. Before

<?php

use a\b;
use c\d;

new b();

?>

4.2.23.2. After

<?php

use a\b;

new b();

?>

4.2.23.3. Suggested Analysis

4.2.23.4. Specs

Short Name Namespaces/RemoveUse
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.24. Remove Visibility

Removes the visibility on constants, properties and methods.

For properties, the visibility is reset to public.

4.2.24.1. Before

<?php

class x {
    private const x = 1;
    private $p = 2;
    private function foo() {}
    private function __construct() {}
}
?>

4.2.24.2. After

<?php

class x {
    const x = 1;
    public $p = 2;
    function foo() {}
    function __construct() {}
}
?>

4.2.24.3. Specs

Short Name Classes/RemoveVisibility
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.25. Remove Written Only Variable

This removes variables that are written only.

4.2.25.1. Before

<?php

function foo() {
    $a = 1;
    $a += 2; // No usage of $a
}

?>

4.2.25.2. After

<?php

function foo() {
}

?>

4.2.25.3. Suggested Analysis

4.2.25.4. Specs

Short Name Structures/RemoveVariable
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.26. Rename A Function

Give a function with a new name.

This cobbler doesn’t update the name of the functioncalls.

This cobbler may be used with functions, and methods. Functions may be identified with their fully qualified name (i.e. pathfoo) and methods with the extended fully qualified name (i.e. : pathaClass::methodName).

4.2.26.1. Before

<?php
    function foo() {

    }
?>

4.2.26.2. After

<?php
    function bar() {

    }
?>

4.2.26.3. Parameters

Name Default Type Description
name foo string The new name of the function.

4.2.26.4. Suggested Analysis

  • No anchor for Utils/Selector

4.2.26.6. Reverse Cobbler

  • This cobbler is its own reverse.

4.2.26.7. Specs

Short Name Structures/RenameFunction
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.27. Rename FunctionCalls

Rename a function call to another function.

4.2.27.1. Before

<?php
    foo(1, 2);
?>

4.2.27.2. After

<?php
    bar(1, 2);
?>

4.2.27.3. Parameters

Name Default Type Description
origin strtolower string The function name to rename. It will be use lower-cased, and as a fully qualified name.
destination mb_strtolower string The function name to rename. It will be use as is. FQN is possible.

4.2.27.4. Suggested Analysis

  • No anchor for Utils/Selector

4.2.27.6. Reverse Cobbler

  • This cobbler is its own reverse.

4.2.27.7. Specs

Short Name Structures/RenameFunctionCall
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.28. Rename Methodcall

Rename a method, in a methodcall, with a new name.

This cobbler doesn’t update the definition of the method. It works both on static and non-static methods.

4.2.28.1. Before

<?php
    $o->method();
?>

4.2.28.2. After

<?php
    $o->newName();
?>

4.2.28.3. Parameters

Name Default Type Description
origin strtolower string The function name to rename. It will be use lower-cased, and as a fully qualified name.
destination mb_strtolower string The function name to rename. It will be use as is. FQN is possible.

4.2.28.4. Suggested Analysis

  • No anchor for Utils/Selector

4.2.28.6. Reverse Cobbler

  • No anchor for Structures/RemoveMethodCall

4.2.28.7. Specs

Short Name Structures/RenameMethodcall
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.29. Rename Parameter

Change the name of a parameter to a new name.

The destination parameter name is a constant. Suggestions : rename all parameters from the top method (in classes) rename parameters $a into $b (currently, no $a available)

Limits : this cobbler doesn’t check that another parameter is already using that name, nor if a local variable is also using that name. This may lead to unexpected results.

4.2.29.1. Before

<?php

foo(a: 1);

function foo($a) {
    return $a;
}

?>

4.2.29.2. After

<?php

foo(b: 1);

function foo($b) {
    return $b;
}

?>

4.2.29.3. Parameters

Name Default Type Description
oldName $A string The original name of the parameter.
newName $B string The new name of the parameter.
method   string The name of the target method. Use a full qualified name for a function, and the class name::method for methods.

4.2.29.4. Specs

Short Name Functions/RenameParameter
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.30. Set Null Type

Adds a Null type to typehints when necessary.

This cobbler only adds a null type when there is already another type. It doesn’t add a null type when no type is set.

It works on methods, functions, closures and arrow functions. It doesn’t work on properties.

The null type is added as a question mark ? when the type is unique, and as null when the types are multiple.

4.2.30.1. Before

<?php

function foo() : int {
    if (rand(0, 1)) {
        return 1;
    } else {
        return null;
    }
}

?>

4.2.30.2. After

<?php

function foo() : ?int {
    if (rand(0, 1)) {
        return 1;
    } else {
        return null;
    }
}

?>

4.2.30.3. Reverse Cobbler

4.2.30.4. Specs

Short Name Functions/SetNullType
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.31. Set Type Void

Adds the void typehint to functions and methods, when possible.

4.2.31.1. Before

<?php

function foo() {
    return;
}

?>

4.2.31.2. After

<?php

function foo() : void {
    return;
}

?>

4.2.31.3. Suggested Analysis

4.2.31.5. Reverse Cobbler

4.2.31.6. Specs

Short Name Functions/SetTypeVoid
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.32. Set Typehints

Automagically add scalar typehints to methods and properties. Arguments and return values are both supported.

When multiple possible types are identified, no typehint is added. If a typehint is already set, no typehint is added.

Magic methods, such as __get(), __set(), __construct(), __desctruct(), etc are not modified by this cobbler.

Methods which have parent’s methods (resp. children’s) are skipped for argument typing (resp return typing) : this may introduce a incompatible definition. On the other hand, methods which have children’s methods (resp. parents’) are modified for argument typing (resp return typing), thanks to covariance (resp. contravariance).

Void (as a scalar type) and Null types are processed in a separate cobbler.

By default, and in case of conflict, array is chosen over iterable and int is chosen over float. There are parameter to alter this behavior.

4.2.32.1. Before

<?php

class x {
    private int $p = 2;

    function foo(int $a = 1) : int {
        return intdiv($a, $this->p);
    }
}
?>

4.2.32.2. After

<?php

class x {
    private int $p = 2;

    function foo(int $a = 1) : int {
        return intdiv($a, $this->p);
    }
}
?>

4.2.32.3. Parameters

Name Default Type Description
array_or_iterable array string When array and iterable are the only suggestions, choose ‘array’, ‘iterable’, or ‘omit’. By default, it is array.
int_or_float float string When int and float are the only suggestions, choose ‘int’, ‘float’, or ‘omit’. By default, it is float.

4.2.32.4. Suggested Analysis

4.2.32.6. Reverse Cobbler

  • No anchor for Functions/RemoveTypehint

4.2.32.7. Specs

Short Name Functions/SetTypehints
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.33. Split Property Definitions

Split multiple properties definition into independent definitions.

This applies to classes and traits.

4.2.33.1. Before

<?php
    class x {
        private $x, $y, $z;
    }
?>

4.2.33.2. After

<?php
    class x {
        private $x;
        private $y;
        private $z;
    }
?>

4.2.33.3. Suggested Analysis

4.2.33.4. Specs

Short Name Classes/SplitPropertyDefinitions
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.34. Switch To Match

Transforms a switch() into a match() expression.

The switch() syntax must have each of the cases assigning the same variable (or similar). There should not be any other operation, besides break;

4.2.34.1. Before

<?php
    switch($a) {
        case 1:
            $b = '1';
            break;
        case 2:
            $b = '3';
            break;
        default:
            $b = '0';
            break;
    }
?>

4.2.34.2. After

<?php
    $b = match($a) {
        1 => '1',
        2 => '3',
        default => '0'
    };
?>

4.2.34.3. Suggested Analysis

4.2.34.5. Reverse Cobbler

4.2.34.6. Specs

Short Name Structures/SwitchToMatch
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.35. Use Available Alias

Apply systematically the use expression in the code.

4.2.35.1. Before

<?php
    use A\B\C as D;
    new A\B\C();
?>

4.2.35.2. After

<?php
    use A\B\C as D;
    new D();
?>

4.2.35.3. Suggested Analysis

4.2.35.4. Specs

Short Name Namespaces/UseAlias
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.36. Var To Public

Replace the var syntax with public keyword.

It is also possible to replace it with protected or private, with the parameter.

4.2.36.1. Before

<?php

class x {
    var $y = 1;
}
?>

4.2.36.2. After

<?php

class x {
    public $y = 1;
}
?>

4.2.36.3. Parameters

Name Default Type Description
var_to_visibility public string The destination visibility to be used. May be one of: public, protected or private.

4.2.36.5. Specs

Short Name Classes/VarToPublic
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud

4.2.37. array_key_exists() Speedup

array_key_exists() is sped up when declared with a use expression.

4.2.37.1. Before

<?php

namespace A {
    array_key_exists($a, $b);
}

?>

4.2.37.2. After

<?php

namespace A {
    use function array_key_exists;

    array_key_exists($a, $b);
}

?>

4.2.37.4. Specs

Short Name Structures/ArrayKeysSpeedup
Exakat version 2.3.0
Available in Entreprise Edition, Exakat Cloud