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. 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.1.1. Before

<?php

class x {
    private int $p = 2;

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

4.2.1.2. After

<?php

class x {
    private int $p = 2;

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

4.2.1.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.1.4. Suggested Analysis

4.2.1.6. Specs

Short Name Functions/SetTypehints
Exakat version 2.3.0

4.2.2. Plus One To Pre Plusplus

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

4.2.2.1. Before

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

4.2.2.2. After

<?php
    ++$a;
?>

4.2.2.3. Specs

Short Name Structures/PlusOneToPre
Exakat version 2.3.0

4.2.3. Post to Pre Plusplus

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

4.2.3.1. Before

<?php
    $a++;
?>

4.2.3.2. After

<?php
    ++$a;
?>

4.2.3.3. Specs

Short Name Structures/PostToPre
Exakat version 2.3.0

4.2.4. Remove Noscream @

Removes the @ operator.

4.2.4.1. Before

<?php
    @$a;
?>

4.2.4.2. After

<?php
    $a;
?>

4.2.4.3. Suggested Analysis

4.2.4.4. Reverse Cobbler

  • This cobbler is its own reverse.

4.2.4.5. Specs

Short Name Structures/RemoveNoScream
Exakat version 2.3.0

4.2.5. 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.5.1. Before

<?php

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

4.2.5.2. After

<?php

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

4.2.5.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.5.5. Specs

Short Name Classes/VarToPublic
Exakat version 2.3.0

4.2.6. Split Property Definitions

Split multiple properties definition into independent definitions.

This applies to classes and traits.

4.2.6.1. Before

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

4.2.6.2. After

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

4.2.6.3. Suggested Analysis

4.2.6.4. Specs

Short Name Classes/SplitPropertyDefinitions
Exakat version 2.3.0

4.2.7. 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.7.1. Before

<?php

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

?>

4.2.7.2. After

<?php

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

?>

4.2.7.3. Specs

Short Name Functions/SetNullType
Exakat version 2.3.0

4.2.8. Set Type Void

Adds the void typehint to functions and methods, when possible

4.2.8.1. Before

<?php

function foo() {
    return;
}

?>

4.2.8.2. After

<?php

function foo() : void {
    return;
}

?>

4.2.8.4. Specs

Short Name Functions/SetTypeVoid
Exakat version 2.3.0