Skip to content

Commit

Permalink
Merge 5ab04a6 into c97371a
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharhamel committed Jun 21, 2019
2 parents c97371a + 5ab04a6 commit 5078246
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
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::ERROR_TYPES['falsy']:
$errorValue = 'false';
break;
case Method::ERROR_TYPES['nullsy']:
$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 5078246

Please sign in to comment.