Skip to content

Commit

Permalink
Introduce <<NameAlias($alias)>> to allow second name for named param.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Jul 14, 2020
1 parent 5d37c34 commit 4b0a02f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Zend/tests/named_params/attribute.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
NameAlias Attribute
--FILE--
<?php

function test(<<NameAlias("bar")>> $foo) {
echo $foo;
}

test(bar: "Hello World");
--EXPECT--
Hello World
31 changes: 31 additions & 0 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@
#include "zend_smart_str.h"

ZEND_API zend_class_entry *zend_ce_attribute;
ZEND_API zend_class_entry *zend_ce_namealias_attribute;

static HashTable internal_attributes;

void validate_namealias_attribute(zend_attribute *attr, uint32_t target, zend_class_entry *scope)
{
if (attr->argc != 1) {
zend_error_noreturn(E_ERROR, "NameAlias::__construct(): Argument #1 ($alias) is required");
}
}

void validate_attribute(zend_attribute *attr, uint32_t target, zend_class_entry *scope)
{
if (attr->argc > 0) {
Expand Down Expand Up @@ -63,6 +71,17 @@ ZEND_METHOD(Attribute, __construct)
ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), flags);
}

ZEND_METHOD(NameAlias, __construct)
{
zend_string *alias = NULL;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_STR(alias)
ZEND_PARSE_PARAMETERS_END();

ZVAL_STR(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), alias);
}

static zend_attribute *get_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset)
{
if (attributes) {
Expand Down Expand Up @@ -293,6 +312,18 @@ void zend_register_attribute_ce(void)

attr = zend_internal_attribute_register(zend_ce_attribute, ZEND_ATTRIBUTE_TARGET_CLASS);
attr->validator = validate_attribute;

INIT_CLASS_ENTRY(ce, "NameAlias", class_NameAlias_methods);
zend_ce_namealias_attribute = zend_register_internal_class(&ce);
zend_ce_namealias_attribute->ce_flags |= ZEND_ACC_FINAL;

ZVAL_UNDEF(&tmp);
str = zend_string_init(ZEND_STRL("alias"), 1);
zend_declare_typed_property(zend_ce_attribute, str, &tmp, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_CODE(IS_STRING, 0, 0));
zend_string_release(str);

attr = zend_internal_attribute_register(zend_ce_namealias_attribute, ZEND_ATTRIBUTE_TARGET_PARAMETER);
attr->validator = validate_namealias_attribute;
}

void zend_attributes_shutdown(void)
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_attributes.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ final class Attribute
{
public function __construct(int $flags = Attribute::TARGET_ALL) {}
}

final class NameAlias
{
public function __construct(string $alias) {}
}
13 changes: 12 additions & 1 deletion Zend/zend_attributes_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 54eede8541597ec2ac5c04e31d14e2db7e8c5556 */
* Stub hash: 59e49a64d9ef3814513d84094652742261f94468 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Attribute___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "Attribute::TARGET_ALL")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_NameAlias___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, alias, IS_STRING, 0)
ZEND_END_ARG_INFO()


ZEND_METHOD(Attribute, __construct);
ZEND_METHOD(NameAlias, __construct);


static const zend_function_entry class_Attribute_methods[] = {
ZEND_ME(Attribute, __construct, arginfo_class_Attribute___construct, ZEND_ACC_PUBLIC)
ZEND_FE_END
};


static const zend_function_entry class_NameAlias_methods[] = {
ZEND_ME(NameAlias, __construct, arginfo_class_NameAlias___construct, ZEND_ACC_PUBLIC)
ZEND_FE_END
};
12 changes: 12 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "zend_inheritance.h"
#include "zend_type_info.h"
#include "zend_smart_str.h"
#include "zend_attributes.h"

/* Virtual current working directory support */
#include "zend_virtual_cwd.h"
Expand Down Expand Up @@ -4300,11 +4301,22 @@ static zend_always_inline uint32_t zend_get_arg_offset_by_name(
|| EXPECTED(fbc->common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
for (uint32_t i = 0; i < num_args; i++) {
zend_arg_info *arg_info = &fbc->op_array.arg_info[i];

if (zend_string_equals(arg_name, arg_info->name)) {
*cache_slot = fbc;
*(uintptr_t *)(cache_slot + 1) = i;
return i;
}

if (fbc->common.attributes != NULL) {
zend_attribute *attribute = zend_get_parameter_attribute_str(fbc->common.attributes, "namealias", sizeof("namealias")-1, i);

if (attribute && zend_string_equals(arg_name, Z_STR(attribute->args[0].value) )) {
*cache_slot = fbc;
*(uintptr_t *)(cache_slot + 1) = i;
return i;
}
}
}
} else {
for (uint32_t i = 0; i < num_args; i++) {
Expand Down

0 comments on commit 4b0a02f

Please sign in to comment.