blib/lib/MooX/StrictHas.pm

filter: Statementstmt 100% 31 / 31 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.0% 47 / 50 CC 7 SCAR 10.1
CC
7
Cov
93%
__ANON__
5.0
_override_function
3.1
import
1.0
CRAP 2.8
↑ Summary
1 package MooX::StrictHas;
2  
3 1 our $VERSION = '0.04';
4  
5 # this bit would be MooX::Utils but without initial _ on func name
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moo::Role ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 #use base qw(Exporter);
12 #our @EXPORT = qw(override_function);
13 5 ▶sub _override_function {
Subroutine _override_function: called CC=3 SCAR=11.3
14 5 my ($target, $name, $func) = @_;
15 5 ▶ my $orig = $target->can($name) or croak "Override '$target\::$name': not found";
Branch: 1 branch
BranchTrueFalseTotal
my $orig = $target->can($name) or croak("Override '${target}::${name}': not found") 5 0 5
16 5 ▶ my $install_tracked = Moo::Role->is_role($target) ? \&Moo::Role::_install_tracked : \&Moo::_install_tracked;
Branch: 1 branch
BranchTrueFalseTotal
'Moo::Role'->is_role($target) ? : 0 5 5
17 5 ▶ $install_tracked->($target, $name, sub { $func->($orig, @_) });
Subroutine has: called CC=1 SCAR=0.0
18 }
19 # end MooX::Utils;
20  
21 1 my %ATTR2MESSAGE = (
22 auto_deref => q{just dereference in your using code},
23 lazy_build => q{Use "is => 'lazy'" instead},
24 does => q{Unsupported; use "isa" instead},
25 );
26 5 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
27 5 my $target = scalar caller;
28 5 ▶ _override_function($target, 'has', sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
29 5 my ($orig, $namespec, %opts) = @_;
30 5 ▶ $namespec = "[@$namespec]" if ref $namespec;
Branch: 1 branch
BranchTrueFalseTotal
if ref $namespec 0 5 5
31 5 my @messages;
32 push @messages, exists($opts{$_})
33 ? "$_ detected on $namespec: $ATTR2MESSAGE{$_}"
34 : ()
35 5 ▶ for sort keys %ATTR2MESSAGE;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{$_} ? : 5 10 15
36 5 ▶ croak join "\n", @messages if @messages;
Branch: 1 branch
BranchTrueFalseTotal
if @messages 4 1 5
37 1 $orig->($namespec, %opts);
38 5 });
39 }
40  
41 =head1 NAME
42  
43 MooX::StrictHas - Forbid "has" attributes lazy_build and auto_deref
44  
45 =begin markdown
46  
47 # PROJECT STATUS
48  
49 | OS | Build status |
50 |:-------:|--------------:|
51 | Linux | [![Build Status](https://travis-ci.com/mohawk2/moox-stricthas.svg?branch=master)](https://travis-ci.org/mohawk2/moox-stricthas) |
52  
53 [![CPAN version](https://badge.fury.io/pl/moox-stricthas.svg)](https://metacpan.org/pod/MooX::StrictHas) [![Coverage Status](https://coveralls.io/repos/github/mohawk2/moox-stricthas/badge.svg?branch=master)](https://coveralls.io/github/mohawk2/moox-stricthas?branch=master)
54  
55 =end markdown
56  
57 =head1 SYNOPSIS
58  
59 package MyMod;
60 use Moo;
61 use MooX::StrictHas;
62 has attr => (
63 is => 'ro',
64 auto_deref => 1, # blows up, not implemented in Moo
65 );
66 has attr2 => (
67 is => 'ro',
68 lazy_build => 1, # blows up, not implemented in Moo
69 );
70 has attr2 => (
71 is => 'ro',
72 does => "Thing", # blows up, not implemented in Moo
73 );
74  
75 =head1 DESCRIPTION
76  
77 This is a L<Moo> extension, intended to aid those porting modules from
78 L<Moose> to Moo. It forbids two attributes for L<Moo/has>, which Moo
79 does not implement, but silently accepts:
80  
81 =over
82  
83 =item auto_deref
84  
85 This is not considered best practice - just dereference in your using code.
86  
87 =item does
88  
89 Unsupported; use C<isa> instead.
90  
91 =item lazy_build
92  
93 Use C<is =E<gt> 'lazy'> instead.
94  
95 =back
96  
97 =head1 AUTHOR
98  
99 Ed J
100  
101 =head1 LICENCE
102  
103 The same terms as Perl itself.
104  
105 =cut
106  
107 1 1;
↑ Summary