Appreciate this is probably an edge case but others may have run into it too. If you use an interactive shell to interact with deserialization via the DocBlockTypeResolver the class will try to autoload before it has been properly expanded via the use statements within the class.
Steps required to reproduce the problem
With this simplified version of the setup:
<?php
namespace ExampleAPI\Response;
use ExampleAPI\Models\Product;
class ProductCollection
{
/** @var Product[] */
public array $data;
}
Expected Result
After deserializing it would be expected that the resolved type would include the use statement but the autoloader in PsySH will scan the project for other classes named Product and will possibly select the wrong class.
The first few lines use class_exists('Product') rather than expanding out from the use statement first. My question is, whilst most people won't run into this problem wouldn't it make sense to expand the class from the use statements first and call class_exists('ExampleAPI\Models\Product') first and then fallback to the non namespaced class if the first is not successful?
Hi!
Looking at your explanation - it looks like a bug (edgecase but still bug). Feel free to create an Pull Request with the fix. :) let me know if you'd need any help with preparing the fix.
Appreciate this is probably an edge case but others may have run into it too. If you use an interactive shell to interact with deserialization via the
DocBlockTypeResolver
the class will try to autoload before it has been properly expanded via theuse
statements within the class.Steps required to reproduce the problem
With this simplified version of the setup:
Expected Result
After deserializing it would be expected that the resolved type would include the
use
statement but the autoloader in PsySH will scan the project for other classes namedProduct
and will possibly select the wrong class.Possible change
The logic to resolve is here: https://github.com/schmittjoh/serializer/blob/master/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php#L250
The first few lines use
class_exists('Product')
rather than expanding out from the use statement first. My question is, whilst most people won't run into this problem wouldn't it make sense to expand the class from the use statements first and callclass_exists('ExampleAPI\Models\Product')
first and then fallback to the non namespaced class if the first is not successful?Happy to PR if you agree.