| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package MooseX::TypeArray; | ||||||||||||||||||||||||||||
| 5 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | $MooseX::TypeArray::VERSION = '0.1.0'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Create composite types where all subtypes must be satisfied | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | exports => [qw( typearray )], | ||||||||||||||||||||||||||||
| 14 | groups => [ default => [qw( typearray )] ], | ||||||||||||||||||||||||||||
| 15 | 3 | }; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $sugarmap = { | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # '' => sub {}, | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | 'ARRAY' => sub { { name => undef, combining => $_[0], } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | 'HASH' => sub { { name => undef, %{ $_[0] }, combining => [ @{ $_[0]->{combining} || [] } ], } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Condition50%1/2
$_[0]{'combining'} || []
MC/DC0%0/1
$_[0]{'combining'} || []
$_[0]{'combining'}
Truth table50%1/2
A
A= $_[0]{'combining'}
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # '_string' => sub {}, | ||||||||||||||||||||||||||||
| 24 | 2 | ▶ | '_string,ARRAY' => sub { { name => $_[0], combining => $_[1], } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | '_string,HASH' => sub { { name => $_[0], %{ $_[1] }, combining => [ @{ $_[1]->{combining} || [] } ], } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Condition50%1/2
$_[1]{'combining'} || []
MC/DC0%0/1
$_[1]{'combining'} || []
$_[1]{'combining'}
Truth table50%1/2
A
A= $_[1]{'combining'}
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | 'ARRAY,HASH' => sub { { name => undef, %{ $_[1] }, combining => [ @{ $_[1]->{combining} || [] }, @{ $_[0] } ], } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Condition50%1/2
$_[1]{'combining'} || []
MC/DC0%0/1
$_[1]{'combining'} || []
$_[1]{'combining'}
Truth table50%1/2
A
A= $_[1]{'combining'}
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | '_string,ARRAY,HASH' => sub { { name => $_[0], %{ $_[2] }, combining => [ @{ $_[2]->{combining} || [] }, @{ $_[1] } ], } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Condition50%1/2
$_[2]{'combining'} || []
MC/DC0%0/1
$_[2]{'combining'} || []
$_[2]{'combining'}
Truth table50%1/2
A
A= $_[2]{'combining'}
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | }; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 11 | ▶ | sub _desugar_typearray { | ||||||||||||||||||||||||||
|
Subroutine
_desugar_typearray: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 32 | 11 | my (@args) = @_; | |||||||||||||||||||||||||||
| 33 | 11 | ▶ | my (@argtypes) = map { ref $_ ? ref $_ : '_string' } @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 11 | my $signature = join q{,}, @argtypes; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 11 | ▶ | if ( exists $sugarmap->{$signature} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 7 | return $sugarmap->{$signature}->(@args); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | return __PACKAGE__->_throw_error( 'Unexpected parameters types passed: <' | ||||||||||||||||||||||||||||
| 40 | . $signature . '>,' . qq{\n} | ||||||||||||||||||||||||||||
| 41 | . 'Expected one from [ ' | ||||||||||||||||||||||||||||
| 42 | 4 | . ( join q{, }, map { '<' . $_ . '>' } sort keys %{$sugarmap} ) | |||||||||||||||||||||||||||
| 43 | . ' ] ' ); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub _check_conflict_names { | ||||||||||||||||||||||||||
|
Subroutine
_check_conflict_names: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||
| 47 | 1 | my ( $name, $package ) = @_; | |||||||||||||||||||||||||||
| 48 | 1 | require Moose::Util::TypeConstraints; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | my $type = Moose::Util::TypeConstraints::find_type_constraint($name); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | if ( defined $type and $type->_package_defined_in eq $package ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $type and $type->_package_defined_in eq $package
MC/DC0%0/2
defined $type and $type->_package_defined_in eq $package
defined $type
$type->_package_defined_in eq $package
Truth table33%1/3
A and B
A= defined $typeB= $type->_package_defined_in eq $package
| |||||||||||||||||||||||||||||
| 53 | 0 | __PACKAGE__->_throw_error( "The type constraint '$name' has already been created in " | |||||||||||||||||||||||||||
| 54 | . $type->_package_defined_in | ||||||||||||||||||||||||||||
| 55 | . " and cannot be created again in $package " ); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 1 | ▶ | if ( $name !~ /\A[[:word:]:.]+\z/sxm ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | __PACKAGE__->_throw_error( | |||||||||||||||||||||||||||
| 59 | sprintf q{%s contains invalid characters for a type name. Names can contain alphanumeric character, ":", and "."%s}, | ||||||||||||||||||||||||||||
| 60 | $name, qq{\n} ); | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 1 | return 1; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | sub _convert_type_names { | ||||||||||||||||||||||||||
|
Subroutine
_convert_type_names: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 66 | 1 | my ( $name, @types ) = @_; | |||||||||||||||||||||||||||
| 67 | 1 | require Moose::Util::TypeConstraints; | |||||||||||||||||||||||||||
| 68 | 1 | my @out; | |||||||||||||||||||||||||||
| 69 | 1 | for my $type (@types) { | |||||||||||||||||||||||||||
| 70 | 2 | my $translated_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint($type); | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | if ( not $translated_type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | __PACKAGE__->_throw_error("Could not locate type constraint ($type) for the TypeArray"); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 2 | push @out, $translated_type; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 1 | return @out; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | ## no critic ( RequireArgUnpacking ) | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | sub typearray { | ||||||||||||||||||||||||||
|
Subroutine
typearray: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 1 | my $config = _desugar_typearray(@_); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | $config->{package_defined_in} = scalar caller 0; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | _check_conflict_names( $config->{name}, $config->{package_defined_in} ) if defined $config->{name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | $config->{combining} = [ _convert_type_names( $config->{name}, @{ $config->{combining} } ) ]; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | require Moose::Meta::TypeConstraint::TypeArray; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | my $constraint = Moose::Meta::TypeConstraint::TypeArray->new( %{$config} ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if ( defined $config->{name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | require Moose::Util::TypeConstraints; | |||||||||||||||||||||||||||
| 97 | 1 | Moose::Util::TypeConstraints::register_type_constraint($constraint); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | return $constraint; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | ## no critic ( RequireArgUnpacking ) | ||||||||||||||||||||||||||||
| 105 | 4 | ▶ | sub _throw_error { | ||||||||||||||||||||||||||
|
Subroutine
_throw_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 4 | shift; | |||||||||||||||||||||||||||
| 107 | 4 | require Moose; | |||||||||||||||||||||||||||
| 108 | 4 | unshift @_, 'Moose'; | |||||||||||||||||||||||||||
| 109 | 4 | goto &Moose::throw_error; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | 1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||