Class: PmdTester::PmdViolation

Inherits:
Object
  • Object
show all
Defined in:
lib/pmdtester/pmd_violation.rb

Overview

This class represents a ‘violation’ element of Pmd xml report and which pmd branch the ‘violation’ is from

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branch:, fname:, info_url:, bline:, rule_name:, ruleset_name:) ⇒ PmdViolation

Returns a new instance of PmdViolation.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pmdtester/pmd_violation.rb', line 33

def initialize(branch:, fname:, info_url:, bline:, rule_name:, ruleset_name:)
  @branch = branch
  @fname = fname
  @message = ''

  @info_url = info_url
  @line = bline
  @rule_name = rule_name

  @ruleset_name = ruleset_name

  @language = determine_language_from_info_url

  @changed = false
  @old_message = nil
  @old_line = nil
end

Instance Attribute Details

#branchObject (readonly)

The pmd branch type, ‘base’ or ‘patch’



8
9
10
# File 'lib/pmdtester/pmd_violation.rb', line 8

def branch
  @branch
end

#fnameObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def fname
  @fname
end

#info_urlObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def info_url
  @info_url
end

#languageObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def language
  @language
end

#lineObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def line
  @line
end

#messageObject

Returns the value of attribute message.



31
32
33
# File 'lib/pmdtester/pmd_violation.rb', line 31

def message
  @message
end

#old_lineObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def old_line
  @old_line
end

#old_messageObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def old_message
  @old_message
end

#rule_nameObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def rule_name
  @rule_name
end

#ruleset_nameObject (readonly)

The schema of ‘violation’ element: <xs:complexType name=“violation”>

<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute name="beginline" type="xs:integer" use="required" />
    <xs:attribute name="endline" type="xs:integer" use="required" />
    <xs:attribute name="begincolumn" type="xs:integer" use="required" />
    <xs:attribute name="endcolumn" type="xs:integer" use="required" />
    <xs:attribute name="rule" type="xs:string" use="required" />
    <xs:attribute name="ruleset" type="xs:string" use="required" />
    <xs:attribute name="package" type="xs:string" use="optional" />
    <xs:attribute name="class" type="xs:string" use="optional" />
    <xs:attribute name="method" type="xs:string" use="optional" />
    <xs:attribute name="variable" type="xs:string" use="optional" />
    <xs:attribute name="externalInfoUrl" type="xs:string" use="optional" />
    <xs:attribute name="priority" type="xs:string" use="required" />
  </xs:extension>
</xs:simpleContent>

</xs:complexType>



30
31
32
# File 'lib/pmdtester/pmd_violation.rb', line 30

def ruleset_name
  @ruleset_name
end

Instance Method Details

#added?Boolean

only makes sense if this is a diff

Returns:

  • (Boolean)


69
70
71
# File 'lib/pmdtester/pmd_violation.rb', line 69

def added?
  branch != BASE && !changed?
end

#changed?Boolean

only makes sense if this is a diff

Returns:

  • (Boolean)


74
75
76
# File 'lib/pmdtester/pmd_violation.rb', line 74

def changed?
  @changed
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/pmdtester/pmd_violation.rb', line 87

def eql?(other)
  rule_name.eql?(other.rule_name) &&
    line.eql?(other.line) &&
    fname.eql?(other.fname) &&
    message.eql?(other.message)
end

#hashObject



94
95
96
# File 'lib/pmdtester/pmd_violation.rb', line 94

def hash
  [line, rule_name, message].hash
end

#line_move?(other) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/pmdtester/pmd_violation.rb', line 51

def line_move?(other)
  message.eql?(other.message) && (line - other.line).abs <= 5
end

#removed?Boolean

only makes sense if this is a diff

Returns:

  • (Boolean)


79
80
81
# File 'lib/pmdtester/pmd_violation.rb', line 79

def removed?
  branch == BASE
end

#sort_keyObject



83
84
85
# File 'lib/pmdtester/pmd_violation.rb', line 83

def sort_key
  line
end

#to_liquidObject



98
99
100
101
102
103
# File 'lib/pmdtester/pmd_violation.rb', line 98

def to_liquid
  {
    'branch' => branch,
    'changed' => changed?
  }
end

#try_merge?(other) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pmdtester/pmd_violation.rb', line 55

def try_merge?(other)
  if branch != BASE && branch != other.branch && rule_name == other.rule_name &&
     !changed? && # not already changed
     (line == other.line || line_move?(other))
    @changed = true
    @old_message = other.message
    @old_line = other.line
    true
  else
    false
  end
end