Skip to content

Commit 7fccdf7

Browse files
ranviskocsismate
authored andcommitted
Add support for generating namespaced constant
Closes GH-10552
1 parent 9660a7f commit 7fccdf7

File tree

10 files changed

+112
-46
lines changed

10 files changed

+112
-46
lines changed

build/gen_stub.php

Lines changed: 15 additions & 3 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

@@ -941,6 +944,15 @@ public function isClassConst(): bool
941944
return false;
942945
}
943946

947+
public function isUnknown(): bool
948+
{
949+
$name = $this->__toString();
950+
if (($pos = strrpos($name, '\\')) !== false) {
951+
$name = substr($name, $pos + 1);
952+
}
953+
return strtolower($name) === "unknown";
954+
}
955+
944956
public function __toString(): string
945957
{
946958
return $this->const;
@@ -1587,7 +1599,7 @@ function (Expr $expr) use ($allConstInfos, &$constType, &$originatingConst, &$is
15871599
if ($expr instanceof Expr\ClassConstFetch) {
15881600
$originatingConstName = new ClassConstName($expr->class, $expr->name->toString());
15891601
} else {
1590-
$originatingConstName = new ConstName($expr->name->toString());
1602+
$originatingConstName = new ConstName($expr->name->getAttribute('namespacedName'), $expr->name->toString());
15911603
}
15921604

15931605
if ($originatingConstName->isUnknown()) {
@@ -3630,7 +3642,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
36303642
foreach ($stmt->consts as $const) {
36313643
$fileInfo->constInfos[] = parseConstLike(
36323644
$prettyPrinter,
3633-
new ConstName($const->name->toString()),
3645+
new ConstName($const->namespacedName, $const->name->toString()),
36343646
$const,
36353647
0,
36363648
$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.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "zend_interfaces.h"
3030
#include "zend_weakrefs.h"
3131
#include "Zend/Optimizer/zend_optimizer.h"
32+
#include "test.h"
3233
#include "test_arginfo.h"
3334

3435
ZEND_DECLARE_MODULE_GLOBALS(zend_test)

ext/zend_test/test.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_TEST_H
18+
#define ZEND_TEST_H
19+
20+
#define ZEND_TEST_NS_CONSTANT_A "namespaced"
21+
22+
#endif

ext/zend_test/test.stub.php

Lines changed: 13 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
@@ -180,6 +183,12 @@ public function method(): ?UnlikelyCompileError {}
180183

181184
namespace ZendTestNS2 {
182185

186+
/**
187+
* @var string
188+
* @cvalue ZEND_TEST_NS_CONSTANT_A
189+
*/
190+
const ZEND_CONSTANT_A = UNKNOWN;
191+
183192
class Foo {
184193
public ZendSubNS\Foo $foo;
185194

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

204213
namespace ZendTestNS2\ZendSubNS {
205214

215+
/** @var string */
216+
const ZEND_CONSTANT_A = \ZendTestNS2\ZEND_CONSTANT_A;
217+
// Reference another namespaced constant.
218+
206219
class Foo {
207220
public function method(): void {}
208221
}

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)