File Coverage

blib/lib/Perinci/Sub/Wrapper/Patch/HandlePHPArray.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 22 23 95.6


line stmt bran cond sub pod time code
1             package Perinci::Sub::Wrapper::Patch::HandlePHPArray;
2              
3 1     1   22742 use 5.010001;
  1         4  
  1         34  
4 1     1   5 use strict;
  1         2  
  1         31  
5 1     1   4 use warnings;
  1         2  
  1         48  
6 1     1   1443 use Log::Any '$log';
  1         3462  
  1         5  
7              
8 1     1   993 use parent qw(Module::Patch);
  1         306  
  1         6  
9              
10             our $VERSION = '0.02'; # VERSION
11              
12             my $code = sub {
13             my $ctx = shift;
14              
15             my ($self, %args) = @_;
16              
17             $ctx->{orig}->(@_);
18              
19             $self->select_section('before_call_before_arg_validation');
20              
21             my $args = $self->{_meta}{args};
22             for my $an (sort keys %$args) {
23             my $aspec = $args->{$an};
24             next unless $aspec->{schema};
25             if ($aspec->{schema}[0] eq 'array') {
26             $self->push_lines("if (ref(\$args{$an}) eq 'HASH' && !keys(\%{\$args{$an}})) { \$args{$an} = [] }");
27             }
28             if ($aspec->{schema}[0] eq 'hash') {
29             $self->push_lines("if (ref(\$args{$an}) eq 'ARRAY' && !\@{\$args{$an}}) { \$args{$an} = {} }");
30             }
31             }
32             };
33              
34             sub patch_data {
35             return {
36 1     1 0 43 v => 3,
37             patches => [
38             {
39             action => 'wrap',
40             sub_name => 'handle_args',
41             code => $code,
42             },
43             ],
44             };
45             }
46              
47             1;
48             # ABSTRACT: Convert {} to [] or vice versa to match functions' expectations
49              
50             __END__