절대 최종이라고 말하지 마세요.. 하.. 마지막.. 혹은...
발행: (2026년 4월 23일 PM 11:21 GMT+9)
1 분 소요
원문: Dev.to
Source: Dev.to
원본 코드
$class_names = wrap($data)
->tryProp('page_config')
->bind(function (Lst $x) {
return $x->filter(function ($y) {
return $y['_type'] === 'header';
})
->tryHead();
})
->bind(function ($x) {
return $x->tryProp('header_attrs');
})
->bind(function ($x) {
return $x->filter(function ($y) {
return $y['_type'] === 'class';
})
->tryHead();
})
->bind(function ($x) {
return $x->tryProp('class');
})
->getOrElse('');
반복되는 패턴
->tryProp('page_config')
->bind(function (Lst $x) {
return $x->filter(function ($y) {
return $y['_type'] === 'header';
})
->tryHead();
})
propEquals를 사용한 리팩터링
->bind(fn (Lst $x)
=> $x->filter(propEquals('_type', 'header'))
->tryHead())
최종 리팩터링 버전
$class_names = wrap($data)
->tryProp('page_config')
->bind(fn (Lst $x) => $x->filter(propEquals('_type', 'header'))->tryHead())
->bind(fn (Kvm $x) => $x->tryProp('header_attrs'))
->bind(fn (Lst $x) => $x->filter(propEquals('_type', 'class'))->tryHead())
->bind(fn (Kvm $x) => $x->tryProp('class'))
->getOrElse('');