Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Change generator to replace error_clear_last and error_get_last with …
…custom error handler
  • Loading branch information
jfoulquie-tnw committed Apr 13, 2022
commit 7fd581391a4d1f77d4a9de21964b50184b4eb2d8
16 changes: 13 additions & 3 deletions generator/src/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,20 @@ public function createExceptionFile(string $moduleName): void

class {$exceptionName} extends \ErrorException implements SafeExceptionInterface
{
public static function createFromPhpError(): self
/**
*
* @param array{type?: int, message?: string, file?: string, line?: int} \$error
* @return \Safe\Exceptions\{$exceptionName}
*/
public static function createFromPhpError(array \$error = null): self
{
\$error = error_get_last();
return new self(\$error['message'] ?? 'An error occured', 0, \$error['type'] ?? 1);
return new self(
\$error['message'] ?? 'An error occured',
0,
\$error['type'] ?? 1,
\$error['file'] ?? __FILE__,
\$error['line'] ?? __LINE__,
);
}
}

Expand Down
30 changes: 26 additions & 4 deletions generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,30 @@ private function writePhpFunction(): string
$moduleName = $this->method->getModuleName();

$phpFunction .= "function {$this->method->getFunctionName()}({$this->displayParamsWithType($this->method->getParams())}){$returnType}
{
error_clear_last();
";
{";

$includeErrorHandler = true;
// Certain methods from curl don't need the custom error handler
if ($moduleName !== 'Curl') {
$params = $this->method->getParams();
if (\count($params) > 0 && in_array($params[0]->getParameter(), ['handle', 'multi_handle', 'share_handle'])) {
$includeErrorHandler = false;
}
}

if ($includeErrorHandler) {
$phpFunction .= "
\$error = [];
set_error_handler( function(int \$errno, string \$errstr, string \$errfile, int \$errline) use (&\$error) {
\$error = [
'type' => \$errno,
'message' => \$errstr,
'file' => \$errfile,
'line' => \$errline,
];
return false;
});\n";
}

if (!$this->method->isOverloaded()) {
$phpFunction .= ' $result = '.$this->printFunctionCall($this->method);
Expand Down Expand Up @@ -88,6 +109,7 @@ private function writePhpFunction(): string
$phpFunction .= ' $result = '.$this->printFunctionCall($method)."\n";
$phpFunction .= ' }';
}
$phpFunction .= "\n restore_error_handler();\n";

$phpFunction .= $this->generateExceptionCode($moduleName, $this->method).$returnStatement. '}
';
Expand Down Expand Up @@ -126,7 +148,7 @@ private function generateExceptionCode(string $moduleName, Method $method) : str
$exceptionName = FileCreator::toExceptionName($moduleName);
return "
if (\$result === $errorValue) {
throw {$exceptionName}::createFromPhpError();
throw {$exceptionName}::createFromPhpError(\$error);
}
";
}
Expand Down