Signatures/TypeConstraint.pm

filter: Statementstmt 88.4% 61 / 69 Branchbran 42.8% 12 / 28 Conditioncond 45.4% 15 / 33 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 65.2% 109 / 167 CC 26 SCAR 33.2
CC
26
Cov
68%
find_registered_constraint
87.7
_walk_data
5.2
_str_node
4.2
CRAP 27.7
↑ Summary Signatures.pm »
1 package Parse::Method::Signatures::TypeConstraint;
2  
3 3 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use MooseX::Types::Util qw/has_available_type_export/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use MooseX::Types::Moose qw/Str HashRef CodeRef ClassName/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Parse::Method::Signatures::Types qw/TypeConstraint/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 has ppi => (
12 is => 'ro',
13 isa => 'PPI::Element',
14 required => 1,
15 handles => {
16 'to_string' => 'content'
17 }
18 );
19  
20 3 has tc => (
21 is => 'ro',
22 isa => TypeConstraint,
23 lazy => 1,
24 builder => '_build_tc',
25 );
26  
27 3 has from_namespace => (
28 is => 'ro',
29 isa => ClassName,
30 predicate => 'has_from_namespace'
31 );
32  
33 has tc_callback => (
34 is => 'ro',
35 isa => CodeRef,
36 28 ▶ default => sub { \&find_registered_constraint },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 3 );
38  
39 sub find_registered_constraint {
40 9 ▶ my ($self, $name) = @_;
Subroutine find_registered_constraint: called CC=13 SCAR=44.7
Pod documented
41  
42 9 my $type;
43 9 ▶ if ($self->has_from_namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_from_namespace) 0 9 9
44 0 my $pkg = $self->from_namespace;
45  
46 0 ▶ if ($type = has_available_type_export($pkg, $name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type = has_available_type_export($pkg, $name)) { } 0 0 0
elsif (not $name =~ /::/) { } 0 0 0
47 0 ▶ croak "The type '$name' was found in $pkg " .
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type->isa("MooseX::Types::UndefinedType") 0 0 0
Condition0%0/3
$type and $type->isa("MooseX::Types::UndefinedType")
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type->isa("MooseX::Types::UndefinedType")
$type $type->isa("MooseX::Types::UndefinedType")
Truth table0%0/3
A and B
A= $typeB= $type->isa("MooseX::Types::UndefinedType")
ABresult
0X0
100
111
48 "but it hasn't yet been defined. Perhaps you need to move the " .
49 "definition into a type library or a BEGIN block.\n"
50 if $type && $type->isa('MooseX::Types::UndefinedType');
51 }
52 elsif ($name !~ /::/) {
53 0 ▶ my $meta = Class::MOP::class_of($pkg) || Class::MOP::Class->initialize($pkg);
Condition0%0/3
Class::MOP::class_of($pkg) || 'Class::MOP::Class'->initialize($pkg)
l!l&&r!l&&!r
000
MC/DC0%0/2
Class::MOP::class_of($pkg) || 'Class::MOP::Class'->initialize($pkg)
Class::MOP::class_of($pkg) 'Class::MOP::Class'->initialize($pkg)
Truth table0%0/3
A || B
A= Class::MOP::class_of($pkg)B= 'Class::MOP::Class'->initialize($pkg)
ABresult
1X1
011
000
54 0 my $func = $meta->get_package_symbol('&' . $name);
55 0 ▶ my $proto = prototype $func if $func;
Branch: 1 branch
BranchTrueFalseTotal
if $func 0 0 0
56  
57 0 ▶ $name = $func->()
Branch: 1 branch
BranchTrueFalseTotal
if $func and defined $proto and not length $proto 0 0 0
Condition0%0/6
$func and defined $proto and not length $proto
!ll&&!rl&&r
000
$func and defined $proto
!ll&&!rl&&r
000
MC/DC0%0/3
$func and defined $proto and not length $proto
$func defined $proto not length $proto
Truth table0%0/4
A and B and not(C)
A= $funcB= defined $protoC= not length $proto
ABCresult
0XX0
10X0
1100
1111
58 if $func && defined $proto && !length $proto;
59 }
60 }
61  
62 9 my $registry = Moose::Util::TypeConstraints->get_type_constraint_registry;
63 9 ▶ return $type || $registry->find_type_constraint($name) || $name;
Condition66%4/6
$type || $registry->find_type_constraint($name) || $name
l!l&&r!l&&!r
810
$type or $registry->find_type_constraint($name)
l!l&&r!l&&!r
081
MC/DC0%0/3
$type || $registry->find_type_constraint($name) || $name
$type $registry->find_type_constraint($name) $name
Truth table50%2/4
A or B || C
A= $typeB= $registry->find_type_constraint($name)C= $name
ABCresult
1XX1
01X1
0011
0000
64 }
65  
66  
67 sub _build_tc {
68 5 ▶ my ($self) = @_;
Subroutine _build_tc: called CC=3 SCAR=11.8
69 5 my $tc = $self->_walk_data($self->ppi);
70  
71 # This makes the error appear from the right place
72 local $Carp::Internal{'Class::MOP::Method::Generated'} = 1
73 5 ▶ unless exists $Carp::Internal{'Class::MOP::Method::Generated'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Carp::Internal{"Class::MOP::Method::Generated"} 5 0 5
74  
75 5 ▶ croak "'@{[$self->ppi]}' could not be parsed to a type constraint - maybe you need to "
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($tc) 0 5 5
76 . "pre-declare the type with class_type"
77 unless blessed $tc;
78 5 return $tc;
79 }
80  
81 sub _walk_data {
82 13 ▶ my ($self, $data) = @_;
Subroutine _walk_data: called CC=5 SCAR=16.6
83  
84 13 ▶ my $res = $self->_union_node($data)
Branch: 1 branch
BranchTrueFalseTotal
my $res = $self->_union_node($data) || $self->_params_node($data) || $self->_str_node($data) || $self->_leaf($data) or confess("failed to visit tc") 0 13 13
Condition77%7/9
$self->_union_node($data) || $self->_params_node($data) || $self->_str_node($data) || $self->_leaf($data)
l!l&&r!l&&!r
670
$self->_union_node($data) or $self->_params_node($data) or $self->_str_node($data)
l!l&&r!l&&!r
517
$self->_union_node($data) or $self->_params_node($data)
l!l&&r!l&&!r
2011
MC/DC0%0/4
$self->_union_node($data) || $self->_params_node($data) || $self->_str_node($data) || $self->_leaf($data)
$self->_union_node($data) $self->_params_node($data) $self->_str_node($data) $self->_leaf($data)
Truth table60%3/5
A or B or C || D
A= $self->_union_node($data)B= $self->_params_node($data)C= $self->_str_node($data)D= $self->_leaf($data)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
85 || $self->_params_node($data)
86 || $self->_str_node($data)
87 || $self->_leaf($data)
88 or confess 'failed to visit tc';
89 13 return $res->();
90 }
91  
92 sub _leaf {
93 7 ▶ my ($self, $data) = @_;
Subroutine _leaf: called CC=1 SCAR=0.0
94  
95 7 ▶ sub { $self->_invoke_callback($data->content) };
Subroutine __ANON__: called CC=1 SCAR=0.0
96 }
97  
98 sub _union_node {
99 13 ▶ my ($self, $data) = @_;
Subroutine _union_node: called CC=2 SCAR=7.0
100 13 ▶ return unless $data->isa('PPI::Statement::Expression::TCUnion');
Branch: 1 branch
BranchTrueFalseTotal
unless $data->isa("PPI::Statement::Expression::TCUnion") 11 2 13
101  
102 2 my @types = map { $self->_walk_data($_) } $data->children;
103 sub {
104 2 ▶ scalar @types == 1 ? @types
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
scalar @types == 1 ? : 0 2 2
105 : Moose::Meta::TypeConstraint::Union->new(type_constraints => \@types)
106 2 };
107 }
108  
109 sub _params_node {
110 11 ▶ my ($self, $data) = @_;
Subroutine _params_node: called CC=2 SCAR=6.9
111 11 ▶ return unless $data->isa('PPI::Statement::Expression::TCParams');
Branch: 1 branch
BranchTrueFalseTotal
unless $data->isa("PPI::Statement::Expression::TCParams") 8 3 11
112  
113 3 my @params = map { $self->_walk_data($_) } @{$data->params};
114 3 my $type = $self->_invoke_callback($data->type);
115 3 ▶ sub { $type->parameterize(@params) }
Subroutine __ANON__: called CC=1 SCAR=0.0
116 3 }
117  
118  
119 sub _str_node {
120 8 ▶ my ($self, $data) = @_;
Subroutine _str_node: called CC=4 SCAR=14.2
121 8 ▶ return unless $data->isa('PPI::Token::StringifiedWord')
Branch: 1 branch
BranchTrueFalseTotal
unless $data->isa("PPI::Token::StringifiedWord") or $data->isa("PPI::Token::Number") or $data->isa("PPI::Token::Quote") 7 1 8
Condition66%4/6
$data->isa("PPI::Token::StringifiedWord") or $data->isa("PPI::Token::Number") or $data->isa("PPI::Token::Quote")
l!l&&r!l&&!r
107
$data->isa("PPI::Token::StringifiedWord") or $data->isa("PPI::Token::Number")
l!l&&r!l&&!r
107
MC/DC33%1/3
$data->isa("PPI::Token::StringifiedWord") or $data->isa("PPI::Token::Number") or $data->isa("PPI::Token::Quote")
$data->isa("PPI::Token::StringifiedWord") $data->isa("PPI::Token::Number") $data->isa("PPI::Token::Quote")
Truth table50%2/4
A or B or C
A= $data->isa("PPI::Token::StringifiedWord")B= $data->isa("PPI::Token::Number")C= $data->isa("PPI::Token::Quote")
ABCresult
1XX1
01X1
0011
0000
122 || $data->isa('PPI::Token::Number')
123 || $data->isa('PPI::Token::Quote');
124  
125 sub {
126 1 ▶ $data->isa('PPI::Token::Number')
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$data->isa('PPI::Token::Number') ? : 0 1 1
127 ? $data->content
128 : $data->string
129 1 };
130 }
131  
132 sub _invoke_callback {
133 10 ▶ my $self = shift;
Subroutine _invoke_callback: called CC=1 SCAR=0.0
134 10 $self->tc_callback->($self, @_);
135 }
136  
137 3 __PACKAGE__->meta->make_immutable;
138  
139 3 1;
140  
141 __END__
142  
143 =head1 NAME
144  
145 Parse::Method::Signatures::TypeConstraint - turn parsed TC data into Moose TC object
146  
147 =head1 DESCRIPTION
148  
149 Class used to turn PPI elements into L<Moose::Meta::TypeConstraint> objects.
150  
151 =head1 ATTRIBUTES
152  
153 =head2 tc
154  
155 =over
156  
157 B<Lazy Build.>
158  
159 =back
160  
161 The L<Moose::Meta::TypeConstraint> object for this type constraint, built when
162 requested. L</tc_callback> will be called for each individual component type in
163 turn.
164  
165 =head2 tc_callback
166  
167 =over
168  
169 B<Type:> CodeRef
170  
171 B<Default:> L</find_registered_constraint>
172  
173 =back
174  
175 Callback used to turn type names into type objects. See
176 L<Parse::Method::Signatures/type_constraint_callback> for more details and an
177 example.
178  
179 =head2 from_namespace
180  
181 =over
182  
183 B<Type:> ClassName
184  
185 =back
186  
187 If provided, then the default C<tc_callback> will search for L<MooseX::Types>
188 in this package.
189  
190 =head1 METHODS
191  
192 =head2 find_registered_constraint
193  
194 Will search for an imported L<MooseX::Types> in L</from_namespace> (if
195 provided). Failing that it will ask the L<Moose::Meta::TypeConstraint::Registry>
196 for a type with the given name.
197  
198 If all else fails, it will simple return the type as a string, so that Moose's
199 auto-vivification of classnames to type will work.
200  
201 =head2 to_string
202  
203 String representation of the type constraint, approximately as parsed.
204  
205 =head1 SEE ALSO
206  
207 L<Parse::Method::Signatures>, L<MooseX::Types>, L<MooseX::Types::Util>.
208  
209 =head1 AUTHORS
210  
211 Florian Ragwitz <rafl@debian.org>.
212  
213 Ash Berlin <ash@cpan.org>.
214  
215 =head1 LICENSE
216  
217 Licensed under the same terms as Perl itself.
218  
↑ Summary Signatures.pm »