src/Partial/Source.php
public static function normalizeIndents(string $string, string $tabulationCost = "   "): string
    {
        $string = self::normalizeEndings($string, false);
        $lines = explode("\n", $string);
        $minIndent = null;
        foreach ($lines as $line) {
            if (!trim($line)) {
                continue;
            }
            $line = str_replace("\t", $tabulationCost, $line);
            //Getting indent size
            if (!preg_match("/^( +)/", $line, $matches)) {
                //Some line has no indent
                return $string;
            }
            if ($minIndent === null) {
                $minIndent = strlen($matches[1]);
            }
            $minIndent = min($minIndent, strlen($matches[1]));
        }
        //Fixing indent
        foreach ($lines as &$line) {
            if (empty($line)) {
                continue;
            }
            //Getting line indent
            preg_match("/^([ \t]+)/", $line, $matches);
            $indent = $matches[1];
            if (!trim($line)) {
                $line = '';
                continue;
            }
            //Getting new indent
            $useIndent = str_repeat(
                " ",
                strlen(str_replace("\t", $tabulationCost, $indent)) - $minIndent
            );
            $line = $useIndent . substr($line, strlen($indent));
            unset($line);
        }

        return join("\n", $lines);
    }
public static function normalizeIndents(string $string, string $tabulationCost = "   "): string
    {
        $string = self::normalizeEndings($string, false);
        $lines = explode("\n", $string);
        $minIndent = null;
        foreach ($lines as $line) {
            if (!trim($line)) {
                continue;
            }
            $line = str_replace("\t", $tabulationCost, $line);
            //Getting indent size
            if (!preg_match("/^( +)/", $line, $matches)) {
                //Some line has no indent
                return $string;
            }
            if ($minIndent === null) {
                $minIndent = strlen($matches[1]);
            }
            $minIndent = min($minIndent, strlen($matches[1]));
        }
        //Fixing indent
        foreach ($lines as &$line) {
            if (empty($line)) {
                continue;
            }
            //Getting line indent
            preg_match("/^([ \t]+)/", $line, $matches);
            $indent = $matches[1];
            if (!trim($line)) {
                $line = '';
                continue;
            }
            //Getting new indent
            $useIndent = str_repeat(
                " ",
                strlen(str_replace("\t", $tabulationCost, $indent)) - $minIndent
            );
            $line = $useIndent . substr($line, strlen($indent));
            unset($line);
        }

        return join("\n", $lines);
    }
public static function normalizeIndents ( string $ string , string $ tabulationCost = "   " ) : string { $ string = self :: normalizeEndings ( $ string , false ) ; $ lines = explode ( "\n" , $ string ) ; $ minIndent = null ; foreach ( $ lines as $ line ) { if ( ! trim ( $ line ) ) { continue ; } $ line = str_replace ( "\t" , $ tabulationCost , $ line ) ; //Getting indent size if ( ! preg_match ( "/^( +)/" , $ line , $ matches ) ) { //Some line has no indent return $ string ; } if ( $ minIndent === null ) { $ minIndent = strlen ( $ matches [ 1 ] ) ; } $ minIndent = min ( $ minIndent , strlen ( $ matches [ 1 ] ) ) ; } //Fixing indent foreach ( $ lines as & $ line ) { if ( empty ( $ line ) ) { continue ; } //Getting line indent preg_match ( "/^([ \t]+)/" , $ line , $ matches ) ; $ indent = $ matches [ 1 ] ; if ( ! trim ( $ line ) ) { $ line = '' ; continue ; } //Getting new indent $ useIndent = str_repeat ( " " , strlen ( str_replace ( "\t" , $ tabulationCost , $ indent ) ) - $ minIndent ) ; $ line = $ useIndent . substr ( $ line , strlen ( $ indent ) ) ; unset ( $ line ) ; } return join ( "\n" , $ lines ) ; }
Shift all string lines to have minimum indent size set to 0.

Example:
|-a
|--b
|--c
|---d

Output:
|a
|-b
|-c
|--d

@param string $string         Input string with multiple lines.
@param string $tabulationCost How to treat \t symbols relatively to spaces. By default, this
is set to 4 spaces.

@return string
Shift all string lines to have minimum indent size set to 0 .