Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all remaining rubocop violations #241

Merged
merged 6 commits into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions lib/puppet/provider/package/pear.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ def self.pearlist(hash)
pearhash[:provider] = :pear
pearhash
end
else
if pearhash = pearsplit(set, channel) # rubocop:disable Lint/AssignmentInCondition
pearhash[:provider] = :pear
pearhash
end
elsif pearhash = pearsplit(set, channel) # rubocop:disable Lint/AssignmentInCondition
pearhash[:provider] = :pear
pearhash
end
end.reject { |p| p.nil? }
end.reject(&:nil?)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if this could actually be replaced with end.compact, but was too scared!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array#compact should work, the only difference to this manual solution I could find was that compact will only remove actual nil, whereas this will work on classes that define a custom #nil? (I see no reason why that would be something you want though).


rescue Puppet::ExecutionFailure => detail
raise Puppet::Error, format('Could not list pears: %s', detail)
Expand Down Expand Up @@ -87,12 +85,10 @@ def install(useversion = true)

command << if @resource[:source]
@resource[:source]
elsif ([email protected](:ensure).is_a? Symbol) && useversion
"#{@resource[:name]}-#{@resource.should(:ensure)}"
else
if ([email protected](:ensure).is_a? Symbol) && useversion
"#{@resource[:name]}-#{@resource.should(:ensure)}"
else
@resource[:name]
end
@resource[:name]
end

pearcmd(*command)
Expand Down
22 changes: 9 additions & 13 deletions lib/puppet/provider/package/pecl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ def self.pecllist(hash)
begin
list = execute(command).split("\n").map do |set|
if hash[:justme]
if %r{^#{hash[:justme]}$}i.match(set)
if %r{^#{hash[:justme]}$}i =~ set
if peclhash = peclsplit(set) # rubocop:disable Lint/AssignmentInCondition
Copy link

@elomatreb elomatreb Sep 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these conditionals be merged into one if (to avoid nesting)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot. It certainly looks like it, doesn't it? Lets look at the original code before any rubocop and refactoring started... https://github.com/voxpupuli/puppet-php/blob/4.0.0-beta1/lib/puppet/provider/package/pear.rb#L29

So assuming 6bbdc78 was correct and rubocop --auto-correct --only Style/EmptyElse wasn't mistaken, then yes to merging the conditionals?

It's a real shame there are no spec tests covering this code...

Copy link

@elomatreb elomatreb Sep 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two versions should be functionally equivalent, as a non-branching if returns nil in Ruby.

I guess the real question is if this:

if %r{^#{hash[:justme]}$}i =~ set
  if peclhash = peclsplit(set) # rubocop:disable Lint/AssignmentInCondition
    peclhash[:provider] = :peclcmd
    peclhash
  end
end

is more readable than:

if %r{^#{hash[:justme]}$}i =~ set &&
   peclhash = peclsplit(set) # rubocop:disable Lint/AssignmentInCondition
  peclhash[:provider] = :peclcmd
  peclhash
end

btw: Are you sure using a disable comment everywhere were you want to use assignment in a condition is the best way to do it? Rubocop also allows assignment in condition if it is wrapped in parentheses, like so:

if %r{^#{hash[:justme]}$}i =~ set &&
   (peclhash = peclsplit(set))
  peclhash[:provider] = :peclcmd
  peclhash
end

peclhash[:provider] = :peclcmd
peclhash
end
end
else
if peclhash = peclsplit(set) # rubocop:disable Lint/AssignmentInCondition
peclhash[:provider] = :peclcmd
peclhash
end
elsif peclhash = peclsplit(set) # rubocop:disable Lint/AssignmentInCondition
peclhash[:provider] = :peclcmd
peclhash
end
end.reject { |p| p.nil? }
end.reject(&:nil?)
rescue Puppet::ExecutionFailure => detail
raise Puppet::Error, format('Could not list pecls: %s', detail)
end
Expand Down Expand Up @@ -79,13 +77,11 @@ def install(useversion = true)

if @resource[:source]
command << @resource[:source]
elsif ([email protected](:ensure).is_a? Symbol) && useversion
command << '-f'
command << "#{peclname}-#{@resource.should(:ensure)}"
else
if ([email protected](:ensure).is_a? Symbol) && useversion
command << '-f'
command << "#{peclname}-#{@resource.should(:ensure)}"
else
command << peclname
end
command << peclname
end

if pipe == @resource[:pipe]
Expand Down