Codor icon indicating copy to clipboard operation
Codor copied to clipboard

Limit number of lines within an if or for(each) loop?

Open bmitch opened this issue 8 years ago • 1 comments

bmitch avatar Aug 04 '17 03:08 bmitch

Also While loops should be considered here.

Any logic inside this loops structures should be abstracted to their own methods or classes and a meaningful name must be chosen to explicitly represent what is being abstracted.

<?php

class UserHandler
{
    private $userRepo;

    public function __construct(UserRepositoryContract $userRepo)
    {
        $this->userRepo = $userRepo;
    }

    public function getUsersFeedbackRate()
    {
        foreach ($this->userRepo->all() as $user) {
            $this->calculateUserFeedbackRate($user);
        }
    }

    private function calculateUserFeedbackRate(UserValues $user)
    {
        // Use the User Values immutable object to get the data about the user and perform your logic
    }
}

Exadra37 avatar Aug 05 '17 12:08 Exadra37