Skip to content

Commit 7366bc9

Browse files
lacatoirelacatoire
andauthored
refactor: replace trigger_error(E_USER_ERROR) with exceptions (PHP 8.4 compatibility) (#218)
Co-authored-by: lacatoire <louis-arnaud.catoire@external.drivalia.com>
1 parent 5d705c2 commit 7366bc9

18 files changed

Lines changed: 65 additions & 60 deletions

phpdotnet/phd/Autoloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function autoload(string $name): void
2626

2727
return;
2828
}
29-
trigger_error(vsprintf('Cannot find file for %s: %s', [$name, $file ?? $filename]), E_USER_ERROR);
29+
throw new \Error(vsprintf('Cannot find file for %s: %s', [$name, $file ?? $filename]));
3030
}
3131
}
3232

phpdotnet/phd/Format/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final function createFormat($format, ...$formatParams) {
4949
}
5050
return $obj;
5151
}
52-
trigger_error("This format is not supported by this package", E_USER_ERROR);
52+
throw new \Error('This format is not supported by this package');
5353
}
5454

5555
public static final function createFactory($package) {

phpdotnet/phd/Options/Handler.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ public function option_d(string $k, mixed $v): array
184184
public function option_docbook(string $k, mixed $v): array
185185
{
186186
if (is_array($v)) {
187-
trigger_error("Can only parse one file at a time", E_USER_ERROR);
187+
throw new \Error('Can only parse one file at a time');
188188
}
189189
if (!file_exists($v) || is_dir($v) || !is_readable($v)) {
190-
trigger_error(sprintf("'%s' is not a readable docbook file", $v), E_USER_ERROR);
190+
throw new \Error(sprintf("'%s' is not a readable docbook file", $v));
191191
}
192192
return [
193193
'xmlRoot' => dirname($v),
@@ -209,19 +209,19 @@ public function option_o(string $k, mixed $v): array
209209
public function option_output(string $k, mixed $v): array
210210
{
211211
if (is_array($v)) {
212-
trigger_error("Only a single output location can be supplied", E_USER_ERROR);
212+
throw new \Error('Only a single output location can be supplied');
213213
}
214214
if (!file_exists($v)) {
215215
$this->outputHandler->v("Creating output directory..", VERBOSE_MESSAGES);
216216
if (!mkdir($v, 0777, true)) {
217-
trigger_error(vsprintf("Can't create output directory : %s", [$v]), E_USER_ERROR);
217+
throw new \Error(vsprintf("Can't create output directory : %s", [$v]));
218218
}
219219
$this->outputHandler->v("Output directory created", VERBOSE_MESSAGES);
220220
} elseif (!is_dir($v)) {
221-
trigger_error("Output directory is a file?", E_USER_ERROR);
221+
throw new \Error('Output directory is a file?');
222222
}
223223
if (!is_dir($v) || !is_readable($v)) {
224-
trigger_error(sprintf("'%s' is not a valid directory", $v), E_USER_ERROR);
224+
throw new \Error(sprintf("'%s' is not a valid directory", $v));
225225
}
226226
$v = (substr($v, strlen($v) - strlen(DIRECTORY_SEPARATOR)) === DIRECTORY_SEPARATOR) ? $v : ($v . DIRECTORY_SEPARATOR);
227227

@@ -234,7 +234,7 @@ public function option_output(string $k, mixed $v): array
234234
public function option_outputfilename(string $k, mixed $v): array
235235
{
236236
if (is_array($v)) {
237-
trigger_error("Only a single output location can be supplied", E_USER_ERROR);
237+
throw new \Error('Only a single output location can be supplied');
238238
}
239239
$file = basename($v);
240240

@@ -281,7 +281,7 @@ public function option_package(string $k, mixed $v): array
281281
foreach((array)$v as $package) {
282282
if (!in_array($package, $this->config->getSupportedPackages())) {
283283
$supported = implode(', ', $this->config->getSupportedPackages());
284-
trigger_error("Invalid Package (Tried: '$package' Supported: '$supported')", E_USER_ERROR);
284+
throw new \Error("Invalid Package (Tried: '$package' Supported: '$supported')");
285285
}
286286
}
287287
return ['package' => (array) $v];
@@ -341,13 +341,13 @@ public function option_skip(string $k, mixed $v): array
341341
public function option_saveconfig(string $k, mixed $v): array
342342
{
343343
if (is_array($v)) {
344-
trigger_error(sprintf("You cannot pass %s more than once", $k), E_USER_ERROR);
344+
throw new \Error(sprintf('You cannot pass %s more than once', $k));
345345
}
346346

347347
$val = is_bool($v) ? true : self::boolval($v);
348348

349349
if (!is_bool($val)) {
350-
trigger_error("yes/no || on/off || true/false || 1/0 expected", E_USER_ERROR);
350+
throw new \Error('yes/no || on/off || true/false || 1/0 expected');
351351
}
352352

353353
return ['saveConfig' => $val];
@@ -381,7 +381,7 @@ public function option_verbose(string $k, mixed $v): array
381381
$verbose = max($verbose, 1);
382382
$verbose <<= 1;
383383
} else {
384-
trigger_error("Unknown option passed to --$k, '$const'", E_USER_ERROR);
384+
throw new \Error("Unknown option passed to --$k, '$const'");
385385
}
386386
}
387387
}
@@ -438,11 +438,11 @@ public function option_c(string $k, mixed $v): array
438438
public function option_color(string $k, mixed $v): array
439439
{
440440
if (is_array($v)) {
441-
trigger_error(sprintf("You cannot pass %s more than once", $k), E_USER_ERROR);
441+
throw new \Error(sprintf('You cannot pass %s more than once', $k));
442442
}
443443
$val = self::boolval($v);
444444
if (!is_bool($val)) {
445-
trigger_error("yes/no || on/off || true/false || 1/0 expected", E_USER_ERROR);
445+
throw new \Error('yes/no || on/off || true/false || 1/0 expected');
446446
}
447447
return ['colorOutput' => $val];
448448
}

phpdotnet/phd/Options/Parser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ private function validateOptions(): void {
8080
$checkArgv = explode('=', $argv[$i]);
8181
if (substr($checkArgv[0], 0, 2) === '--') {
8282
if (!in_array(substr($checkArgv[0], 2), $long)) {
83-
trigger_error('Invalid long option ' . $argv[$i], E_USER_ERROR);
83+
throw new \Error('Invalid long option ' . $argv[$i]);
8484
}
8585
} elseif (substr($checkArgv[0], 0, 1) === '-') {
8686
if (!in_array(substr($checkArgv[0], 1), $short)) {
87-
trigger_error('Invalid short option ' . $argv[$i], E_USER_ERROR);
87+
throw new \Error('Invalid short option ' . $argv[$i]);
8888
}
8989
}
9090
}
@@ -98,15 +98,15 @@ public function getopt(): array {
9898

9999
$args = getopt($this->getShortOptions(), $this->getLongOptions());
100100
if ($args === false) {
101-
trigger_error("Something happend with getopt(), please report a bug", E_USER_ERROR);
101+
throw new \Error('Something happend with getopt(), please report a bug');
102102
}
103103

104104
$parsedOptions = [];
105105
foreach ($args as $k => $v) {
106106
$handler = $this->handlerForOption($k);
107107

108108
if (!is_callable($handler)) {
109-
trigger_error("Hmh, something weird has happend, I don't know this option", E_USER_ERROR);
109+
throw new \Error("Hmh, something weird has happend, I don't know this option");
110110
}
111111

112112
$retVal = call_user_func($handler, $k, $v);

phpdotnet/phd/Package/Generic/ChunkedXHTML.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public function update($event, $value = null) {
7777
$this->postConstruct();
7878
if (file_exists($this->getOutputDir())) {
7979
if (!is_dir($this->getOutputDir())) {
80-
trigger_error("Output directory is a file?", E_USER_ERROR);
80+
throw new \Error('Output directory is a file?');
8181
}
8282
} else {
8383
if (!mkdir($this->getOutputDir(), 0777, true)) {
84-
trigger_error("Can't create output directory", E_USER_ERROR);
84+
throw new \Error("Can't create output directory");
8585
}
8686
}
8787
if ($this->config->css) {

phpdotnet/phd/Package/Generic/Manpage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ public function update($event, $value = null) {
316316
$this->setOutputDir($this->config->outputDir . strtolower($this->toValidName($this->getFormatName())) . '/');
317317
if (file_exists($this->getOutputDir())) {
318318
if (!is_dir($this->getOutputDir())) {
319-
trigger_error("Output directory is a file?", E_USER_ERROR);
319+
throw new \Error('Output directory is a file?');
320320
}
321321
} else {
322322
if (!mkdir($this->getOutputDir(), 0777, true)) {
323-
trigger_error("Can't create output directory", E_USER_ERROR);
323+
throw new \Error("Can't create output directory");
324324
}
325325
}
326326
break;

phpdotnet/phd/Package/Generic/TocFeed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ public function update($event, $value = null)
224224
$dir = $this->getOutputDir();
225225
if (file_exists($dir)) {
226226
if (!is_dir($dir)) {
227-
trigger_error("Output directory is a file?", E_USER_ERROR);
227+
throw new \Error('Output directory is a file?');
228228
}
229229
} else {
230230
if (!mkdir($dir, 0777, true)) {
231-
trigger_error("Can't create output directory", E_USER_ERROR);
231+
throw new \Error("Can't create output directory");
232232
}
233233
}
234234
break;

phpdotnet/phd/Package/Generic/XHTML.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,11 @@ protected function fetchStylesheet($name = null) {
678678
$stylesDir .= 'styles/';
679679
if (file_exists($stylesDir)) {
680680
if (!is_dir($stylesDir)) {
681-
trigger_error("The styles/ directory is a file?", E_USER_ERROR);
681+
throw new \Error('The styles/ directory is a file?');
682682
}
683683
} else {
684684
if (!mkdir($stylesDir, 0777, true)) {
685-
trigger_error("Can't create the styles/ directory.", E_USER_ERROR);
685+
throw new \Error("Can't create the styles/ directory.");
686686
}
687687
}
688688
foreach ((array)$this->config->css as $css) {

phpdotnet/phd/Package/IDE/API.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ public function __construct($dir)
7272
: $dir . DIRECTORY_SEPARATOR;
7373
$this->funclist = file($this->dir . self::FUNCLIST_FILE, FILE_IGNORE_NEW_LINES);
7474
} else {
75-
trigger_error('Is the PhD output directory a file?', E_USER_ERROR);
75+
throw new \Error('Is the PhD output directory a file?');
7676
}
7777
} else {
78-
trigger_error('The PhD output directory does not exist.', E_USER_ERROR);
78+
throw new \Error('The PhD output directory does not exist.');
7979
}
8080
}
8181

phpdotnet/phd/Package/IDE/Base.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,19 @@ public function loadVersionInfo() {
215215
if (file_exists($this->config->phpwebVersionFilename)) {
216216
$this->versions = self::generateVersionInfo($this->config->phpwebVersionFilename);
217217
} else {
218-
trigger_error("Can't load the versions file", E_USER_ERROR);
218+
throw new \Error("Can't load the versions file");
219219
}
220220
}
221221

222222
public function createOutputDirectory() {
223223
$this->setOutputDir($this->config->outputDir . strtolower($this->getFormatName()) . '/');
224224
if (file_exists($this->getOutputDir())) {
225225
if (!is_dir($this->getOutputDir())) {
226-
trigger_error("Output directory is a file?", E_USER_ERROR);
226+
throw new \Error('Output directory is a file?');
227227
}
228228
} else {
229229
if (!mkdir($this->getOutputDir(), 0777, true)) {
230-
trigger_error("Can't create output directory", E_USER_ERROR);
230+
throw new \Error("Can't create output directory");
231231
}
232232
}
233233
}

0 commit comments

Comments
 (0)