该包已被废弃,不再维护。未建议替代包。

一个用于自动修复K&R和PSR-1/2编码规范的脚本。

维护者

详情

github.com/kokororin/fmt

安装: 197

依赖者: 0

建议者: 0

安全: 0

星标: 32

关注者: 3

分支: 4

类型:application

该包尚未发布版本,且信息有限。


README

Build Status

要求

  • 运行格式化器需要PHP >= 5.6.0。注意,如果需要,格式化器可以解析版本4的PHP文件。不支持HHVM。

使用方法

$ php fmt.phar filename.php
$ php fmt.phar --help
Usage: fmt.phar [-hv] [-o=FILENAME] [--config=FILENAME] [--cache[=FILENAME]] [options] <target>
  --align_double_arrow              enable auto align of T_DOUBLE_ARROW
  --align_equals                    enable auto align of ST_EQUAL
  --cache[=FILENAME]                cache file. Default: .php.tools.cache
  --cakephp                         Apply CakePHP coding style
  --config=FILENAME                 configuration file. Default: .phpfmt.ini
  --constructor=type                analyse classes for attributes and generate constructor - camel, snake, golang
  --dry-run                         Runs the formatter without atually changing files; returns exit code 1 if changes would have been applied
  --enable_auto_align               enable auto align of ST_EQUAL and T_DOUBLE_ARROW
  --exclude=pass1,passN,...         disable specific passes
  --help-pass                       show specific information for one pass
  --ignore=PATTERN-1,PATTERN-N,...  ignore file names whose names contain any PATTERN-N
  --indent_with_space=SIZE          use spaces instead of tabs for indentation. Default 4
  --lint-before                     lint files before pretty printing (PHP must be declared in %PATH%/$PATH)
  --list                            list possible transformations
  --list-simple                     list possible transformations - greppable
  --no-backup                       no backup file (original.php~)
  --passes=pass1,passN,...          call specific compiler pass
  --profile=NAME                    use one of profiles present in configuration file
  --psr                             activate PSR1 and PSR2 styles
  --psr1                            activate PSR1 style
  --psr1-naming                     activate PSR1 style - Section 3 and 4.3 - Class and method names case.
  --psr2                            activate PSR2 style
  --selfupdate                      self-update fmt.phar from Github
  --setters_and_getters=type        analyse classes for attributes and generate setters and getters - camel, snake, golang
  --smart_linebreak_after_curly     convert multistatement blocks into multiline blocks
  --version                         version
  --visibility_order                fixes visibiliy order for method in classes - PSR-2 4.2
  --yoda                            yoda-style comparisons
  -h, --help                        this help message
  -o=-                              output the formatted code to standard output
  -o=file                           output the formatted code to "file"
  -v                                verbose

If <target> is "-", it reads from stdin

