Skip to content

Commit

Permalink
Merge 0b7e83b into c97371a
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharhamel committed Jun 21, 2019
2 parents c97371a + 0b7e83b commit 6f50b5d
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 11 deletions.
30 changes: 30 additions & 0 deletions generated/array.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ function array_combine(array $keys, array $values): array
}


/**
* array_flip returns an array in flip
* order, i.e. keys from array become values and values
* from array become keys.
*
* Note that the values of array need to be valid
* keys, i.e. they need to be either integer or
* string. A warning will be emitted if a value has the wrong
* type, and the key/value pair in question will not be included
* in the result.
*
* If a value has several occurrences, the latest key will be
* used as its value, and all others will be lost.
*
* @param array $array An array of key/value pairs to be flipped.
* @return array Returns the flipped array on success and NULL on failure.
* @throws ArrayException
*
*/
function array_flip(array $array): array
{
error_clear_last();
$result = \array_flip($array);
if ($result === null) {
throw ArrayException::createFromPhpError();
}
return $result;
}


/**
* array_multisort can be used to sort several
* arrays at once, or a multi-dimensional array by one or more
Expand Down
5 changes: 5 additions & 0 deletions generated/functionsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'apcu_inc',
'apcu_sma_info',
'array_combine',
'array_flip',
'array_multisort',
'array_walk_recursive',
'arsort',
Expand Down Expand Up @@ -475,6 +476,7 @@
'define',
'highlight_file',
'highlight_string',
'sapi_windows_cp_conv',
'sapi_windows_cp_set',
'sapi_windows_vt100_support',
'sleep',
Expand Down Expand Up @@ -900,6 +902,8 @@
'simplexml_load_file',
'simplexml_load_string',
'socket_accept',
'socket_addrinfo_bind',
'socket_addrinfo_connect',
'socket_bind',
'socket_connect',
'socket_create_listen',
Expand All @@ -909,6 +913,7 @@
'socket_get_option',
'socket_getpeername',
'socket_getsockname',
'socket_import_stream',
'socket_listen',
'socket_read',
'socket_send',
Expand Down
24 changes: 24 additions & 0 deletions generated/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ function highlight_string(string $str, bool $return = false)
}


/**
* Convert string from one codepage to another.
*
* @param int|string $in_codepage The codepage of the subject string.
* Either the codepage name or identifier.
* @param int|string $out_codepage The codepage to convert the subject string to.
* Either the codepage name or identifier.
* @param string $subject The string to convert.
* @return string The subject string converted to
* out_codepage, or NULL on failure.
* @throws MiscException
*
*/
function sapi_windows_cp_conv($in_codepage, $out_codepage, string $subject): string
{
error_clear_last();
$result = \sapi_windows_cp_conv($in_codepage, $out_codepage, $subject);
if ($result === null) {
throw MiscException::createFromPhpError();
}
return $result;
}


/**
* Set the codepage of the current process.
*
Expand Down
59 changes: 59 additions & 0 deletions generated/sockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,46 @@ function socket_accept($socket)
}


/**
* Create a Socket resource, and bind it to the provided AddrInfo resource. The return
* value of this function may be used with socket_listen.
*
* @param resource $addr Resource created from socket_addrinfo_lookup().
* @return resource|null Returns a Socket resource on success or NULL on failure.
* @throws SocketsException
*
*/
function socket_addrinfo_bind($addr)
{
error_clear_last();
$result = \socket_addrinfo_bind($addr);
if ($result === null) {
throw SocketsException::createFromPhpError();
}
return $result;
}


/**
* Create a Socket resource, and connect it to the provided AddrInfo resource. The return
* value of this function may be used with the rest of the socket functions.
*
* @param resource $addr Resource created from socket_addrinfo_lookup()
* @return resource|null Returns a Socket resource on success or NULL on failure.
* @throws SocketsException
*
*/
function socket_addrinfo_connect($addr)
{
error_clear_last();
$result = \socket_addrinfo_connect($addr);
if ($result === null) {
throw SocketsException::createFromPhpError();
}
return $result;
}


/**
* Binds the name given in address to the socket
* described by socket. This has to be done before
Expand Down Expand Up @@ -336,6 +376,25 @@ function socket_getsockname($socket, ?string &$addr, ?int &$port = null): void
}


/**
* Imports a stream that encapsulates a socket into a socket extension resource.
*
* @param resource $stream The stream resource to import.
* @return resource Returns FALSE or NULL on failure.
* @throws SocketsException
*
*/
function socket_import_stream($stream)
{
error_clear_last();
$result = \socket_import_stream($stream);
if ($result === null) {
throw SocketsException::createFromPhpError();
}
return $result;
}


/**
* After the socket socket has been created
* using socket_create and bound to a name with
Expand Down
19 changes: 18 additions & 1 deletion generator/src/DocPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(string $_path)
}

/*
* Detect function which didn't return FALSE on error.
* Detect function which return FALSE on error.
*
* @return bool
*/
Expand Down Expand Up @@ -90,6 +90,23 @@ public function detectFalsyFunction(): bool
return false;
}

/*
* Detect function which return NULL on error.
*
* @return bool
*/
public function detectNullsyFunction(): bool
{
$file = \file_get_contents($this->path);


if (preg_match('/&null;\s+on\s+failure/', $file)) {
return true;
}

return false;
}


/**
* @return \SimpleXMLElement[]
Expand Down
14 changes: 13 additions & 1 deletion generator/src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Method
{
const FALSY_TYPE = 1;
const NULLSY_TYPE = 2;
/**
* @var \SimpleXMLElement
*/
Expand All @@ -27,20 +29,30 @@ class Method
* @var PhpStanFunctionMapReader
*/
private $phpStanFunctionMapReader;
/**
* @var int
*/
private $errorType;

