Class: RuboCop::Cop::OpenProject::UseServiceResultFactoryMethods

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/open_project/use_service_result_factory_methods.rb

Overview

Favor usage of ServiceResult factory methods instead of using ‘success: true/false` in constructor.

Examples:

# bad
ServiceResult.new(success: true, result: 'result')

# bad
ServiceResult.new(success: false, errors: ['error'])

# good
ServiceResult.success(result: 'result')

# good
ServiceResult.failure(errors: ['error'])

# good
ServiceResult.new(success: some_value)

# good
ServiceResult.new(**kwargs)

Constant Summary collapse

MSG =
"Use ServiceResult.%<factory_method>s(...) instead of ServiceResult.new(success: %<success_value>s, ...)."
MSG_IMPLICIT_FAILURE =
"Use ServiceResult.failure instead of ServiceResult.new."
RESTRICT_ON_SEND =
%i[new].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/open_project/use_service_result_factory_methods.rb', line 54

def on_send(node)
  return unless service_result_constructor?(node)

  if success_argument_present?(node)
    add_offense_if_explicit_success_argument(node)
  elsif success_argument_possibly_present?(node)
    return # rubocop:disable Style/RedundantReturn
  else
    add_offense_for_implicit_failure(node)
  end
end