Skip to content

Commit

Permalink
Merge pull request #303 from thecodingmachine/emptyStringsFunctions
Browse files Browse the repository at this point in the history
Empty strings functions
  • Loading branch information
Kharhamel authored Oct 17, 2021
2 parents c1748a0 + ae5df91 commit a5fa665
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions generated/functionsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@
'pg_field_table',
'pg_flush',
'pg_free_result',
'pg_host',
'pg_insert',
'pg_last_oid',
'pg_lo_close',
Expand Down
29 changes: 29 additions & 0 deletions generated/pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,35 @@ function pg_free_result($result): void
}


/**
* pg_host returns the host name of the given
* PostgreSQL connection instance is
* connected to.
*
* @param resource $connection An PgSql\Connection instance.
* When connection is NULL, the default connection is used.
* The default connection is the last connection made by pg_connect
* or pg_pconnect.
* @return string A string containing the name of the host the
* connection is to.
* @throws PgsqlException
*
*/
function pg_host($connection = null): string
{
error_clear_last();
if ($connection !== null) {
$result = \pg_host($connection);
} else {
$result = \pg_host();
}
if ($result === '') {
throw PgsqlException::createFromPhpError();
}
return $result;
}


/**
* pg_insert inserts the values
* of values into the table specified
Expand Down
20 changes: 20 additions & 0 deletions generator/src/DocPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ public function detectNullsyFunction(): bool
return false;
}

/*
* Detect function which return an empty string on error.
*/
public function detectEmptyFunction(): bool
{
$file = file_get_contents($this->path);
if ($file === false) {
throw new \RuntimeException('An error occured while reading '.$this->path);
}
if ($this->getIsDeprecated($file)) {
return false;
}

if (preg_match('/an\s+empty\s+string\s+on\s+error/', $file)) {
return true;
}

return false;
}


/**
* @return \SimpleXMLElement[]
Expand Down
7 changes: 6 additions & 1 deletion generator/src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Method
{
const FALSY_TYPE = 1;
const NULLSY_TYPE = 2;
const EMPTY_TYPE = 3;
/**
* @var \SimpleXMLElement
*/
Expand Down Expand Up @@ -162,7 +163,11 @@ private function stripReturnFalseText(string $string): string
$string = $this->removeString($string, ' and FALSE if an error occurred');
$string = $this->removeString($string, 'the function will return TRUE, or FALSE otherwise');
break;


case self::EMPTY_TYPE:
$string = $this->removeString($string, ' or an empty string on error');
break;

default:
throw new \RuntimeException('Incorrect error type.');
}
Expand Down
5 changes: 3 additions & 2 deletions generator/src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ public function getMethods(array $paths): array
$docPage = new DocPage($path);
$isFalsy = $docPage->detectFalsyFunction();
$isNullsy = $docPage->detectNullsyFunction();
if ($isFalsy || $isNullsy) {
$errorType = $isFalsy ? Method::FALSY_TYPE : Method::NULLSY_TYPE;
$isEmpty = $docPage->detectEmptyFunction();
if ($isFalsy || $isNullsy || $isEmpty) {
$errorType = $isFalsy ? Method::FALSY_TYPE : ($isNullsy ? Method::NULLSY_TYPE : Method::EMPTY_TYPE);

$functionObjects = $docPage->getMethodSynopsis();
if (count($functionObjects) > 1) {
Expand Down
4 changes: 3 additions & 1 deletion generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,16 @@ private function writePhpFunction(): string

private function generateExceptionCode(string $moduleName, Method $method) : string
{
$errorValue = null;
switch ($method->getErrorType()) {
case Method::FALSY_TYPE:
$errorValue = 'false';
break;
case Method::NULLSY_TYPE:
$errorValue = 'null';
break;
case Method::EMPTY_TYPE:
$errorValue = "''";
break;
default:
throw new \LogicException("Method doesn't have an error type");
}
Expand Down
7 changes: 7 additions & 0 deletions generator/tests/DocPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public function testDetectNullsyFunction()
$this->assertFalse($implode->detectNullsyFunction());
$this->assertTrue($arrayReplace->detectNullsyFunction());
}

public function testDetectEmptyFunction()
{
$pgHost = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/pgsql/functions/pg-host.xml');

$this->assertTrue($pgHost->detectEmptyFunction());
}
}
1 change: 1 addition & 0 deletions rector-migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@
'pg_field_table' => 'Safe\pg_field_table',
'pg_flush' => 'Safe\pg_flush',
'pg_free_result' => 'Safe\pg_free_result',
'pg_host' => 'Safe\pg_host',
'pg_insert' => 'Safe\pg_insert',
'pg_last_oid' => 'Safe\pg_last_oid',
'pg_lo_close' => 'Safe\pg_lo_close',
Expand Down

0 comments on commit a5fa665

Please sign in to comment.