public function __construct(\SimpleXMLElement $_functionObject, \SimpleXMLElement $rootEntity, string $moduleName, PhpStanFunctionMapReader $phpStanFunctionMapReader)
public function __construct(\SimpleXMLElement $_functionObject, \SimpleXMLElement $rootEntity, string $moduleName, PhpStanFunctionMapReader $phpStanFunctionMapReader, int $errorType)
{
$this->functionObject = $_functionObject;
$this->rootEntity = $rootEntity;
$this->moduleName = $moduleName;
$this->phpStanFunctionMapReader = $phpStanFunctionMapReader;
$this->errorType = $errorType;
}

public function getFunctionName(): string
{
return $this->functionObject->methodname->__toString();
}

public function getErrorType(): int
{
return $this->errorType;
}

public function getReturnType(): string
{
// If the function returns a boolean, since false is for error, true is for success.
Expand Down
5 changes: 5 additions & 0 deletions generator/src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,9 @@ private function getInnerXml(\SimpleXMLElement $SimpleXMLElement): string
$inner_xml = trim($inner_xml);
return $inner_xml;
}

public function isTypeable(): bool
{
return $this->getType() !== 'mixed' && $this->getType() !== 'resource' && \count(\explode("|", $this->getType())) < 2;
}
}
8 changes: 6 additions & 2 deletions generator/src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public function getMethods(array $paths): array
}

$docPage = new DocPage($path);
if ($docPage->detectFalsyFunction()) {
$isFalsy = $docPage->detectFalsyFunction();
$isNullsy = $docPage->detectNullsyFunction();
if ($isFalsy || $isNullsy) {
$errorType = $isFalsy ? Method::FALSY_TYPE : Method::NULLSY_TYPE;

$functionObjects = $docPage->getMethodSynopsis();
if (count($functionObjects) > 1) {
$overloadedFunctions = array_merge($overloadedFunctions, \array_map(function ($functionObject) {
Expand All @@ -107,7 +111,7 @@ public function getMethods(array $paths): array
}
$rootEntity = $docPage->loadAndResolveFile();
foreach ($functionObjects as $functionObject) {
$function = new Method($functionObject, $rootEntity, $docPage->getModule(), $phpStanFunctionMapReader);
$function = new Method($functionObject, $rootEntity, $docPage->getModule(), $phpStanFunctionMapReader, $errorType);
if (isset($ignoredFunctions[$function->getFunctionName()])) {
continue;
}
Expand Down
18 changes: 15 additions & 3 deletions generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,24 @@ 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;
default:
throw new \LogicException("Method doesn't have an error type");
}

// 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') {
return "
if (\$result === false) {
if (\$result === $errorValue) {
throw CurlException::createFromCurlResource(\$ch);
}
";
Expand All @@ -113,7 +125,7 @@ private function generateExceptionCode(string $moduleName, Method $method) : str

$exceptionName = FileCreator::toExceptionName($moduleName);
return "
if (\$result === false) {
if (\$result === $errorValue) {
throw {$exceptionName}::createFromPhpError();
}
";
Expand All @@ -130,7 +142,7 @@ private function displayParamsWithType(array $params): string

foreach ($params as $param) {
$paramAsString = '';
if ($param->getType() !== 'mixed' && $param->getType() !== 'resource') {
if ($param->isTypeable()) {
if ($param->isNullable()) {
$paramAsString .= '?';
}
Expand Down
9 changes: 9 additions & 0 deletions generator/tests/DocPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ public function testDetectFalsyFunction()
$this->assertTrue($mcryptDecrypt->detectFalsyFunction());
$this->assertTrue($fsockopen->detectFalsyFunction());
}

public function testDetectNullsyFunction()
{
$pregMatch = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/array/functions/array-flip.xml');
$implode = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/strings/functions/implode.xml');

$this->assertTrue($pregMatch->detectNullsyFunction());
$this->assertFalse($implode->detectNullsyFunction());
}
}
8 changes: 4 additions & 4 deletions generator/tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testGetFunctionName()
{
$docPage = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/pcre/functions/preg-match.xml');
$xmlObject = $docPage->getMethodSynopsis();
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader());
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), Method::FALSY_TYPE);
$name = $method->getFunctionName();
$this->assertEquals('preg_match', $name);
}
Expand All @@ -20,7 +20,7 @@ public function testGetFunctionType()
{
$docPage = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/pcre/functions/preg-match.xml');
$xmlObject = $docPage->getMethodSynopsis();
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader());
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), Method::FALSY_TYPE);
$type = $method->getReturnType();
$this->assertEquals('int', $type);
}
Expand All @@ -29,7 +29,7 @@ public function testGetFunctionParam()
{
$docPage = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/pcre/functions/preg-match.xml');
$xmlObject = $docPage->getMethodSynopsis();
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader());
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), Method::FALSY_TYPE);
$params = $method->getParams();
$this->assertEquals('string', $params[0]->getType());
$this->assertEquals('pattern', $params[0]->getParameter());
Expand All @@ -39,7 +39,7 @@ public function testGetInitializer()
{
$docPage = new DocPage(__DIR__ . '/../doc/doc-en/en/reference/apc/functions/apc-cache-info.xml');
$xmlObject = $docPage->getMethodSynopsis();
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader());
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader(), Method::FALSY_TYPE);

$params = $method->getParams();
$this->assertEquals('', $params[0]->getDefaultValue());
Expand Down

0 comments on commit 6f50b5d

Please sign in to comment.