respect-validation/src-dev/CodeGen/OutputFormatter.php
Alexandre Gomes Gaigalas 25387853f3
Generate PrefixMap constants from Mixin attributes
- Add PrefixMapGenerator to produce COMPOSABLE/COMPOSABLE_WITH_ARGUMENT
  constants from #[Mixin] attributes, replacing hand-written arrays
- Move Prefix transformer to reference generated PrefixMap constants
- Extract NamespaceScanner from MixinGenerator for shared directory scanning
- Introduce FluentBuilder subnamespace for builder-chain generators
  (MixinGenerator, PrefixMapGenerator, MethodBuilder, Mixin attribute)
- Add CodeGenerator interface and Config class as shared CodeGen contracts
2026-03-11 13:22:12 -03:00

47 lines
1.2 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Dev\CodeGen;
use function array_keys;
use function array_values;
use function implode;
use function preg_match;
use function preg_replace;
use function trim;
use const PHP_EOL;
final class OutputFormatter
{
public function format(string $content, string $existingContent): string
{
preg_match('/^<\?php\s*\/\*[\s\S]*?\*\//', $existingContent, $matches);
$existingHeader = $matches[0] ?? '';
$replacements = [
'/\n\n\t(public|private|\/\*\*)/m' => PHP_EOL . ' $1',
'/\t/m' => ' ',
'/\?([a-zA-Z]+) \$/' => '$1|null $',
'/\/\*\*\n +\* (.+)\n +\*\//m' => '/** $1 */',
];
return implode(PHP_EOL, [
trim($existingHeader) . PHP_EOL,
'declare(strict_types=1);',
'',
preg_replace(
array_keys($replacements),
array_values($replacements),
$content,
),
]);
}
}