@author Paul Butler <github@paulbutler.org>
@author Mike Robinson
@author Jonathan Beliën <jbe@geo6.be>

@see https://github.com/paulgb/simplediff/blob/master/php/simplediff.php

@param string|array $old
@param string|array $new
166645
public static function diff($old, $new): array
    {
        if (!is_array($old)) {
            $old = preg_split("/[\s]+/", $old);
        }
        if (!is_array($new)) {
            $new = preg_split("/[\s]+/", $new);
        }

        $oldNormalized = array_map(function (string $str) {
            return strtoupper(self::removeAccents($str));
        }, $old);
        $newNormalized = array_map(function (string $str) {
            return strtoupper(self::removeAccents($str));
        }, $new);

        $matrix = [];
        $maxlen = 0;

        foreach ($oldNormalized as $oindex => $ovalue) {
            $nkeys = array_keys($newNormalized, $ovalue);
            foreach ($nkeys as $nindex) {
                $matrix[$oindex][$nindex] = (isset($matrix[$oindex - 1][$nindex - 1]) ? $matrix[$oindex - 1][$nindex - 1] + 1 : 1);

                if ($matrix[$oindex][$nindex] > $maxlen) {
                    $maxlen = $matrix[$oindex][$nindex];
                    $omax = $oindex + 1 - $maxlen;
                    $nmax = $nindex + 1 - $maxlen;
                }
            }
        }

        if ($maxlen == 0) {
            return [
                [
                    'deleted'  => $old,
                    'inserted' => $new,
                ],
            ];
        }

        return array_merge(
            self::diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
            array_slice($new, $nmax, $maxlen),
            self::diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))
        );
    }
656633
public function replaceFile($old, $new, array $content)
    {
        $oldFileContent = file_get_contents($old);
        file_put_contents($new, vsprintf($oldFileContent, $content));
        unlink($old);
    }
725102
public function rename($oldName, $newName)
	{
		$form = $this->get($oldName);
		$this->delete($oldName);
		$this->add($newName, $form['code'], $form['title']);
	}
555001
public function rename($oldname, $newname)
    {
        $this->copy($oldname, $newname);

        // Delete the original object
        if ($this->isDir($oldname))
        {
            $this->deleteFolder($oldname);
        } else {
            $this->delete($oldname);
        }

        return true;
    }
981620
protected function renameResult(array &$result, $old, $new)
    {

        // Check to see if the old item is there
        if (isset($result[$old])) {
            $result[$new] = $result[$old];
            unset($result[$old]);
        }
    }
1102921
public function convertBc($name, array $old)
    {
        $new = $this->fields;

        $new[self::NAME] = $name;

        foreach ($old as $fieldName => $value) {
            $newFieldName = $this->getFieldBc($fieldName);
            $new[$newFieldName] = $value;
        }

        if (empty($new[self::RENDERER])) {
            $new[self::RENDERER] = 'rcm-plugin-bc';
        }

        if (empty($new[self::EDITOR])) {
            $new[self::EDITOR] = 'rcm-plugin-bc';
        }

        return $new;
    }
1343980
public static function merge($new, $old)
    {
        if (isset($new['domain'])) {
            unset($old['domain']);
        }

        $new = array_merge(static::formatAs($new, $old), [
            'namespace' => static::formatNamespace($new, $old),
            'prefix' => static::formatPrefix($new, $old),
            'where' => static::formatWhere($new, $old),
        ]);

        return array_merge_recursive(Arr::except(
            $old, ['namespace', 'prefix', 'where', 'as']
        ), $new);
    }
50549
protected function formatNamespace(array $new, array $old)
    {
        if (isset($new['namespace']) && isset($old['namespace'])) {
            return trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\');
        } elseif (isset($new['namespace'])) {
            return trim($new['namespace'], '\\');
        }

        return $old['namespace'] ?? null;
    }
