Skip to content

Instantly share code, notes, and snippets.

@kkeith-adg
Created March 25, 2021 03:45
Show Gist options
  • Save kkeith-adg/f29cb3008c2888d330a40688b4c65d50 to your computer and use it in GitHub Desktop.
Save kkeith-adg/f29cb3008c2888d330a40688b4c65d50 to your computer and use it in GitHub Desktop.
Authorize.net PHP SDK - PHP 8 Patch
<?php
declare(strict_types=1);
const AUTHNET_PATH = __DIR__ . '/vendor/authorizenet/authorizenet/lib/net/authorize/api/contract/v1';
function get_php_files(string $path): Generator {
$path = rtrim($path, '/');
if (!is_dir($path)) {
throw new Exception(sprintf('Directory does not exist: %s', $path));
}
$files = scandir($path);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$file_path = "{$path}/{$file}";
if (is_dir($file_path)) {
yield from get_php_files($file_path);
continue;
}
if (!str_ends_with($file, '.php')) {
continue;
}
yield $file_path;
}
}
$old =<<<'PHP'
if (get_parent_class() == ""){
return $values;
}
else{
return array_merge(parent::jsonSerialize(), $values);
}
PHP;
$replacements = [
'parent' => 'return array_merge(parent::jsonSerialize(), $values);',
'no-parent' => 'return $values;'
];
foreach (get_php_files(AUTHNET_PATH) as $path) {
if (($contents = file_get_contents($path)) === false) {
throw new Exception(sprintf('Unable to get file contents: %s', $path));
}
if (!str_contains($contents, $old)) {
continue;
}
echo "Patching file: {$path}" . PHP_EOL;
$type = preg_match('#class\s[a-zA-Z0-9_]+\sextends#', $contents) === 1 ? 'parent' : 'no-parent';
$contents = str_replace($old, $replacements[$type], $contents);
if (file_put_contents($path, $contents) === false) {
throw new Exception(sprintf('Unable to write to file: %s', $path));
}
}
echo 'Done' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment