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

Rubocop name cops #440

Merged
merged 9 commits into from Mar 14, 2020
6 changes: 0 additions & 6 deletions .rubocop.yml
Expand Up @@ -50,12 +50,6 @@ Metrics/MethodLength:
Exclude:
- 'test/**/*.rb'

# Rubocop confuses these as instances of "memoization".
Naming/MemoizedInstanceVariableName:
Exclude:
- 'lib/zip/extra_field/old_unix.rb'
- 'lib/zip/extra_field/unix.rb'

# Set a consistent way of checking types.
Style/ClassCheck:
EnforcedStyle: kind_of?
Expand Down
26 changes: 0 additions & 26 deletions .rubocop_todo.yml
Expand Up @@ -36,32 +36,6 @@ Naming/AccessorMethodName:
- 'lib/zip/filesystem.rb'
- 'lib/zip/input_stream.rb'
- 'lib/zip/streamable_stream.rb'
- 'test/file_permissions_test.rb'

# Offense count: 18
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
Naming/BlockParameterName:
Exclude:
- 'lib/zip/file.rb'
- 'lib/zip/filesystem.rb'
- 'samples/zipfind.rb'
- 'test/central_directory_test.rb'
- 'test/file_extract_directory_test.rb'
- 'test/file_extract_test.rb'
- 'test/output_stream_test.rb'
- 'test/test_helper.rb'

# Offense count: 140
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: io, id, to, by, on, in, at, ip, db, os
Naming/MethodParameterName:
Enabled: false

# Offense count: 721
# Configuration parameters: EnforcedStyle.
# SupportedStyles: snake_case, camelCase
Naming/VariableName:
Enabled: false

# Offense count: 7
# Configuration parameters: EnforcedStyle.
Expand Down
4 changes: 2 additions & 2 deletions lib/zip/central_directory.rb
Expand Up @@ -181,8 +181,8 @@ def buf.read(count)
end

# For iterating over the entries.
def each(&proc)
@entry_set.each(&proc)
def each(&a_proc)
@entry_set.each(&a_proc)
end

# Returns the number of entries in the central directory (and
Expand Down
18 changes: 9 additions & 9 deletions lib/zip/crypto/traditional_encryption.rb
Expand Up @@ -24,8 +24,8 @@ def reset_keys!
end
end

def update_keys(n)
@key0 = ~Zlib.crc32(n, ~@key0)
def update_keys(num)
@key0 = ~Zlib.crc32(num, ~@key0)
@key1 = ((@key1 + (@key0 & 0xff)) * 134_775_813 + 1) & 0xffffffff
@key2 = ~Zlib.crc32((@key1 >> 24).chr, ~@key2)
end
Expand Down Expand Up @@ -63,10 +63,10 @@ def reset!

private

def encode(n)
def encode(num)
t = decrypt_byte
update_keys(n.chr)
t ^ n
update_keys(num.chr)
t ^ num
end
end

Expand All @@ -86,10 +86,10 @@ def reset!(header)

private

def decode(n)
n ^= decrypt_byte
update_keys(n.chr)
n
def decode(num)
num ^= decrypt_byte
update_keys(num.chr)
num
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/zip/dos_time.rb
Expand Up @@ -34,13 +34,13 @@ def self.from_time(time)
local(time.year, time.month, time.day, time.hour, time.min, time.sec)
end

def self.parse_binary_dos_format(binaryDosDate, binaryDosTime)
second = 2 * (0b11111 & binaryDosTime)
minute = (0b11111100000 & binaryDosTime) >> 5
hour = (0b1111100000000000 & binaryDosTime) >> 11
day = (0b11111 & binaryDosDate)
month = (0b111100000 & binaryDosDate) >> 5
year = ((0b1111111000000000 & binaryDosDate) >> 9) + 1980
def self.parse_binary_dos_format(bin_dos_date, bin_dos_time)
second = 2 * (0b11111 & bin_dos_time)
minute = (0b11111100000 & bin_dos_time) >> 5
hour = (0b1111100000000000 & bin_dos_time) >> 11
day = (0b11111 & bin_dos_date)
month = (0b111100000 & bin_dos_date) >> 5
year = ((0b1111111000000000 & bin_dos_date) >> 9) + 1980
begin
local(year, month, day, hour, minute, second)
end
Expand Down
14 changes: 7 additions & 7 deletions lib/zip/extra_field.rb
Expand Up @@ -6,23 +6,23 @@ def initialize(binstr = nil)
merge(binstr) if binstr
end

def extra_field_type_exist(binstr, id, len, i)
def extra_field_type_exist(binstr, id, len, index)
field_name = ID_MAP[id].name
if member?(field_name)
self[field_name].merge(binstr[i, len + 4])
self[field_name].merge(binstr[index, len + 4])
else
field_obj = ID_MAP[id].new(binstr[i, len + 4])
field_obj = ID_MAP[id].new(binstr[index, len + 4])
self[field_name] = field_obj
end
end

def extra_field_type_unknown(binstr, len, i)
def extra_field_type_unknown(binstr, len, index)
create_unknown_item unless self['Unknown']
if !len || len + 4 > binstr[i..-1].bytesize
self['Unknown'] << binstr[i..-1]
if !len || len + 4 > binstr[index..-1].bytesize
self['Unknown'] << binstr[index..-1]
return
end
self['Unknown'] << binstr[i, len + 4]
self['Unknown'] << binstr[index, len + 4]
end