支持的转换

  • 添加MissingParentheses 在新实例化中添加额外的括号。
  • AliasToMaster 将函数别名替换为其主函数 - 仅基本语法别名。
  • AlignConstVisibilityEquals 垂直对齐可见性和const块的"="。
  • AlignDoubleArrow 垂直对齐 T_DOUBLE_ARROW (=>)。
  • AlignDoubleSlashComments 垂直对齐 "//"注释。
  • AlignEquals 垂直对齐"="。
  • AlignGroupDoubleArrow 通过行组垂直对齐 T_DOUBLE_ARROW (=>)。
  • AlignPHPCode 对HTML块内的PHP代码进行对齐。
  • AlignTypehint 垂直对齐函数类型提示。
  • AllmanStyleBraces 将所有花括号转换为Allman样式。
  • AutoPreincrement 自动将后增量转换为前增量。
  • AutoSemicolon 在语句末尾添加分号。
  • CakePHPStyle 应用CakePHP编码风格。
  • ClassToSelf 在类、特质或接口内部首选"self"。
  • ClassToStatic 在类、特质或接口内部首选"static"。
  • ConvertOpenTagWithEcho 将从 "<?=" 转换为 "<?php echo "。
  • DocBlockToComment 当在非结构化元素中使用时,用常规注释替换docblocks。
  • DoubleToSingleQuote 将双引号转换为单引号。
  • EchoToPrint 将T_ECHO转换为print。
  • EncapsulateNamespaces 用花括号封装命名空间。
  • GeneratePHPDoc 自动生成PHPDoc块。
  • IndentTernaryConditions 对三元条件应用缩进。
  • JoinToImplode 将implode()别名(join() -> implode())替换。
  • LeftWordWrap 在80列处换行 - 左对齐。
  • LongArray 将短数组转换为长数组。
  • MergeElseIf 合并if和else。
  • SplitElseIf 合并if和else。
  • MergeNamespaceWithOpenTag 确保在namespace之前没有超过一个换行符。
  • MildAutoPreincrement 自动将后增量转换为前增量。 (已弃用。请使用AutoPreincrement代替)。
  • NewLineBeforeReturn 在T_RETURN之前添加一个空行。
  • OrganizeClass 组织类、接口和特质结构。
  • OrderAndRemoveUseClauses 对use块进行排序并删除未使用导入。
  • OnlyOrderUseClauses 对use块进行排序 - 不删除未使用导入。
  • OrderMethod 组织类、接口和特质结构。
  • OrderMethodAndVisibility 组织类、接口和特质结构。
  • PHPDocTypesToFunctionTypehint 从PHPDoc块中读取变量类型并将它们添加到函数签名中。
  • PrettyPrintDocBlocks 美化Doc Blocks。
  • PSR2EmptyFunction 将空函数的主体合并到函数标题的同一行。
  • PSR2MultilineFunctionParams 将函数参数拆分为多行。
  • ReindentAndAlignObjOps 对齐对象操作符。
  • ReindentSwitchBlocks 将switch块的内容缩进一级更深。
  • RemoveIncludeParentheses 从include声明中删除括号。
  • RemoveSemicolonAfterCurly 在关闭花括号后删除分号。
  • RemoveUseLeadingSlash 删除T_USE导入中的前导斜杠。
  • 替换布尔逻辑运算符 将 "and"/"or" 转换为 "&&"/"||"。危险!此步骤可能导致行为变化。
  • 替换is_null 替换 is_null($a) 为 null === $a。
  • 恢复注释 恢复注释内容的所有格式。
  • 返回null 简化空返回。
  • 缩短数组 将旧数组转换为新数组。(array() -> [])
  • 智能换行 在添加隐式花括号块时添加换行。
  • 使用命名空间排序 按长度和字母顺序组织使用语句。
  • 控制结构周围加空格 在控制结构周围添加空格。
  • 感叹号后加空格 在感叹号后添加空格。
  • 感叹号周围加空格 在感叹号周围添加空格。
  • 括号内加空格 在括号内添加空格。
  • 方法间加空格 在方法之间添加空格。
  • 严格行为 在 array_search, base64_decode, in_array, array_keys, mb_detect_encoding 中激活严格选项。危险!此步骤可能导致行为变化。
  • 严格比较 将所有比较转换为严格。危险!此步骤可能导致行为变化。
  • 移除数组中的多余逗号 移除数组块中的尾随逗号。
  • 移除类开头的换行符 移除类开头的花括号后的空行。
  • 移除花括号开头的换行符 移除开头的花括号后的空行。
  • 移除类体内换行符 移除类体内花括号后的空行。
  • 移除空格 移除所有空格。
  • 移除控制结构内的空格 移除控制结构内的空行。
  • 紧缩连接 确保字符串连接没有空格,除了接近数字时。
  • 移除分号前的空格 移除分号前的空行。
  • 升级到Preg 将 ereg_* 调用升级为 preg_*。
  • 换行 每行80个字符。
  • 更新构造函数名称 将旧构造函数名称更新为新名称。 https://php.ac.cn/manual/en/language.oop5.decon.php
  • 执行Yoda比较。

代码格式化器做什么?

K&R配置

之前 之后
<?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;
可以使用“disable_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();

注意它如何排序使用语句,并移除未使用的内容

PSR配置

之前 之后
<?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";
    }
}
注意缩进为4个空格。
<?php
class A {
function a(){
return 10;
}
}
<?php
class A
{
    public function a()
    {
        return 10;
    }
}
注意花括号的位置,以及方法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();

注意它如何排序使用语句,并移除未使用的内容