Task/ClassSniff.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 159 CC 10 SCAR 23.7
CC
10
Cov
0%
print_report
30.0
process_ppi
12.0
run_sniff
12.0
CRAP 10.7
1 package Padre::Task::ClassSniff;
2 BEGIN {
3 $Padre::Task::ClassSniff::VERSION = '0.30';
4 }
5  
6 # ABSTRACT: Running class sniff in the background
7  
8 use strict;
9 use warnings;
10  
11 use Padre::Task::PPI ();
12 use Padre::Wx ();
13 use Scalar::Util qw(blessed);
14 use IPC::Cmd ();
15  
16 use base 'Padre::Task::PPI';
17  
18 sub process_ppi {
19 my $self = shift;
20 my $ppi = shift or return ();
21 my $mode = $self->{mode} || 'print_report';
22  
23  
24 my $sniff_config = $self->{sniff_config} ||= {};
25  
26 if ( not defined $sniff_config->{class} ) {
27 $sniff_config->{class} = $self->find_document_namespace($ppi);
28 }
29  
30 if ( $mode eq 'print_report' ) {
31 $self->print_report($ppi);
32 }
33  
34 return ();
35 }
36  
37 sub find_document_namespace {
38 my $self = shift;
39 my $ppi = shift;
40 my $ns = $ppi->find_first('PPI::Statement::Package');
41 return ()
42 if not defined $ns
43 or !blessed($ns)
44 or !$ns->isa('PPI::Statement::Package');
45 return $ns->namespace;
46 }
47  
48 sub print_report {
49 my $self = shift;
50 my $ppi = shift;
51 my $sniff_config = $self->{sniff_config};
52  
53 if ( not defined $sniff_config->{class} ) {
54 $self->task_warn( Wx::gettext("Could not determine class to run Sniff on.\n") );
55 return ();
56 }
57  
58 my ( $ok, $stdout, $stderr ) = $self->run_sniff( $sniff_config, $self->{text} || $ppi->serialize() );
59 if ( !$ok or not defined $stdout ) {
60 $self->task_warn( "Error running Class::Sniff on class '" . $sniff_config->{class} . "': " . $stderr . "\n" );
61 return ();
62 }
63 if ( defined $stderr and $stderr =~ /\S/ ) {
64 $self->task_warn(
65 "Warning from running Class::Sniff on class '" . $sniff_config->{class} . "': " . $stderr . "\n" );
66 }
67  
68 if ( defined $stdout and $stdout =~ /\S/ ) {
69 $self->task_print( $stdout . "\n" );
70 } else {
71 $self->task_print( "No bad smell from class '" . $sniff_config->{class} . "'\n" );
72 }
73 return ();
74  
75 }
76  
77 sub run_sniff {
78 my $self = shift;
79 my $cfg = shift;
80 my $code = shift;
81  
82 require YAML::Tiny;
83 require IPC::Cmd;
84 require IPC::Open3;
85  
86 my $yaml = YAML::Tiny::Dump($cfg);
87 my @cmd = (
88 Padre->perl_interpreter(),
89 '-Mstrict',
90 '-Mwarnings',
91 '-mYAML::Tiny',
92 '-mClass::Sniff',
93 '-e',
94 );
95 push @cmd, <<'HERE';
96 my $yaml = YAML::Tiny::Load(shift(@ARGV)) or die "Bad config";
97 $yaml = $yaml->[0] if ref($yaml) eq 'ARRAY';
98 my $code = shift(@ARGV);
99 eval $code;
100 die "Could not compile class: $@" if $@;
101 my $sniff = Class::Sniff->new($yaml);
102 die "Could not instantiate Class::Sniff" if not $sniff;
103 print $sniff->report();
104 HERE
105 push @cmd, '--', $yaml, $code;
106  
107 my ( $ok, $errno, undef, $stdout, $stderr ) = IPC::Cmd::run( command => \@cmd, verbose => 0 );
108 $stdout = join "", @$stdout
109 if defined $stdout and ref($stdout) eq 'ARRAY';
110 $stderr = join "", @$stderr
111 if defined $stderr and ref($stderr) eq 'ARRAY';
112 return ( $ok, $stdout, $stderr );
113 }
114  
115 1;
116  
117  
118  
119 =pod
120  
121 =head1 NAME
122  
123 Padre::Task::ClassSniff - Running class sniff in the background
124  
125 =head1 VERSION
126  
127 version 0.30
128  
129 =head1 SYNOPSIS
130  
131 my $task = Padre::Task::ClassSniff->new(
132 mode => 'print_report',
133 sniff_config => { ... },
134 );
135 $task->schedule;
136  
137 =head1 DESCRIPTION
138  
139 Runs Class::Sniff on the first namespace of the current document
140 and prints the results to the Padre output window.
141  
142 =head1 SEE ALSO
143  
144 This class inherits from C<Padre::Task::PPI> and its instances can be scheduled
145 using C<Padre::TaskManager>.
146  
147 The transfer of the objects to and from the worker threads is implemented
148 with L<Storable>.
149  
150 =head1 AUTHORS
151  
152 =over 4
153  
154 =item *
155  
156 Steffen Mueller <smueller@cpan.org>
157  
158 =item *
159  
160 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
161  
162 =back
163  
164 =head1 COPYRIGHT AND LICENSE
165  
166 This software is copyright (c) 2010 by Steffen Mueller.
167  
168 This is free software; you can redistribute it and/or modify it under
169 the same terms as the Perl 5 programming language system itself.
170  
171 =cut
172  
173  
174 __END__