Skip to content

Commit 7fd5813

Browse files
committed
Change generator to replace error_clear_last and error_get_last with custom error handler
1 parent 9373d4c commit 7fd5813

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

generator/src/FileCreator.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,20 @@ public function createExceptionFile(string $moduleName): void
135135
136136
class {$exceptionName} extends \ErrorException implements SafeExceptionInterface
137137
{
138-
public static function createFromPhpError(): self
138+
/**
139+
*
140+
* @param array{type?: int, message?: string, file?: string, line?: int} \$error
141+
* @return \Safe\Exceptions\{$exceptionName}
142+
*/
143+
public static function createFromPhpError(array \$error = null): self
139144
{
140-
\$error = error_get_last();
141-
return new self(\$error['message'] ?? 'An error occured', 0, \$error['type'] ?? 1);
145+
return new self(
146+
\$error['message'] ?? 'An error occured',
147+
0,
148+
\$error['type'] ?? 1,
149+
\$error['file'] ?? __FILE__,
150+
\$error['line'] ?? __LINE__,
151+
);
142152
}
143153
}
144154

generator/src/WritePhpFunction.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,30 @@ private function writePhpFunction(): string
5353
$moduleName = $this->method->getModuleName();
5454

5555
$phpFunction .= "function {$this->method->getFunctionName()}({$this->displayParamsWithType($this->method->getParams())}){$returnType}
56-
{
57-
error_clear_last();
58-
";
56+
{";
57+
58+
$includeErrorHandler = true;
59+
// Certain methods from curl don't need the custom error handler
60+
if ($moduleName !== 'Curl') {
61+
$params = $this->method->getParams();
62+
if (\count($params) > 0 && in_array($params[0]->getParameter(), ['handle', 'multi_handle', 'share_handle'])) {
63+
$includeErrorHandler = false;
64+
}
65+
}
66+
67+
if ($includeErrorHandler) {
68+
$phpFunction .= "
69+
\$error = [];
70+
set_error_handler( function(int \$errno, string \$errstr, string \$errfile, int \$errline) use (&\$error) {
71+
\$error = [
72+
'type' => \$errno,
73+
'message' => \$errstr,
74+
'file' => \$errfile,
75+
'line' => \$errline,
76+
];
77+
return false;
78+
});\n";
79+
}
5980

6081
if (!$this->method->isOverloaded()) {
6182
$phpFunction .= ' $result = '.$this->printFunctionCall($this->method);
@@ -88,6 +109,7 @@ private function writePhpFunction(): string
88109
$phpFunction .= ' $result = '.$this->printFunctionCall($method)."\n";
89110
$phpFunction .= ' }';
90111
}
112+
$phpFunction .= "\n restore_error_handler();\n";
91113

92114
$phpFunction .= $this->generateExceptionCode($moduleName, $this->method).$returnStatement. '}
93115
';
@@ -126,7 +148,7 @@ private function generateExceptionCode(string $moduleName, Method $method) : str
126148
$exceptionName = FileCreator::toExceptionName($moduleName);
127149
return "
128150
if (\$result === $errorValue) {
129-
throw {$exceptionName}::createFromPhpError();
151+
throw {$exceptionName}::createFromPhpError(\$error);
130152
}
131153
";
132154
}

0 commit comments

Comments
 (0)