repinspl/php-cs-fixer-html-indent 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

repinspl/php-cs-fixer-html-indent

最新稳定版本:v0.1.1

Composer 安装命令:

composer require repinspl/php-cs-fixer-html-indent

包简介

PHP-CS-Fixer custom fixers that preserve HTML-context indentation in mixed PHP/HTML files

README 文档

README

Custom fixers for PHP-CS-Fixer that preserve indentation of PHP blocks embedded in HTML/Blade files.

The problem

PHP-CS-Fixer formats PHP blocks without considering the surrounding HTML context. In mixed files (e.g. Blade templates) this breaks the indentation:

Before running php-cs-fixer — correct indentation within the HTML context:

<div>
	<main>
		<?php
			$users = DB::table('users')
				->where('active', true)
				->get();

			foreach ($users as $user) {
				echo $user->name;
			}
		?>
	</main>
</div>

After running php-cs-fixer — HTML context indentation is lost:

<div>
	<main>
		<?php
$users = DB::table('users')
    ->where('active', true)
    ->get();

foreach ($users as $user) {
    echo $user->name;
}
		?>
	</main>
</div>

With this library — php-cs-fixer formats the PHP while the HTML indentation is preserved:

<div>
	<main>
		<?php
			$users = DB::table('users')
				->where('active', true)
				->get();

			foreach ($users as $user) {
				echo $user->name;
			}
		?>
	</main>
</div>

Installation

composer require --dev repinspl/php-cs-fixer-html-indent

Configuration

Register both fixers and enable their rules in your .php-cs-fixer.dist.php:

<?php

$fixers = [
    new \RepinsPL\PhpCsFixerHtmlIndent\HtmlContextDedentFixer(),
    new \RepinsPL\PhpCsFixerHtmlIndent\HtmlContextReindentFixer(),
];

return (new PhpCsFixer\Config())
    ->registerCustomFixers($fixers)
    ->setRules([
        // Your other rules...
        'RepinsPL/html_context_dedent' => true,
        'RepinsPL/html_context_reindent' => true,
    ])
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in(__DIR__)
    );

Important: Both fixers must be enabled together. Dedent without reindent will strip the indentation without restoring it.

How it works

The library provides two fixers that wrap the entire PHP-CS-Fixer pipeline:

Fixer Priority Role
RepinsPL/html_context_dedent 1000 Before other fixers — strips base HTML indentation from PHP blocks
RepinsPL/html_context_reindent -1000 After all fixers — restores base HTML indentation

This allows other fixers (e.g. braces, indentation_type) to work on PHP code without extra indentation and format it correctly. Once formatting is complete, the HTML context indentation is restored.

Both tab-based and space-based indentation are supported. The base indentation style is detected automatically from the HTML context surrounding each PHP block.

Contributing

If you find a case where the fixers produce incorrect indentation, please open an issue with a minimal PHP/HTML snippet that demonstrates the problem. This helps us write a test and fix the bug.

Requirements

  • PHP >= 8.0
  • PHP-CS-Fixer ^3.0

License

MIT

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固