1 # Pre-5.10 perls pollute %INC on unsuccesfull module
2 # require, making it appear as if the module is already
3 # loaded on subsequent require()s
4 # Can't seem to find the exact RT/perldelta entry
7 # shut up spurious warnings without loading warnings.pm
8 *CORE::GLOBAL::require = sub {};
10 *CORE::GLOBAL::require = sub {
11 my $res = eval { CORE::require($_[0]) };
28 use B qw/svref_2object/;
31 # makes sure we can load at least something
33 use DBIx::Class::Carp;
38 # not all modules are loadable at all times
40 # trap deprecation warnings and whatnot
41 local $SIG{__WARN__} = sub {};
44 SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
45 (); # empty RV for @modules
50 # have an exception table for old and/or weird code we are not sure
51 # we *want* to clean in the first place
52 my $skip_idx = { map { $_ => 1 } (
53 (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
54 'SQL::Translator::Producer::DBIx::Class::File', # ditto
56 # not sure how to handle type libraries
57 'DBIx::Class::Storage::DBI::Replicated::Types',
58 'DBIx::Class::Admin::Types',
60 # G::L::D is unclean, but we never inherit from it
61 'DBIx::Class::Admin::Descriptive',
62 'DBIx::Class::Admin::Usage',
65 my $has_cmop = eval { require Class::MOP };
67 # can't use Class::Inspector for the mundane parts as it does not
68 # distinguish imports from anything else, what a crock of...
69 # Class::MOP is not always available either - hence just do it ourselves
71 my $seen; #inheritance means we will see the same method multiple times
73 for my $mod (@modules) {
75 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
77 my %all_method_like = (map
78 { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
79 (reverse @{mro::get_linear_isa($mod)})
82 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
85 if ($has_cmop and my $mc = Class::MOP::class_of($mod)) {
86 if ($mc->can('calculate_all_roles_with_inheritance')) {
87 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
91 for my $name (keys %all_method_like) {
93 next if ( DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN() and $name =~ /^carp(?:_unique|_once)?$/ );
95 # overload is a funky thing - it is neither cleaned, and its imports are named funny
96 next if $name =~ /^\(/;
98 my $gv = svref_2object($all_method_like{$name})->GV;
99 my $origin = $gv->STASH->NAME;
102 local $TODO = 'CAG does not clean its BEGIN constants' if $name =~ /^__CAG_/;
103 is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
105 : " (inherited by $mod)"
109 next if $seen->{"${origin}:${name}"}++;
111 if ($origin eq $mod) {
112 pass ("$name is a native $mod method");
114 elsif ($roles{$origin}) {
115 pass ("${mod}::${name} came from consumption of role $origin");
117 elsif ($parents{$origin}) {
118 pass ("${mod}::${name} came from proper parent-class $origin");
122 for (reverse @{mro::get_linear_isa($mod)} ) {
123 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
128 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
129 . ($via || 'UNKNOWN')
134 next if DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN();
136 # some common import names (these should never ever be methods)
137 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
140 for (reverse @{mro::get_linear_isa($mod)} ) {
141 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
146 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
147 . ($via || 'UNKNOWN')
151 pass ("Import $f not leaked into method list of $mod");
164 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
165 push @modules, join ('::', File::Spec->splitdir($_));
168 }, (-e 'blib' ? 'blib' : 'lib') );
170 return sort @modules;