blib/lib/App/Test/Generator/Analyzer/Return.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 10 / 10 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 88.7% 63 / 71 CC 10 SCAR 18.1
CC
10
Cov
93%
analyze
10.2
__ANON__
1.0
BEGIN
1.0
CRAP 6.1
« lib/App/Test/Generator/Planner/Mock.pm ↑ Summary lib/App/Test/Generator/Mutation/BooleanNegation.pm »
1 package App::Test::Generator::Analyzer::Return;
2  
3 161 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 161 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 161 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 161 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 161 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # --------------------------------------------------
10 # Evidence weights for each detected return pattern.
11 # Higher weights indicate stronger signals that the
12 # detected pattern is the primary return behaviour.
13 # --------------------------------------------------
14 161 Readonly my $WEIGHT_RETURNS_PROPERTY => 20;
15 161 Readonly my $WEIGHT_RETURNS_SELF => 15;
16 161 Readonly my $WEIGHT_RETURNS_CONSTANT => 10;
17  
18 161 our $VERSION = '0.49';
19  
20 =head1 VERSION
21  
22 Version 0.49
23  
24 =head1 DESCRIPTION
25  
26 Analyses the source code of a method and adds evidence to a
27 L<App::Test::Generator::Model::Method> object describing what kind of
28 value the method returns. Evidence is used downstream by
29 L<App::Test::Generator::Model::Method/resolve_return_type> to determine
30 the most likely return type.
31  
32 =head2 new
33  
34 Construct a new Return analyser.
35  
36 my $analyser = App::Test::Generator::Analyzer::Return->new;
37  
38 =head3 Arguments
39  
40 None.
41  
42 =head3 Returns
43  
44 A blessed hashref.
45  
46 =head3 API specification
47  
48 =head4 input
49  
50 {}
51  
52 =head4 output
53  
54 {
55 type => OBJECT,
56 isa => 'App::Test::Generator::Analyzer::Return',
57 }
58  
59 =cut
60  
61 810 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
62 810 my $class = $_[0];
63 810 return bless {}, $class;
64 }
65  
66 =head2 analyze
67  
68 Scan the source code of a method for return patterns and add weighted
69 evidence to the method object. Detects three patterns: returning a
70 property from C<$self>, returning C<$self> itself, and returning a
71 constant literal value.
72  
73 my $analyser = App::Test::Generator::Analyzer::Return->new;
74 $analyser->analyze($method);
75  
76 my $type = $method->resolve_return_type;
77  
78 =head3 Arguments
79  
80 =over 4
81  
82 =item * C<$method>
83  
84 Normally an L<App::Test::Generator::Model::Method> object; evidence is
85 added to it in place via C<add_evidence>. A plain hashref with a
86 C<source> or C<body> key is also accepted defensively (as SchemaExtractor
87 callers may pass one), but in that case no evidence is recorded - only
88 a Model::Method object exposing both C<source()> and C<add_evidence()>
89 actually accumulates evidence.
90  
91 =back
92  
93 =head3 Returns
94  
95 Nothing (undef). All results are communicated via side effects on the
96 C<$method> object.
97  
98 =head3 Notes
99  
100 The interface of this analyser differs from
101 L<App::Test::Generator::Analyzer::ReturnMeta>, which operates on a raw
102 schema hashref. This analyser operates on a C<Model::Method> object
103 directly.
104  
105 =head3 API specification
106  
107 =head4 input
108  
109 {
110 self => { type => OBJECT, isa => 'App::Test::Generator::Analyzer::Return' },
111 method => { type => OBJECT, isa => 'App::Test::Generator::Model::Method' },
112 }
113  
114 =head4 output
115  
116 { type => UNDEF }
117  
118 =cut
119  
120 810 ▶sub analyze {
Subroutine analyze: called CC=10 SCAR=23.2
Pod documented
121 810 my ($self, $method) = @_;
122  
123 # Accept either a Model::Method object or a raw hashref,
124 # since callers in SchemaExtractor pass raw hashrefs
125 my $source = blessed($method) && $method->can('source')
126 ? $method->source()
127 810 ▶ : ($method->{source} // $method->{body} // '');
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($method) && $method->can('source') ? : 805 5 810
Condition62%5/8
builtin::blessed($method) and $method->can("source")
!ll&&!rl&&r
50805
($method->{"source"} // $method->{"body"}) // ""
l!l
50
$method->{"source"} // $method->{"body"}
l!l&&r!l&&!r
410
MC/DC50%1/2
builtin::blessed($method) and $method->can("source")
builtin::blessed($method) $method->can("source")
MC/DC0%0/2
($method->{"source"} // $method->{"body"}) // ""
$method->{"source"} $method->{"body"}
Truth table66%2/3
A and B
A= builtin::blessed($method)B= $method->can("source")
ABresult
0X0
100
111
Truth table66%2/3
A // B
A= $method->{"source"}B= $method->{"body"}
ABresult
1X1
011
000
128  
129 my $add = blessed($method) && $method->can('add_evidence')
130 431 ▶ ? sub { $method->add_evidence(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
131 810 ▶ : sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($method) && $method->can('add_evidence') ? : 805 5 810
Condition66%2/3
builtin::blessed($method) and $method->can("add_evidence")
!ll&&!rl&&r
50805
MC/DC50%1/2
builtin::blessed($method) and $method->can("add_evidence")
builtin::blessed($method) $method->can("add_evidence")
Truth table66%2/3
A and B
A= builtin::blessed($method)B= $method->can("add_evidence")
ABresult
0X0
100
111
132  
133 # --------------------------------------------------
134 # Detect: return $self->{property}
135 # Negative lookahead ensures this does not also match
136 # plain return $self (handled separately below).
137 # Matched with /g so a method with several differing
138 # property-returning branches contributes evidence for
139 # each one found, not just the first in the source.
140 # --------------------------------------------------
141 810 ▶ while($source =~ /return\s+\$self->\{(\w+)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /return\s+\$self->\{(\w+)\}/g) 84 810 894
142 84 $add->(
143 category => 'return',
144 signal => 'returns_property',
145 value => $1,
146 weight => $WEIGHT_RETURNS_PROPERTY,
147 );
148 }
149  
150 # --------------------------------------------------
151 # Detect: return $self
152 # \b after \$self prevents matching variable names that
153 # merely start with "self" (e.g. $self_backup, $selfish);
154 # the negative lookahead then excludes return $self->{...}
155 # which is a property return handled above. Matched with
156 # /g for the same multi-occurrence reason as above.
157 # --------------------------------------------------
158 810 ▶ while($source =~ /return\s+\$self\b(?!->)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /return\s+\$self\b(?!->)/g) 32 810 842
159 32 $add->(
160 category => 'return',
161 signal => 'returns_self',
162 weight => $WEIGHT_RETURNS_SELF,
163 );
164 }
165  
166 # --------------------------------------------------
167 # Detect: return of a constant literal — quoted string,
168 # numeric literal, or undef. All indicate the method
169 # returns a fixed value rather than a computed state.
170 # Matched with /g for the same multi-occurrence reason
171 # as the patterns above.
172 # --------------------------------------------------
173 810 ▶ while($source =~ /return\s+(?:['"\d]|undef\b)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /return\s+(?:['"\d]|undef\b)/g) 320 810 1130
174 320 $add->(
175 category => 'return',
176 signal => 'returns_constant',
177 weight => $WEIGHT_RETURNS_CONSTANT,
178 );
179 }
180  
181 810 return;
182 }
183  
184 =head1 LICENCE AND COPYRIGHT
185  
186 Copyright 2025-2026 Nigel Horne.
187  
188 Usage is subject to GPL2 licence terms.
189 If you use it,
190 please let me know.
191  
192 =cut
193  
194 161 1;
« lib/App/Test/Generator/Planner/Mock.pm ↑ Summary lib/App/Test/Generator/Mutation/BooleanNegation.pm »