blib/lib/Salvation/TC/Meta/Type/Parameterized.pm

filter: Statementstmt 87.8% 58 / 66 Branchbran 66.6% 16 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 94.1% 16 / 17 Podpod 100% 8 / 8 totaltotal 77.6% 101 / 130 SCAR 29.3
CC
16
Cov
78%
__ANON__
5.4
__ANON__
5.3
new
4.6
CRAP 18.8
« blib/lib/Salvation/TC/Meta/Type/Union.pm ↑ Summary blib/lib/Salvation/TC/Type/HashRef.pm »
1 package Salvation::TC::Meta::Type::Parameterized;
2  
3 =head1 NAME
4  
5 Salvation::TC::Meta::Type::Parameterized - Базовый класс для параметризованных типов
6  
7 =cut
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use base 'Salvation::TC::Meta::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Salvation::TC::Exception::WrongType::TC ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 METHODS
19  
20 =cut
21  
22 =head2 new()
23  
24 =cut
25  
26 sub new {
27  
28 46 ▶ my ( $proto, %args ) = @_;
Subroutine new: called CC=4 SCAR=15.2
Pod documented
29  
30 46 ▶ die( 'Parameterized type metaclass must have inner type' ) unless( defined $args{ 'inner' } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"inner"} 0 46 46
31  
32 46 ▶ unless( blessed $args{ 'inner' } && $args{ 'inner' } -> isa( 'Salvation::TC::Meta::Type' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($args{"inner"}) and $args{"inner"}->isa("Salvation::TC::Meta::Type")) 0 46 46
Condition33%1/3
builtin::blessed($args{"inner"}) and $args{"inner"}->isa("Salvation::TC::Meta::Type")
!ll&&!rl&&r
0046
MC/DC0%0/2
builtin::blessed($args{"inner"}) and $args{"inner"}->isa("Salvation::TC::Meta::Type")
builtin::blessed($args{"inner"}) $args{"inner"}->isa("Salvation::TC::Meta::Type")
Truth table33%1/3
A and B
A= builtin::blessed($args{"inner"})B= $args{"inner"}->isa("Salvation::TC::Meta::Type")
ABresult
0X0
100
111
33  
34 0 die( 'Inner type must be a Salvation::TC::Meta::Type' );
35 }
36  
37 46 my $self = $proto -> SUPER::new( %args );
38  
39 46 $self -> { 'container_validator' } = delete( $self -> { 'validator' } );
40 46 $self -> { 'validator' } = $self -> build_validator();
41  
42 46 return $self;
43 }
44  
45 =head2 inner()
46  
47 =cut
48  
49 sub inner {
50  
51 55 ▶ my ( $self ) = @_;
Subroutine inner: called CC=1 SCAR=0.0
Pod documented
52  
53 55 return $self -> { 'inner' };
54 }
55  
56 =head2 iterate( Any $value, CodeRef $code )
57  
58 =cut
59  
60 sub iterate {
61  
62 0 ▶ my ( $self, $value, $code ) = @_;
Subroutine iterate: not called CC=1 SCAR=6.9
Pod documented
63  
64 0 $code -> ( $value, undef );
65  
66 0 return;
67 }
68  
69 =head2 container_validator()
70  
71 =cut
72  
73 sub container_validator {
74  
75 98 ▶ my ( $self ) = @_;
Subroutine container_validator: called CC=1 SCAR=0.0
Pod documented
76  
77 98 return $self -> { 'container_validator' };
78 }
79  
80 =head2 check_container( Any $value )
81  
82 =cut
83  
84 sub check_container {
85  
86 98 ▶ my ( $self, $value ) = @_;
Subroutine check_container: called CC=1 SCAR=0.0
Pod documented
87  
88 98 return $self -> container_validator() -> ( $value );
89 }
90  
91 =head2 build_validator()
92  
93 =cut
94  
95 sub build_validator {
96  
97 46 ▶ my ( $self ) = @_;
Subroutine build_validator: called CC=1 SCAR=0.1
Pod documented
98  
99 46 my $item_type = $self -> inner();
100  
101 return sub {
102  
103 74 ▶ my ( $value ) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.7
104  
105 74 eval { $self -> check_container( $value ) };
106  
107 74 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 11 63 74
108  
109 11 ▶ if( blessed( $@ ) && $@ -> isa( 'Salvation::TC::Exception::WrongType' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa('Salvation::TC::Exception::WrongType')) { } 11 0 11
Condition33%1/3
builtin::blessed($@) and $@->isa("Salvation::TC::Exception::WrongType")
!ll&&!rl&&r
0011
MC/DC0%0/2
builtin::blessed($@) and $@->isa("Salvation::TC::Exception::WrongType")
builtin::blessed($@) $@->isa("Salvation::TC::Exception::WrongType")
Truth table33%1/3
A and B
A= builtin::blessed($@)B= $@->isa("Salvation::TC::Exception::WrongType")
ABresult
0X0
100
111
110  
111 11 ▶ Salvation::TC::Exception::WrongType::TC -> throw(
Branch: 1 branch
BranchTrueFalseTotal
$@->isa('Salvation::TC::Exception::WrongType::TC') ? : 3 8 11
112 type => $self -> name(), value => $value,
113 ( $@ -> isa( 'Salvation::TC::Exception::WrongType::TC' ) ? (
114 prev => $@,
115 ) : () )
116 );
117  
118 } else {
119  
120 0 die( $@ );
121 }
122 };
123  
124 $self -> iterate( $value, sub {
125  
126 86 ▶ my ( $item, $key ) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.8
127  
128 86 eval { $item_type -> check( $item ) };
129  
130 86 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 26 60 86
131  
132 26 ▶ if( blessed( $@ ) && $@ -> isa( 'Salvation::TC::Exception::WrongType' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa('Salvation::TC::Exception::WrongType')) { } 26 0 26
Condition33%1/3
builtin::blessed($@) and $@->isa("Salvation::TC::Exception::WrongType")
!ll&&!rl&&r
0026
MC/DC0%0/2
builtin::blessed($@) and $@->isa("Salvation::TC::Exception::WrongType")
builtin::blessed($@) $@->isa("Salvation::TC::Exception::WrongType")
Truth table33%1/3
A and B
A= builtin::blessed($@)B= $@->isa("Salvation::TC::Exception::WrongType")
ABresult
0X0
100
111
133  
134 26 ▶ Salvation::TC::Exception::WrongType::TC -> throw(
Branch: 1 branch
BranchTrueFalseTotal
$@->isa('Salvation::TC::Exception::WrongType::TC') ? : 13 13 26
135 type => $self -> name(), value => $value,
136 prev => Salvation::TC::Exception::WrongType::TC -> new(
137 type => $item_type -> name(),
138 value => $item,
139 param_name => $key,
140 ( $@ -> isa( 'Salvation::TC::Exception::WrongType::TC' ) ? (
141 prev => $@,
142 ) : () )
143 )
144 );
145  
146 } else {
147  
148 0 die( $@ );
149 }
150 };
151 63 } );
152  
153 37 return 1;
154 46 };
155 }
156  
157 =head2 sign( ArrayRef $signature )
158  
159 Генерирует валидатор для текущего типа на основе подписи.
160  
161 =cut
162  
163 sub sign {
164  
165 4 ▶ my ( $self, $signature ) = @_;
Subroutine sign: called CC=2 SCAR=7.3
Pod documented
166  
167 4 my $signed_type_generator = $self -> signed_type_generator();
168  
169 4 ▶ unless( defined $signed_type_generator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $signed_type_generator) 0 4 4
170  
171 0 die( sprintf( 'Type %s cannot be signed', $self -> name() ) )
172 }
173  
174 4 my $signed_validator = $signed_type_generator -> ( $signature );
175  
176 return sub {
177  
178 10 ▶ $self -> check_container( $_[ 0 ] ) && $signed_validator -> ( $_[ 0 ] )
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$self->check_container($_[0]) and &$signed_validator($_[0]) 10 0 10
179 4 };
180 }
181  
182 =head2 length_checker( Int $min, Maybe[Int] $max )
183  
184 Генерирует валидатор для текущего типа на основе спецификации длины.
185  
186 =cut
187  
188 sub length_checker {
189  
190 5 ▶ my ( $self, $min, $max ) = @_;
Subroutine length_checker: called CC=2 SCAR=7.3
Pod documented
191  
192 5 my $length_type_generator = $self -> length_type_generator();
193  
194 5 ▶ unless( defined $length_type_generator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length_type_generator) 0 5 5
195  
196 0 die( sprintf( 'Length of type %s could not be checked', $self -> name() ) );
197 }
198  
199 5 my $length_validator = $length_type_generator -> ( $min, $max );
200  
201 return sub {
202  
203 14 ▶ $self -> check_container( $_[ 0 ] ) && $length_validator -> ( $_[ 0 ] )
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$self->check_container($_[0]) and &$length_validator($_[0]) 12 0 12
204 5 };
205 }
206  
207 4 1;
208  
209 __END__
« blib/lib/Salvation/TC/Meta/Type/Union.pm ↑ Summary blib/lib/Salvation/TC/Type/HashRef.pm »