Skip to content

Latest commit

 

History

History
1650 lines (1281 loc) · 12.4 KB

PHP-Transformations.md

File metadata and controls

1650 lines (1281 loc) · 12.4 KB

AddMissingParentheses

Before After
$a = new SomeClass;
$a = new SomeClass();

AliasToMaster

Before After
$a = join(',', $arr);
die("done");
$a = implode(',', $arr);
exit("done");

AlignDoubleArrow

Before After
$a = [
  1 => 1,
  22 => 22,
  333 => 333,
];
$a = [
  1   => 1,
  22  => 22,
  333 => 333,
];

AlignDoubleSlashComments

Before After
$a = 1; // Comment 1
$bb = 22;  // Comment 2
$ccc = 333;  // Comment 3
$a = 1;      // Comment 1
$bb = 22;    // Comment 2
$ccc = 333;  // Comment 3

AlignEquals

Before After
$a = 1;
$bb = 22;
$ccc = 333;
$a   = 1;
$bb  = 22;
$ccc = 333;

AlignGroupDoubleArrow

Before After
$a = [
  1 => 1,
  22 => 22,

  333 => 333,
  4444 => 4444,
];
$a = [
  1  => 1,
  22 => 22,

  333  => 333,
  4444 => 4444,
];

AlignTypehint

Before After
function a(
  TypeA $a,
  TypeBB $bb,
  TypeCCC $ccc = array(),
  TypeDDDD $dddd,
  TypeEEEEE $eeeee
){
  noop();
}
function a(
  TypeA     $a,
  TypeBB    $bb,
  TypeCCC   $ccc = array(),
  TypeDDDD  $dddd,
  TypeEEEEE $eeeee
){
  noop();
}

AllmanStyleBraces

Before After
if ($a) {

}
if ($a)
{

}

AutoSemicolon

Before After
$a = new SomeClass()
$a = new SomeClass();

ClassToSelf

Before After
class A {
  const constant = 1;
  function b(){
    A::constant;
  }
}
class A {
  const constant = 1;
  function b(){
    self::constant;
  }
}

ClassToStatic

Before After
class A {
  const constant = 1;
  function b(){
    A::constant;
  }
}
class A {
  const constant = 1;
  function b(){
    static::constant;
  }
}

ConvertOpenTagWithEcho

Before
<?="Hello World"?>
After
<?php echo "Hello World" ?>

DoubleToSingleQuote

Before After
$a = "";
$a = '';

EncapsulateNamespaces

Before After
namespace NS1;
class A {
}
namespace NS1 {
  class A {
  }
}

GeneratePHPDoc

Before After
class A {
  function a(Someclass $a) {
    return 1;
  }
}
class A {
  /**
   * @param Someclass $a
   * @return int
   */
  function a(Someclass $a) {
    return 1;
  }
}

IndentTernaryConditions

Before After
$a = ($b)
? $c
: $d
;
$a = ($b)
  ? $c
  : $d
;

JoinToImplode

Before After
$a = join(',', $arr);
$a = implode(',', $arr);

LongArray

Before After
$a = [$a, $b];
$b = array($b, $c);

MergeElseIf

Before After
if($a){

} else if($b) {

}
if($a){

} elseif($b) {

}

MergeNamespaceWithOpenTag

Before
<?php

namespace A;
?>
After
<?php
namespace A;
?>

NoSpaceAfterPHPDocBlocks

Before After
/**
 * @param int $myInt
 */

function a($myInt){
}
/**
 * @param int $myInt
 */
function a($myInt){
}

OnlyOrderUseClauses

Before After
use C;
use B;

class D {
  function f() {
    new B();
  }
}
use B;
use C;

class D {
  function f() {
    new B();
  }
}

OrderAndRemoveUseClauses

Before After
use C;
use B;

class D {
  function f() {
    new B();
  }
}
use B;

class D {
  function f() {
    new B();
  }
}

OrganizeClass

Before After
class A {
  public function d(){}
  protected function b(){}
  private $a = "";
  private function c(){}
  public function a(){}
  public $b = "";
  const B = 0;
  const A = 0;
}
class A {
  const A = 0;

  const B = 0;

  public $b = "";

  private $a = "";

  public function a(){}

  public function d(){}

  protected function b(){}

  private function c(){}
}

PHPDocTypesToFunctionTypehint

Before After
/**
 * @param int $a
 * @param int $b
 * @return int
 */
function abc($a = 10, $b = 20, $c) {

}
/**
 * @param int $a
 * @param int $b
 * @return int
 */
