Skip to content
Open
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
52 changes: 52 additions & 0 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -7493,6 +7493,58 @@ ZEND_METHOD(ReflectionAttribute, getName)
}
/* }}} */

/* Returns whether this attribute name comes from a namespace */
ZEND_METHOD(ReflectionAttribute, inNamespace)
{
reflection_object *intern;
attribute_reference *attr;

ZEND_PARSE_PARAMETERS_NONE();

GET_REFLECTION_OBJECT_PTR(attr);

const zend_string *name = attr->data->name;
const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
RETURN_BOOL(backslash);
}
/* }}} */

/* Returns the name of namespace where this attribute comes from */
ZEND_METHOD(ReflectionAttribute, getNamespaceName)
{
reflection_object *intern;
attribute_reference *attr;

ZEND_PARSE_PARAMETERS_NONE();

GET_REFLECTION_OBJECT_PTR(attr);

const zend_string *name = attr->data->name;
const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (backslash) {
RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
}
RETURN_EMPTY_STRING();
}

/* Returns the short name of the attribute (without namespace part) */
ZEND_METHOD(ReflectionAttribute, getShortName)
{
reflection_object *intern;
attribute_reference *attr;

ZEND_PARSE_PARAMETERS_NONE();

GET_REFLECTION_OBJECT_PTR(attr);

zend_string *name = attr->data->name;
const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
if (backslash) {
RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
}
RETURN_STR_COPY(name);
}

/* {{{ Returns the target of the attribute */
ZEND_METHOD(ReflectionAttribute, getTarget)
{
Expand Down
3 changes: 3 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@ class ReflectionAttribute implements Reflector
public string $name;

public function getName(): string {}
public function inNamespace(): bool {}
public function getNamespaceName(): string {}
public function getShortName(): string {}
public function getTarget(): int {}
public function isRepeated(): bool {}
public function getArguments(): array {}
Expand Down
14 changes: 13 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

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

8 changes: 4 additions & 4 deletions ext/reflection/php_reflection_decl.h

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
ReflectionAttribute name and namespace methods
--FILE--
<?php

namespace {

#[Attribute]
class AttrNotNamespace {}

}

namespace A\B {

#[Attribute]
class AttrNamespace {}

class Foo {
#[\Attr]
public function fn1() {}

#[AttrNamespace]
public function fn2() {}
}

$rm = new \ReflectionMethod(Foo::class, "fn1");
$attribute = $rm->getAttributes()[0];

var_dump($attribute->inNamespace());
var_dump($attribute->getName());
var_dump($attribute->getNamespaceName());
var_dump($attribute->getShortName());

$rm = new \ReflectionMethod(Foo::class, "fn2");
$attribute = $rm->getAttributes()[0];

var_dump($attribute->inNamespace());
var_dump($attribute->getName());
var_dump($attribute->getNamespaceName());
var_dump($attribute->getShortName());
}

?>
--EXPECT--
bool(false)
string(4) "Attr"
string(0) ""
string(4) "Attr"
bool(true)
string(17) "A\B\AttrNamespace"
string(3) "A\B"
string(13) "AttrNamespace"
Loading