nanch / phpfmt_stable

This is a stable snapshot (version 6125cf9) of the phpfmt plugin for Sublime Text
143 stars 34 forks source link

phpfmt support for Sublime Text 3

Installation

Requirements

Plugin runs with PHP 7.0 or newer installed in the machine running the plugin.

Install this plugin through Package Manager.

Configuration (Windows)

Configuration (OS X and Linux)

Settings

Prefer using the toggle options at command palette. However you might find yourself in need to setup where PHP is running, use this option below for the configuration file.

{
"php_bin":"/usr/local/bin/php",
}

The following features are available through command palette (ctrl+shift+P or cmd+shift+P) :

Currently Supported Transformations:

What does it do?

Before After
<?php
for($i = 0; $i < 10; $i++)
{
if($i%2==0)
echo "Flipflop";
}
<?php
for ($i = 0; $i < 10; $i++) {
  if ($i%2 == 0) {
    echo "Flipflop";
  }
}
<?php
$a = 10;
$otherVar = 20;
$third = 30;
<?php
$a        = 10;
$otherVar = 20;
$third    = 30;
This can be enabled with the option "enable_auto_align"
<?php
namespace NS\Something;
use \OtherNS\C;
use \OtherNS\B;
use \OtherNS\A;
use \OtherNS\D;

$a = new A();
$b = new C();
$d = new D();
<?php
namespace NS\Something;

use \OtherNS\A;
use \OtherNS\C;
use \OtherNS\D;

$a = new A();
$b = new C();
$d = new D();
note how it sorts the use clauses, and removes unused ones

What does it do? - PSR version

Before After
<?php
for($i = 0; $i < 10; $i++)
{
if($i%2==0)
echo "Flipflop";
}
<?php
for ($i = 0; $i < 10; $i++) {
    if ($i%2 == 0) {
        echo "Flipflop";
    }
}
Note the identation of 4 spaces.
<?php
class A {
function a(){
return 10;
}
}
<?php
class A
{
    public function a()
    {
        return 10;
    }
}
Note the braces position, and the visibility adjustment in the method a().
<?php
namespace NS\Something;
use \OtherNS\C;
use \OtherNS\B;
use \OtherNS\A;
use \OtherNS\D;

$a = new A();
$b = new C();
$d = new D();
<?php
namespace NS\Something;

use \OtherNS\A;
use \OtherNS\C;
use \OtherNS\D;

$a = new A();
$b = new C();
$d = new D();
note how it sorts the use clauses, and removes unused ones

Troubleshooting

The Most FAQ

I want to use sublime-phpfmt, but it needs PHP 5.6 or newer and on my production server I have PHP 5.5 or older. What should I do?

Consider installing a standalone PHP 5.6 in a separate directory and have it not configured in the environment. Within the plugin, ensure php_bin parameter is pointed to this standalone installation.

Acknowledgements