Skip to content

Commit 440284f

Browse files
authored
Merge pull request #370 from thecodingmachine/curlHandle
Fixed an error with the CurlException
2 parents 278828d + 85d7bff commit 440284f

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

generated/curl.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function curl_copy_handle(\CurlHandle $handle): \CurlHandle
1818
error_clear_last();
1919
$result = \curl_copy_handle($handle);
2020
if ($result === false) {
21-
throw CurlException::createFromPhpError();
21+
throw CurlException::createFromPhpError($handle);
2222
}
2323
return $result;
2424
}
@@ -39,7 +39,7 @@ function curl_escape(\CurlHandle $handle, string $string): string
3939
error_clear_last();
4040
$result = \curl_escape($handle, $string);
4141
if ($result === false) {
42-
throw CurlException::createFromPhpError();
42+
throw CurlException::createFromPhpError($handle);
4343
}
4444
return $result;
4545
}
@@ -64,7 +64,7 @@ function curl_exec(\CurlHandle $handle)
6464
error_clear_last();
6565
$result = \curl_exec($handle);
6666
if ($result === false) {
67-
throw CurlException::createFromPhpError();
67+
throw CurlException::createFromPhpError($handle);
6868
}
6969
return $result;
7070
}
@@ -541,7 +541,7 @@ function curl_getinfo(\CurlHandle $handle, int $option = null)
541541
$result = \curl_getinfo($handle);
542542
}
543543
if ($result === false) {
544-
throw CurlException::createFromPhpError();
544+
throw CurlException::createFromPhpError($handle);
545545
}
546546
return $result;
547547
}
@@ -627,7 +627,7 @@ function curl_multi_info_read(\CurlMultiHandle $multi_handle, ?int &$queued_mess
627627
error_clear_last();
628628
$result = \curl_multi_info_read($multi_handle, $queued_messages);
629629
if ($result === false) {
630-
throw CurlException::createFromPhpError();
630+
throw CurlException::createFromPhpError($multi_handle);
631631
}
632632
return $result;
633633
}
@@ -791,7 +791,7 @@ function curl_multi_setopt(\CurlMultiHandle $multi_handle, int $option, $value):
791791
error_clear_last();
792792
$result = \curl_multi_setopt($multi_handle, $option, $value);
793793
if ($result === false) {
794-
throw CurlException::createFromPhpError();
794+
throw CurlException::createFromPhpError($multi_handle);
795795
}
796796
}
797797

@@ -3154,7 +3154,7 @@ function curl_setopt(\CurlHandle $handle, int $option, $value): void
31543154
error_clear_last();
31553155
$result = \curl_setopt($handle, $option, $value);
31563156
if ($result === false) {
3157-
throw CurlException::createFromPhpError();
3157+
throw CurlException::createFromPhpError($handle);
31583158
}
31593159
}
31603160

@@ -3173,7 +3173,7 @@ function curl_share_errno(\CurlShareHandle $share_handle): int
31733173
error_clear_last();
31743174
$result = \curl_share_errno($share_handle);
31753175
if ($result === false) {
3176-
throw CurlException::createFromPhpError();
3176+
throw CurlException::createFromPhpError($share_handle);
31773177
}
31783178
return $result;
31793179
}
@@ -3250,7 +3250,7 @@ function curl_share_setopt(\CurlShareHandle $share_handle, int $option, $value):
32503250
error_clear_last();
32513251
$result = \curl_share_setopt($share_handle, $option, $value);
32523252
if ($result === false) {
3253-
throw CurlException::createFromPhpError();
3253+
throw CurlException::createFromPhpError($share_handle);
32543254
}
32553255
}
32563256

@@ -3270,7 +3270,7 @@ function curl_unescape(\CurlHandle $handle, string $string): string
32703270
error_clear_last();
32713271
$result = \curl_unescape($handle, $string);
32723272
if ($result === false) {
3273-
throw CurlException::createFromPhpError();
3273+
throw CurlException::createFromPhpError($handle);
32743274
}
32753275
return $result;
32763276
}

generator/src/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function getDocBlock(): string
117117

118118
$i=1;
119119
foreach ($this->getParams() as $parameter) {
120-
$str .= '@param '.$parameter->getDocBlockType().' $'.$parameter->getParameter().' ';
120+
$str .= '@param '.$parameter->getDocBlockType().' $'.$parameter->getParameterName().' ';
121121
$str .= $this->getStringForXPath("(//docbook:refsect1[@role='parameters']//docbook:varlistentry)[$i]//docbook:para")."\n";
122122
$i++;
123123
}

