0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelで明確配列タイプをする

Last updated at Posted at 2021-01-05

すみませんが、日本語訳はあとにします。

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?