Download the PHP package uuf6429/php-cs-fixer-blockstring without Composer
On this page you can find all versions of the php package uuf6429/php-cs-fixer-blockstring. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download uuf6429/php-cs-fixer-blockstring
More information about uuf6429/php-cs-fixer-blockstring
Files in uuf6429/php-cs-fixer-blockstring
Package php-cs-fixer-blockstring
Short Description A PHP-CS-Fixer extension for formatting the contents of PHP heredoc and nowdoc string blocks.
License MIT
Informations about the package php-cs-fixer-blockstring
๐งน PHP-CS-Fixer Heredoc/Nowdoc Content Formatter
This project extends PHP-CS-Fixer to be able to format the contents of PHP Heredoc and Nowdoc strings (aka Block Strings).
Note that no language-specific formatters are provided by design โ this project instead provides the ability to integrate any type of formatters with minimal code (mainly in your PHP-CS-Fixer configuration file).
While this might sound like a weakness, it in fact makes it possible to integrate virtually any formatter for any language.
๐ Installation
Install via Composer:
Finally, register a custom fixer in your .php-cs-fixer.php config file (1๏ธโฃ) and then set up formatters (2๏ธโฃ):
[!WARNING] If PHP CS Fixer is installed via
php-cs-fixer/shimpackage, you may have to require the bootstrap file:
๐ก Before You Start
1. What does the configuration look like?
The configuration is made up of a map of block string delimiters and formatter pairs. A default formatter can be configured to run for any Block Strings that have other not been configured. For example: In that (fictitious) example, `LineFormatter` is applied to all block strings except `<<2. What's the deal with formatter versions?
You might have noticed that the base formatter class requires having a version. Most formatters require a way for providing such a version. The reason is that by supplying an up-to-date version, the PHP-CS-Fixer cache can be skipped - which is important if the recently updated external fixer is behaving differently - otherwise fixes become outdated because of an outdated cache. Note that the actual value of the version does not matter. Some formatters might be able to figure out the version by themselves.3. What about variable interpolation in Heredoc?
They provide an interesting challenge, which this project solves with the concept of an [`InterpolationCodec`]. It works by replacing interpolation 'segments' with tokens โ ensuring that the content is valid during the formatting stage โ and then they're rolled back to the original value. The codec can be configured for most of the formatters โ you should probably apply such configuration diligently if you plan on having Heredoc strings. Here's an example illustration of the whole flow: That JSON cannot be formatted properly because `$users` is not valid syntax. The [`GeneratedTokenCodec`] codec can be used; it will automatically replace the `$users` part with a token temporarily. By default, it will replace it with `__PHP_VAR_1__` in this specific case โ which, however, is still not valid(!) So instead, we configure it with a different token pattern: `new GeneratedTokenCodec('"__PHP_VAR_%d__"')`. The double quotes ensure that the replaced token is valid JSON: Given that, the formatter will do its job without problems, and then the codec will transform that token back to the original interpolation.4. What about complex variable interpolation in Heredoc?
The `GeneratedTokenCodec` codec additionally allows handling interpolations on a case-by-case basis by providing a callback that acts as a token generation factory. If this callback returns null instead of a string token, the default functionality will be used instead. Additionally, you can always build your own codec simply by having a class implement [`CodecInterface`].5. The 3d-party/external formatter complains that the string has bad syntax.
This is not at all unlikely โ that's one reason why the interpolation codec concept exists โ string interpolation often causes broken syntax. Unfortunately, the codec concept won't help you if you're using some other sort of templating system, such as replacing placeholders with `str_replace()`, `preg_replace()`, `strtr()` or `sprintf()` or similar. You can, however, implement a "formatter" that replaces such placeholders temporarily during formatting and then reverses them back, but since this seems like an uncommon usecase, there aren't any supporting implementations yet (you're welcome to suggest it though).๐ Usage Example
1. Given the following PHP-CS-Fixer configuration:
2. ...and the following source code file (whitespace has been replaced for better display):
3. ...PHP-CS-Fixer will format everything, resulting in (whitespace also substituted):
[!TIP] More example configurations ("recipes") can be found in
uuf6429/php-cs-fixer-blockstring-recipes.
โญ๏ธ Formatters
AbstractFormatter
This is the base class of all formatters. In most cases you don't really want to extend this class, since it does
not handle string interpolation at all โ check out AbstractStringFormatter instead.
Extending this class makes sense in two situations:
- If your class is infrastructural, and you don't really need to handle string interpolation - just like
ChainFormatter - Or if, for whatever reason, the
CodecInterfaceconcept does not work for you and you want to write something from scratch.
AbstractStringFormatter
This formatter base class is aware of string interpolation โ it passes content through a codec before and after formatting (to properly handle string interpolation).
Additionally, it keeps an in-memory cache of formatted content to avoid unnecessary work within the same process.
It can be used to embed any kind of formatter, including (native) PHP-based ones.
Example with your own custom class:
ChainFormatter
This formatter allows multiple formatters to be applied sequentially โ the output of each formatter becomes the input of the next one.
Example:
CliPipeFormatter
It's no secret that the best formatting tools are not directly available in PHP. This formatter off-loads formatting to such external executables.
Example:
The command definition (for version detection or formatting) is an array with the following structure:
cmdarray/string: The command line e.g.'jfmt --format'or['jfmt', '--format'].cwd(optional)string: The current working directory of the command.env(optional)arrayofstringkeys and values: Environment variables to pass to the command.
DockerPipeFormatter
The minimal setup, stable repeatability, and a rich ecosystem make Docker images an ideal source of formatting tools. This formatter exists to take advantage of that.
Example:
SimpleLineFormatter
A formatter that normalizes indentation and removes any trailing whitespace at the end of lines.
Example:
WslPipeFormatter
A formatter making use of Windows Subsystem for Linux (WSL). Of course, you will need to be running on Windows,
and WSL needs to be enabled and set up. Configuration is otherwise almost identical to CliPipeFormatter.
All versions of php-cs-fixer-blockstring with dependencies
ext-json Version *
symfony/process Version ^5 || ^6 || ^7 || ^8
friendsofphp/php-cs-fixer Version ^3
symfony/deprecation-contracts Version ^2 || ^3