Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixed an error with the CurlException #370

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 10 additions & 10 deletions generated/curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion generator/src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
10 changes: 8 additions & 2 deletions generator/src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
Expand All @@ -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
{
Expand Down
11 changes: 6 additions & 5 deletions generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
";
}
Expand Down Expand Up @@ -146,7 +147,7 @@ private function displayParamsWithType(array $params): string
$paramAsString .= ' ';
}

$paramName = $param->getParameter();
$paramName = $param->getParameterName();
if ($param->isVariadic()) {
$paramAsString .= ' ...$'.$paramName;
} else {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion generator/tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions lib/Exceptions/CurlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}