| 1 | package B::Lint::StrictOO; | ||
| 2 | BEGIN { | ||
| 3 | $B::Lint::StrictOO::VERSION = '0.04'; | ||
| 4 | } | ||
| 5 | # ABSTRACT: Apply strict to classes and methods | ||
| 6 | |||
| 7 | |||
| 8 | use 5.006; | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use B::Lint 1.09 (); | ||
| 13 | B::Lint->register_plugin( __PACKAGE__, [ 'oo' ] ); | ||
| 14 | |||
| 15 | use B::Utils 0.10 (); | ||
| 16 | |||
| 17 | use constant _invocant_is_lexical_object => 1; | ||
| 18 | use constant _invocant_is_global_object => 2; | ||
| 19 | use constant _invocant_is_literal_class => 3; | ||
| 20 | use constant _invocant_is_unknown => 4; | ||
| 21 | |||
| 22 | |||
| 23 | sub match { | ||
| 24 | # Arguments: | ||
| 25 | # | ||
| 26 | # 0: the opcode to check. It will always be some subclass of | ||
| 27 | # B::OP but the only ones I'm interested in are B::LISTOP. | ||
| 28 | # | ||
| 29 | # 1: a hash of currently enabled checks. This check is looking | ||
| 30 | # for the 'oo' check. | ||
| 31 | my B::OP $op_entersub = $_[0]; | ||
| 32 | my $check = $_[1]; | ||
| 33 | |||
| 34 | return if ! $check->{oo}; | ||
| 35 | |||
| 36 | # We're looking for method invocations. Method calls are just a | ||
| 37 | # special case of invoking a function so I'm looking for entersub. | ||
| 38 | # | ||
| 39 | # perl -MO=Terse -e 'XXX->xxx'; | ||
| 40 | # | ||
| 41 | # UNOP entersub | ||
| 42 | # OP pushmark | ||
| 43 | # SVOP const PV "XXX" <<<< invocant | ||
| 44 | # ... <<<< more arguments | ||
| 45 | # SVOP method_named PV "xxx" <<<< method name | ||
| 46 | # | ||
| 47 | return if $op_entersub->name ne 'entersub'; | ||
| 48 | |||
| 49 | # Fetch the ops for the invocant and method name | ||
| 50 | my @children = $op_entersub->first; | ||
| 51 | push @children, $children[0]->siblings; | ||
| 52 | |||
| 53 | # Skip past the leading pushmark | ||
| 54 | for ( ; | ||
| 55 | @children && $children[0]->oldname eq 'pushmark'; | ||
| 56 | shift @children ) { | ||
| 57 | } | ||
| 58 | |||
| 59 | # Remove null children | ||
| 60 | for ( ; | ||
| 61 | @children && ! ${$children[-1]}; | ||
| 62 | pop @children ) { | ||
| 63 | } | ||
| 64 | |||
| 65 | my B::SVOP $invocant_op = $children[0]; | ||
| 66 | my B::SVOP $method_op = $children[-1]; | ||
| 67 | |||
| 68 | # Not a method call at all! | ||
| 69 | return if $invocant_op == $method_op; | ||
| 70 | |||
| 71 | my $category = guess_invocant_category( $invocant_op ); | ||
| 72 | |||
| 73 | if ( _invocant_is_literal_class() == $category ) { | ||
| 74 | lint_class_method_call( | ||
| 75 | $invocant_op, | ||
| 76 | $method_op | ||
| 77 | ); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | # TODO: | ||
| 82 | # if ( _invocant_is_lexical_object() == $category | ||
| 83 | # || _invocant_is_global_object() == $category | ||
| 84 | # || _invocant_is_unknown() == $category | ||
| 85 | # ) { | ||
| 86 | # lint_object_method_call( | ||
| 87 | # $invocant_op, | ||
| 88 | # $method_op | ||
| 89 | # ); | ||
| 90 | # return; | ||
| 91 | # } | ||
| 92 | |||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | #sub B::OP::siblings { | ||
| 98 | # my @siblings = $_[0]; | ||
| 99 | # | ||
| 100 | # my $sibling; | ||
| 101 | # while ( $siblings[-1]->can('sibling') ) { | ||
| 102 | # push @siblings, $siblings[-1]->sibling; | ||
| 103 | # } | ||
| 104 | # shift @siblings; | ||
| 105 | # | ||
| 106 | # # Remove trailing B::NULL | ||
| 107 | # pop @siblings while @siblings && ! ${$siblings[-1]}; | ||
| 108 | # | ||
| 109 | # return @siblings; | ||
| 110 | #} | ||
| 111 | |||
| 112 | |||
| 113 | sub class_exists { | ||
| 114 | my $target = $_[0]; | ||
| 115 | my @parts = | ||
| 116 | map { "${_}::" } | ||
| 117 | split /::/, $target; | ||
| 118 | |||
| 119 | my $symbol_table = \ %main::; | ||
| 120 | for my $part ( @parts ) { | ||
| 121 | if ( exists $symbol_table->{$part} ) { | ||
| 122 | $symbol_table = $symbol_table->{$part}; | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub lint_class_method_call { | ||
| 134 | my B::OP $invocant_op = $_[0]; | ||
| 135 | my B::OP $method_op = $_[1]; | ||
| 136 | |||
| 137 | my $class_name; | ||
| 138 | if ( $invocant_op->can('sv_harder') ) { | ||
| 139 | $class_name = $invocant_op->sv_harder->PV; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $method_name; | ||
| 143 | if ( $method_op->can('sv_harder') ) { | ||
| 144 | $method_name = $method_op->sv_harder->PV; | ||
| 145 | } | ||
| 146 | |||
| 147 | # check strict classes | ||
| 148 | if ( defined $class_name ) { | ||
| 149 | if ( class_exists( $class_name ) ) { | ||
| 150 | # Class is ok! | ||
| 151 | |||
| 152 | if ( defined $method_name ) { | ||
| 153 | if ( $class_name->can( $method_name ) ) { | ||
| 154 | # Class + method are ok! | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | B::Lint::warning "Class $class_name can't do method $method_name"; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | else { | ||
| 161 | B::Lint::warning "Symbolic method call"; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | B::Lint::warning "Class $class_name doesn't exist"; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | elsif ( defined $method_name | ||
| 169 | && ! nearby_classes_perform( $method_name ) | ||
| 170 | ) { | ||
| 171 | B::Lint::warning "Object can't do method $method_name"; | ||
| 172 | } | ||
| 173 | |||
| 174 | return; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | sub nearby_classes_perform { | ||
| 179 | my ( $method_name ) = @_; | ||
| 180 | |||
| 181 | for my $class_name ( @{nearby_classes_in_current_file()} ) { | ||
| 182 | return 1 if $class_name->can($method_name); | ||
| 183 | } | ||
| 184 | |||
| 185 | return 0; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub guess_invocant_category { | ||
| 190 | my ( $op ) = @_; | ||
| 191 | |||
| 192 | # We've been handed a B::NULL object which is a representation for | ||
| 193 | # a null pointer. | ||
| 194 | if ( ! $$op ) { | ||
| 195 | return _invocant_is_unknown(); | ||
| 196 | } | ||
| 197 | |||
| 198 | my $op_name = $op->oldname; | ||
| 199 | |||
| 200 | # Descend past this type of code. This is very silly. | ||
| 201 | # | ||
| 202 | # scalar( scalar( scalar( $invocant ) ) )->xxx | ||
| 203 | # | ||
| 204 | for ( ; | ||
| 205 | $op_name eq 'scalar' || $op_name eq 'null'; | ||
| 206 | $op = $op->first, $op_name = $op->name ) { | ||
| 207 | } | ||
| 208 | |||
| 209 | # A class method call | ||
| 210 | # perl -MO=Terse -e 'Foo->xxx' | ||
| 211 | # UNOP entersub | ||
| 212 | # OP pushmark | ||
| 213 | # SVOP const PV "XXX" | ||
| 214 | # SVOP method_named PV "xxx" | ||
| 215 | if ( $op_name eq 'const' ) { | ||
| 216 | return _invocant_is_literal_class(); | ||
| 217 | } | ||
| 218 | |||
| 219 | # TODO: | ||
| 220 | # # An object method call using a lexical: | ||
| 221 | # # | ||
| 222 | # # perl -MO=Terse -e 'my $foo; $foo->xxx' | ||
| 223 | # # | ||
| 224 | # # UNOP entersub | ||
| 225 | # # OP pushmark | ||
| 226 | # # OP padsv | ||
| 227 | # # SVOP method_named PV "xxx" | ||
| 228 | # # | ||
| 229 | # if ( $op_name eq 'padsv' ) { | ||
| 230 | # return _invocant_is_lexical_object(); | ||
| 231 | # } | ||
| 232 | # | ||
| 233 | # # An object method call using a global: | ||
| 234 | # # | ||
| 235 | # # perl -MO=Terse -e '$foo->xxx' | ||
| 236 | # # | ||
| 237 | # # UNOP entersub | ||
| 238 | # # OP pushmark | ||
| 239 | # # UNOP null | ||
| 240 | # # PADOP gvsv GV *foo | ||
| 241 | # # SVOP method_named PV "xxx" | ||
| 242 | # # | ||
| 243 | # if ( $op_name eq 'gvsv' ) { | ||
| 244 | # return _invocant_is_global_object(); | ||
| 245 | # } | ||
| 246 | |||
| 247 | return _invocant_is_unknown(); | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | our %nearby_classes_cache; | ||
| 252 | sub nearby_classes_in_current_file { | ||
| 253 | my $file = B::Lint->file; | ||
| 254 | |||
| 255 | return $nearby_classes_cache{$file} | ||
| 256 | ||= nearby_classes_in_file( $file ); | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | sub nearby_classes_in_file { | ||
| 261 | my ( $file ) = @_; | ||
| 262 | my $src = File::Slurp::read_file( $file ); | ||
| 263 | |||
| 264 | my @mentioned_potential_classes = $src =~ / | ||
| 265 | ( | ||
| 266 | (?> \w+ ) | ||
| 267 | (?: | ||
| 268 | :: | ||
| 269 | (?> \w+ ) | ||
| 270 | )+ | ||
| 271 | ) | ||
| 272 | /gx; | ||
| 273 | |||
| 274 | my %seen; | ||
| 275 | my @mentioned_classes = | ||
| 276 | grep { | ||
| 277 | class_exists( $_ ) | ||
| 278 | && ! $seen{$_}++ | ||
| 279 | } | ||
| 280 | @mentioned_potential_classes; | ||
| 281 | |||
| 282 | return [ sort keys %seen ]; | ||
| 283 | } | ||
| 284 | |||
| 285 | 'Why did the elf cross the road? To get to the '; | ||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | =pod | ||
| 290 | |||
| 291 | =head1 NAME | ||
| 292 | |||
| 293 | B::Lint::StrictOO - Apply strict to classes and methods | ||
| 294 | |||
| 295 | =head1 VERSION | ||
| 296 | |||
| 297 | version 0.04 | ||
| 298 | |||
| 299 | =head1 METHODS | ||
| 300 | |||
| 301 | =head2 match | ||
| 302 | |||
| 303 | =head2 class_exists | ||
| 304 | |||
| 305 | =head2 lint_class_method_call | ||
| 306 | |||
| 307 | =head2 nearby_classes_perform | ||
| 308 | |||
| 309 | =head2 guess_invocant_category | ||
| 310 | |||
| 311 | =head2 nearby_classes_in_current_file | ||
| 312 | |||
| 313 | =head2 nearby_classes_in_file | ||
| 314 | |||
| 315 | =head1 SYNOPSIS | ||
| 316 | Validates that classes exist, that methods that are called on classes | ||
| 317 | and objects, and variables aren't used as method names. | ||
| 318 | |||
| 319 | From the command line: | ||
| 320 | |||
| 321 | perl -MB::Lint::StrictOO -MO=Lint,oo my_file.pl | ||
| 322 | |||
| 323 | Against a program F<my_file.pl>: | ||
| 324 | |||
| 325 | sub Hickory::Dickory::dock; | ||
| 326 | |||
| 327 | Mouse->dockk; # Class Mouse doesn't exist | ||
| 328 | Hickory::Dickory->dock; | ||
| 329 | Hickory::Dickory->$_; # Symbolic method call | ||
| 330 | $obj->dockk; # Object can't do method | ||
| 331 | $obj->dock; | ||
| 332 | $obj->$_; # Symbolic method call | ||
| 333 | |||
| 334 | =head1 PRIVATE API | ||
| 335 | |||
| 336 | =head1 SUPPORT | ||
| 337 | |||
| 338 | You can find documentation for this module with the perldoc command. | ||
| 339 | |||
| 340 | perldoc B::Lint::StrictOO | ||
| 341 | |||
| 342 | You can also look for information at: | ||
| 343 | |||
| 344 | =over 4 | ||
| 345 | |||
| 346 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 347 | |||
| 348 | L<http://annocpan.org/dist/B-Lint-StrictOO> | ||
| 349 | |||
| 350 | =item * CPAN Ratings | ||
| 351 | |||
| 352 | L<http://cpanratings.perl.org/d/B-Lint-StrictOO> | ||
| 353 | |||
| 354 | =item * RT: CPAN's request tracker | ||
| 355 | |||
| 356 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=B-Lint-StrictOO> | ||
| 357 | |||
| 358 | =item * Search CPAN | ||
| 359 | |||
| 360 | L<http://search.cpan.org/dist/B-Lint-StrictOO> | ||
| 361 | |||
| 362 | =back | ||
| 363 | |||
| 364 | =head1 ACKNOWLEDGEMENTS | ||
| 365 | |||
| 366 | =head1 AUTHOR | ||
| 367 | |||
| 368 | Josh Jore <jjore@cpan.org> | ||
| 369 | |||
| 370 | =head1 COPYRIGHT AND LICENSE | ||
| 371 | |||
| 372 | This software is copyright (c) 2011 by Josh Jore. | ||
| 373 | |||
| 374 | This is free software; you can redistribute it and/or modify it under | ||
| 375 | the same terms as the Perl 5 programming language system itself. | ||
| 376 | |||
| 377 | =cut | ||
| 378 | |||
| 379 | |||
| 380 | __END__ |