@@ -240,16 +240,29 @@ protected function formatStringFromLines(string $string): string
240240 /**
241241 * Replace tabs in a string with a number of spaces.
242242 *
243- * @param string $string the containing tabs to convert
244- * @param int $tabSize one tab = how many spaces, a negative does nothing
243+ * @param string $string the containing tabs to convert
244+ * @param int $tabSize one tab = how many spaces, a negative does nothing
245+ * @param bool $onlyLeadingTabs only expand leading tabs
245246 *
246247 * @return string the string with the tabs converted to spaces
247248 */
248- protected function expandTabs (string $ string , int $ tabSize = 4 ): string
249+ protected function expandTabs (string $ string , int $ tabSize = 4 , bool $ onlyLeadingTabs = false ): string
249250 {
250- return $ tabSize >= 0
251- ? \str_replace ("\t" , \str_repeat (' ' , $ tabSize ), $ string )
252- : $ string ;
251+ if ($ tabSize < 0 ) {
252+ return $ string ;
253+ }
254+
255+ if ($ onlyLeadingTabs ) {
256+ return \preg_replace_callback (
257+ "/^[ \t]{1,}/mS " , // tabs and spaces may be mixed
258+ function (array $ matches ): string {
259+ return \str_replace ("\t" , \str_repeat (' ' , $ tabSize ), $ matches [0 ]);
260+ },
261+ $ string
262+ );
263+ }
264+
265+ return \str_replace ("\t" , \str_repeat (' ' , $ tabSize ), $ string );
253266 }
254267
255268 /**
0 commit comments