_plugins/related_posts.rb
# frozen_string_literal: true require 'parallel' module Jekyll class RelatedPostsGenerator < Generator safe true priority :lower # Calculate related posts. # Returns [<Post>]Assignment Branch Condition size for related_posts is too high. [43.91/15]
Method has too many lines. [29/10]
Method `related_posts` has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring.
Cyclomatic complexity for related_posts is too high. [11/6]
Perceived complexity for related_posts is too high. [11/7]
Method `related_posts` has 29 lines of code (exceeds 25 allowed). Consider refactoring.
Method parameter must be at least 3 characters long. def related_posts(me, posts) return [] unless posts.docs.size > 1 highest_freq = @tag_freq.values.max related_scores = Hash.new(0) posts.docs.each do |post|Similar blocks of code found in 2 locations. Consider refactoring. if @use_categories post.data['categories'].each do |category| if me.data['categories'].include?(category) && post != me cat_freq = @tag_freq[category] related_scores[post] += (1 + highest_freq - cat_freq) end end endSimilar blocks of code found in 2 locations. Consider refactoring. if @use_tags post.data['tags'].each do |tag| if me.data['tags'].include?(tag) && post != me cat_freq = @tag_freq[tag] related_scores[post] += (1 + highest_freq - cat_freq) end end end next unless @use_authors post.data['authors'].each do |author| if me.data['authors'].include?(author) && post != me cat_freq = @tag_freq[author] related_scores[post] += (1 + highest_freq - cat_freq) end end end sort_related_posts(related_scores) end # Calculate the frequency of each tag. # Returns {tag => freq, tag => freq, ...}Assignment Branch Condition size for tag_freq is too high. [15.81/15]
Method `tag_freq` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. def tag_freq(posts) @tag_freq = Hash.new(0) posts.docs.each do |post| post.data['categories'].each { |category| @tag_freq[category] += 1 } if @use_categories post.data['tags'].each { |tag| @tag_freq[tag] += 1 } if @use_tags post.data['authors'].each { |author| @tag_freq[author] += 1 } if @use_authors end end # Sort the related posts in order of their score and date # and return just the posts def sort_related_posts(related_scores) related_scores.sort do |a, b| if a[1] < b[1] 1 elsif a[1] > b[1] -1 else b[0].date <=> a[0].date endAvoid multi-line chains of blocks. end.collect { |post, _freq| post } end Assignment Branch Condition size for create_presets is too high. [17.8/15] def create_presets(site) @use_tags = true @use_authors = true @use_categories = false @use_categories = true if site.config['related_categories'] @use_tags = false if !site.config['related_tags'].nil? && site.config['related_tags'] != trueLine is too long. [108/100] @use_authors = false if !site.config['related_authors'].nil? && site.config['related_authors'] != true end def in_threads(site)Use double pipes `||` instead. site.config['n_cores'] ? site.config['n_cores'] : 1 end Assignment Branch Condition size for generate is too high. [19.21/15] def generate(site) return unless site.config['related_posts'] n_posts = site.config['related_posts'] create_presets(site) tag_freq(site.posts) Parallel.map(site.posts.docs.flatten, in_threads: in_threads(site)) do |post| rp = related_posts(post, site.posts)[0, n_posts] post.data.merge!('related_posts' => rp) if rp.size.positive? end end endend