protected function sortTablesWithFKsToEnd(array $tables)
    {
        $return = [];

        while ($tables) {
            /** @var TableMigration $table */
            foreach ($tables as $key => $table) {
                // if there are no fks or the required fks are allready saved.
                if ((!$fks = $table->getForeignKeys()) || (count(array_intersect_key($return, $fks)) === count($fks))) {
                    $return[$table->getName()] = $table;
                    unset($tables[$key]);
                } // if
            } // foreach
        } // while

        return $return;
    }