Treex/Block/Util/Eval.pm

filter: Statementstmt 35.3% 29 / 82 Branchbran 5.2% 2 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 10 / 14 Podpod 60.0% 3 / 5 totaltotal 30.5% 44 / 144 CC 25 SCAR 40.2
CC
25
Cov
25%
process_zone
132.0
process_document
56.0
process_bundle
42.0
CRAP 56.0
« Treex/Core/Run.pm ↑ Summary Treex/Core/Node/InClause.pm »
1 package Treex::Block::Util::Eval;
2 3 $Treex::Block::Util::Eval::VERSION = '2.20210102';
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 extends 'Treex::Core::Block';
6  
7 3 has [
8 qw( document bundle zone
9 atree ttree ntree ptree
10 anode tnode nnode pnode
11 _args)
12 ]
13 => ( is => 'rw' );
14  
15 3 has expand_code => (
16 is=> 'ro',
17 isa => 'Bool',
18 default => 1,
19 documentation => 'Should "$." be expanded to "$this->" in all eval codes?'
20 );
21  
22 2 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.1
Pod undocumented
23 2 my ( $self, $arg_ref ) = @_;
24 2 ▶ $arg_ref->{_atree} = any { $arg_ref->{$_} } qw(atree anode);
Subroutine __ANON__: called CC=1 SCAR=0.0
25 2 ▶ $arg_ref->{_ttree} = any { $arg_ref->{$_} } qw(ttree tnode);
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 ▶ $arg_ref->{_ntree} = any { $arg_ref->{$_} } qw(ntree nnode);
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 ▶ $arg_ref->{_ptree} = any { $arg_ref->{$_} } qw(ptree pnode);
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 ▶ $arg_ref->{_zone} = any { $arg_ref->{$_} } qw(zone _atree _ttree _ntree _ptree);
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 ▶ $arg_ref->{_bundle} = any { $arg_ref->{$_} } qw(bundle _zone);
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 2 ▶ if ( !any { $arg_ref->{$_} } qw(doc document _bundle) ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $arg_ref->{$_}; } , "doc", "document", "_bundle")) 0 2 2
32 0 log_fatal "At least one of the following parameters must be non-empty:"
33 . " document, bundle, zone, [atnp]tree, [atnp]node.";
34 }
35 2 ▶ if ($arg_ref->{doc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"doc"}) 0 2 2
36 0 $self->set_document($arg_ref->{doc});
37 }
38  
39 2 $self->_set_args($arg_ref);
40 2 return;
41 }
42  
43 0 ▶sub expand_eval_code {
Subroutine expand_eval_code: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my ($self, $to_eval) = @_;
45 0 ▶ return "$to_eval;1;" if !$self->expand_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->expand_code 0 0 0
46 0 $to_eval =~ s/\$\./\$this->/g;
47 0 return "$to_eval;1;";
48 }
49  
50 ## no critic (ProhibitStringyEval) This block needs string evals
51 0 ▶sub process_document {
Subroutine process_document: not called CC=7 SCAR=40.3
Pod documented
52 0 my ( $self, $document ) = @_;
53 0 my $doc = $document;
54 0 my $this = $document;
55 0 ▶ if ( $self->document ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document) 0 0 0
56 0 my $to_eval = $self->expand_eval_code($self->document);
57 0 ▶ eval($to_eval) or log_fatal("While evaluating '$to_eval' got error: $@");
Branch: 1 branch
BranchTrueFalseTotal
eval $to_eval or log_fatal("While evaluating '${to_eval}' got error: $@") 0 0 0
58 }
59  
60 0 ▶ if ( $self->_args->{_bundle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_args->{"_bundle"}) 0 0 0
61 0 my $bundleNo = 1;
62 0 foreach my $bundle ( $document->get_bundles() ) {
63 0 ▶ if ( !$self->select_bundles || $self->_is_bundle_selected->{$bundleNo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}) 0 0 0
Condition0%0/3
not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}
not $self->select_bundles $self->_is_bundle_selected->{$bundleNo}
Truth table0%0/3
not(A) or B
A= not $self->select_bundlesB= $self->_is_bundle_selected->{$bundleNo}
ABresult
1X1
011
000
64 0 $self->process_bundle($bundle, $bundleNo);
65 }
66 0 $bundleNo++;
67 }
68 }
69 0 return;
70 }
71  
72 0 ▶sub process_bundle {
Subroutine process_bundle: not called CC=6 SCAR=37.4
Pod documented
73 0 my ( $self, $bundle, $bundleNo ) = @_;
74 0 ▶ if ($self->report_progress){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->report_progress) 0 0 0
75 0 log_info "Processing bundle $bundleNo";
76 }
77  
78 # Extract variables $document ($doc), so they can be used in eval code
79 0 my $document = $bundle->get_document();
80 0 my $doc = $document;
81 0 my $this = $bundle;
82 0 ▶ if ( $self->bundle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bundle) 0 0 0
83 0 ▶ if ( !eval $self->expand_eval_code($self->bundle) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval $self->expand_eval_code($self->bundle)) 0 0 0
84 0 log_fatal "Eval error: $@";
85 }
86 }
87  
88 # quit if no parameters zone|?tree|?node
89 0 ▶ return if !$self->_args->{_zone};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_args->{"_zone"} 0 0 0
90
91 0 foreach my $zone ( $self->get_selected_zones($bundle->get_all_zones()) ) {
92 0 $self->process_zone($zone, $bundleNo);
93 }
94 0 return;
95 }
96  
97 0 ▶sub process_zone {
Subroutine process_zone: not called CC=11 SCAR=48.8
Pod documented
98 0 my ( $self, $zone, $bundleNo ) = @_;
99  
100 # Extract variables $bundle, $document ($doc), so they can be used in eval code
101 0 my $bundle = $zone->get_bundle();
102 0 my $document = $bundle->get_document();
103 0 my $doc = $document;
104 0 my $this = $zone;
105 0 ▶ if ( $self->zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->zone) 0 0 0
106 0 ▶ if ( !eval $self->expand_eval_code($self->zone) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval $self->expand_eval_code($self->zone)) 0 0 0
107 0 log_fatal "Eval error: $@";
108 }
109 }
110  
111 0 foreach my $layer (qw(a t n p)) {
112 0 ▶ next if !$zone->has_tree($layer);
Branch: 1 branch
BranchTrueFalseTotal
unless $zone->has_tree($layer) 0 0 0
113 0 my $tree = $zone->get_tree($layer);
114 0 $this = $tree;
115 0 ▶ if ( my $code = $self->_args->{"${layer}tree"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->_args->{"${layer}tree"}) 0 0 0
116 0 ▶ if ( !eval $self->expand_eval_code("my \$${layer}tree = \$tree; $code") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval $self->expand_eval_code("my \$${layer}tree = \$tree; $code")) 0 0 0
117 0 log_fatal "Eval error: $@";
118 }
119 }
120 0 ▶ if ( my $code = $self->_args->{"${layer}node"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->_args->{"${layer}node"}) 0 0 0
121 0 ▶ foreach my $node ( $tree->get_descendants({ordered => ($layer =~ /[at]/ ? 1 : 0)}) ) {
Branch: 1 branch
BranchTrueFalseTotal
$layer =~ /[at]/ ? : 0 0 0
122 0 $this = $node;
123 0 ▶ if ( !eval $self->expand_eval_code("my \$${layer}node = \$node; $code;") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval $self->expand_eval_code("my \$${layer}node = \$node; $code;")) 0 0 0
124 0 log_fatal "Eval error: $@";
125 }
126 }
127 }
128 }
129 0 return;
130 }
131  
132 3 1;
133  
134 __END__
« Treex/Core/Run.pm ↑ Summary Treex/Core/Node/InClause.pm »