624707
private function buildDiff(Text $old, Text $new, $diffTable) {
        $inserted = 0;
        $deleted = 0;
        $diff = [];

        $row = count($this->old);
        $col = count($this->new);
        while ($row > 0 || $col > 0) {
            if ($row > 0 && $col > 0 && $this->old[$row - 1] === $this->new[$col - 1]) {
                $diff[] = new UnmodifiedEntry($old, $row - 1, $col - 1);
                $row--;
                $col--;
            } elseif ($col > 0 && $diffTable[$row][$col] === $diffTable[$row][$col - 1]) {
                $diff[] = new InsertedEntry($new, $col - 1);
                $inserted++;
                $col--;
            } else {
                $diff[] = new DeletedEntry($old, $row - 1);
                $deleted++;
                $row--;
            }
        }

        return [array_reverse($diff), $inserted, $deleted];
    }
854059
protected function auditUpdatedAttributes(array &$old, array &$new)
    {
        foreach ($this->getDirty() as $attribute => $value) {
            if ($this->isAttributeAuditable($attribute)) {
                $old[$attribute] = array_get($this->original, $attribute);
                $new[$attribute] = array_get($this->attributes, $attribute);
            }
        }

        if ($this->shouldDynamicTableExist()) {
            foreach ($this->properties->getDirty() as $attribute => $value) {
                if (! in_array($attribute, $this->auditableExclusions)) {
                    $old[$this->type->identifier][$attribute] = array_get($this->properties->getOriginal(), $attribute);
                    $new[$this->type->identifier][$attribute] = array_get($this->properties->getAttributes(), $attribute);
                }
            }
        }
    }
1146402
function diff( $old, $new, $options = false )
    {
        $diff = new eZDiff();
        $diff->setDiffEngineType( $diff->engineType( 'container' ) );
        $diff->initDiffEngine();
        $diffObject = $diff->diff( $old, $new );
        return $diffObject;
    }
1353064
public function generateDiff($key, $oldValue, $newValue)
    {
        $this->diff[$key] = [
            self::OLD_VALUE => $oldValue,
            self::NEW_VALUE => $newValue
        ];
    }
1042424
protected function getForeignKeysMigrations($new = [], $old = [])
    {
        if (empty($new['tables'])) {
            return [];
        }
        $output = [];
        foreach ($new['tables'] as $tableId => $newTable) {
            if ($newTable['table']['table_name'] === $this->options['default_migration_table']) {
                continue;
            }
            $oldTable = $old['tables'][$tableId];

            if (!empty($oldTable['foreign_keys'])) {
                foreach ($oldTable['foreign_keys'] as $fkName => $fkData) {
                    if (!isset($newTable['foreign_keys'][$fkName])) {
                        $output[] = $this->getForeignKeyRemove($tableId, $fkName);
                    }
                }
            }
            if (!empty($newTable['foreign_keys'])) {
                foreach ($newTable['foreign_keys'] as $fkName => $fkData) {
                    if (!isset($oldTable['foreign_keys'][$fkName])) {
                        $output[] = $this->getForeignKeyCreate($tableId, $fkName);
                    }
                }
            }
        }

        return $output;
    }
1105726
public function rename($new, $old = null)
    {
        $this->arguments[] = 'rename';
        if ($old) {
            $this->arguments[] = $old;
        }
        $this->arguments[] = $new;
        return $this;
    }
1217052
public static function mergeGroup($new, $old)
	{
		$new['namespace'] = static::formatUsesPrefix($new, $old);

		$new['prefix'] = static::formatGroupPrefix($new, $old);

		if (isset($new['domain'])) unset($old['domain']);

		return array_merge_recursive(array_except($old, array('namespace', 'prefix')), $new);
	}
106403
private function diffRecords($old, $new)
	{
		$diff = Set::diff($old, $new);
		$formatted = array();

		foreach ($diff as $key => $value) {
			if (!isset($old[$key]) || !isset($new[$key])) {
				continue;
			}

			$formatted[$key] = array('old' => $old[$key], 'new' => $new[$key]);
		}

		return $formatted;
	}
1320698
protected static function formatPrefix($new, $old)
    {
        $old = $old['prefix'] ?? null;

        return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old;
    }