php / php/doc-en

Issue with ReflectionAttribute::getTaget(), PHP Version 8.4.6, Win x64

Open
#4,698 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Extension: reflection
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Affected page

https://www.php.net/manual/en/reflectionattribute.gettarget.php

ReflectionAttribute::getTaget() returns wrong value; it does not return the flags actually defined inside the Attribute-attribute of the CustomAttribute-class.

Please note, if this isn't a bug but expected behavior, it should be mentioned in the documentation: e. g. if the entry point is via new ReflectionClass('My') and '1' is always returned in this context.

Since this isn't mentioned in the documentation at all, I interpret this as unexpected behavior.

(Describe the bug, including incorrect behavior, broken links, or missing content.)

Example:

<?php
#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_CLASS)]
class MyAttribute {}

#[MyAttribute]
class My{
	#[MyAttribute]
	private $foo;
}

var_dump(Attribute::TARGET_PROPERTY|Attribute::TARGET_CLASS); // actual 9, expected 9
var_dump(new ReflectionClass('My')
    ->getAttributes('MyAttribute')[0]
    ->getTarget()); // actual 1, expected 9 ◄ ?

var_dump(new ReflectionProperty('My', 'foo')
    ->getAttributes('MyAttribute')[0]
    ->getTarget()); // actual 8, expected 9 ◄ ?

ReflectionAttribute::getTarget() should always return the flag actually set. Based on the example above, this would be 9 instead of 1 or 8, namely from Attribute::TARGET_PROPERTY|Attribute::TARGET_CLASS

Suggested fix

None.

— If the following approach is supposed to be the solution, this should actually be mentioned in the documentation:

var_dump(
	new ReflectionClass(
		new ReflectionClass('My')
			->getAttributes('MyAttribute')[0]
			->name
	)->getAttributes('Attribute')[0]
		->getArguments()
); // 9

Because:

var_dump(
	new ReflectionClass(
		new ReflectionClass('My')
			->getAttributes('MyAttribute')[0]
			->name
	)->getAttributes('Attribute')[0]
		->getTarget()
); // returns also 1

In my humble opinion, getTarget() never returns the full bitmask. The only thing you can do with it is figure out which reflector class (ReflectionClass, ReflectionProperty, …) opened the reflection.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the affected ReflectionAttribute::getTarget() manual page and compare its description with the supplied ReflectionClass and ReflectionProperty examples. Confirm the documented meaning of the returned target flags, then update the page to explain the behavior if it is expected; otherwise, leave the documentation change for a confirmed engine fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.