Skip to content

Associative mode for ReflectionAttribute::getArguments #18165

@divinity76

Description

@divinity76

Description

Would be nice if ReflectionAttribute::getArguments had an associative mode, for example

<?php

#[Attribute(Attribute::TARGET_PARAMETER)]
class ArgumentDescription {
    public function __construct(
        public string $description,
        public mixed $example = null
    ) {}
}

function test(
    #[ArgumentDescription("amount in USD", 9.99)]
    float $amount,
) {}
$ref = new ReflectionFunction(test(...));
$parameter = $ref->getParameters()[0];
$parameterAttribute = $parameter->getAttributes()[0];
var_dump($parameterAttribute->getArguments());
var_dump($parameterAttribute->getArguments(associative: true));

could print

array(2) {
  [0]=>
  string(13) "amount in USD"
  [1]=>
  float(9.99)
}
array(2) {
  ["description"]=>
  string(13) "amount in USD"
  ["example"]=>
  float(9.99)
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions