Code Review: One-Liner with Actions, Map, Splat Operator and DTO

preview_player
Показать описание
Sometimes I notice some code snippets that are totally worth discussing and dissecting piece by piece, cause you may learn a lot. So, let's take a look at Martin's example.

Related links:

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

I love watching these reaction videos and code reviews. Makes people see how other people structure their code, what functions, classes, methods they use and even learning about PHP features that are not so well known? I think people would appreciate more of videos of this kind? Thank you

Flankymanga
Автор

3:19 the "spread operator" is a common thing in javascript, I didn't know it was added to PHP

ricko
Автор

More often I used operator ... in VUE. Thanks to your video. I see it similar in PHP. Now I will probably use it more often on the side of the backend.

misha-pitegorsk
Автор

Thank for another video!

Actually, you example of Spread Operator for this particular case was a bit confusing.
I think you missed pointing out that in this case, the 'tags' key already exists in the array.
It is being updated after some processing, much like using array_merge.
The spread operator is being used to merge a new 'tags' array and hence substitute the original value.
Before merging, the value is processed (from an initial string with comma separated values, into an array of strings).

Before: [ 'id' => 1, 'tags' => 'php, livewire', ]
After: [ 'id' => 1, 'tags' => ['php', 'livewire'] ]

o_lobato
Автор

I really appreciate videos like this, thanks Povilas!

zoba
Автор

Nice, very helpful. It's good there is more speaks about architecture. I want to advise to speak more about "Dependency Injection" is hard theme for understanding, and how to use it in Action class in __construct method, and we can quickly change the dependencies in App Service Provider, for example, "$this->app->bind(Storage::class, S3Storage::class);" Storage it's interface, S3Storage - it's S3 storage class. It's very interesting to understand that, and make code less painful for refactoring.

L-A-O-S
Автор

That looks like functional programming, in Angular is very common to have pipes like that. Awesome!!!!

javieru
Автор

Thank you for your contributions in the community 😊

devfahim
Автор

Honestly - i don't like code like this. There is too much happening and i need to read every line and every character of it and get into details, i only use fluent setter when using query builder, otherwise i try to create methods with easier to understand names and what they do so you don't need to read whole code to see what is happening.

It would be much easier to understand if each of this method calls where hidden behind private method with it's own name, like filterWithoutEmail, mapToXyz and so on.

Jurigag
Автор

I can’t realise why developers don’t care about cpu usage. I suppose that file can be large, so collection can contains to many items. So iterating multiple times through big collection it’s very costly

LEMPZZofficial
Автор

Would try it out now, seems very handy way <3

lifecyclevlog
Автор

In node the ... is called the spread operator and does the same thing. Ive used it plenty in node but actually had no idea it existed in php

Batlas
Автор

That's very nice. It's already built-in other languages, such as java very long time ago.

shadiziedan
Автор

This is basically other languages/frameworks being used in Laravel. The spread operator in javascript is used in functional javascript to create a new data element with no reference to the past object. Same for map, filter, and the callback functions. Much more functional paradigm.

DTOs are something that is very common in Symfony which is essentially another abstraction layer on top of an entity used to process data. Laravel uses models and scopes, which I find to be a much more intuitive approach to data management. My experience with Symfony is that is verbose, inflexible, and abstracted to the point that things become unintuitive.

JC-hppc
Автор

I am not a fan of writing return and then a dozen lines of code logic. Sometimes you want to know what it's the result of previous function to do that you need to move return to the end /or delete it.

rafab
Автор

Really interesting and complex. Thanks

GergelyCsermely
Автор

I think DTOs work really well together with a proper repository pattern when your application may have varying data sources. The methods of the repository interface should return some kind of DTO instead of an array. That way you can ensure that all implementations of that interface returns a consistent data structure that you can use inside your application.

Also you get autocompletion from your IDE when using DTOs which I really like.

Felx
Автор

It’s hard to judge this without knowing the constraints the original author was working under. Most CSV or similar operations I’ve written over the years are working with data sets that can’t be loaded into memory like that. And if they could be loaded, you certainly wouldn’t want to iterate through them 5 times in reading, validating and storing the data. It would take too long and waste more memory. So my immediate impression is to dislike this approach. But if the author knows it’s only being used for small files and that can’t / won’t change, then I guess it’s ok. Seems a bit too “clever” to my old eyes, but then I’ve written similar (single iteration, far more OOP styled) import code for good reasons. So I shouldn’t judge.

JimOHalloran
Автор

I was just trying to make a csv importer and little complex seeder in the morning! It would help, gr8 thanks!

... (spread in js ) used a lot of time in js but in PHP, didn't use it any project yet.

mshiur
Автор

Spread operator is popular in javascript in object json.
Maybe because in old php not support for array associative is the reason that people rarely use it.

bboydarknesz