| 1 | package Perl::Metrics2::Plugin; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Perl::Metrics2::Plugin - Base class for Perl::Metrics Plugins | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Implement a simple metrics package which counts up the | ||
| 12 | # use of each type of magic variable. | ||
| 13 | package Perl::Metrics2::Plugin::Magic; | ||
| 14 | |||
| 15 | use base 'Perl::Metrics2::Plugin'; | ||
| 16 | |||
| 17 | # Creates the metric 'all_magic'. | ||
| 18 | # The total number of magic variables. | ||
| 19 | sub metric_all_magic { | ||
| 20 | my ($self, $document) = @_; | ||
| 21 | return scalar grep { $_->isa('PPI::Token::Magic') } | ||
| 22 | $document->tokens; | ||
| 23 | } | ||
| 24 | |||
| 25 | # The number of $_ "scalar_it" magic vars | ||
| 26 | sub metric_scalar_it { | ||
| 27 | my ($self, $document) = @_; | ||
| 28 | return scalar grep { $_->content eq '$_' } | ||
| 29 | grep { $_->isa('PPI::Token::Magic') } | ||
| 30 | $document->tokens; | ||
| 31 | } | ||
| 32 | |||
| 33 | # ... and so on, and so forth. | ||
| 34 | |||
| 35 | 1; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | The L<Perl::Metrics> system does not in and of itself generate any actual | ||
| 40 | metrics data, it merely acts as a processing and storage engine. | ||
| 41 | |||
| 42 | The generation of the actual metrics data is done via metrics packages, | ||
| 43 | which as implemented as C<Perl::Metrics2::Plugin> sub-classes. | ||
| 44 | |||
| 45 | =head2 Implementing Your Own Metrics Package | ||
| 46 | |||
| 47 | Implementing a metrics package is pretty easy. | ||
| 48 | |||
| 49 | First, create a Perl::Metrics2::Plugin::Something package, inheriting | ||
| 50 | from C<Perl::Metrics2::Plugin>. | ||
| 51 | |||
| 52 | The create a subroutine for each metric, named metric_$name. | ||
| 53 | |||
| 54 | For each subroutine, you will be passed the plugin object itself, and the | ||
| 55 | L<PPI::Document> object to generate the metric for. | ||
| 56 | |||
| 57 | Return the metric value from the subroutine. And add as many metric_ | ||
| 58 | methods as you wish. Methods not matching the pattern /^metric_(.+)$/ | ||
| 59 | will be ignored, and you may use them for whatever support methods you | ||
| 60 | wish. | ||
| 61 | |||
| 62 | =head1 METHODS | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | use strict; | ||
| 67 | use Carp (); | ||
| 68 | use Class::Inspector (); | ||
| 69 | use Params::Util qw{ _IDENTIFIER _INSTANCE }; | ||
| 70 | use Perl::Metrics2 (); | ||
| 71 | |||
| 72 | use vars qw{$VERSION}; | ||
| 73 | BEGIN { | ||
| 74 | $VERSION = '0.06'; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | ##################################################################### | ||
| 82 | # Constructor | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =head2 new | ||
| 87 | |||
| 88 | The C<new> constructor is quite trivial at this point, and is provided | ||
| 89 | merely as a convenience. You don't really need to think about this. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub new { | ||
| 94 | my $class = ref $_[0] ? ref shift : shift; | ||
| 95 | my $self = bless { }, $class; | ||
| 96 | return $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | =pod | ||
| 100 | |||
| 101 | =head2 class | ||
| 102 | |||
| 103 | A convenience method to get the class for the plugin object, | ||
| 104 | to avoid having to use ref directly (and making the intent of | ||
| 105 | any code a little clearer). | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub class { ref $_[0] || $_[0] } | ||
| 110 | |||
| 111 | =pod | ||
| 112 | |||
| 113 | =head2 destructive | ||
| 114 | |||
| 115 | The destructive method is used by the plugin to indicate that the PPI | ||
| 116 | document passed in will be altered during the metric generation. | ||
| 117 | |||
| 118 | The value is used by the metrics engine to optimise document cloning and | ||
| 119 | reduce the number of expensive cloning to a minimum. | ||
| 120 | |||
| 121 | This value defaults to true for safety reasons, and should be overridden | ||
| 122 | in your subclass if your metrics are not destructive. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub destructive { 1 } | ||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | ##################################################################### | ||
| 133 | # Perl::Metrics2::Plugin API | ||
| 134 | |||
| 135 | # Flush out old records | ||
| 136 | sub flush { | ||
| 137 | my $self = shift; | ||
| 138 | my $class = $self->class; | ||
| 139 | my $version = $class->VERSION; | ||
| 140 | Perl::Metrics2->do( | ||
| 141 | 'delete from file_metric where package = ? and version = ?', | ||
| 142 | {}, $class, $version, | ||
| 143 | ); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub process_document { | ||
| 147 | my $self = shift; | ||
| 148 | my $class = ref $self; | ||
| 149 | my %params = @_; | ||
| 150 | my $document = $params{document}; | ||
| 151 | my $md5 = $params{md5}; | ||
| 152 | my $hintsafe = $params{hintsafe}; | ||
| 153 | unless ( _INSTANCE($document, 'PPI::Document') ) { | ||
| 154 | Carp::croak("Did not provide a PPI::Document object"); | ||
| 155 | } | ||
| 156 | |||
| 157 | # Generate the new metrics values | ||
| 158 | my %metric = $self->process_metrics($document); | ||
| 159 | |||
| 160 | # Flush out the old records and write the new metrics | ||
| 161 | unless ( $hintsafe ) { | ||
| 162 | # This can be an expensive call. | ||
| 163 | # The hintsafe optional param lets the parent | ||
| 164 | # indicate that this check is not required. | ||
| 165 | Perl::Metrics2::FileMetric->delete( | ||
| 166 | 'where md5 = ? and package = ?', | ||
| 167 | $md5, $class, | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | |||
| 171 | # Temporary accelerate version | ||
| 172 | SCOPE: { | ||
| 173 | my $sth = Perl::Metrics2->dbh->prepare( | ||
| 174 | 'INSERT INTO file_metric ( md5, package, version, name, value ) VALUES ( ?, ?, ?, ?, ? )' | ||
| 175 | ); | ||
| 176 | foreach my $name ( sort keys %metric ) { | ||
| 177 | $sth->execute( $md5, $class, $class->VERSION, $name, $metric{$name} ); | ||
| 178 | } | ||
| 179 | $sth->finish; | ||
| 180 | } | ||
| 181 | |||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub process_metrics { | ||
| 186 | my $class = ref($_[0]) || $_[0]; | ||
| 187 | die "Plugin $class does not implement process_metrics"; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | =pod | ||
| 193 | |||
| 194 | =head1 SUPPORT | ||
| 195 | |||
| 196 | Bugs should be reported via the CPAN bug tracker at | ||
| 197 | |||
| 198 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl-Metrics2> | ||
| 199 | |||
| 200 | For other issues, contact the author. | ||
| 201 | |||
| 202 | =head1 AUTHOR | ||
| 203 | |||
| 204 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 205 | |||
| 206 | =head1 SEE ALSO | ||
| 207 | |||
| 208 | L<Perl::Metrics>, L<PPI> | ||
| 209 | |||
| 210 | =head1 COPYRIGHT | ||
| 211 | |||
| 212 | Copyright 2009 Adam Kennedy. | ||
| 213 | |||
| 214 | This program is free software; you can redistribute | ||
| 215 | it and/or modify it under the same terms as Perl itself. | ||
| 216 | |||
| 217 | The full text of the license can be found in the | ||
| 218 | LICENSE file included with this module. | ||
| 219 | |||
| 220 | =cut |