| Conditions | 9 |
| Paths | 8 |
| Total Lines | 81 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 66 | protected function getRelatedPostByRelevance(int|string $postId, string $direction = 'previous'): ?object |
||
| 67 | { |
||
| 68 | $currentPost = $this->postRepository->findById($postId); |
||
| 69 | |||
| 70 | if (!$currentPost) { |
||
| 71 | return null; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Load current post's categories and tags |
||
| 75 | $currentPost->load(['categories', 'tags']); |
||
| 76 | $categoryIds = $currentPost->categories->pluck('id')->toArray(); |
||
| 77 | $tagIds = $currentPost->tags->pluck('id')->toArray(); |
||
| 78 | |||
| 79 | if (empty($categoryIds) && empty($tagIds)) { |
||
| 80 | // No categories or tags, return null (no navigation) |
||
| 81 | return null; |
||
| 82 | } |
||
| 83 | |||
| 84 | // Get all published posts except current one |
||
| 85 | $posts = $this->postRepository->getModel() |
||
| 86 | ->wherePublished() |
||
| 87 | ->where('id', '!=', $postId) |
||
| 88 | ->with(['slugable', 'categories', 'tags', 'author']) |
||
| 89 | ->get(); |
||
| 90 | |||
| 91 | if ($posts->isEmpty()) { |
||
| 92 | return null; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Calculate relevance score for each post |
||
| 96 | $scoredPosts = $posts->map(function ($post) use ($categoryIds, $tagIds) { |
||
| 97 | $post->load(['categories', 'tags']); |
||
| 98 | |||
| 99 | $postCategoryIds = $post->categories->pluck('id')->toArray(); |
||
| 100 | $postTagIds = $post->tags->pluck('id')->toArray(); |
||
| 101 | |||
| 102 | // Calculate shared categories and tags |
||
| 103 | $sharedCategories = count(array_intersect($categoryIds, $postCategoryIds)); |
||
| 104 | $sharedTags = count(array_intersect($tagIds, $postTagIds)); |
||
| 105 | |||
| 106 | // Weight categories higher than tags |
||
| 107 | $relevanceScore = ($sharedCategories * 3) + ($sharedTags * 1); |
||
| 108 | |||
| 109 | $post->relevance_score = $relevanceScore; |
||
| 110 | |||
| 111 | return $post; |
||
| 112 | }); |
||
| 113 | |||
| 114 | // Filter posts with relevance score > 0 |
||
| 115 | $relevantPosts = $scoredPosts |
||
| 116 | ->filter(function ($post) { |
||
| 117 | return $post->relevance_score > 0; |
||
| 118 | }) |
||
| 119 | ->sortByDesc('relevance_score') |
||
| 120 | ->values(); // Reset array keys |
||
| 121 | |||
| 122 | if ($relevantPosts->isEmpty()) { |
||
| 123 | return null; |
||
| 124 | } |
||
| 125 | |||
| 126 | // Group posts by relevance score |
||
| 127 | $groupedByScore = $relevantPosts->groupBy('relevance_score'); |
||
| 128 | $scores = $groupedByScore->keys()->sortDesc(); |
||
| 129 | |||
| 130 | if ($direction === 'previous') { |
||
| 131 | // Get highest scoring post(s), pick first one |
||
| 132 | $highestScorePosts = $groupedByScore->get($scores->first()); |
||
| 133 | |||
| 134 | return $highestScorePosts->first(); |
||
| 135 | } else { |
||
| 136 | // For 'next', try to get a different post |
||
| 137 | if ($scores->count() > 1) { |
||
| 138 | // If we have multiple score levels, get from second highest |
||
| 139 | $secondHighestPosts = $groupedByScore->get($scores->get(1)); |
||
| 140 | |||
| 141 | return $secondHighestPosts->first(); |
||
| 142 | } else { |
||
| 143 | // If all posts have same score, get second post if available |
||
| 144 | $highestScorePosts = $groupedByScore->get($scores->first()); |
||
| 145 | |||
| 146 | return $highestScorePosts->count() > 1 ? $highestScorePosts->get(1) : $highestScorePosts->first(); |
||
| 147 | } |
||
| 151 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths