ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

phpfmt

by driade ST3 Trending

PHP formatter for Sublime Text 4, with PHP 8 support.

Details

  • 1021.0.0
  • github.​com
  • github.​com
  • 3 days ago
  • 14 minutes ago
  • 10 years ago

Installs

  • Total 169K
  • Win 108K
  • Mac 34K
  • Linux 27K
Mar 29 Mar 28 Mar 27 Mar 26 Mar 25 Mar 24 Mar 23 Mar 22 Mar 21 Mar 20 Mar 19 Mar 18 Mar 17 Mar 16 Mar 15 Mar 14 Mar 13 Mar 12 Mar 11 Mar 10 Mar 9 Mar 8 Mar 7 Mar 6 Mar 5 Mar 4 Mar 3 Mar 2 Mar 1 Feb 29 Feb 28 Feb 27 Feb 26 Feb 25 Feb 24 Feb 23 Feb 22 Feb 21 Feb 20 Feb 19 Feb 18 Feb 17 Feb 16 Feb 15 Feb 14 Feb 13
Windows 4 18 19 21 24 11 16 17 12 16 20 17 18 5 10 14 11 20 20 13 19 7 15 9 20 9 12 15 14 13 30 17 15 4 11 14 14 16 17 8 15 12 11 11 12 17
Mac 1 4 5 5 2 0 5 1 6 4 8 7 1 1 3 2 4 4 8 2 1 5 5 4 3 1 3 4 1 5 5 4 4 2 5 1 3 2 2 6 2 0 2 4 3 4
Linux 0 2 5 3 2 4 2 3 6 2 2 4 3 2 6 0 4 4 6 5 0 2 5 2 4 6 3 2 7 8 3 4 1 2 1 7 7 6 1 4 3 2 3 3 3 5

Readme

Source
raw.​githubusercontent.​com

phpfmt support for Sublime Text 4 with PHP 8.x support

PHPFmt

This project is a clone of https://github.com/nanch/phpfmt_stable with PHP 8 support and numerous bugfixes, since the original project is abandoned.

Contributing

Please don't hesitate opening a pull request or an issue in case you find something wrong. Provide as much information as you can, with code samples of the expected result, to make it easier to spot the error.

Installation

Requirements

  • You must have a running copy of PHP on the machine you are running Sublime Text

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

Install this plugin through Package Manager

  • In Sublime Text press ctrl+shift+P
  • Choose Package Control: Install Package
  • Choose phpfmt

Configuration (Windows)

  • Edit configuration file (%AppData%\Sublime Text\Packages\phpfmt\phpfmt.sublime-nings)
  • For field "php_bin" enter the path to the php.exe Example: "php_bin":"c:/PHP/php.exe"

Configuration (OS X and Linux)

  • Edit configuration file (phpfmt.sublime-settings)
  • For field "php_bin" enter the path to the php Example: "php_bin":"/usr/local/bin/php"

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",
}

You may find an example configuration file in https://github.com/driade/phpfmt8/blob/master/driade.sublime-settings

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

  • phpfmt: format now
  • phpfmt: indentation with spaces
  • phpfmt: toggle additional transformations
  • phpfmt: toggle excluded transformations
  • phpfmt: toggle skip execution when .php.tools.ini is missing
  • phpfmt: toggle autocomplete
  • phpfmt: toggle dependency autoimport
  • phpfmt: toggle format on save
  • phpfmt: toggle PSR1 - Class and Methods names
  • phpfmt: toggle PSR1
  • phpfmt: toggle PSR2
  • phpfmt: toggle WP Coding Standards
  • phpfmt: analyse this
  • phpfmt: build autocomplete database
  • phpfmt: getter and setter (camelCase)
  • phpfmt: getter and setter (Go)
  • phpfmt: getter and setter (snake_case)
  • phpfmt: generate PHPDoc block
  • phpfmt: look for .php.tools.ini
  • phpfmt: reorganize content of class
  • phpfmt: enable/disable additional transformations
  • phpfmt: troubleshoot information
  • phpfmt: update PHP binary path

