<?php


$manualTweaks = [
    [
        'files' => [ 'RAGLD.php'],
        'tweaks' => [
            'hexdec\(sha1\(rand\(\) . microtime\(\)\)\)' => 'hexdec(substr(sha1(rand() . microtime()), 0, 8))',
            'trim\(`which rapper`\) == \'\'\)' => 'exec(\'rapper -h\') === \'\')',
            '^abstract class' => "#[\\AllowDynamicProperties]\nabstract class",
            '\$this->config\(\'debug\', true\);' => '# $this->config(\'debug\', true);',
            'parent::__construct\(\)' => 'parent::__construct([\'debug\' => true]);',
            'if \(empty\(\$this->requestedMimeTypes\)\) \$this->requestedMimeTypes\[\'type\'\]\[\] = \'text/html\';' => 'if ($this->requestedMimeTypes == false || empty($this->requestedMimeTypes)) $this->requestedMimeTypes = [\'type\' => [\'text/html\']];',
        ]
    ],
    [
        'files' => [ 'services/linked-data/LinkedDataPublisher.php' ],
        'tweaks' => [
            'strToLower\(\$r->value->getLang\(\)\)' => 'strToLower($r->value->getLang() ?? \'\')'
        ],
    ],
    [
        'files' => [ 'lib/easyrdf/lib/EasyRdf/ParsedUri.php'],
        'tweaks' => [
            'implode\(\$newSegments, \'/\'\);' => 'implode(\'/\', $newSegments);',
        ]
    ],
    [
        'files' => [ 'lib/easyrdf/lib/EasyRdf/Sparql/Client.php' ],
        'tweaks' => [
            'parse_url\(\$queryUri, PHP_URL_QUERY\)\)' => 'parse_url($queryUri, PHP_URL_QUERY) ?? "")'
        ]
    ],
    [
        'files' => [ 'lib/easyrdf/lib/EasyRdf/Http/Response.php' ],
        'tweaks' => [
            '^class' => "#[\\AllowDynamicProperties]\nclass",
            'strtolower\(\$this->getHeader\(\'transfer-encoding\'\)\)' => 'strtolower($this->getHeader(\'transfer-encoding\') ?? \'\')'
        ]
    ],
    [
        'files' => [ 'lib/easyrdf/lib/EasyRdf/Collection.php' ],
        'tweaks' => [ 
            'public function offsetSet\(\$offset, \$value\)$' => "#[\\ReturnTypeWillChange]\n    public function offsetSet(mixed \$offset, mixed \$value): mixed"
        ]
    ],
    [
        'files' => [ 'lib/easyrdf/lib/EasyRdf/Container.php' ],
        'tweaks' => [ 
            'public function offsetSet\(\$offset, \$value\)$' => "#[\\ReturnTypeWillChange]\n    public function offsetSet(mixed \$offset, mixed \$value): mixed",
            'public function offsetUnset\(\$offset\)$' => "#[\\ReturnTypeWillChange]\n    public function offsetUnset(mixed \$offset): mixed"
        ]
    ],
    [
        'files' => [ 'lib/slim/Slim/Views/TwigView.php' ],
        'tweaks' => [
            'public function render\(\$template\)$' => "public function render(\$template, \$data=null)"
        ]
    ],
    [
        'files' => [ 'lib/slim/Slim/Http/Util.php' ],
        'tweaks' => [
            '\$strip = is_null.*$' => '$strip = false;',
            '\$header = rtrim\(\$header,' => '$header = rtrim($header ?? "",'
        ]
    ],
    [
        'files' => [ 'lib/twig/lib/Twig/Lexer.php' ],
        'tweaks' => [ 
            '\$match, null, \$this->cursor' => '$match, 0, $this->cursor' 
        ]
    ],
    [
        'files' => [
            'services/canned-query/CannedQueryMongo.php',
            'services/canned-query/CannedQuerySQL.php',
            'services/canned-query/CannedQuerySparql.php',
            'services/canned-query/CannedQuery.php',
        ],
        'tweaks' => [
            'public function rewind\(\) \{$' => 'public function rewind(): void {',
            'public function current\(\) \{$' => 'public function current(): mixed {',
            'public function key\(\) \{$' => 'public function key(): mixed {',
            'public function next\(\) \{$' => 'public function next(): void {',
            'public function valid\(\) \{$' => 'public function valid(): bool {',
            'public function count\(\) \{$' => 'public function count(): int {',
            "\t\\\$desc = preg_replace" => "\tif (\$desc != null) \$desc = preg_replace",
        ]
    ]
];

$genericEdits = [
  'public function getIterator\(\)$'                => 'public function getIterator(): \\ArrayIterator',
  'public function offsetExists\(\$offset\)$'       => 'public function offsetExists(mixed $offset): bool',
  'public function offsetGet\(\$offset\)$'          => 'public function offsetGet(mixed $offset): mixed',
  'public function offsetSet\(\$offset, \$value\)$' => 'public function offsetSet(mixed $offset, mixed $value): void',
  'public function offsetUnset\(\$offset\)$'        => 'public function offsetUnset(mixed $offset): void',
  'public function count\(\)$'                      => 'public function count(): int',
  'public function seek\(\$position\)$'             => 'public function seek(int $position): void',
  'public function current\(\)$'                    => 'public function current(): mixed',
  'public function next\(\)$'                       => 'public function next(): void',
  'public function key\(\)$'                        => 'public function key(): mixed',
  'public function valid\(\)$'                      => 'public function valid(): bool',
  'public function rewind\(\)$'                     => 'public function rewind(): void',
];


foreach($manualTweaks as $operation) {
    foreach($operation['files'] as $f) {
        $old = file_get_contents($f);
        $new = $old;
        foreach($operation['tweaks'] as $search => $replace) {
            #$numMatches = preg_match_all("@$search@m", $new, $matches);
            #print("$numMatches\t$search\n");
            $new = preg_replace("@$search@m", $replace, $new);
        }
        if ($old != $new) {
            file_put_contents($f, $new);
            print("Updated $f\n");
        }
    }
}

exec("find . -name '*.php' -type f", $allFiles);
foreach($allFiles as $f) {
    if (strpos($f, 'Twig/TokenStream.php') !== false) continue;
    $f = trim($f);
    $old = file_get_contents($f);
    $new = $old;
    foreach($genericEdits as $search => $replace) {
        #$numMatches = preg_match_all("@$search@m", $new, $matches);
        #print("$numMatches\t$search\n");
        $new = preg_replace("@$search@m", $replace, $new);
    }
    if ($old != $new) {
        file_put_contents($f, $new);
        print("Updated $f\n");
    }
}


/*
 last resport -- turn off deprecation warnings
 sudo sed $inplace 's@//  initialise environment@//  initialise environment\nerror_reporting(E_ALL ^ E_DEPRECATED);@' index.php
 sudo sed $inplace 's@error_reporting.*@error_reporting(E_ALL ^ E_DEPRECATED);@' RAGLD.php
*/
