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. Change Class¶
This cobbler replaces a class by another one, and leave the original class intact.
This cobbler is useful for inserting new classes instead of native PHP or library related ones: the usage shall be changed, but not the definition.
It might also be useful to update code, but keep older classes available for backward compatibility or fallback strategies.
4.2.5.1. Before¶
<?php
class oldClass {}
$a = new oldClass;
?>
4.2.5.2. After¶
<?php
class oldClass {}
$a = new newClass;
?>
4.2.5.4. Reverse Cobbler¶
4.2.5.5. Specs¶
Short Name | Classes/ChangeClass |
Exakat version | 2.3.0 |
Available in |
4.2.6. 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.6.1. Before¶
<?php
class y {
function a1(string $error, R $r = null) : int|string
{
}
?>
4.2.6.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.6.3. Reverse Cobbler¶
- No anchor for Attributes/RemovePhpdoc
4.2.6.4. Specs¶
Short Name | Attributes/CreatePhpdoc |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.7. Gather Use Expression¶
Move lone use expression to the beginning of the file.
4.2.7.1. Before¶
<?php
use A;
++$a;
use B;
?>
4.2.7.2. After¶
<?php
use A;
use B;
++$a;
?>
4.2.7.3. Suggested Analysis¶
4.2.7.4. Specs¶
Short Name | Namespaces/GatherUse |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.8. 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.8.1. Before¶
<?php
$a = function () { return 1; };
$b = fn () => 2;
?>
4.2.8.2. After¶
<?php
$a = static function () { return 1; };
$b = static fn () => 2;
?>
4.2.8.3. Suggested Analysis¶
4.2.8.4. Reverse Cobbler¶
- No anchor for Functions/RemoveStaticFromFunction
4.2.8.5. Specs¶
Short Name | Functions/MakeStaticFunction |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.9. 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.9.1. Before¶
4.2.9.2. After¶
4.2.9.3. Parameters¶
Name | Default | Type | Description |
configFile | string | The .yaml file in the project folder. |
4.2.9.4. Specs¶
Short Name | Utils/Multi |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.10. Plus One To Pre Plusplus¶
Transforms a + 1 or - 1 operation into a plus-plus (or minus-minus).
4.2.10.1. Before¶
<?php
$a = $a + 1;
?>
4.2.10.2. After¶
<?php
++$a;
?>
4.2.10.3. Specs¶
Short Name | Structures/PlusOneToPre |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.11. Post to Pre Plusplus¶
Transforms a post plus-plus (or minus-minus) operator, into a pre plus-plus (or minus-minus) operator.
4.2.11.1. Before¶
<?php
$a++;
?>
4.2.11.2. After¶
<?php
++$a;
?>
4.2.11.3. Specs¶
Short Name | Structures/PostToPre |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.12. 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.12.1. Before¶
<?php
// removing method \x::method1
class x {
function method1() {}
function method2() {}
}
?>
4.2.12.2. After¶
<?php
// removed method \x::method1
class x {
function method2() {}
}
?>
4.2.12.3. Parameters¶
Name | Default | Type | Description |
name | x::method1 | string | Fully qualified name of the method to remove. Only one allowed. |
4.2.12.4. Specs¶
Short Name | Classes/RemoveMethod |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.13. Remove Abstract¶
Remove the abstract option, from classes and methods.
4.2.13.1. Before¶
<?php
abstract class x {
function foo() {}
abstract function moo() ;
}
?>
4.2.13.2. After¶
<?php
class x {
function foo() {}
function moo() {}
}
?>
4.2.13.3. Specs¶
Short Name | Classes/RemoveAbstract |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.14. 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.14.1. Before¶
<?php
foreach($i = 0; $i < 10; ++$i) { $total += 1; }
?>
4.2.14.2. After¶
<?php
foreach($i = 0; $i < 10; ++$i) $total += 1;
?>
4.2.14.3. Reverse Cobbler¶
4.2.14.4. Specs¶
Short Name | Structures/RemoveBracketsAroundSingleInstruction |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.15. 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.15.1. Before¶
<?php
$a = ;
?>
4.2.15.2. After¶
<?php
$a = {$b};
?>
4.2.15.3. Specs¶
Short Name | Structures/RemoveDollarCurly |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.16. Remove Final¶
This cobbler removes the final
keyword on classes and methods.
4.2.16.1. Before¶
<?php
final class y {
final function foo() {}
}
?>
4.2.16.2. After¶
<?php
class y {
function foo() {}
}
?>
4.2.16.4. Reverse Cobbler¶
- Add Final Class
- No anchor for Classes/AddFinalMethod
4.2.16.5. Specs¶
Short Name | Classes/RemoveFinal |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.17. 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.17.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.17.2. After¶
<?php
foo(1);
?>
4.2.17.3. Suggested Analysis¶
4.2.17.4. Specs¶
Short Name | Structures/RemoveCode |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.18. Remove Noscream @¶
Removes the @ operator.
4.2.18.1. Before¶
<?php
@$a;
?>
4.2.18.2. After¶
<?php
$a;
?>
4.2.18.3. Suggested Analysis¶
4.2.18.4. Reverse Cobbler¶
- This cobbler is its own reverse.
4.2.18.5. Specs¶
Short Name | Structures/RemoveNoScream |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.19. Remove Parenthesis¶
Remove useless parenthesis from return expression.
4.2.19.1. Before¶
<?php
function foo() {
return (1);
}
?>
4.2.19.2. After¶
<?php
function foo() {
return 1;
}
?>
4.2.19.3. Suggested Analysis¶
4.2.19.4. Specs¶
Short Name | Structures/RemoveParenthesis |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.20. 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.20.1. Before¶
<?php
readonly class x {
private readonly string $x;
}
?>
4.2.20.2. After¶
<?php
class x {
private string $x;
}
?>
4.2.20.3. Suggested Analysis¶
4.2.20.4. Specs¶
Short Name | Classes/RemoveReadonly |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.21. Remove Static From Closures And Arrow Functions¶
Removes the static option from closures and arrow functions.
4.2.21.1. Before¶
<?php
$a = static function () { return 1; };
$b = static fn () => 2;
?>
4.2.21.2. After¶
<?php
$a = function () { return 1; };
$b = fn () => 2;
?>
4.2.21.3. Suggested Analysis¶
4.2.21.4. Reverse Cobbler¶
4.2.21.5. Specs¶
Short Name | Functions/RemoveStaticFromClosure |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.22. Remove The Attribute¶
Remove attributes from all supporting structures.
Attributes are located on functions, classes, class constants, properties, methods and arguments.
4.2.22.1. Before¶
<?php
#[Attribute]
function foo(#[AttributeArgument] $arg) {
}
?>
4.2.22.2. After¶
<?php
function foo($arg) {
}
?>
4.2.22.3. Specs¶
Short Name | Attributes/RemoveAttribute |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.23. 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.23.1. Before¶
<?php
class x {
private string $p;
function foo(D\E $arg) : void {
}
}
?>
4.2.23.2. After¶
<?php
class x {
private $p;
function foo($arg) {
}
}
?>
4.2.23.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.23.4. Suggested Analysis¶
4.2.23.5. Reverse Cobbler¶
4.2.23.6. Specs¶
Short Name | Functions/RemoveTypes |
Exakat version | 2.2.5 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.24. Remove Unused Use¶
Removes the unused use expression from the top of the file. Groupuse are not processed yet.
4.2.24.1. Before¶
<?php
use a\b;
use c\d;
new b();
?>
4.2.24.2. After¶
<?php
use a\b;
new b();
?>
4.2.24.3. Suggested Analysis¶
4.2.24.4. Specs¶
Short Name | Namespaces/RemoveUse |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.25. Remove Visibility¶
Removes the visibility on constants, properties and methods.
For properties, the visibility is reset to public.
4.2.25.1. Before¶
<?php
class x {
private const x = 1;
private $p = 2;
private function foo() {}
private function __construct() {}
}
?>
4.2.25.2. After¶
<?php
class x {
const x = 1;
public $p = 2;
function foo() {}
function __construct() {}
}
?>
4.2.25.3. Specs¶
Short Name | Classes/RemoveVisibility |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.26. Remove Written Only Variable¶
This removes variables that are written only.
4.2.26.1. Before¶
<?php
function foo() {
$a = 1;
$a += 2; // No usage of $a
}
?>
4.2.26.2. After¶
<?php
function foo() {
}
?>
4.2.26.3. Suggested Analysis¶
4.2.26.4. Specs¶
Short Name | Structures/RemoveVariable |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.27. 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.27.1. Before¶
<?php
function foo() {
}
?>
4.2.27.2. After¶
<?php
function bar() {
}
?>
4.2.27.3. Parameters¶
Name | Default | Type | Description |
name | foo | string | The new name of the function. |
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/RenameFunction |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.28. Rename A Function¶
This cobbler renames a function from a name A to a name B.
4.2.28.1. Before¶
<?php
function foo() {}
foo();
?>
4.2.28.2. After¶
<?php
function bar() {}
bar();
?>
4.2.28.3. Parameters¶
Name | Default | Type | Description |
origin | string | The function to rename | |
destination | string | The destination’s function name |
4.2.28.4. Reverse Cobbler¶
- rename-a-function
4.2.28.5. Specs¶
Short Name | Functions/RenameFunction |
Exakat version | 2.3.0 |
Available in |
4.2.29. Rename A Namespace¶
Changes the name of a namespaces from A to B.
Make sure that the new namspace is distinct from the previous ones : merging namespaces is not recommended nor checked. This cobbler is better suited a giving an unused name to a namespace.
4.2.29.1. Before¶
<?php
namespace A;
function foo() {}
?>
4.2.29.2. After¶
<?php
namespace B;
function foo() {}
?>
4.2.29.3. Parameters¶
Name | Default | Type | Description |
origin | string | The original namespace. | |
destination | string | The destination namespace. |
4.2.29.4. Reverse Cobbler¶
4.2.29.5. Specs¶
Short Name | Rename/RenameNamespace |
Exakat version | 2.6.0 |
Available in |
4.2.30. Rename Class¶
Rename a class into another one.
The rename applies the new name to the class, and its usage : static calls, types, extends and instanceof.
4.2.30.1. Before¶
<?php
class x {}
function foo(x $a) {}
?>
4.2.30.2. After¶
<?php
class Y {}
function foo(Y $a) {}
?>
4.2.30.3. Parameters¶
Name | Default | Type | Description |
origin | string | The class to rename | |
destination | string | The destination’s class name |
4.2.30.4. Reverse Cobbler¶
- rename-class
4.2.30.5. Specs¶
Short Name | Classes/RenameClass |
Exakat version | 2.3.0 |
Available in |
4.2.31. Rename Class¶
Rename a class into another one.
The rename applies the new name to the class, and its usage : static calls, types, extends and instanceof.
4.2.31.1. Before¶
<?php
class x {
function m() {}
}
(new x)->m();
?>
4.2.31.2. After¶
<?php
class x {
function newM() {}
}
(new x)->newM();
?>
4.2.31.3. Parameters¶
Name | Default | Type | Description |
origin | string | The method to rename, along with its parent class. Like theClass::Method | |
destination | string | The destination’s method name. Only the name. |
4.2.31.4. Reverse Cobbler¶
- rename-class
4.2.31.5. Specs¶
Short Name | Classes/RenameMethod |
Exakat version | 2.3.0 |
Available in |
4.2.32. Rename Class¶
Rename a trait into another one.
The rename applies the new name to the trait, and its usage : use cases in classes and traits, static calls (PHP 8.0-).
4.2.32.1. Before¶
<?php
trait t {}
class x {
use t;
}
?>
4.2.32.2. After¶
<?php
trait newT {}
class x {
use newT;
}
?>
4.2.32.3. Parameters¶
Name | Default | Type | Description |
origin | string | The class to rename | |
destination | string | The destination’s class name |
4.2.32.4. Specs¶
Short Name | Traits/RenameTrait |
Exakat version | 2.3.0 |
Available in |
4.2.33. Rename Class Constant¶
Rename a class constant into another one.
The rename applies the new name to the class constant, and its usage.
4.2.33.1. Before¶
<?php
class x {
const A = 1;
}
echo x::A;
?>
4.2.33.2. After¶
<?php
class x {
const B = 1;
}
echo x::B;
?>
4.2.33.3. Parameters¶
Name | Default | Type | Description |
origin | string | The class constant to rename, along with its class name. x::A | |
destination | string | The destination’s class constant name. B |
4.2.33.4. Reverse Cobbler¶
4.2.33.5. Specs¶
Short Name | Classes/RenameConstant |
Exakat version | 2.3.0 |
Available in |
4.2.34. Rename Constant¶
This cobbler renames a constant and replace it with another constant.
4.2.34.1. Before¶
<?php
const A = 1;
echo A;
echo \A;
?>
4.2.34.2. After¶
<?php
const B = 1;
echo B;
echo \B;
?>
4.2.34.3. Parameters¶
Name | Default | Type | Description |
origin | string | The constant to rename | |
destination | string | The destination’s constant name |
4.2.34.4. Reverse Cobbler¶
4.2.34.5. Specs¶
Short Name | Constants/RenameConstant |
Exakat version | 2.3.0 |
Available in |
4.2.35. Rename Enums¶
Rename a class into another one.
The rename applies the new name to the class, and its usage : static calls, types, extends and instanceof.
4.2.35.1. Before¶
<?php
enum E {}
function foo(E $a) {}
?>
4.2.35.2. After¶
<?php
enum EFG {}
function foo(EFG $a) {}
?>
4.2.35.3. Parameters¶
Name | Default | Type | Description |
origin | string | The class to rename | |
destination | string | The destination’s class name |
4.2.35.4. Reverse Cobbler¶
4.2.35.5. Specs¶
Short Name | Enums/RenameEnums |
Exakat version | 2.3.0 |
Available in |
4.2.36. Rename FunctionCalls¶
Rename a function call to another function.
4.2.36.1. Before¶
<?php
foo(1, 2);
?>
4.2.36.2. After¶
<?php
bar(1, 2);
?>
4.2.36.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.36.4. Suggested Analysis¶
- No anchor for Utils/Selector
4.2.36.6. Reverse Cobbler¶
- This cobbler is its own reverse.
4.2.36.7. Specs¶
Short Name | Structures/RenameFunctionCall |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.37. Rename Interface¶
Rename an interface into another one.
The rename applies the new name to the class, and its usage : static constants, types, extends and instanceof.
4.2.37.1. Before¶
<?php
interface i {}
function foo(i $a) : j {}
?>
4.2.37.2. After¶
<?php
class j {}
function foo(j $a) : j {}
?>
4.2.37.3. Parameters¶
Name | Default | Type | Description |
origin | string | The class to rename | |
destination | string | The destination’s class name |
4.2.37.4. Reverse Cobbler¶
4.2.37.5. Specs¶
Short Name | Interfaces/RenameInterface |
Exakat version | 2.5.0 |
Available in |
4.2.38. 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.38.1. Before¶
<?php
$o->method();
?>
4.2.38.2. After¶
<?php
$o->newName();
?>
4.2.38.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.38.4. Suggested Analysis¶
- No anchor for Utils/Selector
4.2.38.6. Reverse Cobbler¶
- No anchor for Structures/RemoveMethodCall
4.2.38.7. Specs¶
Short Name | Structures/RenameMethodcall |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.39. 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.39.1. Before¶
<?php
foo(a: 1);
function foo($a) {
return $a;
}
?>
4.2.39.2. After¶
<?php
foo(b: 1);
function foo($b) {
return $b;
}
?>
4.2.39.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.39.4. Specs¶
Short Name | Functions/RenameParameter |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.40. Rename Property¶
Rename a property into another one.
The rename applies the new name to the property, and its usage : static calls, and normal calls.
4.2.40.1. Before¶
<?php
class x {
private $p = 1;
function m() {
$this->p = 2;
}
}
?>
4.2.40.2. After¶
<?php
class x {
private $newP = 1;
function m() {
$this->newP = 2;
}
}
?>
4.2.40.3. Parameters¶
Name | Default | Type | Description |
origin | string | The property to rename, along with its parent class. Like theClass::$property | |
destination | string | The destination’s property name. Only the name. |
4.2.40.4. Specs¶
Short Name | Classes/RenameProperty |
Exakat version | 2.3.0 |
Available in |
4.2.41. 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.41.1. Before¶
<?php
function foo() : int {
if (rand(0, 1)) {
return 1;
} else {
return null;
}
}
?>
4.2.41.2. After¶
<?php
function foo() : ?int {
if (rand(0, 1)) {
return 1;
} else {
return null;
}
}
?>
4.2.41.3. Reverse Cobbler¶
4.2.41.4. Specs¶
Short Name | Functions/SetNullType |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.42. Set Type Void¶
Adds the void typehint to functions and methods, when possible.
4.2.42.1. Before¶
<?php
function foo() {
return;
}
?>
4.2.42.2. After¶
<?php
function foo() : void {
return;
}
?>
4.2.42.3. Suggested Analysis¶
4.2.42.5. Reverse Cobbler¶
4.2.42.6. Specs¶
Short Name | Functions/SetTypeVoid |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.43. 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.43.1. Before¶
<?php
class x {
private int $p = 2;
function foo(int $a = 1) : int {
return intdiv($a, $this->p);
}
}
?>
4.2.43.2. After¶
<?php
class x {
private int $p = 2;
function foo(int $a = 1) : int {
return intdiv($a, $this->p);
}
}
?>
4.2.43.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.43.4. Suggested Analysis¶
4.2.43.6. Reverse Cobbler¶
- No anchor for Functions/RemoveTypehint
4.2.43.7. Specs¶
Short Name | Functions/SetTypehints |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.44. Split Property Definitions¶
Split multiple properties definition into independent definitions.
This applies to classes and traits.
4.2.44.1. Before¶
<?php
class x {
private $x, $y, $z;
}
?>
4.2.44.2. After¶
<?php
class x {
private $x;
private $y;
private $z;
}
?>
4.2.44.3. Suggested Analysis¶
4.2.44.4. Specs¶
Short Name | Classes/SplitPropertyDefinitions |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.45. 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.45.1. Before¶
<?php
switch($a) {
case 1:
$b = '1';
break;
case 2:
$b = '3';
break;
default:
$b = '0';
break;
}
?>
4.2.45.2. After¶
<?php
$b = match($a) {
1 => '1',
2 => '3',
default => '0'
};
?>
4.2.45.3. Suggested Analysis¶
4.2.45.5. Reverse Cobbler¶
4.2.45.6. Specs¶
Short Name | Structures/SwitchToMatch |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.46. Use Available Alias¶
Apply systematically the use expression in the code.
4.2.46.1. Before¶
<?php
use A\B\C as D;
new A\B\C();
?>
4.2.46.2. After¶
<?php
use A\B\C as D;
new D();
?>
4.2.46.3. Suggested Analysis¶
4.2.46.4. Specs¶
Short Name | Namespaces/UseAlias |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.47. 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.47.1. Before¶
<?php
class x {
var $y = 1;
}
?>
4.2.47.2. After¶
<?php
class x {
public $y = 1;
}
?>
4.2.47.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.47.5. Specs¶
Short Name | Classes/VarToPublic |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |
4.2.48. array_key_exists() Speedup¶
array_key_exists() is sped up when declared with a use expression.
4.2.48.1. Before¶
<?php
namespace A {
array_key_exists($a, $b);
}
?>
4.2.48.2. After¶
<?php
namespace A {
use function array_key_exists;
array_key_exists($a, $b);
}
?>
4.2.48.3. Suggested Analysis¶
4.2.48.4. Specs¶
Short Name | Structures/ArrayKeysSpeedup |
Exakat version | 2.3.0 |
Available in | Entreprise Edition, Exakat Cloud |