Skip to content

Add support for generating namespaced constant #10552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,8 +931,11 @@ public function isUnknown(): bool
class ConstName extends AbstractConstName {
public string $const;

public function __construct(string $const)
public function __construct(?Name $namespace, string $const)
{
if ($namespace && ($namespace = $namespace->slice(0, -1))) {
$const = $namespace->toString() . '\\' . $const;
}
$this->const = $const;
}

Expand All @@ -941,6 +944,15 @@ public function isClassConst(): bool
return false;
}

public function isUnknown(): bool
{
$name = $this->__toString();
if (($pos = strrpos($name, '\\')) !== false) {
$name = substr($name, $pos + 1);
}
return strtolower($name) === "unknown";
}

public function __toString(): string
{
return $this->const;
Expand Down Expand Up @@ -1587,7 +1599,7 @@ function (Expr $expr) use ($allConstInfos, &$constType, &$originatingConst, &$is
if ($expr instanceof Expr\ClassConstFetch) {
$originatingConstName = new ClassConstName($expr->class, $expr->name->toString());
} else {
$originatingConstName = new ConstName($expr->name->toString());
$originatingConstName = new ConstName($expr->name->getAttribute('namespacedName'), $expr->name->toString());
}

if ($originatingConstName->isUnknown()) {
Expand Down Expand Up @@ -3630,7 +3642,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
foreach ($stmt->consts as $const) {
$fileInfo->constInfos[] = parseConstLike(
$prettyPrinter,
new ConstName($const->name->toString()),
new ConstName($const->namespacedName, $const->name->toString()),
$const,
0,
$stmt->getDocComment(),
Expand Down
22 changes: 11 additions & 11 deletions ext/ftp/ftp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/** @generate-class-entries */

namespace FTP {
namespace {
/**
* @var int
* @cvalue FTPTYPE_ASCII
Expand Down Expand Up @@ -59,16 +59,6 @@
*/
const FTP_MOREDATA = UNKNOWN;

/**
* @strict-properties
* @not-serializable
*/
final class Connection
{
}
}

namespace {
function ftp_connect(string $hostname, int $port = 21, int $timeout = 90): FTP\Connection|false {}

#ifdef HAVE_FTP_SSL
Expand Down Expand Up @@ -145,3 +135,13 @@ function ftp_quit(FTP\Connection $ftp): bool {}
function ftp_set_option(FTP\Connection $ftp, int $option, $value): bool {}
function ftp_get_option(FTP\Connection $ftp, int $option): int|bool {}
}

namespace FTP {
/**
* @strict-properties
* @not-serializable
*/
final class Connection
{
}
}
2 changes: 1 addition & 1 deletion ext/ftp/ftp_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 28 additions & 29 deletions ext/pgsql/pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/** @generate-class-entries */

namespace PgSql {
namespace {
/* libpq version */

/**
Expand Down Expand Up @@ -413,34 +413,6 @@
*/
const PGSQL_DML_STRING = UNKNOWN;

/**
* @strict-properties
* @not-serializable
*/
final class Connection
{
}

/**
* @strict-properties
* @not-serializable
*/
final class Result
{
}

/**
* @strict-properties
* @not-serializable
*/
final class Lob
{
}

}

namespace {

function pg_connect(string $connection_string, int $flags = 0): PgSql\Connection|false {}

function pg_pconnect(string $connection_string, int $flags = 0): PgSql\Connection|false {}
Expand Down Expand Up @@ -923,3 +895,30 @@ function pg_delete(PgSql\Connection $connection, string $table_name, array $cond
*/
function pg_select(PgSql\Connection $connection, string $table_name, array $conditions, int $flags = PGSQL_DML_EXEC, int $mode = PGSQL_ASSOC): array|string|false {}
}

namespace PgSql {
/**
* @strict-properties
* @not-serializable
*/
final class Connection
{
}

/**
* @strict-properties
* @not-serializable
*/
final class Result
{
}

/**
* @strict-properties
* @not-serializable
*/
final class Lob
{
}

}
2 changes: 1 addition & 1 deletion ext/pgsql/pgsql_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "zend_interfaces.h"
#include "zend_weakrefs.h"
#include "Zend/Optimizer/zend_optimizer.h"
#include "test.h"
#include "test_arginfo.h"

ZEND_DECLARE_MODULE_GLOBALS(zend_test)
Expand Down
22 changes: 22 additions & 0 deletions ext/zend_test/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/

#ifndef ZEND_TEST_H
#define ZEND_TEST_H

#define ZEND_TEST_NS_CONSTANT_A "namespaced"

#endif
13 changes: 13 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
const ZEND_TEST_DEPRECATED = 42;

/** @var string */
const ZEND_CONSTANT_A = "global";

require "Zend/zend_attributes.stub.php";

interface _ZendTestInterface
Expand Down Expand Up @@ -180,6 +183,12 @@ public function method(): ?UnlikelyCompileError {}

namespace ZendTestNS2 {

/**
* @var string
* @cvalue ZEND_TEST_NS_CONSTANT_A
*/
const ZEND_CONSTANT_A = UNKNOWN;

class Foo {
public ZendSubNS\Foo $foo;

Expand All @@ -203,6 +212,10 @@ function namespaced_deprecated_aliased_func(): void {}

namespace ZendTestNS2\ZendSubNS {

/** @var string */
const ZEND_CONSTANT_A = \ZendTestNS2\ZEND_CONSTANT_A;
// Reference another namespaced constant.

class Foo {
public function method(): void {}
}
Expand Down
5 changes: 4 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ext/zend_test/tests/gen_stub_test_02.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
gen_stub.php: constants
--EXTENSIONS--
zend_test
--FILE--
<?php

var_dump(ZEND_CONSTANT_A);
var_dump(ZendTestNS2\ZEND_CONSTANT_A);
var_dump(ZendTestNS2\ZendSubNS\ZEND_CONSTANT_A);

?>
--EXPECT--
string(6) "global"
string(10) "namespaced"
string(10) "namespaced"