From 85d7bff2e7cb5906ddf82d673273cb81606e382e Mon Sep 17 00:00:00 2001 From: Kharhamel Date: Wed, 20 Jul 2022 19:42:13 +0200 Subject: [PATCH] Fixed an error with the CurlException --- generated/curl.php | 20 ++++++++++---------- generator/src/Method.php | 2 +- generator/src/Parameter.php | 10 ++++++++-- generator/src/WritePhpFunction.php | 11 ++++++----- generator/tests/MethodTest.php | 2 +- lib/Exceptions/CurlException.php | 4 ++-- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/generated/curl.php b/generated/curl.php index 6473b6d2..3c89871c 100644 --- a/generated/curl.php +++ b/generated/curl.php @@ -18,7 +18,7 @@ function curl_copy_handle(\CurlHandle $handle): \CurlHandle error_clear_last(); $result = \curl_copy_handle($handle); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($handle); } return $result; } @@ -39,7 +39,7 @@ function curl_escape(\CurlHandle $handle, string $string): string error_clear_last(); $result = \curl_escape($handle, $string); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($handle); } return $result; } @@ -64,7 +64,7 @@ function curl_exec(\CurlHandle $handle) error_clear_last(); $result = \curl_exec($handle); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($handle); } return $result; } @@ -541,7 +541,7 @@ function curl_getinfo(\CurlHandle $handle, int $option = null) $result = \curl_getinfo($handle); } if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($handle); } return $result; } @@ -627,7 +627,7 @@ function curl_multi_info_read(\CurlMultiHandle $multi_handle, ?int &$queued_mess error_clear_last(); $result = \curl_multi_info_read($multi_handle, $queued_messages); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($multi_handle); } return $result; } @@ -791,7 +791,7 @@ function curl_multi_setopt(\CurlMultiHandle $multi_handle, int $option, $value): error_clear_last(); $result = \curl_multi_setopt($multi_handle, $option, $value); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($multi_handle); } } @@ -3154,7 +3154,7 @@ function curl_setopt(\CurlHandle $handle, int $option, $value): void error_clear_last(); $result = \curl_setopt($handle, $option, $value); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($handle); } } @@ -3173,7 +3173,7 @@ function curl_share_errno(\CurlShareHandle $share_handle): int error_clear_last(); $result = \curl_share_errno($share_handle); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($share_handle); } return $result; } @@ -3250,7 +3250,7 @@ function curl_share_setopt(\CurlShareHandle $share_handle, int $option, $value): error_clear_last(); $result = \curl_share_setopt($share_handle, $option, $value); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($share_handle); } } @@ -3270,7 +3270,7 @@ function curl_unescape(\CurlHandle $handle, string $string): string error_clear_last(); $result = \curl_unescape($handle, $string); if ($result === false) { - throw CurlException::createFromPhpError(); + throw CurlException::createFromPhpError($handle); } return $result; } diff --git a/generator/src/Method.php b/generator/src/Method.php index 1a7f120c..d3406fd4 100644 --- a/generator/src/Method.php +++ b/generator/src/Method.php @@ -117,7 +117,7 @@ private function getDocBlock(): string $i=1; foreach ($this->getParams() as $parameter) { - $str .= '@param '.$parameter->getDocBlockType().' $'.$parameter->getParameter().' '; + $str .= '@param '.$parameter->getDocBlockType().' $'.$parameter->getParameterName().' '; $str .= $this->getStringForXPath("(//docbook:refsect1[@role='parameters']//docbook:varlistentry)[$i]//docbook:para")."\n"; $i++; } diff --git a/generator/src/Parameter.php b/generator/src/Parameter.php index d7f87587..4add27d8 100644 --- a/generator/src/Parameter.php +++ b/generator/src/Parameter.php @@ -19,7 +19,7 @@ class Parameter public function __construct(\SimpleXMLElement $parameter, ?PhpStanFunction $phpStanFunction, int $position) { $this->parameter = $parameter; - $phpStanParam = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameter(), $position) : null; + $phpStanParam = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameterName(), $position) : null; $this->type = $phpStanParam ? $phpStanParam->getType() : new PhpStanType($this->parameter->type->__toString()); //todo: is this if useful? } @@ -40,11 +40,17 @@ public function getDocBlockType(): string return $this->type->getDocBlockType(); } - public function getParameter(): string + public function getParameterName(): string { // The db2_bind_param method has parameters with a dash in it... yep... (patch submitted) return \str_replace('-', '_', $this->parameter->parameter->__toString()); } + + public function getParameterType(): string + { + // The db2_bind_param method has parameters with a dash in it... yep... (patch submitted) + return \str_replace('-', '_', $this->parameter->type->__toString()); + } public function isByReference(): bool { diff --git a/generator/src/WritePhpFunction.php b/generator/src/WritePhpFunction.php index 3a9fe236..64d2e522 100644 --- a/generator/src/WritePhpFunction.php +++ b/generator/src/WritePhpFunction.php @@ -75,7 +75,7 @@ private function writePhpFunction(): string $defaultValue = $lastParameter->getDefaultValue(); $defaultValueToString = $this->defaultValueToString($defaultValue); } - $phpFunction .= 'if ($'.$lastParameter->getParameter().' !== '.$defaultValueToString.') {'."\n"; + $phpFunction .= 'if ($'.$lastParameter->getParameterName().' !== '.$defaultValueToString.') {'."\n"; $phpFunction .= ' $result = '.$this->printFunctionCall($method)."\n"; $phpFunction .= ' }'; $inElse = true; @@ -114,10 +114,11 @@ private function generateExceptionCode(string $moduleName, Method $method) : str // Special case for CURL: we need the first argument of the method if this is a resource. if ($moduleName === 'Curl') { $params = $method->getParams(); - if (\count($params) > 0 && $params[0]->getParameter() === 'ch') { + if (\count($params) > 0 && in_array($params[0]->getParameterType(), ['CurlHandle', 'CurlMultiHandle', 'CurlShareHandle'])) { + $name = $params[0]->getParameterName(); return " if (\$result === $errorValue) { - throw CurlException::createFromCurlResource(\$ch); + throw CurlException::createFromPhpError(\$$name); } "; } @@ -146,7 +147,7 @@ private function displayParamsWithType(array $params): string $paramAsString .= ' '; } - $paramName = $param->getParameter(); + $paramName = $param->getParameterName(); if ($param->isVariadic()) { $paramAsString .= ' ...$'.$paramName; } else { @@ -180,7 +181,7 @@ private function printFunctionCall(Method $function): string if ($parameter->isVariadic()) { $str = '...'; } - return $str.'$'.$parameter->getParameter(); + return $str.'$'.$parameter->getParameterName(); }, $function->getParams())); $functionCall .= ');'; return $functionCall; diff --git a/generator/tests/MethodTest.php b/generator/tests/MethodTest.php index 11d06c86..82723aae 100644 --- a/generator/tests/MethodTest.php +++ b/generator/tests/MethodTest.php @@ -32,7 +32,7 @@ public function testGetFunctionParam() $method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), Method::FALSY_TYPE); $params = $method->getParams(); $this->assertEquals('string', $params[0]->getSignatureType()); - $this->assertEquals('pattern', $params[0]->getParameter()); + $this->assertEquals('pattern', $params[0]->getParameterName()); } public function testGetTypeHintFromRessource() diff --git a/lib/Exceptions/CurlException.php b/lib/Exceptions/CurlException.php index d0dbdb69..3401b57b 100644 --- a/lib/Exceptions/CurlException.php +++ b/lib/Exceptions/CurlException.php @@ -8,8 +8,8 @@ class CurlException extends \Exception implements SafeExceptionInterface /** * @param \CurlHandle $ch */ - public static function createFromPhpError($ch): self + public static function createFromPhpError($ch = null): self { - return new self(\curl_error($ch), \curl_errno($ch)); + return new self($ch ? \curl_error($ch) : '', $ch ? \curl_errno($ch) : 0); } }