Add Diff::rawDiffFromSebastianDiff()
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 3 Aug 2022 10:41:56 +0000 (12:41 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 3 Aug 2022 10:43:17 +0000 (12:43 +0200)
This eases the migration, because consumers can switch to sebastian/diff's
generation logic, while preserving the legacy output format.

wcfsetup/install/files/lib/util/Diff.class.php

index 2bae23353d138a6b82624c814626e540d6fb3bab..68c4384f0c3bc02136ec98d490869b9eada74f36 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace wcf\util;
 
+use SebastianBergmann\Diff\Differ;
+
 /**
  * Diff calculates the longest common subsequence of two given
  * arrays and is able to generate the differences (added / removed items)
@@ -333,6 +335,26 @@ class Diff
         return \implode("\n", $result);
     }
 
+    /**
+     * Transforms the output of sebastian/diff's Differ::diffToArray() into
+     * the output of self::getRawDiff().
+     *
+     * @since 5.6
+     */
+    public static function rawDiffFromSebastianDiff(array $arrayDiff): array
+    {
+        return \array_map(static function ($entry) {
+            return [
+                match($entry[1]) {
+                    Differ::ADDED => self::ADDED,
+                    Differ::REMOVED => self::REMOVED,
+                    Differ::OLD => self::SAME,
+                },
+                $entry[0],
+            ];
+        }, $arrayDiff);
+    }
+
     /**
      * @see Diff::getUnixDiff()
      */