Currently Supported Transformations:

  • AddMissingParentheses Add extra parentheses in new instantiations.
  • AliasToMaster Replace function aliases to their masters - only basic syntax alias.
  • AlignConstVisibilityEquals Vertically align “=” of visibility and const blocks.
  • AlignDoubleArrow Vertically align T_DOUBLE_ARROW (=>).
  • AlignDoubleSlashComments Vertically align “//” comments.
  • AlignEquals Vertically align “=”.
  • AlignGroupDoubleArrow Vertically align T_DOUBLE_ARROW (=>) by line groups.
  • AlignPHPCode Align PHP code within HTML block.
  • AlignTypehint Vertically align function type hints.
  • AllmanStyleBraces Transform all curly braces into Allman-style.
  • AutoPreincrement Automatically convert postincrement to preincrement.
  • AutoSemicolon Add semicolons in statements ends.
  • CakePHPStyle Applies CakePHP Coding Style
  • ClassToSelf “self” is preferred within class, trait or interface.
  • ClassToStatic “static” is preferred within class, trait or interface.
  • ConvertOpenTagWithEcho Convert from “<?=” to “<?php echo ”.
  • DocBlockToComment Replace docblocks with regular comments when used in non structural elements.
  • DoubleToSingleQuote Convert from double to single quotes.
  • EchoToPrint Convert from T_ECHO to print.
  • EncapsulateNamespaces Encapsulate namespaces with curly braces
  • GeneratePHPDoc Automatically generates PHPDoc blocks
  • IndentTernaryConditions Applies indentation to ternary conditions.
  • JoinToImplode Replace implode() alias (join() -> implode()).
  • LeftWordWrap Word wrap at 80 columns - left justify.
  • LongArray Convert short to long arrays.
  • MergeElseIf Merge if with else.
  • SplitElseIf Merge if with else.
  • MergeNamespaceWithOpenTag Ensure there is no more than one linebreak before namespace
  • MildAutoPreincrement Automatically convert postincrement to preincrement. (Deprecated pass. Use AutoPreincrement instead).
  • NewLineBeforeReturn Add an empty line before T_RETURN.
  • OrganizeClass Organize class, interface and trait structure.
  • OrderAndRemoveUseClauses Order use block and remove unused imports.
  • OnlyOrderUseClauses Order use block - do not remove unused imports.
  • OrderMethod Organize class, interface and trait structure.
  • OrderMethodAndVisibility Organize class, interface and trait structure.
  • PHPDocTypesToFunctionTypehint Read variable types from PHPDoc blocks and add them in function signatures.
  • PrettyPrintDocBlocks Prettify Doc Blocks
  • PSR2EmptyFunction Merges in the same line of function header the body of empty functions.
  • PSR2MultilineFunctionParams Break function parameters into multiple lines.
  • ReindentAndAlignObjOps Align object operators.
  • ReindentSwitchBlocks Reindent one level deeper the content of switch blocks.
  • ReindentEnumBlocks Reindent one level deeper the content of enum blocks.
  • RemoveIncludeParentheses Remove parentheses from include declarations.
  • RemoveSemicolonAfterCurly Remove semicolon after closing curly brace.
  • RemoveUseLeadingSlash Remove leading slash in T_USE imports.
  • ReplaceBooleanAndOr Convert from “and”/“or” to “&&”/“||”. Danger! This pass leads to behavior change.
  • ReplaceIsNull Replace is_null($a) with null === $a.
  • RestoreComments Revert any formatting of comments content.
  • ReturnNull Simplify empty returns.
  • ShortArray Convert old array into new array. (array() -> [])
  • SmartLnAfterCurlyOpen Add line break when implicit curly block is added.
  • SortUseNameSpace Organize use clauses by length and alphabetic order.
  • SpaceAroundControlStructures Add space around control structures.
  • SpaceAfterExclamationMark Add space after exclamation mark.
  • SpaceAroundParentheses Add spaces inside parentheses.
  • SpaceAroundExclamationMark Add spaces around exclamation mark.
  • SpaceBetweenMethods Put space between methods.
  • StrictBehavior Activate strict option in array_search, base64_decode, in_array, array_keys, mb_detect_encoding. Danger! This pass leads to behavior change.
  • StrictComparison All comparisons are converted to strict. Danger! This pass leads to behavior change.
  • StripExtraCommaInArray Remove trailing commas within array blocks
  • StripNewlineAfterClassOpen Strip empty lines after class opening curly brace.
  • StripNewlineAfterCurlyOpen Strip empty lines after opening curly brace.
  • StripNewlineWithinClassBody Strip empty lines after class opening curly brace.
  • StripSpaces Remove all empty spaces
  • StripSpaceWithinControlStructures Strip empty lines within control structures.
  • TightConcat Ensure string concatenation does not have spaces, except when close to numbers.
  • TrimSpaceBeforeSemicolon Remove empty lines before semi-colon.
  • UpgradeToPreg Upgrade ereg* calls to preg*
  • WordWrap Word wrap at 80 columns.
  • WrongConstructorName Update old constructor names into new ones. http://php.net/manual/en/language.oop5.decon.php
  • YodaComparisons Execute Yoda Comparisons.

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

  • Be sure you can run PHP from the command line.
  • If you need support, please open an issue at fmt issues

Acknowledgements