generator/src/Parameter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Parameter
1919
public function __construct(\SimpleXMLElement $parameter, ?PhpStanFunction $phpStanFunction, int $position)
2020
{
2121
$this->parameter = $parameter;
22-
$phpStanParam = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameter(), $position) : null;
22+
$phpStanParam = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameterName(), $position) : null;
2323

2424
$this->type = $phpStanParam ? $phpStanParam->getType() : new PhpStanType($this->parameter->type->__toString()); //todo: is this if useful?
2525
}
@@ -40,11 +40,17 @@ public function getDocBlockType(): string
4040
return $this->type->getDocBlockType();
4141
}
4242

43-
public function getParameter(): string
43+
public function getParameterName(): string
4444
{
4545
// The db2_bind_param method has parameters with a dash in it... yep... (patch submitted)
4646
return \str_replace('-', '_', $this->parameter->parameter->__toString());
4747
}
48+
49+
public function getParameterType(): string
50+
{
51+
// The db2_bind_param method has parameters with a dash in it... yep... (patch submitted)
52+
return \str_replace('-', '_', $this->parameter->type->__toString());
53+
}
4854

4955
public function isByReference(): bool
5056
{

generator/src/WritePhpFunction.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function writePhpFunction(): string
7575
$defaultValue = $lastParameter->getDefaultValue();
7676
$defaultValueToString = $this->defaultValueToString($defaultValue);
7777
}
78-
$phpFunction .= 'if ($'.$lastParameter->getParameter().' !== '.$defaultValueToString.') {'."\n";
78+
$phpFunction .= 'if ($'.$lastParameter->getParameterName().' !== '.$defaultValueToString.') {'."\n";
7979
$phpFunction .= ' $result = '.$this->printFunctionCall($method)."\n";
8080
$phpFunction .= ' }';
8181
$inElse = true;
@@ -114,10 +114,11 @@ private function generateExceptionCode(string $moduleName, Method $method) : str
114114
// Special case for CURL: we need the first argument of the method if this is a resource.
115115
if ($moduleName === 'Curl') {
116116
$params = $method->getParams();
117-
if (\count($params) > 0 && $params[0]->getParameter() === 'ch') {
117+
if (\count($params) > 0 && in_array($params[0]->getParameterType(), ['CurlHandle', 'CurlMultiHandle', 'CurlShareHandle'])) {
118+
$name = $params[0]->getParameterName();
118119
return "
119120
if (\$result === $errorValue) {
120-
throw CurlException::createFromCurlResource(\$ch);
121+
throw CurlException::createFromPhpError(\$$name);
121122
}
122123
";
123124
}
@@ -146,7 +147,7 @@ private function displayParamsWithType(array $params): string
146147
$paramAsString .= ' ';
147148
}
148149

149-
$paramName = $param->getParameter();
150+
$paramName = $param->getParameterName();
150151
if ($param->isVariadic()) {
151152
$paramAsString .= ' ...$'.$paramName;
152153
} else {
@@ -180,7 +181,7 @@ private function printFunctionCall(Method $function): string
180181
if ($parameter->isVariadic()) {
181182
$str = '...';
182183
}
183-
return $str.'$'.$parameter->getParameter();
184+
return $str.'$'.$parameter->getParameterName();
184185
}, $function->getParams()));
185186
$functionCall .= ');';
186187
return $functionCall;

generator/tests/MethodTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testGetFunctionParam()
3232
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), Method::FALSY_TYPE);
3333
$params = $method->getParams();
3434
$this->assertEquals('string', $params[0]->getSignatureType());
35-
$this->assertEquals('pattern', $params[0]->getParameter());
35+
$this->assertEquals('pattern', $params[0]->getParameterName());
3636
}
3737

3838
public function testGetTypeHintFromRessource()

lib/Exceptions/CurlException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class CurlException extends \Exception implements SafeExceptionInterface
88
/**
99
* @param \CurlHandle $ch
1010
*/
11-
public static function createFromPhpError($ch): self
11+
public static function createFromPhpError($ch = null): self
1212
{
13-
return new self(\curl_error($ch), \curl_errno($ch));
13+
return new self($ch ? \curl_error($ch) : '', $ch ? \curl_errno($ch) : 0);
1414
}
1515
}

0 commit comments

Comments
 (0)