function abc(int $a = 10, int $b = 20, $c): int {

}

PrettyPrintDocBlocks

Before After
/**
 * some description.
 * @param array $b
 * @param LongTypeName $c
 */
function A(array $b, LongTypeName $c) {
}
/**
 * some description.
 * @param array        $b
 * @param LongTypeName $c
 */
function A(array $b, LongTypeName $c) {
}

PSR2EmptyFunction

Before After
// PSR2 Mode - From
function a()
{}
function a() {}

PSR2IndentWithSpace

Before After
    $codeAlignedWithTabs = true
    $codeAlignedWithTabs = false

PSR2MultilineFunctionParams

Before After
function a($a, $b, $c)
{}
function a(
  $a,
  $b,
  $c
) {}

ReindentAndAlignObjOps

Before After
$aaaaa->b
->c;
$aaaaa->b
      ->c;

ReindentSwitchBlocks

Before After
switch ($a) {
case 1:
  echo 'a';
}
switch ($a) {
  case 1:
    echo 'a';
}

RemoveIncludeParentheses

Before After
require_once("file.php");
require_once "file.php";

RemoveSemicolonAfterCurly

Before After
function xxx() {
    // code
};
function xxx() {
    // code
}

RemoveUseLeadingSlash

Before After
namespace NS1;
use \B;
use \D;

new B();
new D();
namespace NS1;
use B;
use D;

new B();
new D();

ReplaceBooleanAndOr

Before After
if ($a and $b or $c) {...}
if ($a && $b || $c) {...}

ReplaceIsNull

Before After
is_null($a);
null === $a;

ReturnNull

Before After
function a(){
  return null;
}
function a(){
  return;
}

ShortArray

Before After
echo array();
echo [];

SmartLnAfterCurlyOpen

Before After
if($a) echo array();
if($a) {
  echo array();
}

SpaceAroundControlStructures

Before After
if ($a) {

}
if ($b) {

}
if ($a) {

}

if ($b) {

}

SpaceAroundExclamationMark

Before After
if (!true) foo();
if ( ! true) foo();

SpaceBetweenMethods

Before After
class A {
  function b(){

  }
  function c(){

  }
}
class A {
  function b(){

  }

  function c(){

  }

}

SplitElseIf

Before After
if($a){
} elseif($b) {
}
if($a){
} else if($b) {
}

StrictBehavior

Before After
array_search($needle, $haystack);
base64_decode($str);
in_array($needle, $haystack);

array_keys($arr);
mb_detect_encoding($arr);

array_keys($arr, [1]);
mb_detect_encoding($arr, 'UTF8');
array_search($needle, $haystack, true);
base64_decode($str, true);
in_array($needle, $haystack, true);

array_keys($arr, null, true);
mb_detect_encoding($arr, null, true);

array_keys($arr, [1], true);
mb_detect_encoding($arr, 'UTF8', true);

StrictComparison

Before After
if($a == $b){}
if($a != $b){}
if($a === $b){}
if($a !== $b){}

StripExtraCommaInArray

Before After
$a = [$a, $b, ];
$b = array($b, $c, );
$a = [$a, $b];
$b = array($b, $c);

StripNewlineAfterClassOpen

Before After
class A {

  protected $a;
}
class A {
  protected $a;
}

StripNewlineAfterCurlyOpen

Before After
for ($a = 0; $a < 10; $a++){

  if($a){

    // do something
  }
}
for ($a = 0; $a < 10; $a++){
  if($a){
    // do something
  }
}

StripSpaces

Before After
$a = [$a, $b];
$b = array($b, $c);
$a=[$a,$b];$b=array($b,$c);

StripSpaceWithinControlStructures

Before After
for ($a = 0; $a < 10; $a++){

  if($a){

    // do something
  }

}
for ($a = 0; $a < 10; $a++){
  if($a){
    // do something
  }
}

TightConcat

Before After
$a = 'a' . 'b';
$a = 'a' . 1 . 'b';
$a = 'a'.'b';
$a = 'a'. 1 .'b';

UpgradeToPreg

Before After
$var = ereg("[A-Z]", $var);
$var = eregi_replace("[A-Z]", "", $var)
$var = spliti("[A-Z]", $var);
$var = preg_match("/[A-Z]/Di", $var);
$var = preg_replace("/[A-Z]/Di", "", $var);
$var = preg_split("/[A-Z]/Di", $var);

WrongConstructorName

Before After
class A {
  function A(){

  }
}
class A {
  function __construct(){

  }
}

YodaComparisons

Before After
if($a == 1){

}
if(1 == $a){

}