Skip to content

Commit a5fa665

Browse files
authored
Merge pull request #303 from thecodingmachine/emptyStringsFunctions
Empty strings functions
2 parents c1748a0 + ae5df91 commit a5fa665

File tree

8 files changed

+70
-4
lines changed

8 files changed

+70
-4
lines changed

generated/functionsList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@
721721
'pg_field_table',
722722
'pg_flush',
723723
'pg_free_result',
724+
'pg_host',
724725
'pg_insert',
725726
'pg_last_oid',
726727
'pg_lo_close',

generated/pgsql.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,35 @@ function pg_free_result($result): void
384384
}
385385

386386

387+
/**
388+
* pg_host returns the host name of the given
389+
* PostgreSQL connection instance is
390+
* connected to.
391+
*
392+
* @param resource $connection An PgSql\Connection instance.
393+
* When connection is NULL, the default connection is used.
394+
* The default connection is the last connection made by pg_connect
395+
* or pg_pconnect.
396+
* @return string A string containing the name of the host the
397+
* connection is to.
398+
* @throws PgsqlException
399+
*
400+
*/
401+
function pg_host($connection = null): string
402+
{
403+
error_clear_last();
404+
if ($connection !== null) {
405+
$result = \pg_host($connection);
406+
} else {
407+
$result = \pg_host();
408+
}
409+
if ($result === '') {
410+
throw PgsqlException::createFromPhpError();
411+
}
412+
return $result;
413+
}
414+
415+
387416
/**
388417
* pg_insert inserts the values
389418
* of values into the table specified

generator/src/DocPage.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ public function detectNullsyFunction(): bool
160160
return false;
161161
}
162162

163+
/*
164+
* Detect function which return an empty string on error.
165+
*/
166+
public function detectEmptyFunction(): bool
167+
{
168+
$file = file_get_contents($this->path);
169+
if ($file === false) {
170+
throw new \RuntimeException('An error occured while reading '.$this->path);
171+
}
172+
if ($this->getIsDeprecated($file)) {
173+
return false;
174+
}
175+
176+
if (preg_match('/an\s+empty\s+string\s+on\s+error/', $file)) {
177+
return true;
178+
}
179+
180+
return false;
181+
}
182+
163183

164184
/**
165185
* @return \SimpleXMLElement[]

generator/src/Method.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Method
1010
{
1111
const FALSY_TYPE = 1;
1212
const NULLSY_TYPE = 2;
13+
const EMPTY_TYPE = 3;
1314
/**
1415
* @var \SimpleXMLElement
1516
*/
@@ -162,7 +163,11 @@ private function stripReturnFalseText(string $string): string
162163
$string = $this->removeString($string, ' and FALSE if an error occurred');
163164
$string = $this->removeString($string, 'the function will return TRUE, or FALSE otherwise');
164165
break;
165-
166+
167+
case self::EMPTY_TYPE:
168+
$string = $this->removeString($string, ' or an empty string on error');
169+
break;
170+
166171
default:
167172
throw new \RuntimeException('Incorrect error type.');
168173
}

generator/src/Scanner.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ public function getMethods(array $paths): array
102102
$docPage = new DocPage($path);
103103
$isFalsy = $docPage->detectFalsyFunction();
104104
$isNullsy = $docPage->detectNullsyFunction();
105-
if ($isFalsy || $isNullsy) {
106-
$errorType = $isFalsy ? Method::FALSY_TYPE : Method::NULLSY_TYPE;
105+
$isEmpty = $docPage->detectEmptyFunction();
106+
if ($isFalsy || $isNullsy || $isEmpty) {
107+
$errorType = $isFalsy ? Method::FALSY_TYPE : ($isNullsy ? Method::NULLSY_TYPE : Method::EMPTY_TYPE);
107108

108109
$functionObjects = $docPage->getMethodSynopsis();
109110
if (count($functionObjects) > 1) {

generator/src/WritePhpFunction.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ private function writePhpFunction(): string
9797

9898
private function generateExceptionCode(string $moduleName, Method $method) : string
9999
{
100-
$errorValue = null;
101100
switch ($method->getErrorType()) {
102101
case Method::FALSY_TYPE:
103102
$errorValue = 'false';
104103
break;
105104
case Method::NULLSY_TYPE:
106105
$errorValue = 'null';
107106
break;
107+
case Method::EMPTY_TYPE:
108+
$errorValue = "''";
109+
break;
108110
default:
109111
throw new \LogicException("Method doesn't have an error type");
110112
}

generator/tests/DocPageTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@ public function testDetectNullsyFunction()
4747
$this->assertFalse($implode->detectNullsyFunction());
4848
$this->assertTrue($arrayReplace->detectNullsyFunction());
4949
}
50+
51+
public function testDetectEmptyFunction()
52+
{
53+
$pgHost = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/pgsql/functions/pg-host.xml');
54+
55+
$this->assertTrue($pgHost->detectEmptyFunction());
56+
}
5057
}

rector-migrate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@
731731
'pg_field_table' => 'Safe\pg_field_table',
732732
'pg_flush' => 'Safe\pg_flush',
733733
'pg_free_result' => 'Safe\pg_free_result',
734+
'pg_host' => 'Safe\pg_host',
734735
'pg_insert' => 'Safe\pg_insert',
735736
'pg_last_oid' => 'Safe\pg_last_oid',
736737
'pg_lo_close' => 'Safe\pg_lo_close',

0 commit comments

Comments
 (0)