Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect data from collection #812

Merged
merged 17 commits into from
Aug 13, 2024

Conversation

clementbirkle
Copy link
Contributor

@clementbirkle clementbirkle commented Jul 3, 2024

This PR introduces a new feature that allows the use of well-annotated collections without the need for annotations inside the Data class. By ensuring that collections are well-annotated, we can achieve strong typing and better static analysis, eliminating the need to redundantly annotate properties in classes that utilize these collections.

For example:

// before
use App\Data\SongData;
use Illuminate\Support\Collection;

class AlbumData extends Data
{    
    /** @var Collection<int, SongData> */
    public Collection $songs;
}
// after
use App\Collection\SongDataCollection;

class AlbumData extends Data
{    
    public SongDataCollection $songs;
}

Copy link
Member

@rubenvanassche rubenvanassche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool PR!

Two remarks about caching + could you also add a more integration like test in DataTest where you create a data object with such a special DataCollection property? Then we know for sure everything is working.

Thanks!

@clementbirkle
Copy link
Contributor Author

@rubenvanassche Thank you for your remarks. I have created an integration test and added some caching.
Please let me know if I need to change anything else.

PS: I noticed a new error with PHPStan, but I don't think it's linked to this PR:

------ ---------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/Support/Validation/RuleDenormalizer.php                                                                                                               
 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------- 
         Ignored error pattern #^Call to method Illuminate\\Support\\Collection<\(int\|string\),string\|null>\:\:isEmpty\(\) will always evaluate to false\.$# in  
         path /Users/clementbirkle/www/tests/laravel-data/src/Support/Validation/RuleDenormalizer.php was not matched in reported errors.                          
 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------- 

@rubenvanassche
Copy link
Member

Thanks!

@rubenvanassche rubenvanassche merged commit 9d7edfa into spatie:main Aug 13, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants