Пример #1
0
 protected static function matchContent($from, $to)
 {
     $trim = static::IGNORE_BRACES ? "{}\n\r\t " : "\n\r\t ";
     $out = trim(file_get_contents($fromPath = $from));
     $in = trim(Sbp::testFileContent($toPath = preg_replace('#^(.+)(/[^/]+)$#', '$1/.src$2', $from)));
     $to = str_replace(array("\n", "\r", "\t", ' '), '', $out);
     $from = str_replace(array("\n", "\r", "\t", ' '), '', $in);
     $cleaner = function ($value) use($trim) {
         $value = preg_replace('#/\\*.*\\*/#U', '', trim($value, $trim));
         $value = preg_replace('#\\s*[\\[\\]\\(\\)\\{\\},!]\\s*#', '$1', $value);
         return $value;
     };
     $to = $cleaner($to);
     $from = $cleaner($from);
     $lastDiffKey = -2 * static::WRAP_LINES;
     $lastPrintedKey = $lastDiffKey;
     if ($from !== $to) {
         echo "\n=====================================\n- {$toPath} (parsed)\n+ {$fromPath}\n";
         foreach (array('in', 'out') as $var) {
             ${$var} = preg_split('#\\r\\n|\\r|\\n#', preg_replace('#(\\n[\\t ]*)(\\n[\\t ]*)}([\\t ]*)(?=\\S)#', '$1}$2$3', ${$var}));
         }
         foreach ($in as $key => $line) {
             if ($cleaner($line) === $cleaner(isset($out[$key]) ? $out[$key] : '')) {
                 if ($key - $lastDiffKey === static::WRAP_LINES) {
                     echo " [...]\n";
                 } elseif ($key - $lastDiffKey < static::WRAP_LINES && $key) {
                     echo " " . str_replace("\t", '    ', $line) . "\n";
                     $lastPrintedKey = $key;
                 }
             } else {
                 for ($i = max(0, $lastPrintedKey + 1, $key - static::WRAP_LINES); $i < $key; $i++) {
                     echo " " . str_replace("\t", '    ', $in[$i]) . "\n";
                 }
                 echo "-" . str_replace("\t", '    ', $line) . "\n";
                 echo "+" . str_replace("\t", '    ', $out[$key]) . "\n";
                 $lastDiffKey = $key;
                 $lastPrintedKey = $key;
             }
         }
     }
     return $from === $to;
 }