Skip to content

Commit 1cd0389

Browse files
committed
Add support for generating namespaced constant
1 parent 0493187 commit 1cd0389

File tree

8 files changed

+78
-47
lines changed

8 files changed

+78
-47
lines changed

build/gen_stub.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,11 @@ public function isUnknown(): bool
931931
class ConstName extends AbstractConstName {
932932
public string $const;
933933

934-
public function __construct(string $const)
934+
public function __construct(?Name $namespace, string $const)
935935
{
936+
if ($namespace && ($namespace = $namespace->slice(0, -1))) {
937+
$const = $namespace->toString() . '\\' . $const;
938+
}
936939
$this->const = $const;
937940
}
938941

@@ -1600,7 +1603,7 @@ public function enterNode(Node $expr)
16001603
if ($expr instanceof Expr\ClassConstFetch) {
16011604
$originatingConstName = new ClassConstName($expr->class, $expr->name->toString());
16021605
} else {
1603-
$originatingConstName = new ConstName($expr->name->toString());
1606+
$originatingConstName = new ConstName($expr->name->getAttribute('namespacedName'), $expr->name->toString());
16041607
}
16051608

16061609
if ($originatingConstName->isUnknown()) {
@@ -1658,7 +1661,7 @@ function (Expr $expr) use ($allConstInfos, &$isUnknownConstValue) {
16581661
return null;
16591662
}
16601663

1661-
throw new Exception("Constant " . $originatingConstName->__toString() . " cannot be found");
1664+
throw new Exception("Constant " . $constName . " cannot be found");
16621665
}
16631666
);
16641667

@@ -3677,7 +3680,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
36773680
foreach ($stmt->consts as $const) {
36783681
$fileInfo->constInfos[] = parseConstLike(
36793682
$prettyPrinter,
3680-
new ConstName($const->name->toString()),
3683+
new ConstName($const->namespacedName, $const->name->toString()),
36813684
$const,
36823685
0,
36833686
$stmt->getDocComment(),

ext/ftp/ftp.stub.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/** @generate-class-entries */
44

5-
namespace FTP {
5+
namespace {
66
/**
77
* @var int
88
* @cvalue FTPTYPE_ASCII
@@ -59,16 +59,6 @@
5959
*/
6060
const FTP_MOREDATA = UNKNOWN;
6161

62-
/**
63-
* @strict-properties
64-
* @not-serializable
65-
*/
66-
final class Connection
67-
{
68-
}
69-
}
70-
71-
namespace {
7262
function ftp_connect(string $hostname, int $port = 21, int $timeout = 90): FTP\Connection|false {}
7363

7464
#ifdef HAVE_FTP_SSL
@@ -145,3 +135,13 @@ function ftp_quit(FTP\Connection $ftp): bool {}
145135
function ftp_set_option(FTP\Connection $ftp, int $option, $value): bool {}
146136
function ftp_get_option(FTP\Connection $ftp, int $option): int|bool {}
147137
}
138+
139+
namespace FTP {
140+
/**
141+
* @strict-properties
142+
* @not-serializable
143+
*/
144+
final class Connection
145+
{
146+
}
147+
}

ext/ftp/ftp_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/pgsql/pgsql.stub.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/** @generate-class-entries */
44

5-
namespace PgSql {
5+
namespace {
66
/* libpq version */
77

88
/**
@@ -413,34 +413,6 @@
413413
*/
414414
const PGSQL_DML_STRING = UNKNOWN;
415415

416-
/**
417-
* @strict-properties
418-
* @not-serializable
419-
*/
420-
final class Connection
421-
{
422-
}
423-
424-
/**
425-
* @strict-properties
426-
* @not-serializable
427-
*/
428-
final class Result
429-
{
430-
}
431-
432-
/**
433-
* @strict-properties
434-
* @not-serializable
435-
*/
436-
final class Lob
437-
{
438-
}
439-
440-
}
441-
442-
namespace {
443-
444416
function pg_connect(string $connection_string, int $flags = 0): PgSql\Connection|false {}
445417

446418
function pg_pconnect(string $connection_string, int $flags = 0): PgSql\Connection|false {}
@@ -923,3 +895,30 @@ function pg_delete(PgSql\Connection $connection, string $table_name, array $cond
923895
*/
924896
function pg_select(PgSql\Connection $connection, string $table_name, array $conditions, int $flags = PGSQL_DML_EXEC, int $mode = PGSQL_ASSOC): array|string|false {}
925897
}
898+
899+
namespace PgSql {
900+
/**
901+
* @strict-properties
902+
* @not-serializable
903+
*/
904+
final class Connection
905+
{
906+
}
907+
908+
/**
909+
* @strict-properties
910+
* @not-serializable
911+
*/
912+
final class Result
913+
{
914+
}
915+
916+
/**
917+
* @strict-properties
918+
* @not-serializable
919+
*/
920+
final class Lob
921+
{
922+
}
923+
924+
}

ext/pgsql/pgsql_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zend_test/test.stub.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
const ZEND_TEST_DEPRECATED = 42;
1414

15+
/** @var string */
16+
const ZEND_CONSTANT_A = "global";
17+
1518
require "Zend/zend_attributes.stub.php";
1619

1720
interface _ZendTestInterface
@@ -199,6 +202,9 @@ public function method(): ?UnlikelyCompileError {}
199202

200203
namespace ZendTestNS2 {
201204

205+
/** @var string */
206+
const ZEND_CONSTANT_A = "namespaced";
207+
202208
class Foo {
203209
public ZendSubNS\Foo $foo;
204210

@@ -222,6 +228,10 @@ function namespaced_deprecated_aliased_func(): void {}
222228

223229
namespace ZendTestNS2\ZendSubNS {
224230

231+
/** @var string */
232+
const ZEND_CONSTANT_A = \ZendTestNS2\ZEND_CONSTANT_A;
233+
// Reference another namespaced constant.
234+
225235
class Foo {
226236
public function method(): void {}
227237
}

ext/zend_test/test_arginfo.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
gen_stub.php: constants
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
var_dump(ZEND_CONSTANT_A);
9+
var_dump(ZendTestNS2\ZEND_CONSTANT_A);
10+
var_dump(ZendTestNS2\ZendSubNS\ZEND_CONSTANT_A);
11+
12+
?>
13+
--EXPECT--
14+
string(6) "global"
15+
string(10) "namespaced"
16+
string(10) "namespaced"

0 commit comments

Comments
 (0)