55use uuf6429 \PhpCsFixerBlockstring \BlockString \BlockString ;
66use uuf6429 \PhpCsFixerBlockstring \InterpolationCodec \CodecInterface ;
77use uuf6429 \PhpCsFixerBlockstring \InterpolationCodec \PlainStringCodec ;
8+ use uuf6429 \PhpCsFixerBlockstring \LineEndingNormalizer \DefaultNormalizer ;
9+ use uuf6429 \PhpCsFixerBlockstring \LineEndingNormalizer \NormalizerInterface ;
810
911/**
1012 * This formatter base class is aware of string interpolation - it passes content through a codec before and after
2527 * }
2628 * }
2729 *
28- * ['formatters' => [ new MyFormatter('1.0', new PlainStringCodec() ) ]]
30+ * ['formatters' => [ new MyFormatter('1.0') ]]
2931 * ```
3032 *
3133 * Example with an anonymous class:
3234 *
3335 * ```php
3436 * ['formatters' => [
35- * new class ('1.0', new PlainStringCodec() ) extends AbstractCodecFormatter
37+ * new class ('1.0') extends AbstractCodecFormatter
3638 * {
3739 * protected function formatContent(string $original): string
3840 * {
@@ -58,12 +60,21 @@ abstract class AbstractCodecFormatter extends AbstractFormatter
5860 */
5961 protected CodecInterface $ interpolationCodec ;
6062
61- public function __construct (string $ version , ?CodecInterface $ interpolationCodec )
62- {
63+ /**
64+ * @readonly
65+ */
66+ private NormalizerInterface $ lineEndingNormalizer ;
67+
68+ public function __construct (
69+ string $ version ,
70+ ?CodecInterface $ interpolationCodec =null ,
71+ ?NormalizerInterface $ lineEndingNormalizer =null
72+ ) {
6373 parent ::__construct ($ version );
6474
6575 $ this ->objectIndex = self ::$ objectCounter ++;
6676 $ this ->interpolationCodec = $ interpolationCodec ?? new PlainStringCodec ();
77+ $ this ->lineEndingNormalizer = $ lineEndingNormalizer ?? new DefaultNormalizer (DefaultNormalizer::NO_CHANGE , DefaultNormalizer::NO_CHANGE );
6778 }
6879
6980 final public function formatBlock (BlockString $ blockString ): BlockString
@@ -72,8 +83,10 @@ final public function formatBlock(BlockString $blockString): BlockString
7283
7384 $ cacheKey = $ this ->objectIndex . ': ' . md5 ($ codecResult ->content );
7485 if (!isset (self ::$ cache [$ cacheKey ])) {
75- self ::$ cache [$ cacheKey ] = $ this ->formatContent (
76- $ this ->removeIndentation ($ codecResult ->content , $ blockString ->indentation )
86+ $ content = $ this ->removeIndentation ($ codecResult ->content , $ blockString ->indentation );
87+ self ::$ cache [$ cacheKey ] = $ this ->lineEndingNormalizer ->normalize (
88+ $ this ->formatContent ($ content ),
89+ $ content
7790 );
7891 }
7992 $ newContent = $ this ->applyIndentation (self ::$ cache [$ cacheKey ], $ blockString ->indentation );
@@ -90,11 +103,11 @@ abstract protected function formatContent(string $original): string;
90103
91104 private function removeIndentation (string $ lines , string $ indentation ): string
92105 {
93- return substr (str_replace ("\n{$ indentation }" , "\n" , $ lines ), strlen ($ indentation ));
106+ return substr (str_replace ("\n{$ indentation }" , "\n" , $ lines ), strlen ($ indentation )); // TODO feels wrong (eol)
94107 }
95108
96109 private function applyIndentation (string $ lines , string $ indentation ): string
97110 {
98- return $ indentation . str_replace ("\n" , "\n{$ indentation }" , $ lines );
111+ return $ indentation . str_replace ("\n" , "\n{$ indentation }" , $ lines ); // TODO feels wrong (eol)
99112 }
100113}
0 commit comments