すみませんが、日本語訳はあとにします。
The origin code
public function get_user_badge(array $user_ids, array $course_ids, Carbon $from, Carbon $end, $direction = 'desc'){}
Refactoring 1: clarify array type
Util Classes
use Illuminate\Support\Collection;
class IntegerCollection extends Collection
{
public function __construct(int ...$values)
{
parent::__construct($values);
}
}
class UserIds extends IntegerCollection {}
class CourseIds extends IntegerCollection {}
The code will rewrite as bellow:
public function get_user_badge(UserIds $userIds, CourseIds $courseIds, Carbon $from, Carbon $end, $direction = 'desc'){}
// Usage
$userIds = new UserIds($user_ids);
$courseIds = new CourseIds($course_ids);
get_user_badge($userIds, $courseIds, ...)
Refactoring 2: Combine $from/$end to InclusiveRange class
Util Classes
abstract class BaseRange
{
public $begin;
public $end;
public function __construct(Carbon $begin, Carbon $end)
{
$this->begin = $begin;
$this->end = $end;
}
//...some ultil functions in here
}
class InclusiveRange extends BaseRange{}
The code will rewrite as bellow:
public function get_user_badge(UserIds $userIds, CourseIds $courseIds, InclusiveRange $range, $direction = 'desc'){}
// Usage
$inclusiveRange = new InclusiveRange($from, $end)
get_user_badge($userIds, $courseIds, $inclusiveRange...)
Original post is: https://khoinv.com/post/639390454460432384/clarify-type-in-laravel-code