Conditions | 7 |
Paths | 6 |
Total Lines | 75 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
72 | public function getNavigationPosts(Post $currentPost): array |
||
73 | { |
||
74 | $categoryIds = $currentPost->categories->pluck('id')->toArray(); |
||
75 | $tagIds = $currentPost->tags->pluck('id')->toArray(); |
||
76 | |||
77 | if (empty($categoryIds) && empty($tagIds)) { |
||
78 | return ['previous' => null, 'next' => null]; |
||
79 | } |
||
80 | |||
81 | // Get all published posts except current one |
||
82 | $posts = Post::query() |
||
83 | ->wherePublished() |
||
84 | ->where('id', '!=', $currentPost->id) |
||
85 | ->with(['slugable', 'categories', 'tags', 'author']) |
||
86 | ->get(); |
||
87 | |||
88 | if ($posts->isEmpty()) { |
||
89 | return ['previous' => null, 'next' => null]; |
||
90 | } |
||
91 | |||
92 | // Calculate relevance score for each post |
||
93 | $scoredPosts = $posts->map(function ($post) use ($categoryIds, $tagIds) { |
||
94 | $post->load(['categories', 'tags']); |
||
95 | |||
96 | $postCategoryIds = $post->categories->pluck('id')->toArray(); |
||
97 | $postTagIds = $post->tags->pluck('id')->toArray(); |
||
98 | |||
99 | // Calculate shared categories and tags |
||
100 | $sharedCategories = count(array_intersect($categoryIds, $postCategoryIds)); |
||
101 | $sharedTags = count(array_intersect($tagIds, $postTagIds)); |
||
102 | |||
103 | // Weight categories higher than tags |
||
104 | $relevanceScore = ($sharedCategories * 3) + ($sharedTags * 1); |
||
105 | |||
106 | $post->relevance_score = $relevanceScore; |
||
107 | |||
108 | return $post; |
||
109 | }); |
||
110 | |||
111 | // Filter posts with relevance score > 0 |
||
112 | $relevantPosts = $scoredPosts |
||
113 | ->filter(function ($post) { |
||
114 | return $post->relevance_score > 0; |
||
115 | }) |
||
116 | ->sortByDesc('relevance_score') |
||
117 | ->values(); |
||
118 | |||
119 | if ($relevantPosts->isEmpty()) { |
||
120 | return ['previous' => null, 'next' => null]; |
||
121 | } |
||
122 | |||
123 | // Group posts by relevance score |
||
124 | $groupedByScore = $relevantPosts->groupBy('relevance_score'); |
||
125 | $scores = $groupedByScore->keys()->sortDesc(); |
||
126 | |||
127 | $previous = null; |
||
128 | $next = null; |
||
129 | |||
130 | // Get highest scoring post as previous |
||
131 | $highestScorePosts = $groupedByScore->get($scores->first()); |
||
132 | $previous = $highestScorePosts->first(); |
||
133 | |||
134 | // For next, try to get a different post |
||
135 | if ($scores->count() > 1) { |
||
136 | // If we have multiple score levels, get from second highest |
||
137 | $secondHighestPosts = $groupedByScore->get($scores->get(1)); |
||
138 | $next = $secondHighestPosts->first(); |
||
139 | } else { |
||
140 | // If all posts have same score, get second post if available |
||
141 | $next = $highestScorePosts->count() > 1 ? $highestScorePosts->get(1) : null; |
||
142 | } |
||
143 | |||
144 | return [ |
||
145 | 'previous' => $previous, |
||
146 | 'next' => $next, |
||
147 | ]; |
||
150 |