@@ -98,6 +98,28 @@ protected function formatAttributes(MarkupElement $element)
9898 return $ code ;
9999 }
100100
101+ protected function deduplicatePhpTags ($ commentPattern , &$ content , &$ childContent )
102+ {
103+ // @codeCoverageIgnoreStart
104+ $ content = preg_replace ('/ \\s \\?>$/ ' , '' , $ content );
105+ $ childContent = preg_replace ('/^< \\?(?:php)? \\s/ ' , '' , $ childContent );
106+ if ($ commentPattern &&
107+ ($ pos = mb_strpos ($ childContent , $ commentPattern )) !== false && (
108+ ($ end = mb_strpos ($ childContent , '?> ' )) === false ||
109+ $ pos < $ end
110+ ) &&
111+ preg_match ('/ \\} \\s*$/ ' , $ content )
112+ ) {
113+ $ content = preg_replace (
114+ '/ \\} \\s*$/ ' ,
115+ preg_replace ('/ \\?>< \\?php(?:php)?(\s+ \\?>< \\?php(?:php)?)*/ ' , '\\\\0 ' , $ childContent , 1 ),
116+ $ content
117+ );
118+ $ childContent = '' ;
119+ }
120+ // @codeCoverageIgnoreEnd
121+ }
122+
101123 protected function formatTwigChildElement ($ child , $ previous , &$ content , $ commentPattern )
102124 {
103125 $ childContent = $ this ->formatter ->format ($ child );
@@ -106,22 +128,7 @@ protected function formatTwigChildElement($child, $previous, &$content, $comment
106128 $ previous instanceof CodeElement &&
107129 $ previous ->isCodeBlock ()
108130 ) {
109- $ content = preg_replace ('/ \\s \\?>$/ ' , '' , $ content );
110- $ childContent = preg_replace ('/^< \\?(?:php)? \\s/ ' , '' , $ childContent );
111- if ($ commentPattern &&
112- ($ pos = mb_strpos ($ childContent , $ commentPattern )) !== false && (
113- ($ end = mb_strpos ($ childContent , '?> ' )) === false ||
114- $ pos < $ end
115- ) &&
116- preg_match ('/ \\} \\s*$/ ' , $ content )
117- ) {
118- $ content = preg_replace (
119- '/ \\} \\s*$/ ' ,
120- preg_replace ('/ \\?>< \\?php(?:php)?(\s+ \\?>< \\?php(?:php)?)*/ ' , '\\\\0 ' , $ childContent , 1 ),
121- $ content
122- );
123- $ childContent = '' ;
124- }
131+ $ this ->deduplicatePhpTags ($ commentPattern , $ content , $ childContent );
125132 }
126133
127134 if (preg_match ('/^\{% else/ ' , $ childContent )) {
0 commit comments