diff --git a/generated/functionsList.php b/generated/functionsList.php index 6ecaa3c4..e14d7a57 100644 --- a/generated/functionsList.php +++ b/generated/functionsList.php @@ -542,7 +542,6 @@ 'header_register_callback', 'inet_ntop', 'openlog', - 'setrawcookie', 'syslog', 'oci_bind_array_by_name', 'oci_bind_by_name', @@ -874,7 +873,6 @@ 'session_regenerate_id', 'session_register', 'session_reset', - 'session_set_cookie_params', 'session_unregister', 'session_unset', 'session_write_close', @@ -1007,7 +1005,6 @@ 'uopz_extend', 'uopz_implement', 'base64_decode', - 'base64_encode', 'get_headers', 'import_request_variables', 'settype', diff --git a/generated/gmp.php b/generated/gmp.php index 9bfe2ba9..1ac8e06b 100644 --- a/generated/gmp.php +++ b/generated/gmp.php @@ -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); @@ -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); @@ -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 * */ diff --git a/generated/hash.php b/generated/hash.php index bf46da1d..ff916a41 100644 --- a/generated/hash.php +++ b/generated/hash.php @@ -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); diff --git a/generated/network.php b/generated/network.php index 8c20736e..67f709ec 100644 --- a/generated/network.php +++ b/generated/network.php @@ -478,45 +478,6 @@ function openlog(string $ident, int $option, int $facility): void } -/** - * setrawcookie is exactly the same as - * setcookie except that the cookie value will not be - * automatically urlencoded when sent to the browser. - * - * @param string $name - * @param string $value - * @param int $expire - * @param string $path - * @param string $domain - * @param bool $secure - * @param bool $httponly - * @throws NetworkException - * - */ -function setrawcookie(string $name, string $value = null, int $expire = 0, string $path = null, string $domain = null, bool $secure = false, bool $httponly = false): void -{ - error_clear_last(); - if ($httponly !== false) { - $result = \setrawcookie($name, $value, $expire, $path, $domain, $secure, $httponly); - } elseif ($secure !== false) { - $result = \setrawcookie($name, $value, $expire, $path, $domain, $secure); - } elseif ($domain !== null) { - $result = \setrawcookie($name, $value, $expire, $path, $domain); - } elseif ($path !== null) { - $result = \setrawcookie($name, $value, $expire, $path); - } elseif ($expire !== 0) { - $result = \setrawcookie($name, $value, $expire); - } elseif ($value !== null) { - $result = \setrawcookie($name, $value); - } else { - $result = \setrawcookie($name); - } - if ($result === false) { - throw NetworkException::createFromPhpError(); - } -} - - /** * syslog generates a log message that will be * distributed by the system logger. diff --git a/generated/oci8.php b/generated/oci8.php index 6813b61f..f173dc1b 100644 --- a/generated/oci8.php +++ b/generated/oci8.php @@ -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 * */ @@ -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 * */ diff --git a/generated/session.php b/generated/session.php index 9e84d68a..87e5b0b1 100644 --- a/generated/session.php +++ b/generated/session.php @@ -153,51 +153,6 @@ function session_reset(): void } -/** - * Set cookie parameters defined in the php.ini file. The effect of this - * function only lasts for the duration of the script. Thus, you need to - * call session_set_cookie_params for every request and - * before session_start is called. - * - * This function updates the runtime ini values of the corresponding PHP ini configuration - * keys which can be retrieved with the ini_get. - * - * @param int $lifetime Lifetime of the - * session cookie, defined in seconds. - * @param string $path Path on the domain where - * the cookie will work. Use a single slash ('/') for all paths on the - * domain. - * @param string|null $domain Cookie domain, for - * example 'www.php.net'. To make cookies visible on all subdomains then - * the domain must be prefixed with a dot like '.php.net'. - * @param bool $secure If TRUE cookie will only be sent over - * secure connections. - * @param bool $httponly If set to TRUE then PHP will attempt to send the - * httponly - * flag when setting the session cookie. - * @throws SessionException - * - */ -function session_set_cookie_params(int $lifetime, string $path = null, string $domain = null, bool $secure = false, bool $httponly = false): void -{ - error_clear_last(); - if ($httponly !== false) { - $result = \session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly); - } elseif ($secure !== false) { - $result = \session_set_cookie_params($lifetime, $path, $domain, $secure); - } elseif ($domain !== null) { - $result = \session_set_cookie_params($lifetime, $path, $domain); - } elseif ($path !== null) { - $result = \session_set_cookie_params($lifetime, $path); - } else { - $result = \session_set_cookie_params($lifetime); - } - if ($result === false) { - throw SessionException::createFromPhpError(); - } -} - - /** * session_unregister unregisters the global variable * named name from the current session. diff --git a/generated/shmop.php b/generated/shmop.php index cec7215c..70edec16 100644 --- a/generated/shmop.php +++ b/generated/shmop.php @@ -29,7 +29,7 @@ function shmop_delete($shmid): void * shmop_open * @param int $start Offset from which to start reading * @param int $count The number of bytes to read. - * 0 reads shmop_size($shmId) - $offset bytes. + * 0 reads shmop_size($shmid) - $start bytes. * @return string Returns the data . * @throws ShmopException * diff --git a/generated/simplexml.php b/generated/simplexml.php index 692ef13e..8b9d0710 100644 --- a/generated/simplexml.php +++ b/generated/simplexml.php @@ -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); @@ -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); @@ -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); diff --git a/generated/url.php b/generated/url.php index a3a968d3..626ad8b5 100644 --- a/generated/url.php +++ b/generated/url.php @@ -28,31 +28,6 @@ function base64_decode(string $data, bool $strict = false): string } -/** - * Encodes the given data with base64. - * - * This encoding is designed to make binary data survive transport through - * transport layers that are not 8-bit clean, such as mail bodies. - * - * Base64-encoded data takes about 33% more space than the original - * data. - * - * @param string $data The data to encode. - * @return string The encoded data, as a string . - * @throws UrlException - * - */ -function base64_encode(string $data): string -{ - error_clear_last(); - $result = \base64_encode($data); - if ($result === false) { - throw UrlException::createFromPhpError(); - } - return $result; -} - - /** * get_headers returns an array with the headers sent * by the server in response to a HTTP request. diff --git a/generator/src/Method.php b/generator/src/Method.php index 927eca0e..66a96dcc 100644 --- a/generator/src/Method.php +++ b/generator/src/Method.php @@ -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". @@ -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)) { @@ -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++; @@ -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()); } } @@ -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; } @@ -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; diff --git a/generator/src/Parameter.php b/generator/src/Parameter.php index c2629215..a8842487 100644 --- a/generator/src/Parameter.php +++ b/generator/src/Parameter.php @@ -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); } /** diff --git a/generator/src/PhpStanFunctions/PhpStanParameter.php b/generator/src/PhpStanFunctions/PhpStanParameter.php index 86d42aca..2a9d5268 100644 --- a/generator/src/PhpStanFunctions/PhpStanParameter.php +++ b/generator/src/PhpStanFunctions/PhpStanParameter.php @@ -3,6 +3,8 @@ namespace Safe\PhpStanFunctions; +use Safe\Type; + class PhpStanParameter { /** @@ -63,7 +65,7 @@ public function getName(): string */ public function getType(): string { - return $this->type; + return Type::toRootNamespace($this->type); } /** diff --git a/generator/src/Type.php b/generator/src/Type.php new file mode 100644 index 00000000..c4e38bce --- /dev/null +++ b/generator/src/Type.php @@ -0,0 +1,42 @@ +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 ''; } @@ -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(); "; @@ -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 { @@ -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) { @@ -174,7 +176,7 @@ private function printFunctionCall(Method $function): string $str = '...'; } return $str.'$'.$parameter->getParameter(); - }, $function->getFunctionParam())); + }, $function->getParams())); $functionCall .= ');'; return $functionCall; } diff --git a/generator/tests/GeneratedFilesTest.php b/generator/tests/GeneratedFilesTest.php index acdfa71d..696bc891 100644 --- a/generator/tests/GeneratedFilesTest.php +++ b/generator/tests/GeneratedFilesTest.php @@ -2,11 +2,10 @@ namespace Safe; -use const E_WARNING; -use function error_reporting; use PHPUnit\Framework\TestCase; use function restore_error_handler; use Safe\Exceptions\StringsException; +use SimpleXMLElement; /** * This test must be called AFTER generation of files has occurred @@ -47,4 +46,31 @@ public function testPregMatch() \preg_match($spotifyRegex, $url, $originalMatches); $this->assertSame($originalMatches, $matches); } + + public function testObjects() + { + require_once __DIR__.'/../../generated/simplexml.php'; + require_once __DIR__.'/../../lib/Exceptions/SafeExceptionInterface.php'; + require_once __DIR__.'/../../lib/Exceptions/AbstractSafeException.php'; + require_once __DIR__.'/../../generated/Exceptions/SimplexmlException.php'; + + $xmlStr = << + + + PHP: Behind the Parser + + +XML; + + $movies = simplexml_load_string($xmlStr); + + $this->assertInstanceOf(SimpleXMLElement::class, $movies); + + $domImplementation = new \DOMImplementation(); + $doc = $domImplementation->createDocument(null, 'foo'); + + $xmlElem = simplexml_import_dom($doc); + $this->assertInstanceOf(SimpleXMLElement::class, $xmlElem); + } } diff --git a/generator/tests/MethodTest.php b/generator/tests/MethodTest.php index a106f5c3..0eb83b72 100644 --- a/generator/tests/MethodTest.php +++ b/generator/tests/MethodTest.php @@ -19,7 +19,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()); - $type = $method->getFunctionType(); + $type = $method->getReturnType(); $this->assertEquals('int', $type); } @@ -27,7 +27,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()); - $params = $method->getFunctionParam(); + $params = $method->getParams(); $this->assertEquals('string', $params[0]->getType()); $this->assertEquals('pattern', $params[0]->getParameter()); } @@ -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()); } diff --git a/generator/tests/TypeTest.php b/generator/tests/TypeTest.php new file mode 100644 index 00000000..d2f69e6d --- /dev/null +++ b/generator/tests/TypeTest.php @@ -0,0 +1,16 @@ +assertSame('\\stdClass', Type::toRootNamespace('stdClass')); + $this->assertSame('\\SimpleXMLElement', Type::toRootNamespace('SimpleXMLElement')); + $this->assertSame('bool', Type::toRootNamespace('bool')); + $this->assertSame('int', Type::toRootNamespace('int')); + } +} diff --git a/rector-migrate.yml b/rector-migrate.yml index f1840abe..bde4c848 100644 --- a/rector-migrate.yml +++ b/rector-migrate.yml @@ -543,7 +543,6 @@ services: header_register_callback: 'Safe\header_register_callback' inet_ntop: 'Safe\inet_ntop' openlog: 'Safe\openlog' - setrawcookie: 'Safe\setrawcookie' syslog: 'Safe\syslog' oci_bind_array_by_name: 'Safe\oci_bind_array_by_name' oci_bind_by_name: 'Safe\oci_bind_by_name' @@ -875,7 +874,6 @@ services: session_regenerate_id: 'Safe\session_regenerate_id' session_register: 'Safe\session_register' session_reset: 'Safe\session_reset' - session_set_cookie_params: 'Safe\session_set_cookie_params' session_unregister: 'Safe\session_unregister' session_unset: 'Safe\session_unset' session_write_close: 'Safe\session_write_close' @@ -1008,7 +1006,6 @@ services: uopz_extend: 'Safe\uopz_extend' uopz_implement: 'Safe\uopz_implement' base64_decode: 'Safe\base64_decode' - base64_encode: 'Safe\base64_encode' get_headers: 'Safe\get_headers' import_request_variables: 'Safe\import_request_variables' settype: 'Safe\settype'