PHP code example of uuf6429 / php-cs-fixer-blockstring
1. Go to this page and download the library: Download uuf6429/php-cs-fixer-blockstring library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
uuf6429 / php-cs-fixer-blockstring example snippets
declare(strict_types=1);
use PhpCsFixer;
use uuf6429\PhpCsFixerBlockstring\Fixer\BlockStringFixer;
return (new PhpCsFixer\Config())
->registerCustomFixers([new BlockStringFixer()]) // 👈 1️⃣
->setRules([
BlockStringFixer::NAME => BlockStringFixer::config([
// 👈 2️⃣
]),
])
->setFinder(...);
>
BlockStringFixer::NAME => [
'formatters' => [
new LineFormatter(),
'JSON' => new JsonFormatter(),
]
]
echo <<<JSON
{"users": $users}
JSON;
echo <<<JSON
{"users": "__PHP_VAR_1__"}
JSON;
declare(strict_types=1);
use uuf6429\PhpCsFixerBlockstring\Fixer\BlockStringFixer;
use uuf6429\PhpCsFixerBlockstring\Formatter;
use uuf6429\PhpCsFixerBlockstring\InterpolationCodec\GeneratedTokenCodec;
use uuf6429\PhpCsFixerBlockstringTests\Fixtures;
return (new PhpCsFixer\Config())
->registerCustomFixers([new BlockStringFixer()])
->setRiskyAllowed(true)
->setRules([
BlockStringFixer::NAME => BlockStringFixer::config(
[
// 1️⃣ SimpleLineFormatter
// Normalizes indentation of any block not explicitly configured below
new Formatter\SimpleLineFormatter(
4, // indentSize
"\t", // indentChar
new GeneratedTokenCodec() // interpolationCodec
),
// 2️⃣ CliPipeFormatter
// Formats SQL using a CLI tool installed locally
'SQL' => new Formatter\CliPipeFormatter(
['cmd' => ['php', __DIR__ . '/sqlformat.php', '--version']], // versionValueOrCommand
['cmd' => ['php', __DIR__ . '/sqlformat.php', '-']], // formatCommand
),
// 3️⃣ ChainFormatter
// Combines two formatters:
// 1. A custom formatter that sorts object keys.
// 2. A docker-based formatter that runs the json through jq.
'JSON' => new Formatter\ChainFormatter(
new Fixtures\Formatters\JsonObjectKeySorter(),
new Formatter\DockerPipeFormatter(
'ghcr.io/jqlang/jq', // image
[], // options
[], // command
'missing', // pullMode
new GeneratedTokenCodec('"__PHP_VAR_%d__"') // interpolationCodec
),
),
]
),
]);
final class MyFormatter extends AbstractStringFormatter
{
public function __construct()
{
// It is // The bare minimum is to pass the class name, but you can also pass a more complex value (e.g. an array
// of settings). Avoid passing objects though, especially non-serializable ones.
parent::__construct(self::class);
}
protected function formatContent(string $original): string
{
return 'new content';
}
}
return (new PhpCsFixer\Config())
->registerCustomFixers([new BlockStringFixer()])
->setRules([
BlockStringFixer::NAME => BlockStringFixer::config([
'TEXT' => new MyFormatter(),
]),
]);
return (new PhpCsFixer\Config())
->registerCustomFixers([new BlockStringFixer()])
->setRules([
BlockStringFixer::NAME => BlockStringFixer::config([
'JSON' => new ChainFormatter(
new CliPipeFormatter('v1', ['cmd' => 'some-old-tool']),
new SimpleLineFormatter(4, "\t"),
),
]),
]);
return (new PhpCsFixer\Config())
->registerCustomFixers([new BlockStringFixer()])
->setRules([
BlockStringFixer::NAME => BlockStringFixer::config([
'J' => new CliPipeFormatter(
// Either a version as a string, or the command to get the version (as an array).
versionValueOrCommand: '1.0',
// An array defining the external command to do the formatting.
formatCommand: ['cmd' => 'jfmt -'],
// A codec for handling placeholers in template strings; depends on the content being formatted.
interpolationCodec: new PlainStringCodec(),
// A normalizer for handling end-of-line characters.
lineEndingNormalizer: null,
// Factory for creating processes. Defaults to Symfony process factory.
processFactory: null,
)
]),
]);
return (new PhpCsFixer\Config())
->registerCustomFixers([new BlockStringFixer()])
->setRules([
BlockStringFixer::NAME => BlockStringFixer::config([
'JSON' => new DockerPipeFormatter(
// The docker image; might contain url, tag or even the digest.
image: 'ghcr.io/jqlang/jq',
// Optional docker arguments, such as for setting env vars.
options: ['-e', 'SOME_ENV=value'],
// The command to run within the container, including any arguments.
command: ['bin/tool', '--dry-run', '-'],
// How/when the image should be pulled: 'never', 'always' or 'missing'.
pullMode: 'always',
// A codec for handling interpolations; depends on the content being formatted.
interpolationCodec: new PlainStringCodec(),
// A normalizer for handling end-of-line characters.
lineEndingNormalizer: null,
// Factory for creating processes. Defaults to Symfony process factory.
processFactory: null,
)
]),
]);
return (new PhpCsFixer\Config())
->registerCustomFixers([new BlockStringFixer()])
->setRules([
BlockStringFixer::NAME => BlockStringFixer::config([
'TEXT' => new SimpleLineFormatter(
// The number of spaces defining one indentation level in your project.
indentSize: 4,
// The actual character used for indentation (space or tab).
indentChar: "\t",
// A codec for handling interpolations; depends on the content being formatted.
interpolationCodec: new PlainStringCodec(),
// A normalizer for handling end-of-line characters.
lineEndingNormalizer: null,
)
]),
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.