]> git.vpit.fr Git - perl/modules/Perl-Critic-Policy-Dynamic-NoIndirect.git/commitdiff
Fix not reporting multiple violations with the same method on the same line
authorVincent Pit <vince@profvince.com>
Thu, 9 Jul 2009 08:44:04 +0000 (10:44 +0200)
committerVincent Pit <vince@profvince.com>
Thu, 9 Jul 2009 08:44:04 +0000 (10:44 +0200)
lib/Perl/Critic/Policy/Dynamic/NoIndirect.pm
t/10-basic.t

index aa4a03aed94b034333bf88b401e01bf1b2491a1e..5236bda9a6516ef6e048775d6741e1674119c6b8 100644 (file)
@@ -71,28 +71,27 @@ sub violates_dynamic {
   }
  }
 
   }
  }
 
- @errs = sort { $a->[3] <=> $b->[3] } @errs;
-
  my @violations;
 
  if (@errs) {
  my @violations;
 
  if (@errs) {
-  my ($err, $obj, $meth, $line);
+  my %errs_tags;
+  for (@errs) {
+   my ($obj, $meth, $line) = @$_[0, 1, 3];
+   $line -= $offset;
+   my $tag = join "\0", $line, $meth, $obj;
+   push @{$errs_tags{$tag}}, [ $obj, $meth ];
+  }
 
   $doc->find(sub {
 
   $doc->find(sub {
-   unless ($err) {
-    return 1 unless @errs;
-    $err = shift @errs;
-    ($obj, $meth, $line) = @$err[0, 1, 3];
-    $line -= $offset;
-   }
-
    my $elt = $_[1];
    my $pos = $elt->location;
    my $elt = $_[1];
    my $pos = $elt->location;
+   return 0 unless $pos;
 
 
-   if ($pos and $pos->[0] == $line and $elt eq $meth
-                                   and $elt->snext_sibling eq $obj) {
-    push @violations, [ $obj, $meth, $elt ];
-    undef $err;
+   my $tag = join "\0", $pos->[0], $elt, $elt->snext_sibling;
+   if (my $errs = $errs_tags{$tag}) {
+    push @violations, do { my $e = pop @$errs; push @$e, $elt; $e };
+    delete $errs_tags{$tag} unless @$errs;
+    return 1 unless %errs_tags;
    }
 
    return 0;
    }
 
    return 0;
index 29c2f103670bbdb913bdcb2dd3417f9fd88c8055..68715f2d72fa84638ea4ae3386b4cc1d2ed7ecbd 100644 (file)
@@ -5,11 +5,11 @@ use warnings;
 
 my ($tests, $subtests);
 BEGIN {
 
 my ($tests, $subtests);
 BEGIN {
- $tests    = 13;
+ $tests    = 15;
  $subtests = 3;
 }
 
  $subtests = 3;
 }
 
-use Test::More tests => $tests + $subtests * 21;
+use Test::More tests => $tests + $subtests * 25;
 
 use Perl::Critic::TestUtils qw/pcritique_with_violations/;
 
 
 use Perl::Critic::TestUtils qw/pcritique_with_violations/;
 
@@ -72,6 +72,10 @@ my $x = new X    new X;
 ----
 [ 'new', 'X', 1, 9 ], [ 'new', 'X', 1, 18 ]
 ####
 ----
 [ 'new', 'X', 1, 9 ], [ 'new', 'X', 1, 18 ]
 ####
+my $x = new X    new Y;
+----
+[ 'new', 'X', 1, 9 ], [ 'new', 'Y', 1, 18 ]
+####
 my $x = new X;
 my $y = new X;
 ----
 my $x = new X;
 my $y = new X;
 ----
@@ -107,6 +111,11 @@ my $x = new $obj    new $obj;
 ----
 [ 'new', '$obj', 2, 9 ], [ 'new', '$obj', 2, 21 ]
 ####
 ----
 [ 'new', '$obj', 2, 9 ], [ 'new', '$obj', 2, 21 ]
 ####
+our ($o1, $o2);
+my $x = new $o1     new $o2;
+----
+[ 'new', '$o1', 2, 9 ], [ 'new', '$o2', 2, 21 ]
+####
 our $obj;
 my $x = new $obj;
 my $y = new $obj;
 our $obj;
 my $x = new $obj;
 my $y = new $obj;