<commit_before># encoding: utf-8
require 'rails_best_practices/reviews/review'

module RailsBestPractices
  module Reviews
    # Review a controller file to make sure that complex model creation should not exist in controller, should be replaced with factory method.
    #
    # See the best practice details here http://rails-bestpractices.com/posts/6-replace-complex-creation-with-factory-method.
    #
    # Implementation:
    #
    # Review process:
    #   check all method defines in the controller files,
    #   if there are multiple attribute assignments apply to one subject,
    #   and the subject is a local variable or an instance variable,
    #   and after them there is a call node with message :save or :save!,
    #   then these attribute assignments are complex creation, should be replaced with factory method.
    class ReplaceComplexCreationWithFactoryMethodReview < Review
      def url
        "http://rails-bestpractices.com/posts/6-replace-complex-creation-with-factory-method"
      end

      def interesting_nodes
        [:defn]
      end

      def interesting_files
        CONTROLLER_FILES
      end

      def initialize(options = {})
        super()
        @attrasgn_count = options['attribute_assignment_count'] || 2
      end

      # check method define node to see if there are multiple attribute assignments, more than @attrasgn_count, on one local variable or instance variable before save.
      #
      # it wll check every attrasgn nodes in method define node,
      # if there are multiple attrasgn nodes who have the same subject,
      # and the subject is a local variable or an instance variable,
      # and after them, there is a call node with message :save or :save!,
      # then these attribute assignments are complex creation, should be replaced with factory method.
      def start_defn(node)
        node.recursive_children do |child_node|
          case child_node.node_type
          when :attrasgn
            remember_variable_use_count(child_node)
          when :call
            check_variable_save(child_node)
          else
          end
        end
        reset_variable_use_count
      end

      private
        # check the call node to see if it is with message :save or :save!,
        # and the count attribute assignment on the subject of the call node is greater than @attrasgn_count defined,
        # then it is a complex creation, should be replaced with factory method.
        def check_variable_save(node)
          if [:save, :save!].include? node.message
            variable = node.subject
            if variable_use_count[variable] > @attrasgn_count
              add_error "replace complex creation with factory method (#{variable} attribute_assignment_count > #{@attrasgn_count})"
            end
          end
        end
    end
  end
end<commit_msg>avoid calling > on nil object<commit_after># encoding: utf-8
require 'rails_best_practices/reviews/review'

module RailsBestPractices
  module Reviews
    # Review a controller file to make sure that complex model creation should not exist in controller, should be replaced with factory method.
    #
    # See the best practice details here http://rails-bestpractices.com/posts/6-replace-complex-creation-with-factory-method.
    #
    # Implementation:
    #
    # Review process:
    #   check all method defines in the controller files,
    #   if there are multiple attribute assignments apply to one subject,
    #   and the subject is a local variable or an instance variable,
    #   and after them there is a call node with message :save or :save!,
    #   then these attribute assignments are complex creation, should be replaced with factory method.
    class ReplaceComplexCreationWithFactoryMethodReview < Review
      def url
        "http://rails-bestpractices.com/posts/6-replace-complex-creation-with-factory-method"
      end

      def interesting_nodes
        [:defn]
      end

      def interesting_files
        CONTROLLER_FILES
      end

      def initialize(options = {})
        super()
        @attrasgn_count = options['attribute_assignment_count'] || 2
      end

      # check method define node to see if there are multiple attribute assignments, more than @attrasgn_count, on one local variable or instance variable before save.
      #
      # it wll check every attrasgn nodes in method define node,
      # if there are multiple attrasgn nodes who have the same subject,
      # and the subject is a local variable or an instance variable,
      # and after them, there is a call node with message :save or :save!,
      # then these attribute assignments are complex creation, should be replaced with factory method.
      def start_defn(node)
        node.recursive_children do |child_node|
          case child_node.node_type
          when :attrasgn
            remember_variable_use_count(child_node)
          when :call
            check_variable_save(child_node)
          else
          end
        end
        reset_variable_use_count
      end

      private
        # check the call node to see if it is with message :save or :save!,
        # and the count attribute assignment on the subject of the call node is greater than @attrasgn_count defined,
        # then it is a complex creation, should be replaced with factory method.
        def check_variable_save(node)
          if [:save, :save!].include? node.message
            variable = node.subject
            if variable_use_count[variable].to_i > @attrasgn_count
              add_error "replace complex creation with factory method (#{variable} attribute_assignment_count > #{@attrasgn_count})"
            end
          end
        end
    end
  end
end
{'fraction_non_alphanumeric': 0.05498161442829627, 'fraction_numerical': 0.0014008054631413063, 'mean_word_length': 2.6083385975994946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6350311', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1428, 'n_words': 581}