From 218560339fb04809d3cd3710fea219e730af564b Mon Sep 17 00:00:00 2001 From: lacatoire Date: Tue, 11 Nov 2025 21:19:34 +0100 Subject: [PATCH] refactor: replace trigger_error(E_USER_ERROR) with exceptions (PHP 8.4 compatibility) --- phpdotnet/phd/Autoloader.php | 2 +- phpdotnet/phd/Format/Factory.php | 2 +- phpdotnet/phd/Options/Handler.php | 26 +++++++++---------- phpdotnet/phd/Options/Parser.php | 8 +++--- .../phd/Package/Generic/ChunkedXHTML.php | 4 +-- phpdotnet/phd/Package/Generic/Manpage.php | 4 +-- phpdotnet/phd/Package/Generic/TocFeed.php | 4 +-- phpdotnet/phd/Package/Generic/XHTML.php | 4 +-- phpdotnet/phd/Package/IDE/API.php | 4 +-- phpdotnet/phd/Package/IDE/Base.php | 6 ++--- phpdotnet/phd/Package/IDE/demo.php | 16 ++++++------ phpdotnet/phd/Package/PEAR/ChunkedXHTML.php | 4 +-- phpdotnet/phd/Package/PHP/ChunkedXHTML.php | 4 +-- phpdotnet/phd/Package/PHP/EnhancedCHM.php | 15 +++++++---- phpdotnet/phd/Package/PHP/HowTo.php | 4 +-- phpdotnet/phd/Package/PHP/Web.php | 6 ++--- phpdotnet/phd/Package/PHP/XHTML.php | 6 ++--- render.php | 6 ++--- 18 files changed, 65 insertions(+), 60 deletions(-) diff --git a/phpdotnet/phd/Autoloader.php b/phpdotnet/phd/Autoloader.php index 9ef5d0b8..38e28137 100644 --- a/phpdotnet/phd/Autoloader.php +++ b/phpdotnet/phd/Autoloader.php @@ -26,7 +26,7 @@ public static function autoload(string $name): void return; } - trigger_error(vsprintf('Cannot find file for %s: %s', [$name, $file ?? $filename]), E_USER_ERROR); + throw new \Error(vsprintf('Cannot find file for %s: %s', [$name, $file ?? $filename])); } } diff --git a/phpdotnet/phd/Format/Factory.php b/phpdotnet/phd/Format/Factory.php index e2046451..4544aea2 100644 --- a/phpdotnet/phd/Format/Factory.php +++ b/phpdotnet/phd/Format/Factory.php @@ -49,7 +49,7 @@ public final function createFormat($format, ...$formatParams) { } return $obj; } - trigger_error("This format is not supported by this package", E_USER_ERROR); + throw new \Error('This format is not supported by this package'); } public static final function createFactory($package) { diff --git a/phpdotnet/phd/Options/Handler.php b/phpdotnet/phd/Options/Handler.php index a2656227..9af33e90 100644 --- a/phpdotnet/phd/Options/Handler.php +++ b/phpdotnet/phd/Options/Handler.php @@ -184,10 +184,10 @@ public function option_d(string $k, mixed $v): array public function option_docbook(string $k, mixed $v): array { if (is_array($v)) { - trigger_error("Can only parse one file at a time", E_USER_ERROR); + throw new \Error('Can only parse one file at a time'); } if (!file_exists($v) || is_dir($v) || !is_readable($v)) { - trigger_error(sprintf("'%s' is not a readable docbook file", $v), E_USER_ERROR); + throw new \Error(sprintf("'%s' is not a readable docbook file", $v)); } return [ 'xmlRoot' => dirname($v), @@ -209,19 +209,19 @@ public function option_o(string $k, mixed $v): array public function option_output(string $k, mixed $v): array { if (is_array($v)) { - trigger_error("Only a single output location can be supplied", E_USER_ERROR); + throw new \Error('Only a single output location can be supplied'); } if (!file_exists($v)) { $this->outputHandler->v("Creating output directory..", VERBOSE_MESSAGES); if (!mkdir($v, 0777, true)) { - trigger_error(vsprintf("Can't create output directory : %s", [$v]), E_USER_ERROR); + throw new \Error(vsprintf("Can't create output directory : %s", [$v])); } $this->outputHandler->v("Output directory created", VERBOSE_MESSAGES); } elseif (!is_dir($v)) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } if (!is_dir($v) || !is_readable($v)) { - trigger_error(sprintf("'%s' is not a valid directory", $v), E_USER_ERROR); + throw new \Error(sprintf("'%s' is not a valid directory", $v)); } $v = (substr($v, strlen($v) - strlen(DIRECTORY_SEPARATOR)) === DIRECTORY_SEPARATOR) ? $v : ($v . DIRECTORY_SEPARATOR); @@ -234,7 +234,7 @@ public function option_output(string $k, mixed $v): array public function option_outputfilename(string $k, mixed $v): array { if (is_array($v)) { - trigger_error("Only a single output location can be supplied", E_USER_ERROR); + throw new \Error('Only a single output location can be supplied'); } $file = basename($v); @@ -281,7 +281,7 @@ public function option_package(string $k, mixed $v): array foreach((array)$v as $package) { if (!in_array($package, $this->config->getSupportedPackages())) { $supported = implode(', ', $this->config->getSupportedPackages()); - trigger_error("Invalid Package (Tried: '$package' Supported: '$supported')", E_USER_ERROR); + throw new \Error("Invalid Package (Tried: '$package' Supported: '$supported')"); } } return ['package' => (array) $v]; @@ -341,13 +341,13 @@ public function option_skip(string $k, mixed $v): array public function option_saveconfig(string $k, mixed $v): array { if (is_array($v)) { - trigger_error(sprintf("You cannot pass %s more than once", $k), E_USER_ERROR); + throw new \Error(sprintf('You cannot pass %s more than once', $k)); } $val = is_bool($v) ? true : self::boolval($v); if (!is_bool($val)) { - trigger_error("yes/no || on/off || true/false || 1/0 expected", E_USER_ERROR); + throw new \Error('yes/no || on/off || true/false || 1/0 expected'); } return ['saveConfig' => $val]; @@ -381,7 +381,7 @@ public function option_verbose(string $k, mixed $v): array $verbose = max($verbose, 1); $verbose <<= 1; } else { - trigger_error("Unknown option passed to --$k, '$const'", E_USER_ERROR); + throw new \Error("Unknown option passed to --$k, '$const'"); } } } @@ -438,11 +438,11 @@ public function option_c(string $k, mixed $v): array public function option_color(string $k, mixed $v): array { if (is_array($v)) { - trigger_error(sprintf("You cannot pass %s more than once", $k), E_USER_ERROR); + throw new \Error(sprintf('You cannot pass %s more than once', $k)); } $val = self::boolval($v); if (!is_bool($val)) { - trigger_error("yes/no || on/off || true/false || 1/0 expected", E_USER_ERROR); + throw new \Error('yes/no || on/off || true/false || 1/0 expected'); } return ['colorOutput' => $val]; } diff --git a/phpdotnet/phd/Options/Parser.php b/phpdotnet/phd/Options/Parser.php index c8763000..af50f0c6 100644 --- a/phpdotnet/phd/Options/Parser.php +++ b/phpdotnet/phd/Options/Parser.php @@ -80,11 +80,11 @@ private function validateOptions(): void { $checkArgv = explode('=', $argv[$i]); if (substr($checkArgv[0], 0, 2) === '--') { if (!in_array(substr($checkArgv[0], 2), $long)) { - trigger_error('Invalid long option ' . $argv[$i], E_USER_ERROR); + throw new \Error('Invalid long option ' . $argv[$i]); } } elseif (substr($checkArgv[0], 0, 1) === '-') { if (!in_array(substr($checkArgv[0], 1), $short)) { - trigger_error('Invalid short option ' . $argv[$i], E_USER_ERROR); + throw new \Error('Invalid short option ' . $argv[$i]); } } } @@ -98,7 +98,7 @@ public function getopt(): array { $args = getopt($this->getShortOptions(), $this->getLongOptions()); if ($args === false) { - trigger_error("Something happend with getopt(), please report a bug", E_USER_ERROR); + throw new \Error('Something happend with getopt(), please report a bug'); } $parsedOptions = []; @@ -106,7 +106,7 @@ public function getopt(): array { $handler = $this->handlerForOption($k); if (!is_callable($handler)) { - trigger_error("Hmh, something weird has happend, I don't know this option", E_USER_ERROR); + throw new \Error("Hmh, something weird has happend, I don't know this option"); } $retVal = call_user_func($handler, $k, $v); diff --git a/phpdotnet/phd/Package/Generic/ChunkedXHTML.php b/phpdotnet/phd/Package/Generic/ChunkedXHTML.php index 83e0481d..a8860eab 100644 --- a/phpdotnet/phd/Package/Generic/ChunkedXHTML.php +++ b/phpdotnet/phd/Package/Generic/ChunkedXHTML.php @@ -77,11 +77,11 @@ public function update($event, $value = null) { $this->postConstruct(); if (file_exists($this->getOutputDir())) { if (!is_dir($this->getOutputDir())) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } } else { if (!mkdir($this->getOutputDir(), 0777, true)) { - trigger_error("Can't create output directory", E_USER_ERROR); + throw new \Error("Can't create output directory"); } } if ($this->config->css) { diff --git a/phpdotnet/phd/Package/Generic/Manpage.php b/phpdotnet/phd/Package/Generic/Manpage.php index 7357f802..d4b86364 100644 --- a/phpdotnet/phd/Package/Generic/Manpage.php +++ b/phpdotnet/phd/Package/Generic/Manpage.php @@ -316,11 +316,11 @@ public function update($event, $value = null) { $this->setOutputDir($this->config->outputDir . strtolower($this->toValidName($this->getFormatName())) . '/'); if (file_exists($this->getOutputDir())) { if (!is_dir($this->getOutputDir())) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } } else { if (!mkdir($this->getOutputDir(), 0777, true)) { - trigger_error("Can't create output directory", E_USER_ERROR); + throw new \Error("Can't create output directory"); } } break; diff --git a/phpdotnet/phd/Package/Generic/TocFeed.php b/phpdotnet/phd/Package/Generic/TocFeed.php index 0365b007..729a3391 100644 --- a/phpdotnet/phd/Package/Generic/TocFeed.php +++ b/phpdotnet/phd/Package/Generic/TocFeed.php @@ -224,11 +224,11 @@ public function update($event, $value = null) $dir = $this->getOutputDir(); if (file_exists($dir)) { if (!is_dir($dir)) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } } else { if (!mkdir($dir, 0777, true)) { - trigger_error("Can't create output directory", E_USER_ERROR); + throw new \Error("Can't create output directory"); } } break; diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index 8c419e42..156c5d03 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -678,11 +678,11 @@ protected function fetchStylesheet($name = null) { $stylesDir .= 'styles/'; if (file_exists($stylesDir)) { if (!is_dir($stylesDir)) { - trigger_error("The styles/ directory is a file?", E_USER_ERROR); + throw new \Error('The styles/ directory is a file?'); } } else { if (!mkdir($stylesDir, 0777, true)) { - trigger_error("Can't create the styles/ directory.", E_USER_ERROR); + throw new \Error("Can't create the styles/ directory."); } } foreach ((array)$this->config->css as $css) { diff --git a/phpdotnet/phd/Package/IDE/API.php b/phpdotnet/phd/Package/IDE/API.php index 97f6543f..73bc7302 100644 --- a/phpdotnet/phd/Package/IDE/API.php +++ b/phpdotnet/phd/Package/IDE/API.php @@ -72,10 +72,10 @@ public function __construct($dir) : $dir . DIRECTORY_SEPARATOR; $this->funclist = file($this->dir . self::FUNCLIST_FILE, FILE_IGNORE_NEW_LINES); } else { - trigger_error('Is the PhD output directory a file?', E_USER_ERROR); + throw new \Error('Is the PhD output directory a file?'); } } else { - trigger_error('The PhD output directory does not exist.', E_USER_ERROR); + throw new \Error('The PhD output directory does not exist.'); } } diff --git a/phpdotnet/phd/Package/IDE/Base.php b/phpdotnet/phd/Package/IDE/Base.php index f1992040..b05b1f66 100644 --- a/phpdotnet/phd/Package/IDE/Base.php +++ b/phpdotnet/phd/Package/IDE/Base.php @@ -215,7 +215,7 @@ public function loadVersionInfo() { if (file_exists($this->config->phpwebVersionFilename)) { $this->versions = self::generateVersionInfo($this->config->phpwebVersionFilename); } else { - trigger_error("Can't load the versions file", E_USER_ERROR); + throw new \Error("Can't load the versions file"); } } @@ -223,11 +223,11 @@ public function createOutputDirectory() { $this->setOutputDir($this->config->outputDir . strtolower($this->getFormatName()) . '/'); if (file_exists($this->getOutputDir())) { if (!is_dir($this->getOutputDir())) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } } else { if (!mkdir($this->getOutputDir(), 0777, true)) { - trigger_error("Can't create output directory", E_USER_ERROR); + throw new \Error("Can't create output directory"); } } } diff --git a/phpdotnet/phd/Package/IDE/demo.php b/phpdotnet/phd/Package/IDE/demo.php index fd7601cd..7dcb8e46 100644 --- a/phpdotnet/phd/Package/IDE/demo.php +++ b/phpdotnet/phd/Package/IDE/demo.php @@ -64,21 +64,21 @@ function usage() case 'd': case 'dir': if (is_array($v)) { - trigger_error('Is not possible to pass the --dir option more then once', E_USER_ERROR); + throw new \Error('Is not possible to pass the --dir option more then once'); } $OPTION['dir'] = $v; break; case 'f': case 'function': if (is_array($v)) { - trigger_error('Is not possible to pass the --function option more then once', E_USER_ERROR); + throw new \Error('Is not possible to pass the --function option more then once'); } $OPTION['function'] = $v; break; case 'c': case 'class': if (is_array($v)) { - trigger_error('Is not possible to pass the --class option more then once', E_USER_ERROR); + throw new \Error('Is not possible to pass the --class option more then once'); } $OPTION['class'] = $v; break; @@ -107,7 +107,7 @@ function usage() $OPTION['help'] = true; break; default: - trigger_error('Invalid Option: ' . $k, E_USER_ERROR); + throw new \Error('Invalid Option: ' . $k); } } @@ -117,11 +117,11 @@ function usage() } if ($OPTION['dir'] == NULL) { - trigger_error('You must specify the PhD output directory with the --dir option.', E_USER_ERROR); + throw new \Error('You must specify the PhD output directory with the --dir option.'); } if ($OPTION['function'] == NULL && $OPTION['class'] == NULL) { - trigger_error('You must pass either --class or --function options.', E_USER_ERROR); + throw new \Error('You must pass either --class or --function options.'); } $api = new PhD\Package_IDE_API($OPTION['dir']); @@ -129,7 +129,7 @@ function usage() if ($OPTION['class'] != NULL) { $methods = $api->getMethodsByClass($OPTION['class']); if ($methods == NULL) { - trigger_error('Invalid Class name: ' . $OPTION['class'], E_USER_ERROR); + throw new \Error('Invalid Class name: ' . $OPTION['class']); } foreach ($methods as $method) { echo $method . PHP_EOL; @@ -140,7 +140,7 @@ function usage() $function = $api->getFunctionByName($OPTION['function']); if ($function == NULL) { - trigger_error('Invalid Function: ' . $OPTION['function'], E_USER_ERROR); + throw new \Error('Invalid Function: ' . $OPTION['function']); } if ($OPTION['signature'] === true) { diff --git a/phpdotnet/phd/Package/PEAR/ChunkedXHTML.php b/phpdotnet/phd/Package/PEAR/ChunkedXHTML.php index 8a8383b5..c924b0cf 100755 --- a/phpdotnet/phd/Package/PEAR/ChunkedXHTML.php +++ b/phpdotnet/phd/Package/PEAR/ChunkedXHTML.php @@ -80,11 +80,11 @@ public function update($event, $value = null) { $this->postConstruct(); if (file_exists($this->getOutputDir())) { if (!is_dir($this->getOutputDir())) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } } else { if (!mkdir($this->getOutputDir(), 0777, true)) { - trigger_error("Can't create output directory", E_USER_ERROR); + throw new \Error("Can't create output directory"); } } if ($this->config->css) { diff --git a/phpdotnet/phd/Package/PHP/ChunkedXHTML.php b/phpdotnet/phd/Package/PHP/ChunkedXHTML.php index 9c26c31b..bac8973d 100644 --- a/phpdotnet/phd/Package/PHP/ChunkedXHTML.php +++ b/phpdotnet/phd/Package/PHP/ChunkedXHTML.php @@ -241,12 +241,12 @@ private function mkdir(string $dir): void { if (file_exists($dir)) { if (!is_dir($dir)) { - trigger_error("The specified directory is a file: {$dir}", E_USER_ERROR); + throw new \Error("The specified directory is a file: {$dir}"); } return; } if (!mkdir($dir, 0777, true)) { - trigger_error("Can't create the specified directory: {$dir}", E_USER_ERROR); + throw new \Error("Can't create the specified directory: {$dir}"); } } diff --git a/phpdotnet/phd/Package/PHP/EnhancedCHM.php b/phpdotnet/phd/Package/PHP/EnhancedCHM.php index 25532a10..fd437701 100644 --- a/phpdotnet/phd/Package/PHP/EnhancedCHM.php +++ b/phpdotnet/phd/Package/PHP/EnhancedCHM.php @@ -26,15 +26,20 @@ public function update($event, $value = null) { // Use %TEMP%/usernotes as base directory for Usernotes. $temp = sys_get_temp_dir(); if (!$temp || !is_dir($temp)) { - trigger_error('Unable to locate the systems temporary system directory for EnhancedCHM.', E_USER_ERROR); + throw new \Error('Unable to locate the systems temporary system directory for EnhancedCHM.'); break; } $this->userNotesBaseDir = $temp . DIRECTORY_SEPARATOR . 'usernotes' . DIRECTORY_SEPARATOR; // Make the usernotes directory. - if(!file_exists($this->userNotesBaseDir) || is_file($this->userNotesBaseDir)) { - mkdir($this->userNotesBaseDir, 0777, true) or trigger_error(vsprintf("Can't create the usernotes directory : %s", [$this->userNotesBaseDir]), E_USER_ERROR); + if (!file_exists($this->userNotesBaseDir) || is_file($this->userNotesBaseDir)) { + if (!mkdir($this->userNotesBaseDir, 0777, true)) { + throw new \RuntimeException(sprintf( + "Can't create the usernotes directory: %s", + $this->userNotesBaseDir + )); + } } // Get the local last-updated value. @@ -54,7 +59,7 @@ public function update($event, $value = null) { } if (!extension_loaded('bz2')) { - trigger_error('The BZip2 extension is not available.', E_USER_ERROR); + throw new \Error('The BZip2 extension is not available.'); break; } @@ -65,7 +70,7 @@ public function update($event, $value = null) { // Use a decompression stream filter to save having to store anything locally other than the expanded user notes. if (false === ($fpNotes = fopen('http://www.php.net/backend/notes/all.bz2', 'rb'))) { - trigger_error('Failed to access the usernotes archive.', E_USER_ERROR); + throw new \Error('Failed to access the usernotes archive.'); break; } diff --git a/phpdotnet/phd/Package/PHP/HowTo.php b/phpdotnet/phd/Package/PHP/HowTo.php index 20db365e..8a0b0782 100644 --- a/phpdotnet/phd/Package/PHP/HowTo.php +++ b/phpdotnet/phd/Package/PHP/HowTo.php @@ -28,11 +28,11 @@ public function update($event, $value = null) { $this->postConstruct(); if (file_exists($this->getOutputDir())) { if (!is_dir($this->getOutputDir())) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } } else { if (!mkdir($this->getOutputDir(), 0777, true)) { - trigger_error("Can't create output directory", E_USER_ERROR); + throw new \Error("Can't create output directory"); } } break; diff --git a/phpdotnet/phd/Package/PHP/Web.php b/phpdotnet/phd/Package/PHP/Web.php index ad36b7cf..2ba54010 100644 --- a/phpdotnet/phd/Package/PHP/Web.php +++ b/phpdotnet/phd/Package/PHP/Web.php @@ -89,11 +89,11 @@ public function update($event, $value = null) { $this->loadHistoryInfo(); if (file_exists($this->getOutputDir())) { if (!is_dir($this->getOutputDir())) { - trigger_error("Output directory is a file?", E_USER_ERROR); + throw new \Error('Output directory is a file?'); } } else { if (!mkdir($this->getOutputDir(), 0777, true)) { - trigger_error("Can't create output directory", E_USER_ERROR); + throw new \Error("Can't create output directory"); } } if ($this->getFormatName() == "PHP-Web") { @@ -411,7 +411,7 @@ public static function generateSourcesInfo($filename) { $r = new \XMLReader; if (!$r->open($filename)) { - trigger_error(vsprintf("Can't open the sources file (%s)", [$filename]), E_USER_ERROR); + throw new \Error(vsprintf("Can't open the sources file (%s)", [$filename])); return array(); } $info = array(); diff --git a/phpdotnet/phd/Package/PHP/XHTML.php b/phpdotnet/phd/Package/PHP/XHTML.php index 1109038e..c185336a 100644 --- a/phpdotnet/phd/Package/PHP/XHTML.php +++ b/phpdotnet/phd/Package/PHP/XHTML.php @@ -263,7 +263,7 @@ public static function generateVersionInfo($filename) { $r = new \XMLReader; if (!$r->open($filename)) { - trigger_error(vsprintf("Can't open the version info file (%s)", [$filename]), E_USER_ERROR); + throw new \Error(vsprintf("Can't open the version info file (%s)", [$filename])); } $versions = array(); while($r->read()) { @@ -300,7 +300,7 @@ protected static function generateDeprecatedInfo($filename) { $r = new \XMLReader; if (!$r->open($filename)) { - trigger_error(vsprintf("Can't open the version info file (%s)", [$filename]), E_USER_ERROR); + throw new \Error(vsprintf("Can't open the version info file (%s)", [$filename])); } $deprecated = array(); while($r->read()) { @@ -334,7 +334,7 @@ public static function generateAcronymInfo($filename) { $r = new \XMLReader; if (!$r->open($filename)) { - trigger_error(vsprintf("Could not open file for accessing acronym information (%s)", [$filename]), E_USER_ERROR); + throw new \Error(vsprintf('Could not open file for accessing acronym information (%s)', [$filename])); } $acronyms = array(); diff --git a/render.php b/render.php index f7b3e3e3..6fe4fae8 100644 --- a/render.php +++ b/render.php @@ -47,16 +47,16 @@ /* If no docbook file was passed, die */ if (!is_dir($config->xmlRoot) || !is_file($config->xmlFile)) { - trigger_error("No Docbook file given. Specify it on the command line with --docbook.", E_USER_ERROR); + throw new \Error('No Docbook file given. Specify it on the command line with --docbook.'); } if (!file_exists($config->outputDir)) { $outputHandler->v("Creating output directory..", VERBOSE_MESSAGES); if (!mkdir($config->outputDir, 0777, True)) { - trigger_error(vsprintf("Can't create output directory : %s", [$config->outputDir]), E_USER_ERROR); + throw new \Error(vsprintf("Can't create output directory : %s", [$config->outputDir])); } $outputHandler->v("Output directory created", VERBOSE_MESSAGES); } elseif (!is_dir($config->outputDir)) { - trigger_error("Output directory is not a file?", E_USER_ERROR); + throw new \Error('Output directory is not a file?'); } // This needs to be moved. Preferably into the PHP package.