driade / phpfmt8

PHP formatter for Sublime Text 4, with PHP 8 support.
BSD 3-Clause "New" or "Revised" License
46 stars 2 forks source link
formatting php

PHPFmt for Sublime Text 4

PHPFmt

PHPFmt is a PHP code formatter tailored for Sublime Text 4 with full support for PHP 8.x. It's a continuation of the phpfmt_stable project, with added PHP 8 compatibility and numerous bug fixes.

Features

Installation

Requirements

Steps

  1. Open Sublime Text and press Ctrl+Shift+P.
  2. Select Package Control: Install Package.
  3. Search for phpfmt and install it.

Configuration

Windows

Edit the configuration file at %AppData%\Sublime Text\Packages\phpfmt\phpfmt.sublime-settings and set the php_bin path:

{
    "php_bin": "c:/PHP/php.exe"
}

macOS and Linux

Edit phpfmt.sublime-settings and set the php_bin path:

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

You may find an example configuration file in https://github.com/driade/phpfmt8/blob/master/driade.sublime-settings , where you can see how to configure the extension.

Usage

PHPFmt provides a variety of commands accessible via the command palette (Ctrl+Shift+P or Cmd+Shift+P):

... and more.

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

Ensure PHP is accessible from the command line. If issues arise, open an issue here.

Contributing

Contributions are welcome! Please submit pull requests or issues with detailed information and code samples.

VSCode

If you're using Visual Studio Code, please consider installing vscode-phpfmt extension by @kokororin

This extension leverages the same phpfmt8 engine, providing seamless PHP formatting directly within VS Code.

Acknowledgements