def create_unknown_item
Expand Down
2 changes: 1 addition & 1 deletion lib/zip/extra_field/old_unix.rb
Expand Up @@ -25,7 +25,7 @@ def merge(binstr)
@uid ||= uid
@gid ||= gid
@atime ||= atime
@mtime ||= mtime
@mtime ||= mtime # rubocop:disable Naming/MemoizedInstanceVariableName
end

def ==(other)
Expand Down
2 changes: 1 addition & 1 deletion lib/zip/extra_field/unix.rb
Expand Up @@ -21,7 +21,7 @@ def merge(binstr)

uid, gid = content.unpack('vv')
@uid ||= uid
@gid ||= gid
@gid ||= gid # rubocop:disable Naming/MemoizedInstanceVariableName
end

def ==(other)
Expand Down
64 changes: 32 additions & 32 deletions lib/zip/file.rb
Expand Up @@ -168,9 +168,9 @@ def open_buffer(io, options = {})
# whereas ZipInputStream jumps through the entire archive accessing the
# local entry headers (which contain the same information as the
# central directory).
def foreach(aZipFileName, &block)
::Zip::File.open(aZipFileName) do |zipFile|
zipFile.each(&block)
def foreach(zip_file_name, &block)
::Zip::File.open(zip_file_name) do |zip_file|
zip_file.each(&block)
end
end

Expand Down Expand Up @@ -255,8 +255,8 @@ def split(zip_file_name, segment_size = MAX_SEGMENT_SIZE, delete_zip_file = true
# Returns an input stream to the specified entry. If a block is passed
# the stream object is passed to the block and the stream is automatically
# closed afterwards just as with ruby's builtin File.open method.
def get_input_stream(entry, &aProc)
get_entry(entry).get_input_stream(&aProc)
def get_input_stream(entry, &a_proc)
get_entry(entry).get_input_stream(&a_proc)
end

# Returns an output stream to the specified entry. If entry is not an instance
Expand All @@ -267,7 +267,7 @@ def get_input_stream(entry, &aProc)
def get_output_stream(entry, permission_int = nil, comment = nil,
extra = nil, compressed_size = nil, crc = nil,
compression_method = nil, size = nil, time = nil,
&aProc)
&a_proc)

new_entry =
if entry.kind_of?(Entry)
Expand All @@ -282,7 +282,7 @@ def get_output_stream(entry, permission_int = nil, comment = nil,
new_entry.unix_perms = permission_int
zip_streamable_entry = StreamableStream.new(new_entry)
@entry_set << zip_streamable_entry
zip_streamable_entry.get_output_stream(&aProc)
zip_streamable_entry.get_output_stream(&a_proc)
end

# Returns the name of the zip archive
Expand Down Expand Up @@ -319,19 +319,19 @@ def remove(entry)

# Renames the specified entry.
def rename(entry, new_name, &continue_on_exists_proc)
foundEntry = get_entry(entry)
found_entry = get_entry(entry)
check_entry_exists(new_name, continue_on_exists_proc, 'rename')
@entry_set.delete(foundEntry)
foundEntry.name = new_name
@entry_set << foundEntry
@entry_set.delete(found_entry)
found_entry.name = new_name
@entry_set << found_entry
end

# Replaces the specified entry with the contents of srcPath (from
# Replaces the specified entry with the contents of src_path (from
# the file system).
def replace(entry, srcPath)
check_file(srcPath)
def replace(entry, src_path)
check_file(src_path)
remove(entry)
add(entry, srcPath)
add(entry, src_path)
end

# Extracts entry to file dest_path.
Expand Down Expand Up @@ -409,37 +409,37 @@ def get_entry(entry)
end

# Creates a directory
def mkdir(entryName, permissionInt = 0o755)
raise Errno::EEXIST, "File exists - #{entryName}" if find_entry(entryName)
def mkdir(entry_name, permission = 0o755)
raise Errno::EEXIST, "File exists - #{entry_name}" if find_entry(entry_name)

entryName = entryName.dup.to_s
entryName << '/' unless entryName.end_with?('/')
@entry_set << ::Zip::StreamableDirectory.new(@name, entryName, nil, permissionInt)
entry_name = entry_name.dup.to_s
entry_name << '/' unless entry_name.end_with?('/')
@entry_set << ::Zip::StreamableDirectory.new(@name, entry_name, nil, permission)
end

private

def directory?(newEntry, srcPath)
srcPathIsDirectory = ::File.directory?(srcPath)
if newEntry.directory? && !srcPathIsDirectory
def directory?(new_entry, src_path)
path_is_directory = ::File.directory?(src_path)
if new_entry.directory? && !path_is_directory
raise ArgumentError,
"entry name '#{newEntry}' indicates directory entry, but " \
"'#{srcPath}' is not a directory"
elsif !newEntry.directory? && srcPathIsDirectory
newEntry.name += '/'
"entry name '#{new_entry}' indicates directory entry, but " \
"'#{src_path}' is not a directory"
elsif !new_entry.directory? && path_is_directory
new_entry.name += '/'
end
newEntry.directory? && srcPathIsDirectory
new_entry.directory? && path_is_directory
end

def check_entry_exists(entryName, continue_on_exists_proc, procedureName)
def check_entry_exists(entry_name, continue_on_exists_proc, proc_name)
continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
return unless @entry_set.include?(entryName)
return unless @entry_set.include?(entry_name)

if continue_on_exists_proc.call
remove get_entry(entryName)
remove get_entry(entry_name)
else
raise ::Zip::EntryExistsError,
procedureName + " failed. Entry #{entryName} already exists"
proc_name + " failed. Entry #{entry_name} already exists"
end
end

Expand Down