Improve collection!

Posted 2 years ago by luddinus


How can I refactor this code? I want to iterate some fields, check if a method exists and then call it, here is the code:

$someCollection->filter(function ($value, $attribute) {
   // Imagine "getClassName" is very "expensive" method
   return class_exists($this->getClassName($attribute));
})->map(function ($value, $attribute) {
   // getClassName method again...
   return app($this->getClassName($attribute));

Any ideas?


Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.