composer-attribute-collector
composer-attribute-collector copied to clipboard
Undefined method named "__set_state"
Hello :wave:
Thank you for this very handy library :pray: I'm experiencing with it, trying to automate few things in my code base.
But on some Symfony project of mine, I'm stuck using it because of an error, preventing the whole stack to start:
Attempted to call an undefined method named "__set_state" of class "Symfony\Component\Validator\Constraints\Choice".
The error occurs in the vendor/attributes.php file in that section of the file:
'Symfony\\Component\\Validator\\Constraints\\All' =>
array (
0 =>
array (
0 =>
array (
0 =>
array (
0 =>
\Symfony\Component\Validator\Constraints\Choice::__set_state(array(
'payload' => NULL,
'choices' =>
array (
0 => 'all',
),
'callback' => NULL,
'multiple' => false,
'strict' => true,
'min' => NULL,
'max' => NULL,
'message' => 'The value you selected is not a valid choice.',
'multipleMessage' => 'One or more of the given values is invalid.',
'minMessage' => 'You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.',
'maxMessage' => 'You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.',
'match' => true,
)),
),
),
1 => 'App\\Entity\\AccessToken',
2 => 'scopes',
),
),
It seems to be related to that part of my App code:
use Symfony\Component\Validator\Constraints as Assert;
class AccessToken
{
public const SCOPES = ['all'];
/**
* @var array<string>
*/
#[Assert\Count(min: 1)]
#[Assert\All([new Assert\Choice(self::SCOPES)])]
private array $scopes = [];
}
I believe it is about how nested attributes are handled. Is there anything I can do to help?
Best regards, Yann
Having the same issue with Attributes like:
#[ExprExample(
'.items | map(.) = [1,2,3]',
'map over an iterator',
[
'items' => new ArrayIterator([1, 2, 3]),
],
)]
or
#[ExprExample(
'.items | map(.) = [1,2,3]',
'map over an iterator',
[
'items' => new ArrayIterator([1, 2, 3]),
],
)]
Hi, thanks for raising the issue. Because the data is exported with var_export, objects passed as parameters have to implement __set_state. I'm not sure there's a simple solution for it. I know it's not ideal, but, in the meantime, you could extend Symfony\Component\Validator\Constraints\Choice to define that __set_state method.
Yeah, that is indeed a workaround, but I'm not sure it will suit for most projects
(for the particular use case of mine, I've created an home made solution that do not require the library, so I'm not waiting for a quick fix)
@yann-eugone @christianankert Would you kindly test the branch 2.1-use-reflection and see if it solves the issue? It includes an alternative mode for the "attributes" file that uses reflection to instantiate attributes instead of embedding the parameters. You must add the "use-reflection": true to your extra section. It's explained in the README.
"extra": {
"composer-attribute-collector": {
"include": [
"src"
],
"use-reflection": true
}
}
https://github.com/olvlvl/composer-attribute-collector/compare/main...2.1-use-reflection
Alternatively, in the branch main-use-serialization attribute arguments are serialized. So we continue to avoid reflections.
https://github.com/olvlvl/composer-attribute-collector/pull/36
Fixed in v2.2 (dev).