Skip to content

Commit

Permalink
Fixing class not in root namespace in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Sep 24, 2018
1 parent f6e2069 commit af2d0ec
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 39 deletions.
12 changes: 6 additions & 6 deletions generated/gmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
/**
* Export a GMP number to a binary string
*
* @param GMP|string|int $gmpnumber The GMP number being exported
* @param \GMP|string|int $gmpnumber The GMP number being exported
* @param int $word_size Default value is 1. The number of bytes in each chunk of binary data. This is mainly used in conjunction with the options parameter.
* @param int $options Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN.
* @return string Returns a string .
* @throws GmpException
*
*/
function gmp_export(GMP $gmpnumber, int $word_size = 1, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN): string
function gmp_export(\GMP $gmpnumber, int $word_size = 1, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN): string
{
error_clear_last();
$result = \gmp_export($gmpnumber, $word_size, $options);
Expand All @@ -31,11 +31,11 @@ function gmp_export(GMP $gmpnumber, int $word_size = 1, int $options = GMP_MSW_F
* @param string $data The binary string being imported
* @param int $word_size Default value is 1. The number of bytes in each chunk of binary data. This is mainly used in conjunction with the options parameter.
* @param int $options Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN.
* @return GMP Returns a GMP number .
* @return \GMP Returns a GMP number .
* @throws GmpException
*
*/
function gmp_import(string $data, int $word_size = 1, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN): GMP
function gmp_import(string $data, int $word_size = 1, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN): \GMP
{
error_clear_last();
$result = \gmp_import($data, $word_size, $options);
Expand All @@ -49,12 +49,12 @@ function gmp_import(string $data, int $word_size = 1, int $options = GMP_MSW_FIR
/**
*
*
* @param GMP|string|int $seed The seed to be set for the gmp_random,
* @param \GMP|string|int $seed The seed to be set for the gmp_random,
* gmp_random_bits, and
* gmp_random_range functions.
*
* Either a GMP number resource in PHP 5.5 and earlier, a GMP object in PHP 5.6 and later, or a numeric string provided that it is possible to convert the latter to a number.
* @return GMP Returns NULL on success .
* @return \GMP Returns NULL on success .
* @throws GmpException
*
*/
Expand Down
6 changes: 3 additions & 3 deletions generated/hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ function hash_hkdf(string $algo, string $ikm, int $length = 0, string $info = ''
/**
*
*
* @param HashContext $hcontext Hashing context returned by hash_init.
* @param \HashContext $hcontext Hashing context returned by hash_init.
* @param string $filename URL describing location of file to be hashed; Supports fopen wrappers.
* @param HashContext|null $scontext Stream context as returned by stream_context_create.
* @param \HashContext|null $scontext Stream context as returned by stream_context_create.
* @throws HashException
*
*/
function hash_update_file(HashContext $hcontext, string $filename, $scontext = null): void
function hash_update_file(\HashContext $hcontext, string $filename, $scontext = null): void
{
error_clear_last();
$result = \hash_update_file($hcontext, $filename, $scontext);
Expand Down
4 changes: 2 additions & 2 deletions generated/oci8.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ function oci_free_statement($statement): void
* @param string $tdo Should be a valid named type (uppercase).
* @param string $schema Should point to the scheme, where the named type was created. The name
* of the current user is the default value.
* @return OCI-Collection Returns a new OCICollection object .
* @return \OCI-Collection Returns a new OCICollection object .
* @throws Oci8Exception
*
*/
Expand Down Expand Up @@ -1058,7 +1058,7 @@ function oci_new_cursor($connection)
* @param int $type Valid values for type are:
* OCI_DTYPE_FILE, OCI_DTYPE_LOB and
* OCI_DTYPE_ROWID.
* @return OCI-Lob Returns a new LOB or FILE descriptor on success, FALSE on error.
* @return \OCI-Lob Returns a new LOB or FILE descriptor on success, FALSE on error.
* @throws Oci8Exception
*
*/
Expand Down
14 changes: 7 additions & 7 deletions generated/simplexml.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
* document and makes it into a SimpleXML node. This new object can
* then be used as a native SimpleXML element.
*
* @param DOMNode $node A DOM Element node
* @param \DOMNode $node A DOM Element node
* @param string $class_name You may use this optional parameter so that
* simplexml_import_dom will return an object of
* the specified class. That class should extend the
* SimpleXMLElement class.
* @return SimpleXMLElement Returns a SimpleXMLElement .
* @return \SimpleXMLElement Returns a SimpleXMLElement .
* @throws SimplexmlException
*
*/
function simplexml_import_dom(DOMNode $node, string $class_name = "SimpleXMLElement"): SimpleXMLElement
function simplexml_import_dom(\DOMNode $node, string $class_name = "SimpleXMLElement"): \SimpleXMLElement
{
error_clear_last();
$result = \simplexml_import_dom($node, $class_name);
Expand Down Expand Up @@ -49,12 +49,12 @@ function simplexml_import_dom(DOMNode $node, string $class_name = "SimpleXMLElem
* @param string $ns Namespace prefix or URI.
* @param bool $is_prefix TRUE if ns is a prefix, FALSE if it's a URI;
* defaults to FALSE.
* @return SimpleXMLElement Returns an object of class SimpleXMLElement with
* @return \SimpleXMLElement Returns an object of class SimpleXMLElement with
* properties containing the data held within the XML document, .
* @throws SimplexmlException
*
*/
function simplexml_load_file(string $filename, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $is_prefix = false): SimpleXMLElement
function simplexml_load_file(string $filename, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $is_prefix = false): \SimpleXMLElement
{
error_clear_last();
$result = \simplexml_load_file($filename, $class_name, $options, $ns, $is_prefix);
Expand All @@ -78,12 +78,12 @@ function simplexml_load_file(string $filename, string $class_name = "SimpleXMLEl
* @param string $ns Namespace prefix or URI.
* @param bool $is_prefix TRUE if ns is a prefix, FALSE if it's a URI;
* defaults to FALSE.
* @return SimpleXMLElement Returns an object of class SimpleXMLElement with
* @return \SimpleXMLElement Returns an object of class SimpleXMLElement with
* properties containing the data held within the xml document, .
* @throws SimplexmlException
*
*/
function simplexml_load_string(string $data, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $is_prefix = false): SimpleXMLElement
function simplexml_load_string(string $data, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $is_prefix = false): \SimpleXMLElement
{
error_clear_last();
$result = \simplexml_load_string($data, $class_name, $options, $ns, $is_prefix);
Expand Down
16 changes: 8 additions & 8 deletions generator/src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getFunctionName(): string
return $this->functionObject->methodname->__toString();
}

public function getFunctionType(): string
public function getReturnType(): string
{
// If the function returns a boolean, since false is for error, true is for success.
// Let's replace this with a "void".
Expand All @@ -54,13 +54,13 @@ public function getFunctionType(): string
return 'mixed';
}

return $type;
return Type::toRootNamespace($type);
}

/**
* @return Parameter[]
*/
public function getFunctionParam(): array
public function getParams(): array
{
if ($this->params === null) {
if (!isset($this->functionObject->methodparam)) {
Expand Down Expand Up @@ -93,7 +93,7 @@ private function getDocBlock(): string
$str .= "\n\n";

$i=1;
foreach ($this->getFunctionParam() as $parameter) {
foreach ($this->getParams() as $parameter) {
$str .= '@param '.$parameter->getBestType().' $'.$parameter->getParameter().' ';
$str .= $this->getStringForXPath("(//docbook:refsect1[@role='parameters']//docbook:varlistentry)[$i]//docbook:para")."\n";
$i++;
Expand Down Expand Up @@ -157,9 +157,9 @@ private function getBestReturnType(): ?string
$phpStanFunction = $this->getPhpStanData();
// Get the type from PhpStan database first, then from the php doc.
if ($phpStanFunction !== null) {
return $phpStanFunction->getReturnType();
return Type::toRootNamespace($phpStanFunction->getReturnType());
} else {
return $this->getFunctionType();
return Type::toRootNamespace($this->getReturnType());
}
}

Expand Down Expand Up @@ -196,7 +196,7 @@ public function getModuleName(): string
*/
public function isOverloaded(): bool
{
foreach ($this->getFunctionParam() as $parameter) {
foreach ($this->getParams() as $parameter) {
if ($parameter->isOptionalWithNoDefault() && !$parameter->isByReference()) {
return true;
}
Expand All @@ -207,7 +207,7 @@ public function isOverloaded(): bool
public function cloneAndRemoveAParameter(): Method
{
$new = clone $this;
$params = $this->getFunctionParam();
$params = $this->getParams();
\array_pop($params);
$new->params = $params;
return $new;
Expand Down
3 changes: 2 additions & 1 deletion generator/src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function __construct(\SimpleXMLElement $parameter, ?PhpStanFunction $phpS
*/
public function getType(): string
{
return $this->parameter->type->__toString();
$type = $this->parameter->type->__toString();
return Type::toRootNamespace($type);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion generator/src/PhpStanFunctions/PhpStanParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Safe\PhpStanFunctions;

use Safe\Type;

class PhpStanParameter
{
/**
Expand Down Expand Up @@ -63,7 +65,7 @@ public function getName(): string
*/
public function getType(): string
{
return $this->type;
return Type::toRootNamespace($this->type);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions generator/src/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php


namespace Safe;

class Type
{
/**
* Returns true if the type passed in parameter is a class, false if it is scalar or resource
*
* @param string $type
* @return bool
*/
private static function isClass(string $type): bool
{
if ($type === '') {
throw new \RuntimeException('Empty type passed');
}
if ($type === 'stdClass') {
return true;
}
// Classes start with uppercase letters. Otherwise, it's most likely a scalar.
if ($type[0] === strtoupper($type[0])) {
return true;
}
return false;
}

/**
* Put classes in the root namespace
*
* @param string $type
* @return string
*/
public static function toRootNamespace(string $type): string
{
if (self::isClass($type)) {
return '\\'.$type;
}
return $type;
}
}
18 changes: 10 additions & 8 deletions generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Safe;

use function in_array;
use function is_numeric;
use function strtolower;
use function strtoupper;
use function var_export;

class WritePhpFunction
Expand All @@ -24,7 +26,7 @@ public function __construct(Method $method)
public function getPhpPrototypeFunction(): string
{
if ($this->method->getFunctionName()) {
return 'function '.$this->method->getFunctionName().'('.$this->displayParamsWithType($this->method->getFunctionParam()).')'.': '.$this->method->getFunctionType().'{}';
return 'function '.$this->method->getFunctionName().'('.$this->displayParamsWithType($this->method->getParams()).')'.': '.$this->method->getReturnType().'{}';
} else {
return '';
}
Expand All @@ -47,18 +49,18 @@ public function getPhpFunctionalFunction(): string
private function writePhpFunction(): string
{
$phpFunction = $this->method->getPhpDoc();
if ($this->method->getFunctionType() !== 'mixed' && $this->method->getFunctionType() !== 'resource') {
$returnType = ': ' . $this->method->getFunctionType();
if ($this->method->getReturnType() !== 'mixed' && $this->method->getReturnType() !== 'resource') {
$returnType = ': ' . $this->method->getReturnType();
} else {
$returnType = '';
}
$returnStatement = '';
if ($this->method->getFunctionType() !== 'void') {
if ($this->method->getReturnType() !== 'void') {
$returnStatement = " return \$result;\n";
}
$moduleName = $this->method->getModuleName();

$phpFunction .= "function {$this->method->getFunctionName()}({$this->displayParamsWithType($this->method->getFunctionParam())}){$returnType}
$phpFunction .= "function {$this->method->getFunctionName()}({$this->displayParamsWithType($this->method->getParams())}){$returnType}
{
error_clear_last();
";
Expand All @@ -69,7 +71,7 @@ private function writePhpFunction(): string
$method = $this->method;
$inElse = false;
do {
$lastParameter = $method->getFunctionParam()[count($method->getFunctionParam())-1];
$lastParameter = $method->getParams()[count($method->getParams())-1];
if ($inElse) {
$phpFunction .= ' else';
} else {
Expand Down Expand Up @@ -106,7 +108,7 @@ private function generateExceptionCode(string $moduleName, Method $method) : str
{
// Special case for CURL: we need the first argument of the method if this is a resource.
if ($moduleName === 'Curl') {
$params = $method->getFunctionParam();
$params = $method->getParams();
if (\count($params) > 0 && $params[0]->getParameter() === 'ch') {
return "
if (\$result === false) {
Expand Down Expand Up @@ -174,7 +176,7 @@ private function printFunctionCall(Method $function): string
$str = '...';
}
return $str.'$'.$parameter->getParameter();
}, $function->getFunctionParam()));
}, $function->getParams()));
$functionCall .= ');';
return $functionCall;
}
Expand Down
6 changes: 6 additions & 0 deletions generator/tests/GeneratedFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Safe;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use function restore_error_handler;
use Safe\Exceptions\StringsException;
Expand Down Expand Up @@ -66,5 +67,10 @@ public function testObjects()
$movies = simplexml_load_string($xmlStr);

$this->assertInstanceOf(SimpleXMLElement::class, $movies);


$doc = DOMDocument::loadXML('<node>foobar</node>');
$xmlElem = simplexml_import_dom($doc);
$this->assertInstanceOf(SimpleXMLElement::class, $xmlElem);
}
}
6 changes: 3 additions & 3 deletions generator/tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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());
$type = $method->getFunctionType();
$type = $method->getReturnType();
$this->assertEquals('int', $type);
}

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());
$params = $method->getFunctionParam();
$params = $method->getParams();
$this->assertEquals('string', $params[0]->getType());
$this->assertEquals('pattern', $params[0]->getParameter());
}
Expand All @@ -37,7 +37,7 @@ public function testGetInitializer() {
$xmlObject = $docPage->getMethodSynopsis();
$method = new Method($xmlObject[0], $docPage->loadAndResolveFile(), $docPage->getModule(), new PhpStanFunctionMapReader());

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

Expand Down
16 changes: 16 additions & 0 deletions generator/tests/TypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Safe;

use PHPUnit\Framework\TestCase;

class TypeTest extends TestCase
{
public function testIsClass()
{
$this->assertSame('\\stdClass', Type::toRootNamespace('stdClass'));
$this->assertSame('\\SimpleXMLElement', Type::toRootNamespace('SimpleXMLElement'));
$this->assertSame('bool', Type::toRootNamespace('bool'));
$this->assertSame('int', Type::toRootNamespace('int'));
}
}

0 comments on commit af2d0ec

Please sign in to comment.