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

filter: Statementstmt 93.1% 54 / 58 Branchbran 72.2% 13 / 18 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 78.1% 86 / 110 SCAR 29.5
CC
17
Cov
81%
new
9.7
__ANON__
7.2
coerce
5.4
CRAP 19.1
« blib/lib/Salvation/TC/Meta/Type.pm ↑ Summary blib/lib/Salvation/TC/Meta/Type/Parameterized.pm »
1 package Salvation::TC::Meta::Type::Union;
2  
3 =head1 NAME
4  
5 Salvation::TC::Meta::Type::Union - Класс для объединённых типов
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 4 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use base 'Salvation::TC::Meta::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
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 9 ▶ my ( $proto, %args ) = @_;
Subroutine new: called CC=7 SCAR=22.7
Pod documented
29  
30 9 ▶ die( 'Type union metaclass must have types list' ) unless( defined $args{ 'types' } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"types"} 0 9 9
31 9 ▶ die( 'Types list must be an ArrayRef' ) if( ref( $args{ 'types' } ) ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"types"} ne "ARRAY" 0 9 9
32  
33 9 foreach ( @{ $args{ 'types' } } ) {
34  
35 19 ▶ unless( defined $_ && blessed $_ && $_ -> isa( 'Salvation::TC::Meta::Type' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_ and builtin::blessed($_) and $_->isa("Salvation::TC::Meta::Type")) 0 19 19
Condition33%2/6
defined $_ and builtin::blessed($_) and $_->isa("Salvation::TC::Meta::Type")
!ll&&!rl&&r
0019
defined $_ and builtin::blessed($_)
!ll&&!rl&&r
0019
MC/DC0%0/3
defined $_ and builtin::blessed($_) and $_->isa("Salvation::TC::Meta::Type")
defined $_ builtin::blessed($_) $_->isa("Salvation::TC::Meta::Type")
Truth table25%1/4
A and B and C
A= defined $_B= builtin::blessed($_)C= $_->isa("Salvation::TC::Meta::Type")
ABCresult
0XX0
10X0
1100
1111
36  
37 0 die( 'Types list must be an ArrayRef[Salvation::TC::Meta::Type]' );
38 }
39 }
40  
41 9 $args{ 'validator' } = $proto -> build_validator( @args{ 'name', 'types' } );
42  
43 9 return $proto -> SUPER::new( %args );
44 }
45  
46 =head2 types()
47  
48 =cut
49  
50 sub types {
51  
52 3 ▶ my ( $self ) = @_;
Subroutine types: called CC=1 SCAR=0.0
Pod documented
53  
54 3 return $self -> { 'types' };
55 }
56  
57 =head2 build_validator( Str $name, ArrayRef[Salvation::TC::Meta::Type] $types )
58  
59 =cut
60  
61 sub build_validator {
62  
63 9 ▶ my ( $self, $name, $types ) = @_;
Subroutine build_validator: called CC=1 SCAR=0.0
Pod documented
64  
65 return sub {
66  
67 9 ▶ my ( $value ) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.8
68  
69 9 my @errors = ();
70  
71 9 foreach my $type ( @$types ) {
72  
73 17 my $check_passed = true;
74  
75 17 eval { $type -> check( $value ) };
76  
77 17 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 11 6 17
78  
79 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
80  
81 11 ▶ push( @errors, Salvation::TC::Exception::WrongType::TC -> new(
Branch: 1 branch
BranchTrueFalseTotal
$@->isa('Salvation::TC::Exception::WrongType::TC') ? : 10 1 11
82 type => $type -> name(), value => $value,
83 ( $@ -> isa( 'Salvation::TC::Exception::WrongType::TC' ) ? (
84 prev => $@,
85 ) : () )
86 ) );
87  
88 11 $check_passed = false;
89  
90 } else {
91  
92 0 die( $@ );
93 }
94 }
95  
96 17 ▶ return true if( $check_passed );
Branch: 1 branch
BranchTrueFalseTotal
if $check_passed 6 11 17
97 }
98  
99 Salvation::TC::Exception::WrongType::TC -> throw(
100 3 type => $name, value => $value, prev => \@errors,
101 );
102 9 };
103 }
104  
105 =head2 coerce( Any $value )
106  
107 =cut
108  
109 sub coerce {
110  
111 3 ▶ my ( $self, $value ) = @_;
Subroutine coerce: called CC=5 SCAR=16.8
Pod documented
112  
113 3 foreach my $type ( @{ $self -> types() } ) {
114  
115 6 eval {
116 6 my $new_value = $type -> coerce( $value );
117  
118 6 $type -> check( $new_value ); # true или die
119  
120 3 $value = $new_value;
121 };
122  
123 6 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 3 6
124  
125 3 ▶ if( blessed( $@ ) && $@ -> isa( 'Salvation::TC::Exception::WrongType' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa('Salvation::TC::Exception::WrongType')) { } 3 0 3
Condition33%1/3
builtin::blessed($@) and $@->isa("Salvation::TC::Exception::WrongType")
!ll&&!rl&&r
003
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
126  
127 3 next;
128  
129 } else {
130  
131 0 die( $@ );
132 }
133 }
134  
135 3 return $value; # Moose возвращает либо старое, либо приведённое значение
136 }
137  
138 0 return $value; # Moose возвращает либо старое, либо приведённое значение
139 }
140  
141  
142 4 1;
143  
144 __END__
« blib/lib/Salvation/TC/Meta/Type.pm ↑ Summary blib/lib/Salvation/TC/Meta/Type/Parameterized.pm »