blib/lib/App/Test/Generator/SchemaExtractor.pm

filter: Statementstmt 85.4% 3417 / 4000 Branchbran 75.6% 2116 / 2798 Conditioncond 64.4% 953 / 1478 MC/DCmcdc 44.3% 376 / 848 Subroutinesub 92.9% 158 / 170 Podpod 100% 3 / 3 totaltotal 75.5% 7023 / 9297 CC 2065 SCAR 46.8
CC
2065
Cov
78%
_parse_schema_hash
756.0
_apply_pod_input_table
598.0
_analyze_pod
405.6
CRAP 107.3
« lib/App/Test/Generator.pm ↑ Summary bin/fuzz-harness-generator »
1 package App::Test::Generator::SchemaExtractor;
2  
3 157 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 157 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 157 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 157 ▶use App::Test::Generator::Model::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 157 ▶use App::Test::Generator::Analyzer::Complexity;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 157 ▶use App::Test::Generator::Analyzer::Return;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 157 ▶use App::Test::Generator::Analyzer::ReturnMeta;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 157 ▶use App::Test::Generator::Analyzer::SideEffect;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 157 ▶use App::Test::Generator::Provenance qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 SOURCE_VALIDATOR SOURCE_INPUT_SPEC SOURCE_POD
14 SOURCE_CODE SOURCE_SIGNATURE SOURCE_FIELD
15 157 );
16  
17 157 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 157 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 157 ▶use Pod::Simple::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 157 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 157 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 157 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 157 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 157 ▶use Scalar::Util qw(looks_like_number refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 157 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 157 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 157 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 157 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 157 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # --------------------------------------------------
32 # Confidence score thresholds for input and output analysis
33 # --------------------------------------------------
34 157 Readonly my $CONFIDENCE_HIGH_THRESHOLD => 60;
35 157 Readonly my $CONFIDENCE_MEDIUM_THRESHOLD => 35;
36 157 Readonly my $CONFIDENCE_LOW_THRESHOLD => 15;
37  
38 # --------------------------------------------------
39 # Confidence level label strings
40 # --------------------------------------------------
41 157 Readonly my $LEVEL_HIGH => 'high';
42 157 Readonly my $LEVEL_MEDIUM => 'medium';
43 157 Readonly my $LEVEL_LOW => 'low';
44 157 Readonly my $LEVEL_VERY_LOW => 'very_low';
45 157 Readonly my $LEVEL_NONE => 'none';
46  
47 # --------------------------------------------------
48 # Analysis limits
49 # --------------------------------------------------
50 157 Readonly my $DEFAULT_MAX_PARAMETERS => 20;
51 157 Readonly my $DEFAULT_CONFIDENCE_THRESH => 0.5;
52 157 Readonly my $POD_WALK_LIMIT => 200;
53 157 Readonly my $SIGNATURE_TIMEOUT_SECS => 3;
54 157 Readonly my $MEMORY_LIMIT_BYTES => 50_000_000;
55  
56 # --------------------------------------------------
57 # Patterns for rejecting dangerous signature expressions
58 # in _compile_signature_isolated
59 # --------------------------------------------------
60 157 Readonly my $UNSAFE_KEYWORD_RE => qr/\b(?:system|exec|open|fork|require|do|eval|qx)\b/;
61 157 Readonly my $UNSAFE_CHAR_RE => qr/[`{};]/;
62  
63 # --------------------------------------------------
64 # strict_pod levels — integer values stored internally
65 # but referred to by name everywhere in code
66 # --------------------------------------------------
67 157 Readonly my $STRICT_POD_OFF => 0;
68 157 Readonly my $STRICT_POD_WARN => 1;
69 157 Readonly my $STRICT_POD_FATAL => 2;
70  
71 # --------------------------------------------------
72 # Numeric boundary values for test hint generation
73 # --------------------------------------------------
74 157 Readonly my $INT32_MAX => 2_147_483_647;
75  
76 # --------------------------------------------------
77 # Boolean return score thresholds
78 # --------------------------------------------------
79 157 Readonly my $BOOLEAN_SCORE_THRESHOLD => 30;
80  
81 =head1 NAME
82  
83 App::Test::Generator::SchemaExtractor - Extract test schemas from Perl modules
84  
85 =head1 VERSION
86  
87 Version 0.52
88  
89 =cut
90  
91 157 our $VERSION = '0.52';
92  
93 =head1 SYNOPSIS
94  
95 use App::Test::Generator::SchemaExtractor;
96  
97 my $extractor = App::Test::Generator::SchemaExtractor->new(
98 input_file => 'lib/MyModule.pm',
99 output_dir => 'schemas/',
100 verbose => 1,
101 );
102  
103 my $schemas = $extractor->extract_all();
104  
105 =head1 DESCRIPTION
106  
107 App::Test::Generator::SchemaExtractor analyzes Perl modules and generates
108 structured YAML schema files suitable for automated test generation by L<App::Test::Generator>.
109 This module employs
110 static analysis techniques to infer parameter types, constraints, and
111 method behaviors directly from your source code.
112  
113 =head2 Analysis Methods
114  
115 The extractor combines multiple analysis approaches for a comprehensive schema generation:
116  
117 =over 4
118  
119 =item * B<POD Documentation Analysis>
120  
121 Parses embedded documentation to extract:
122 - Parameter names, types, and descriptions from =head2 sections
123 - Method signatures with positional parameters
124 - Return value specifications from "Returns:" sections
125 - Constraints (ranges, patterns, required/optional status)
126 (in a formal C<=head4 Input> spec, C<required =E<gt> 1> means
127 C<optional: 0> and an explicit C<optional> flag wins; a bare list of
128 C<name =E<gt> { ... }> entries with no outer braces contributes only
129 its top-level C<required> flags)
130 - Semantic type detection (email, URL, filename)
131  
132 =item * B<Code Pattern Detection>
133  
134 Analyzes source code using PPI to identify:
135 - Method signatures and parameter extraction patterns
136 - Type validation (ref(), isa(), blessed())
137 - Constraint patterns (length checks, numeric comparisons, regex matches)
138 - Return statement analysis and value type inference
139 - Object instantiation requirements and accessor methods
140  
141 =item * B<Signature Analysis>
142  
143 Examines method declarations for:
144 - Parameter names and positional information
145 - Instance vs. class method detection
146 - Method modifiers (Moose-style before/after/around)
147 - Various parameter declaration styles (shift, @_ assignment)
148  
149 =item * B<Heuristic Inference>
150  
151 Applies Perl-specific domain knowledge:
152 - Boolean return detection from method names (is_*, has_*, can_*)
153 - Common Perl idioms and coding patterns
154 - Context awareness (scalar vs list, wantarray usage)
155 - Object-oriented patterns (constructors, accessors, chaining)
156  
157 =back
158  
159 =head2 Generated Schema Structure
160  
161 The extracted schemas follow this YAML structure:
162  
163 function: method_name
164 module: Package::Name
165 input:
166 param1:
167 type: string
168 min: 3
169 max: 50
170 optional: 0
171 position: 0
172 param2:
173 type: integer
174 min: 0
175 max: 100
176 optional: 1
177 position: 1
178 output:
179 type: boolean
180 value: 1
181 new: Package::Name # if object instantiation required
182 config:
183 test_empty: 1
184 test_nuls: 0
185 test_undef: 0
186 test_non_ascii: 0
187  
188 =head2 Advanced Detection Capabilities
189  
190 =over 4
191  
192 =item * B<Accessor Method Detection>
193  
194 Automatically identifies getter, setter, and combined accessor methods
195 by analyzing common patterns like C<return $self-E<gt>{property}> and
196 C<$self-E<gt>{property} = $value>.
197  
198 =item * B<Params::Get Integration>
199  
200 Recognises parameters extracted via C<Params::Get::get_params('key', \@_)>,
201 treating the quoted key as a named parameter equivalent to a traditional
202 C<my ($self, $key) = @_> signature. This prevents false positives from
203 C<--strict-pod> when the method body never declares an explicit C<$key>
204 variable.
205  
206 =item * B<Direct-Index Self Style>
207  
208 Recognises C<my $self = $_[0]> as a valid method-invocant pattern. Parameters
209 at C<$_[1]>, C<$_[2]>, etc. are extracted as positional parameters. Without
210 this, the signature fallback would incorrectly pick up C<my (...) = @_> from
211 inner closures defined in the method body and treat those variables as the
212 outer method's parameters.
213  
214 =item * B<Boolean Return Inference>
215  
216 Detects boolean-returning methods through multiple signals:
217 - Method name patterns (is_*, has_*, can_*)
218 - Return patterns (consistent 1/0 returns)
219 - POD descriptions ("returns true on success")
220 - Ternary operators with boolean results
221  
222 =item * B<Context Awareness>
223  
224 Identifies methods that use C<wantarray> and can return different
225 values in scalar vs list context.
226  
227 =item * B<Object Lifecycle Management>
228  
229 Detects instance methods requiring object instantiation and
230 automatically adds the C<new> field to schemas.
231  
232 =item * B<Enhanced Object Detection>
233  
234 The extractor includes sophisticated object detection capabilities that go beyond simple instance method identification:
235  
236 =over 4
237  
238 =item * B<Factory Method Recognition>
239  
240 Automatically identifies methods that create and return object instances, such as methods named C<create_*>, C<make_*>, C<build_*>, or C<get_*>. Factory methods are correctly classified as class methods that don't require pre-existing objects for testing.
241  
242 =item * B<Singleton Pattern Detection>
243  
244 Recognizes singleton patterns through multiple signals: method names like C<instance> or C<get_instance>, static variables holding instance references, lazy initialization patterns (C<$instance ||= new()>), and consistent return of the same instance variable.
245  
246 =item * B<Constructor Parameter Analysis>
247  
248 Examines C<new> methods to determine required and optional parameters, validation requirements, and default values. This enables test generators to provide appropriate constructor arguments when object instantiation is needed.
249  
250 A constructor that rejects an empty arrayref, or calls methods on (or
251 checks C<blessed> on) the elements of a required arrayref parameter, needs
252 live objects that a YAML fixture cannot hold. Its C<new> schema gets a
253 C<skip> reason such as C<needs a non-empty arrayref of live objects in
254 'databases'>, and its instance methods are emitted with C<new: ~> so
255 harnesses skip them.
256  
257 =item * B<Inheritance Relationship Handling>
258  
259 Detects parent classes through C<use parent>, C<use base>, and C<@ISA> declarations. Identifies when methods use C<SUPER::> calls and determines whether the current class or a parent class constructor should be used for object instantiation.
260  
261 =item * B<External Object Dependency Detection>
262  
263 Identifies when methods create or depend on objects from other classes, enabling proper test setup with mock objects or real dependencies.
264  
265 =back
266  
267 These enhancements ensure that generated test schemas accurately reflect the object-oriented structure of the code, leading to more meaningful and effective test generation.
268  
269 =back
270  
271 =head2 Confidence Scoring
272  
273 Each generated schema includes detailed confidence assessments:
274  
275 =over 4
276  
277 =item * B<High Confidence>
278  
279 Multiple independent analysis sources converge on consistent,
280 well-constrained parameters with explicit validation logic and
281 comprehensive documentation.
282  
283 =item * B<Medium Confidence>
284  
285 Reasonable evidence from code patterns or partial documentation,
286 but may lack comprehensive constraints or have some ambiguities.
287  
288 =item * B<Low Confidence>
289  
290 Minimal evidence - primarily based on naming conventions,
291 default assumptions, or single-source analysis.
292  
293 =item * B<Very Low Confidence>
294  
295 Barely any detectable signals - schema should be thoroughly
296 reviewed before use in test generation.
297  
298 =back
299  
300 =head2 Use Cases
301  
302 =over 4
303  
304 =item * B<Automated Test Generation>
305  
306 Generate comprehensive test suites with L<App::Test::Generator> using
307 extracted schemas as input. The schemas provide the necessary structure
308 for generating both positive and negative test cases.
309  
310 =item * B<API Documentation Generation>
311  
312 Supplement existing documentation with automatically inferred interface
313 specifications, parameter requirements, and return types.
314  
315 =item * B<Code Quality Assessment>
316  
317 Identify methods with poor documentation, inconsistent parameter handling,
318 or unclear interfaces that may benefit from refactoring.
319  
320 =item * B<Refactoring Assistance>
321  
322 Detect method dependencies, object instantiation requirements, and
323 parameter usage patterns to inform refactoring decisions.
324  
325 =item * B<Legacy Code Analysis>
326  
327 Quickly understand the interface contracts of legacy Perl codebases
328 without extensive manual code reading.
329  
330 =back
331  
332 =head2 Integration with Testing Ecosystem
333  
334 The generated schemas are specifically designed to work with the
335 L<App::Test::Generator> ecosystem:
336  
337 # Extract schemas from your module
338 my $extractor = App::Test::Generator::SchemaExtractor->new(...);
339 my $schemas = $extractor->extract_all();
340  
341 # Use with test generator (typically as separate steps)
342 # fuzz-harness-generator -r schemas/method_name.yml
343  
344 =head2 Limitations and Considerations
345  
346 =over 4
347  
348 =item * B<Dynamic Code Patterns>
349  
350 Highly dynamic code (string evals, AUTOLOAD, symbolic references)
351 may not be fully detected by static analysis.
352  
353 =item * B<Complex Validation Logic>
354  
355 Sophisticated validation involving multiple parameters or external
356 dependencies may require manual schema refinement.
357  
358 =item * B<Confidence Heuristics>
359  
360 Confidence scores are based on heuristics and should be reviewed
361 by developers familiar with the codebase.
362  
363 =item * B<Perl Idiom Recognition>
364  
365 Some Perl-specific idioms may require custom pattern recognition
366 beyond the built-in detectors.
367  
368 =item * B<Documentation Dependency>
369  
370 Analysis quality improves significantly with comprehensive POD
371 documentation following consistent patterns.
372  
373 =item * B<Multi-Package Files>
374  
375 Only the first package in a file is analysed. A file that declares
376 additional packages, such as a small error class, is still extracted, but
377 its schemas carry a C<skip> reason so generated harnesses are not run
378 against it. Perl 5.38 C<class> blocks are not detected in such a file:
379 the C<class>/C<method> scan reads the whole source, so it runs only when
380 the file declares at most one C<package>.
381  
382 =back
383  
384 =head2 Best Practices for Optimal Results
385  
386 =over 4
387  
388 =item * B<Comprehensive POD Documentation>
389  
390 Write detailed POD with explicit parameter documentation using
391 consistent patterns like C<$param - type (constraints), description>.
392  
393 =item * B<Consistent Coding Patterns>
394  
395 Use consistent parameter validation patterns and method signatures
396 throughout your codebase.
397  
398 =item * B<Schema Review Process>
399  
400 Review and refine automatically generated schemas, particularly
401 those with low confidence scores.
402  
403 =item * B<Descriptive Naming>
404  
405 Use descriptive method and parameter names that clearly indicate
406 purpose and expected types.
407  
408 =item * B<Progressive Enhancement>
409  
410 Start with automatically generated schemas and progressively
411 refine them based on test results and code understanding.
412  
413 =back
414  
415 The module is particularly valuable for large codebases where manual schema
416 creation would be prohibitively time-consuming, and for maintaining test
417 coverage as code evolves through continuous integration pipelines.
418  
419 =head2 Advanced Type Detection
420  
421 The schema extractor includes enhanced type detection capabilities that identify specialized Perl types beyond basic strings and integers.
422 L<DateTime> and L<Time::Piece> objects are detected through isa() checks and method call patterns, while date strings (ISO 8601, YYYY-MM-DD) and UNIX timestamps are recognized through regex validation and numeric range checks.
423 File handles and file paths are identified via I/O operations and file test operators, coderefs are detected through ref() checks and invocation patterns, and enum-like parameters are extracted from validation code including regex patterns (C</^(a|b|c)$/>), hash lookups, grep statements, and if/elsif chains.
424 These detected types are preserved in the generated YAML schemas with appropriate semantic annotations, enabling test generators to create more accurate and meaningful test cases.
425  
426 =head3 Example Advanced Type Schema
427  
428 For a method like:
429  
430 sub process_event {
431 my ($self, $timestamp, $status, $callback) = @_;
432 croak unless $timestamp > 1000000000;
433 croak unless $status =~ /^(active|pending|complete)$/;
434 croak unless ref($callback) eq 'CODE';
435 $callback->($timestamp, $status);
436 }
437  
438 The extractor generates:
439  
440 ---
441 function: process_event
442 module: MyModule
443 input:
444 timestamp:
445 type: integer
446 # min: 0
447 # max: 2147483647
448 position: 0
449 _note: Unix timestamp
450 semantic: unix_timestamp
451 status:
452 type: string
453 enum:
454 - active
455 - pending
456 - complete
457 position: 1
458 _note: 'Must be one of: active, pending, complete'
459 callback:
460 type: coderef
461 position: 2
462 _note: 'CODE reference - provide sub { } in tests'
463  
464 =head1 RELATIONSHIP DETECTION
465  
466 The schema extractor detects relationships and dependencies between parameters,
467 enabling more sophisticated validation and test generation.
468  
469 =head2 Relationship Types
470  
471 =over 4
472  
473 =item * B<mutually_exclusive>
474  
475 Parameters that cannot be used together.
476  
477 die if $file && $content; # Can't specify both
478  
479 Generated schema:
480  
481 relationships:
482 - type: mutually_exclusive
483 params: [file, content]
484 description: Cannot specify both file and content
485  
486 =item * B<required_group>
487  
488 At least one parameter from the group must be specified (OR logic).
489  
490 die unless $id || $name; # Must provide one
491  
492 Generated schema:
493  
494 relationships:
495 - type: required_group
496 params: [id, name]
497 logic: or
498 description: Must specify either id or name
499  
500 =item * B<conditional_requirement>
501  
502 If one parameter is specified, another becomes required (IF-THEN logic).
503  
504 die if $async && !$callback; # async requires callback
505  
506 Generated schema:
507  
508 relationships:
509 - type: conditional_requirement
510 if: async
511 then_required: callback
512 description: When async is specified, callback is required
513  
514 =item * B<dependency>
515  
516 One parameter depends on another being present.
517  
518 die "Port requires host" if $port && !$host;
519  
520 Generated schema:
521  
522 relationships:
523 - type: dependency
524 param: port
525 requires: host
526 description: port requires host to be specified
527  
528 =item * B<value_constraint>
529  
530 Specific value requirements between parameters.
531  
532 die if $ssl && $port != 443; # ssl requires port 443
533  
534 Generated schema:
535  
536 relationships:
537 - type: value_constraint
538 if: ssl
539 then: port
540 operator: ==
541 value: 443
542 description: When ssl is specified, port must equal 443
543  
544 =item * B<value_conditional>
545  
546 Parameter required when another has a specific value.
547  
548 die if $mode eq 'secure' && !$key;
549  
550 Generated schema:
551  
552 relationships:
553 - type: value_conditional
554 if: mode
555 equals: secure
556 then_required: key
557 description: When mode equals 'secure', key is required
558  
559 =back
560  
561 =head2 Default Value Extraction
562  
563 The extractor comprehensively extracts default values from both code and POD documentation:
564  
565 =head3 Code Pattern Recognition
566  
567 Extracts defaults from multiple Perl idioms:
568  
569 =over 4
570  
571 =item * Logical OR operator: C<$param = $param || 'default'>
572  
573 =item * Defined-or operator: C<$param //= 'default'>
574  
575 =item * Ternary operator: C<$param = defined $param ? $param : 'default'>
576  
577 =item * Unless conditional: C<$param = 'default' unless defined $param>
578  
579 =item * Chained defaults: C<$param = $param || $self->{_default} || 'fallback'>
580  
581 =item * Multi-line patterns: C<$param = {} unless $param>
582  
583 =back
584  
585 =head3 POD Pattern Recognition
586  
587 Extracts defaults from documentation:
588  
589 =over 4
590  
591 =item * Standard format: C<Default: 'value'>
592  
593 =item * Alternative format: C<Defaults to: 'value'>
594  
595 =item * Inline format: C<Optional, default: 'value'>
596  
597 =item * Parameter lists: C<$param - type, default 'value'>
598  
599 =back
600  
601 =head3 Value Processing
602  
603 Properly handles:
604  
605 =over 4
606  
607 =item * String literals with quotes and escape sequences
608  
609 =item * Numeric values (integers and floats)
610  
611 =item * Boolean values (true/false converted to 1/0)
612  
613 =item * Empty data structures ([] and {})
614  
615 =item * Special values (undef, __PACKAGE__)
616  
617 =item * Complex expressions (preserved as-is when unevaluatable)
618  
619 =item * Quote operators (q{}, qq{}, qw{})
620  
621 =back
622  
623 =head3 Type Inference
624  
625 When a parameter has a default value but no explicit type annotation,
626 the type is automatically inferred from the default:
627  
628 $options = {} # inferred as hashref
629 $items = [] # inferred as arrayref
630 $count = 42 # inferred as integer
631 $ratio = 3.14 # inferred as number
632 $enabled = 1 # inferred as boolean
633  
634 =head2 Context-Aware Return Analysis
635  
636 The extractor provides comprehensive analysis of method return behavior,
637 including context sensitivity, error handling conventions, and method chaining patterns.
638  
639 When a method's POD contains a C<=head4 Output> block in
640 L<Params::Validate::Strict> schema format, the C<type> declared there is
641 used as the authoritative output type and takes precedence over all
642 heuristic code analysis:
643  
644 =head4 Output
645  
646 {
647 type => 'hashref',
648 }
649  
650 This is the recommended way to document methods whose return type would
651 otherwise be misidentified (e.g. a method that returns C<$self-E<gt>{cache}>
652 where the cache happens to hold a hashref).
653  
654 Using parentheses as the outer container emits C<type: array>, indicating a
655 list-returning method. L<App::Test::Generator> 0.39+ (with L<Test::Returns>
656 0.03+) captures these results in list context automatically:
657  
658 =head4 Output
659  
660 (
661 {
662 type => 'hashref',
663 },
664 ...
665 )
666  
667 =head3 List vs Scalar Context Detection
668  
669 Automatically detects methods that return different values based on calling context:
670  
671 sub get_items {
672 my $self = $_[0];
673 return wantarray ? @items : scalar(@items);
674 }
675  
676 Detection captures:
677  
678 =over 4
679  
680 =item * C<_context_aware> flag - Method uses wantarray
681  
682 =item * C<_list_context> - Type returned in list context (e.g., 'array')
683  
684 =item * C<_scalar_context> - Type returned in scalar context (e.g., 'integer')
685  
686 =back
687  
688 Recognizes both ternary operator patterns and conditional return patterns.
689  
690 =head3 Void Context Methods
691  
692 Identifies methods that don't return meaningful values:
693  
694 =over 4
695  
696 =item * Setters (C<set_*> methods)
697  
698 =item * Mutators (C<add_*, remove_*, delete_*, clear_*, reset_*, update_*>)
699  
700 =item * Loggers (C<log, debug, warn, error, info>)
701  
702 =item * Methods with only empty returns
703  
704 =back
705  
706 Example:
707  
708 sub set_name {
709 my ($self, $name) = @_;
710 $self->{name} = $name;
711 return; # Void context
712 }
713  
714 Sets C<_void_context> flag and C<type =E<gt> 'void'>.
715  
716 =head3 Method Chaining Detection
717  
718 Identifies chainable methods that return C<$self> for fluent interfaces:
719  
720 sub set_width {
721 my ($self, $width) = @_;
722 $self->{width} = $width;
723 return $self; # Chainable
724 }
725  
726 Detection provides:
727  
728 =over 4
729  
730 =item * C<_returns_self> - Returns invocant for chaining
731  
732 =item * C<class> - The class name being returned
733  
734 =back
735  
736 Also detects chaining documentation in POD (keywords: "chainable", "fluent interface",
737 "returns self", "method chaining").
738  
739 =head3 Error Return Conventions
740  
741 Analyzes how methods signal errors:
742  
743 B<Pattern Detection:>
744  
745 =over 4
746  
747 =item * C<undef_on_error> - Explicit C<return undef if/unless condition>
748  
749 =item * C<implicit_undef> - Bare C<return if/unless condition>
750  
751 =item * C<empty_list> - C<return ()> for list context errors
752  
753 =item * C<zero_on_error> - Returns 0/false for boolean error indication
754  
755 =item * C<exception_handling> - Uses eval blocks with error checking
756  
757 =back
758  
759 B<Example Analysis:>
760  
761 sub fetch_user {
762 my ($self, $id) = @_;
763  
764 return undef unless $id; # undef_on_error
765 return undef if $id < 0; # undef_on_error
766  
767 return $self->{users}{$id};
768 }
769  
770 Results in:
771  
772 _error_return: 'undef'
773 _success_failure_pattern: 1
774 _error_handling: {
775 undef_on_error: ['$id', '$id < 0']
776 }
777  
778 B<Success/Failure Pattern:>
779  
780 Methods that return different types for success vs. failure are flagged with
781 C<_success_failure_pattern>. Common patterns:
782  
783 =over 4
784  
785 =item * Returns value on success, undef on failure
786  
787 =item * Returns true on success, false on failure
788  
789 =item * Returns data on success, empty list on failure
790  
791 =back
792  
793 =head3 Success Indicator Detection
794  
795 Methods that always return true (typically for side effects):
796  
797 sub update_status {
798 my ($self, $status) = @_;
799 $self->{status} = $status;
800 return 1; # Success indicator
801 }
802  
803 Sets C<_success_indicator> flag when method consistently returns 1.
804  
805 =head3 Schema Output
806  
807 Enhanced return analysis adds these fields to method schemas:
808  
809 output:
810 type: boolean # Inferred return type
811 _context_aware: 1 # Uses wantarray
812 _list_context:
813 type: array
814 _scalar_context:
815 type: integer
816 _returns_self: 1 # Returns $self
817 _void_context: 1 # No meaningful return
818 _success_indicator: 1 # Always returns true
819 _error_return: undef # How errors are signaled
820 _success_failure_pattern: 1 # Mixed return types
821 _error_handling: # Detailed error patterns
822 undef_on_error: [...]
823 exception_handling: 1
824  
825 This comprehensive analysis enables:
826  
827 =over 4
828  
829 =item * Better test generation (testing both contexts, error paths)
830  
831 =item * Documentation generation (clear error conventions)
832  
833 =item * API design validation (consistent error handling)
834  
835 =item * Contract specification (precise return behavior)
836  
837 =back
838  
839 =head2 Example
840  
841 For a method like:
842  
843 sub connect {
844 my ($self, $host, $port, $ssl, $file, $content) = @_;
845  
846 die if $file && $content; # mutually exclusive
847 die unless $host || $file; # required group
848 die "Port requires host" if $port && !$host; # dependency
849 die if $ssl && $port != 443; # value constraint
850  
851 # ... connection logic
852 }
853  
854 The extractor generates:
855  
856 relationships:
857 - type: mutually_exclusive
858 params: [file, content]
859 description: Cannot specify both file and content
860 - type: required_group
861 params: [host, file]
862 logic: or
863 description: Must specify either host or file
864 - type: dependency
865 param: port
866 requires: host
867 description: port requires host to be specified
868 - type: value_constraint
869 if: ssl
870 then: port
871 operator: ==
872 value: 443
873 description: When ssl is specified, port must equal 443
874  
875 =head1 MODERN PERL FEATURES
876  
877 This module adds support for:
878  
879 =head2 Subroutine Signatures (Perl 5.20+)
880  
881 sub connect($host, $port = 3306, %options) {
882 ...
883 }
884  
885 Extracts: required params, optional params with defaults, slurpy params
886  
887 =head2 Type Constraints (Perl 5.36+)
888  
889 sub calculate($x :Int, $y :Num) {
890 ...
891 }
892  
893 Recognizes: Int, Num, Str, Bool, ArrayRef, HashRef, custom classes
894  
895 =head3 Subroutine Attributes
896  
897 sub get_value :lvalue :Returns(Int) {
898 ...
899 }
900  
901 Detects: :lvalue, :method, :Returns(Type), custom attributes
902  
903 =head2 Postfix Dereferencing (Perl 5.20+)
904  
905 my @array = $arrayref->@*;
906 my %hash = $hashref->%*;
907 my @slice = $arrayref->@[1,3,5];
908  
909 Tracks usage of modern dereferencing syntax
910  
911 =head2 Field Declarations (Perl 5.38+)
912  
913 field $host :param = 'localhost';
914 field $port :param(port_number) = 3306;
915 field $logger :param :isa(Log::Any);
916  
917 Extracts fields and maps them to parameters
918  
919 =head2 Modern Perl Features Support
920  
921 The schema extractor supports modern Perl syntax introduced in versions 5.20, 5.36, and 5.38+.
922  
923 =head3 Subroutine Signatures (Perl 5.20+)
924  
925 Automatically extracts parameters from native Perl signatures:
926  
927 use feature 'signatures';
928  
929 sub connect($host, $port = 3306, $database = undef) {
930 ...
931 }
932  
933 Extracted schema includes:
934  
935 =over 4
936  
937 =item * Parameter positions
938  
939 =item * Optional vs required parameters
940  
941 =item * Default values from signature
942  
943 =item * Slurpy parameters (@array, %hash)
944  
945 =back
946  
947 B<Example:>
948  
949 # Signature with defaults
950 sub process($file, %options) { ... }
951  
952 # Extracts:
953 # $file: position 0, required
954 # %options: named: 1, optional, slurpy hash
955  
956 =head3 Type Constraints in Signatures (Perl 5.36+)
957  
958 Recognizes type constraints in signature parameters:
959  
960 sub calculate($x :Int, $y :Num, $name :Str = "result") {
961 return $x + $y;
962 }
963  
964 Supported constraint types:
965  
966 =over 4
967  
968 =item * C<:Int, :Integer> -> integer
969  
970 =item * C<:Num, :Number> -> number
971  
972 =item * C<:Str, :String> -> string
973  
974 =item * C<:Bool, :Boolean> -> boolean
975  
976 =item * C<:ArrayRef, :Array> -> arrayref
977  
978 =item * C<:HashRef, :Hash> -> hashref
979  
980 =item * C<:ClassName> -> object with isa constraint
981  
982 =back
983  
984 Type constraints are combined with defaults when both are present.
985  
986 =head3 Subroutine Attributes
987  
988 Extracts and documents subroutine attributes:
989  
990 sub get_value :lvalue {
991 my $self = shift;
992 return $self->{value};
993 }
994  
995 sub calculate :Returns(Int) :method {
996 my ($self, $x, $y) = @_;
997 return $x + $y;
998 }
999  
1000 Recognized attributes stored in C<_attributes> field:
1001  
1002 =over 4
1003  
1004 =item * C<:lvalue> - Method can be assigned to
1005  
1006 =item * C<:method> - Explicitly marked as method
1007  
1008 =item * C<:Returns(Type)> - Declares return type
1009  
1010 =item * Custom attributes with values: C<:MyAttr(value)>
1011  
1012 =back
1013  
1014 =head3 Postfix Dereferencing (Perl 5.20+)
1015  
1016 Detects usage of postfix dereferencing syntax:
1017  
1018 use feature 'postderef';
1019  
1020 sub process_array {
1021 my ($self, $arrayref) = @_;
1022 my @array = $arrayref->@*; # Array dereference
1023 my @slice = $arrayref->@[1,3,5]; # Array slice
1024 return @array;
1025 }
1026  
1027 sub process_hash {
1028 my ($self, $hashref) = @_;
1029 my %hash = $hashref->%*; # Hash dereference
1030 return keys %hash;
1031 }
1032  
1033 Tracked features stored in C<_modern_features>:
1034  
1035 =over 4
1036  
1037 =item * C<array_deref> - Uses C<-E<gt>@*>
1038  
1039 =item * C<hash_deref> - Uses C<-E<gt>%*>
1040  
1041 =item * C<scalar_deref> - Uses C<-E<gt>$*>
1042  
1043 =item * C<code_deref> - Uses C<-E<gt>&*>
1044  
1045 =item * C<array_slice> - Uses C<-E<gt>@[...]>
1046  
1047 =item * C<hash_slice> - Uses C<-E<gt>%{...}>
1048  
1049 =back
1050  
1051 =head3 Field Declarations (Perl 5.38+)
1052  
1053 Extracts field declarations from class syntax and maps them to method parameters:
1054  
1055 use feature 'class';
1056  
1057 class DatabaseConnection {
1058 field $host :param = 'localhost';
1059 field $port :param = 3306;
1060 field $username :param(user);
1061 field $password :param;
1062 field $logger :param :isa(Log::Any);
1063  
1064 method connect() {
1065 # Fields available as instance variables
1066 }
1067 }
1068  
1069 Field attributes:
1070  
1071 =over 4
1072  
1073 =item * C<:param> - Field is a constructor parameter (uses field name)
1074  
1075 =item * C<:param(name)> - Field maps to parameter with different name
1076  
1077 =item * C<:isa(Class)> - Type constraint for the field
1078  
1079 =item * Default values in field declarations
1080  
1081 =back
1082  
1083 Extracted schema includes both field information in C<_fields> and merged parameter
1084 information in C<input>, allowing proper validation of class constructors.
1085  
1086 =head3 Mixed Modern and Traditional Syntax
1087  
1088 The extractor handles code that mixes modern and traditional syntax:
1089  
1090 sub modern($x, $y = 5) {
1091 # Modern signature with default
1092 }
1093  
1094 sub traditional {
1095 my ($self, $x, $y) = @_;
1096 $y //= 5; # Traditional default in code
1097 # Both extract same parameter information
1098 }
1099  
1100 Priority order for parameter information:
1101  
1102 =over 4
1103  
1104 =item 1. Signature declarations (highest priority)
1105  
1106 =item 2. Field declarations (for class methods)
1107  
1108 =item 3. POD documentation
1109  
1110 =item 4. Code analysis (lowest priority)
1111  
1112 =back
1113  
1114 This ensures that explicit declarations in signatures take precedence over
1115 inferred information from code analysis.
1116  
1117 =head3 Backwards Compatibility
1118  
1119 All modern Perl feature detection is optional and automatic:
1120  
1121 =over 4
1122  
1123 =item * Traditional C<sub> declarations continue to work
1124  
1125 =item * Code without modern features extracts parameters as before
1126  
1127 =item * Modern features are additive - they enhance rather than replace existing extraction
1128  
1129 =item * Each parameter carries one C<_source> value -- C<validator>, C<input_spec>, C<pod>, C<code>, C<signature> or C<field> -- which the harness uses for its type-checking decisions
1130  
1131 =item * Each constraint that can come from more than one place also carries its own provenance in C<_constraint_source>, a hashref keyed by C<min>, C<max>, C<memberof>, C<matches>, C<nomatch> and C<notmemberof>. A minimum documented in POD and a maximum enforced by the code on the same field therefore stay separate decisions: the documented one is a hint for valid data, the enforced one is a check
1132  
1133 =back
1134  
1135 =head2 _yamltest_hints
1136  
1137 Each method schema returned by L</extract_all> now optionally includes a
1138 C<_yamltest_hints> key, which provides guidance for automated test generation
1139 based on the code analysis.
1140  
1141 This is intended to help L<App::Test::Generator> create meaningful tests,
1142 including boundary and invalid input cases, without manually specifying them.
1143  
1144 The structure is a hashref with the following keys:
1145  
1146 =over 4
1147  
1148 =item * boundary_values
1149  
1150 An arrayref of numeric values that represent boundaries detected from
1151 comparisons in the code. These are derived from literals in statements
1152 like C<$x < 0> or C<$y >= 255>. The generator can use these to create
1153 boundary tests.
1154  
1155 Example:
1156  
1157 _yamltest_hints:
1158 boundary_values: [0, 1, 100, 255]
1159  
1160 =item * invalid_inputs
1161  
1162 An arrayref of values that are likely to be rejected by the method,
1163 based on checks like C<defined>, empty strings, or numeric validations.
1164  
1165 Example:
1166  
1167 _yamltest_hints:
1168 invalid_inputs: [undef, '', -1]
1169  
1170 =item * equivalence_classes
1171  
1172 An arrayref intended to capture detected equivalence classes or patterns
1173 among inputs. Currently this is empty by default, but future enhancements
1174 may populate it based on detected input groupings.
1175  
1176 Example:
1177  
1178 _yamltest_hints:
1179 equivalence_classes: []
1180  
1181 =back
1182  
1183 =head3 Usage
1184  
1185 When calling C<extract_all>, each method schema will include
1186 C<_yamltest_hints> if any hints were detected:
1187  
1188 my $schemas = $extractor->extract_all;
1189 my $hints = $schemas->{example_method}->{_yamltest_hints};
1190  
1191 You can then feed these hints into automated test generators to produce
1192 negative tests, boundary tests, and parameter-specific test cases.
1193  
1194 =head3 Notes
1195  
1196 =over 4
1197  
1198 =item * Hints are inferred heuristically from code and validation statements.
1199  
1200 =item * Not all inputs are guaranteed to be detected; the feature is additive
1201 and will never remove information from the schema.
1202  
1203 =item * Currently, equivalence classes are not populated, but the field exists
1204 for future extension.
1205  
1206 =item * Boundary and invalid input hints are deduplicated to avoid repeated
1207 test values.
1208  
1209 =back
1210  
1211 =head3 Examples
1212  
1213 Given a method like:
1214  
1215 sub example {
1216 my ($x) = @_;
1217 die "negative" if $x < 0;
1218 return unless defined($x);
1219 return $x * 2;
1220 }
1221  
1222 After running:
1223  
1224 my $extractor = App::Test::Generator::SchemaExtractor->new(
1225 input_file => 'TestHints.pm',
1226 output_dir => '/tmp',
1227 quiet => 1,
1228 );
1229  
1230 my $schemas = $extractor->extract_all;
1231  
1232 The schema for the method "example" will include:
1233  
1234 $schemas->{example} = {
1235 function => 'example',
1236 _confidence => {
1237 input => 'unknown',
1238 output => 'unknown',
1239 },
1240 input => {
1241 x => {
1242 type => 'scalar',
1243 optional => 0,
1244 }
1245 },
1246 output => {
1247 type => 'scalar',
1248 },
1249 _yamltest_hints => {
1250 boundary_values => [0, 1],
1251 invalid_inputs => [undef, -1],
1252 equivalence_classes => [],
1253 },
1254 _notes => '...',
1255 _analysis => {
1256 input_confidence => 'low',
1257 output_confidence => 'unknown',
1258 confidence_factors => {
1259 input => {...},
1260 output => {...},
1261 },
1262 overall_confidence => 'low',
1263 },
1264 _fields => {},
1265 _modern_features => {},
1266 _attributes => {},
1267 };
1268  
1269 =head1 METHODS
1270  
1271 =head2 new
1272  
1273 Construct a new SchemaExtractor for a given Perl source file.
1274  
1275 my $extractor = App::Test::Generator::SchemaExtractor->new(
1276 input_file => 'lib/MyModule.pm', # Required
1277 output_dir => 'schemas/', # Optional - only needed if writing schemas
1278 verbose => 1, # Default: 0
1279 include_private => 1, # Default: 0
1280 max_parameters => 50, # Default: 20
1281 confidence_threshold => 0.7, # Default: 0.5
1282 strict_pod => 0|1|2, # Default: 0 (off)
1283 allow_signature_exec => 1, # Default: 0 (off)
1284 );
1285  
1286 =head3 Arguments
1287  
1288 =over 4
1289  
1290 =item * C<$input_file>
1291  
1292 Path to the Perl source file to analyse. Required. Must exist on disk.
1293  
1294 =item * C<output_dir>
1295  
1296 Directory to write generated schema YAML files. Optional - only
1297 required if C<_write_schema> will be called. Callers passing
1298 C<no_write =E<gt> 1> to C<extract_all> do not need to supply it.
1299  
1300 =item * C<verbose>
1301  
1302 Print progress messages to stdout during analysis. Optional, default 0.
1303  
1304 =item * C<include_private>
1305  
1306 Include methods whose names begin with C<_> in the analysis. Optional,
1307 default 0. Methods whose name begins with C<_new>, C<_init>, or
1308 C<_build> are always included regardless of this setting (a prefix
1309 match, so e.g. C<_build_attribute> and C<_init_logger> qualify too,
1310 matching common Moose builder/initializer naming conventions).
1311  
1312 =item * C<max_parameters>
1313  
1314 Safety limit on the number of parameters analysed per method to prevent
1315 runaway processing on pathological code. Optional, default 20.
1316  
1317 =item * C<confidence_threshold>
1318  
1319 Minimum confidence score (0.0-1.0) below which a schema is marked with
1320 C<_low_confidence =E<gt> 1>. Optional, default 0.5.
1321  
1322 =item * C<strict_pod>
1323  
1324 Controls POD/code agreement validation. C<0> disables validation,
1325 C<1> emits warnings, C<2> croaks on first disagreement. Also accepts
1326 the strings C<off>, C<warn>, and C<fatal>. Optional, default 0.
1327  
1328 =item * C<allow_signature_exec>
1329  
1330 Opt-in flag allowing extraction of parameter types from a
1331 L<Type::Params> C<signature_for()> declaration. This requires actually
1332 running the C<signature_for> expression (sliced from the target
1333 module's own source) in a forked C<perl -T> process, since
1334 L<Type::Params> types are runtime objects that cannot be introspected
1335 statically. Every other extraction path in this module is static
1336 (L<PPI>-only) analysis that never executes any of the target module's
1337 code; this is the one exception. Optional, default 0 (the
1338 C<signature_for> path is silently skipped, with a warning under
1339 C<verbose>, when off). Only enable this for modules whose code you
1340 already trust enough to execute.
1341  
1342 =back
1343  
1344 =head3 Returns
1345  
1346 A blessed hashref. Croaks if C<input_file> is missing or does not
1347 exist on disk.
1348  
1349 =head3 Side effects
1350  
1351 Reads and parses the input file using L<PPI> at construction time.
1352  
1353 =head3 API specification
1354  
1355 =head4 input
1356  
1357 {
1358 input_file => { type => SCALAR },
1359 output_dir => { type => SCALAR, optional => 1 },
1360 verbose => { type => SCALAR, optional => 1 },
1361 include_private => { type => SCALAR, optional => 1 },
1362 max_parameters => { type => SCALAR, optional => 1 },
1363 confidence_threshold => { type => SCALAR, optional => 1 },
1364 strict_pod => { type => SCALAR, optional => 1 },
1365 allow_signature_exec => { type => SCALAR, optional => 1 },
1366 }
1367  
1368 =head4 output
1369  
1370 {
1371 type => OBJECT,
1372 isa => 'App::Test::Generator::SchemaExtractor',
1373 }
1374  
1375 =cut
1376  
1377 1159 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
1378 1159 my $class = shift;
1379  
1380 # Handle hash or hashref arguments
1381 1159 ▶ my $params = Params::Get::get_params('input_file', @_) || {};
Condition50%1/2
Params::Get::get_params('input_file', @_) || {}
l!l
11570
MC/DC0%0/1
Params::Get::get_params('input_file', @_) || {}
Params::Get::get_params('input_file', @_)
Truth table50%1/2
A
A= Params::Get::get_params('input_file', @_)
Aresult
11
00
1382  
1383 1157 ▶ croak(__PACKAGE__, ': input_file required') unless exists $params->{input_file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{"input_file"} 0 1157 1157
1384  
1385 my $self = {
1386 input_file => $params->{input_file},
1387 # output_dir is optional — only required if _write_schema will be called.
1388 # Callers using extract_all(no_write => 1) do not need to supply it.
1389 output_dir => $params->{output_dir},
1390 verbose => $params->{verbose} // 0,
1391 include_private => $params->{include_private} // 0, # include _private methods
1392 confidence_threshold => $params->{confidence_threshold} // $DEFAULT_CONFIDENCE_THRESH,
1393 max_parameters => $params->{max_parameters} // $DEFAULT_MAX_PARAMETERS, # safety limit
1394 strict_pod => _validate_strictness_level($params->{strict_pod}), # Enable strict POD checking
1395 1157 ▶ allow_signature_exec => $params->{allow_signature_exec} // 0, # opt-in: execute Type::Params signature_for() exprs from the target module
Condition100%12/12
$params->{"verbose"} // 0
l!l
341816
$params->{"include_private"} // 0
l!l
1321025
$params->{"confidence_threshold"} // $DEFAULT_CONFIDENCE_THRESH
l!l&&r!l&&!r
131874152
$params->{"max_parameters"} // $DEFAULT_MAX_PARAMETERS
l!l&&r!l&&!r
127878152
$params->{"allow_signature_exec"} // 0
l!l
1291027
MC/DC100%1/1
$params->{"verbose"} // 0
$params->{"verbose"}
MC/DC100%1/1
$params->{"include_private"} // 0
$params->{"include_private"}
MC/DC100%2/2
$params->{"confidence_threshold"} // $DEFAULT_CONFIDENCE_THRESH
$params->{"confidence_threshold"} $DEFAULT_CONFIDENCE_THRESH
MC/DC100%2/2
$params->{"max_parameters"} // $DEFAULT_MAX_PARAMETERS
$params->{"max_parameters"} $DEFAULT_MAX_PARAMETERS
MC/DC100%1/1
$params->{"allow_signature_exec"} // 0
$params->{"allow_signature_exec"}
Truth table100%2/2
A
A= $params->{"verbose"}
Aresult
11
00
Truth table100%2/2
A
A= $params->{"include_private"}
Aresult
11
00
Truth table100%3/3
A // B
A= $params->{"confidence_threshold"}B= $DEFAULT_CONFIDENCE_THRESH
ABresult
1X1
011
000
Truth table100%3/3
A // B
A= $params->{"max_parameters"}B= $DEFAULT_MAX_PARAMETERS
ABresult
1X1
011
000
Truth table100%2/2
A
A= $params->{"allow_signature_exec"}
Aresult
11
00
1396 };
1397  
1398 # Validate input file exists
1399 1156 ▶ unless (-f $self->{input_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"input_file"}) 2 1154 1156
1400 2 croak(__PACKAGE__, ": Input file '$self->{input_file}' does not exist");
1401 }
1402  
1403 1154 return bless $self, $class;
1404 }
1405  
1406 =head2 extract_all
1407  
1408 Extract schemas for all qualifying methods in the module and return
1409 them as a hashref.
1410  
1411 my $schemas = $extractor->extract_all();
1412  
1413 # Suppress writing .yml files to disk
1414 my $schemas = $extractor->extract_all(no_write => 1);
1415  
1416 =head3 Arguments
1417  
1418 =over 4
1419  
1420 =item * C<no_write>
1421  
1422 When true, schema files are not written to C<output_dir>. The returned
1423 hashref is still fully populated. Useful when the caller wants to
1424 inspect or augment schemas before deciding whether to write them.
1425 Optional, default 0.
1426  
1427 =back
1428  
1429 =head3 Returns
1430  
1431 A hashref mapping method name strings to schema hashrefs. Each schema
1432 contains at minimum the keys C<function>, C<module>, C<input>,
1433 C<output>, and C<_analysis>. See L</Generated Schema Structure> for
1434 the full structure.
1435  
1436 =head3 Side effects
1437  
1438 Parses the input file with L<PPI>. Writes one YAML file per method to
1439 C<output_dir> unless C<no_write> is set. Creates C<output_dir> if it
1440 does not exist and writing is enabled.
1441  
1442 =head3 Notes
1443  
1444 Private methods (names beginning with C<_>) are excluded unless
1445 C<include_private =E<gt> 1> was passed to C<new>. Duplicate method
1446 names are deduplicated with a warning logged to stdout in verbose mode.
1447  
1448 POD/code agreement validation is applied if C<strict_pod> was set in
1449 C<new>. At level 2 (fatal), the first disagreement causes an immediate
1450 croak.
1451  
1452 =head3 API specification
1453  
1454 =head4 input
1455  
1456 {
1457 self => { type => OBJECT, isa => 'App::Test::Generator::SchemaExtractor' },
1458 no_write => { type => SCALAR, optional => 1 },
1459 }
1460  
1461 =head4 output
1462  
1463 {
1464 type => HASHREF,
1465 keys => {
1466 '*' => {
1467 type => HASHREF,
1468 keys => {
1469 function => { type => SCALAR },
1470 module => { type => SCALAR },
1471 input => { type => HASHREF },
1472 output => { type => HASHREF },
1473 _analysis => { type => HASHREF },
1474 },
1475 },
1476 },
1477 }
1478  
1479 =cut
1480  
1481 317 ▶sub extract_all {
Subroutine extract_all: called CC=129 SCAR=51.8
Pod documented
1482 317 my $self = shift;
1483 317 ▶ my $params = Params::Get::get_params(undef, @_) || {};
Condition100%2/2
Params::Get::get_params(undef, @_) || {}
l!l
110207
MC/DC100%1/1
Params::Get::get_params(undef, @_) || {}
Params::Get::get_params(undef, @_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, @_)
Aresult
11
00
1484  
1485 317 $self->_log("Parsing $self->{input_file}...");
1486 317 $self->_log('Strict POD mode: ' . (qw(off warn fatal))[$self->{strict_pod}]);
1487  
1488 # $! is not meaningful here — PPI does not set errno on failure
1489 my $document = PPI::Document->new($self->{input_file})
1490 317 ▶ or croak "Failed to parse $self->{input_file}";
Branch: 1 branch
BranchTrueFalseTotal
my $document = "PPI::Document"->new($self->{"input_file"}) or croak(qq[Failed to parse $self->{"input_file"}]) 317 0 317
1491  
1492 # Store document for later use
1493 317 $self->{_document} = $document;
1494  
1495 317 my $package_name = $self->_extract_package_name($document);
1496 317 ▶ $self->{_package_name} //= $package_name;
Condition66%2/3
$self->{"_package_name"} //= $package_name
l!l&&r!l&&!r
30980
MC/DC0%0/2
$self->{"_package_name"} //= $package_name
$self->{"_package_name"} $package_name
Truth table66%2/3
A //= B
A= $self->{"_package_name"}B= $package_name
ABresult
1X1
011
000
1497 317 $self->_log("Package: $package_name");
1498  
1499 317 my $methods = $self->_find_methods($document);
1500 317 $self->_log('Found ' . scalar(@$methods) . ' methods (pre-dedup)');
1501  
1502 317 my %schemas;
1503 my %delegates;
1504 317 my $ctor_body;
1505 317 foreach my $method (@{$methods}) {
1506 817 $self->_log("\nAnalyzing method: $method->{name}");
1507  
1508 817 my $schema = $self->_analyze_method($method);
1509 813 $schemas{$method->{name}} = $schema;
1510 813 $schema->{'module'} = $package_name;
1511 813 ▶ $ctor_body = $method->{body} if($method->{name} eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $method->{"name"} eq "new" 98 715 813
1512  
1513 813 my $target = $self->_delegated_method($method->{body});
1514 813 ▶ $delegates{$method->{name}} = $target if defined($target);
Branch: 1 branch
BranchTrueFalseTotal
if defined $target 3 810 813
1515 }
1516  
1517 # Second pass: enrich instance-method schemas with constructor args so the
1518 # fuzz harness can build $self automatically. The new() schema (if present)
1519 # provides type info; we generate one representative value per required param.
1520 # Enrichable: new: {} (no-arg ctor) or new: {hash} (with args).
1521 # Unenrichable (coderef/object params): leave new: as the package-name string
1522 # so _write_schema still emits new: ~ and self-fuzz.t keeps skipping those.
1523 # An arrayref of live objects can be stood in for when the constructor's
1524 # POD names the class the elements must be: see _constructor_object_stubs().
1525 313 my $object_stubs = {};
1526 313 ▶ if (exists $schemas{new}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schemas{"new"}) 98 215 313
1527 98 my ($new_method) = grep { $_->{name} eq 'new' } @{$methods};
1528 $object_stubs = _constructor_object_stubs(
1529 $schemas{new},
1530 $new_method ? $new_method->{pod} : undef,
1531 98 ▶ $ctor_body,
Branch: 1 branch
BranchTrueFalseTotal
$new_method ? : 98 0 98
1532 );
1533 98 ▶ $self->_log(' STUB: stood in for ' . join(', ', sort keys %{$object_stubs})) if %{$object_stubs};
Branch: 1 branch
BranchTrueFalseTotal
if %{$object_stubs;} 1 97 98
1534  
1535 98 my $ctor_args = $self->_ctor_representative_args($schemas{new});
1536 98 ▶ if (defined $ctor_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctor_args) 81 17 98
1537 81 for my $method_name (keys %schemas) {
1538 265 ▶ next if $method_name eq 'new';
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "new" 81 184 265
1539 184 my $schema = $schemas{$method_name};
1540 184 ▶ next unless exists $schema->{new} && defined $schema->{new};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"new"} and defined $schema->{"new"} 20 164 184
Condition66%2/3
exists $schema->{"new"} and defined $schema->{"new"}
!ll&&!rl&&r
200164
MC/DC50%1/2
exists $schema->{"new"} and defined $schema->{"new"}
exists $schema->{"new"} defined $schema->{"new"}
Truth table66%2/3
A and B
A= exists $schema->{"new"}B= defined $schema->{"new"}
ABresult
0X0
100
111
1541 164 $schema->{new} = $ctor_args;
1542 164 $self->_log(" ENRICH: $method_name new: enriched with ctor args");
1543 }
1544 }
1545 }
1546  
1547 # A constructor that insists on a non-empty arrayref of live objects
1548 # cannot be built from YAML: no generated array is made of blessed
1549 # instances whose methods it can call. Database::Join->new is the case
1550 # that exposed this. Recording the precondition on new() itself lets
1551 # callers skip that constructor and, through the block below, gives the
1552 # instance methods new: ~ as well.
1553 313 ▶ if($ctor_body && $schemas{new}
Branch: 1 branch
BranchTrueFalseTotal
if ($ctor_body and $schemas{"new"} and my $reason = _constructor_requires_live_objects($ctor_body, $schemas{"new"})) 2 311 313
Condition83%5/6
$ctor_body and $schemas{"new"} and my $reason = _constructor_requires_live_objects($ctor_body, $schemas{"new"})
!ll&&!rl&&r
215962
$ctor_body and $schemas{"new"}
!ll&&!rl&&r
215098
MC/DC66%2/3
$ctor_body and $schemas{"new"} and my $reason = _constructor_requires_live_objects($ctor_body, $schemas{"new"})
$ctor_body $schemas{"new"} my $reason = _constructor_requires_live_objects($ctor_body, $schemas{"new"})
Truth table75%3/4
A and B and C
A= $ctor_bodyB= $schemas{"new"}C= my $reason = _constructor_requires_live_objects($ctor_body, $schemas{"new"})
ABCresult
0XX0
10X0
1100
1111
1554 && (my $reason = _constructor_requires_live_objects($ctor_body, $schemas{new}))) {
1555 2 ▶ $schemas{new}{skip} = $reason unless %{$object_stubs};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$object_stubs;} 1 1 2
1556 2 ▶ $self->_log(" UNBUILDABLE: new: $reason") unless %{$object_stubs};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$object_stubs;} 1 1 2
1557 }
1558  
1559 # A constructor whose input includes an object it will call methods on,
1560 # with no can/isa saying how to stand one in, cannot be built from YAML
1561 # either. Database::BI::Model::Boundary takes a cache and a user agent,
1562 # Geocode a geocoder and a cache, DataSource a cache and translations:
1563 # a generated string is not an object, and the constructors rightly
1564 # refuse one. The same applies to a data directory they read files
1565 # from. Reporting the precondition on new() also makes every instance
1566 # method new: ~ below, so the fuzzer skips the whole family rather than
1567 # failing the object construction at every call.
1568 313 ▶ if($ctor_body && $schemas{new}
Branch: 1 branch
BranchTrueFalseTotal
if ($ctor_body and $schemas{"new"} and my $reason = _constructor_needs_live_dependencies($schemas{"new"})) 8 305 313
Condition83%5/6
$ctor_body and $schemas{"new"} and my $reason = _constructor_needs_live_dependencies($schemas{"new"})
!ll&&!rl&&r
215908
$ctor_body and $schemas{"new"}
!ll&&!rl&&r
215098
MC/DC66%2/3
$ctor_body and $schemas{"new"} and my $reason = _constructor_needs_live_dependencies($schemas{"new"})
$ctor_body $schemas{"new"} my $reason = _constructor_needs_live_dependencies($schemas{"new"})
Truth table75%3/4
A and B and C
A= $ctor_bodyB= $schemas{"new"}C= my $reason = _constructor_needs_live_dependencies($schemas{"new"})
ABCresult
0XX0
10X0
1100
1111
1569 && (my $reason = _constructor_needs_live_dependencies($schemas{new}))) {
1570 # A stand-in for the documented object list makes the constructor
1571 # buildable: the objects it cannot stand in for are optional, so a
1572 # call that omits them is valid.
1573 8 ▶ $schemas{new}{skip} //= $reason unless %{$object_stubs};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$object_stubs;} 8 0 8
Condition33%1/3
$schemas{"new"}{"skip"} //= $reason
l!l&&r!l&&!r
080
MC/DC0%0/2
$schemas{"new"}{"skip"} //= $reason
$schemas{"new"}{"skip"} $reason
Truth table33%1/3
A //= B
A= $schemas{"new"}{"skip"}B= $reason
ABresult
1X1
011
000
1574 8 ▶ $self->_log(" UNBUILDABLE: new: $reason") unless %{$object_stubs};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$object_stubs;} 8 0 8
1575 }
1576  
1577 # A constructor can refuse to build the package itself: an abstract base
1578 # class croaks on direct instantiation, and a constructor that croaks over
1579 # an argument the extractor cannot supply cannot be built either. Either
1580 # way no harness can get a $self, so the instance methods are not fuzzable
1581 # this way: leaving new: as the package name makes _write_schema emit
1582 # new: ~, which callers report as skipped rather than failed.
1583 313 ▶ if($ctor_body && $schemas{new} && _constructor_refuses_direct_instantiation($ctor_body, $schemas{new})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctor_body and $schemas{"new"} and _constructor_refuses_direct_instantiation($ctor_body, $schemas{"new"})) 2 311 313
Condition83%5/6
$ctor_body and $schemas{"new"} and _constructor_refuses_direct_instantiation($ctor_body, $schemas{"new"})
!ll&&!rl&&r
215962
$ctor_body and $schemas{"new"}
!ll&&!rl&&r
215098
MC/DC66%2/3
$ctor_body and $schemas{"new"} and _constructor_refuses_direct_instantiation($ctor_body, $schemas{"new"})
$ctor_body $schemas{"new"} _constructor_refuses_direct_instantiation($ctor_body, $schemas{"new"})
Truth table75%3/4
A and B and C
A= $ctor_bodyB= $schemas{"new"}C= _constructor_refuses_direct_instantiation($ctor_body, $schemas{"new"})
ABCresult
0XX0
10X0
1100
1111
1584 # The class itself cannot be constructed, so its new() is expected to
1585 # die rather than return an object.
1586 2 $schemas{new}{output} = { _STATUS => 'DIES' };
1587 2 for my $method_name (keys %schemas) {
1588 4 ▶ next if($method_name eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "new" 2 2 4
1589 2 my $schema = $schemas{$method_name};
1590 2 ▶ next unless(exists($schema->{new}) && defined($schema->{new}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"new"} and defined $schema->{"new"} 0 2 2
Condition33%1/3
exists $schema->{"new"} and defined $schema->{"new"}
!ll&&!rl&&r
002
MC/DC0%0/2
exists $schema->{"new"} and defined $schema->{"new"}
exists $schema->{"new"} defined $schema->{"new"}
Truth table33%1/3
A and B
A= exists $schema->{"new"}B= defined $schema->{"new"}
ABresult
0X0
100
111
1591 2 $schema->{new} = $package_name;
1592 2 $self->_log(" UNBUILDABLE: $method_name new: ~ ($package_name refuses to be built)");
1593 }
1594 }
1595  
1596 # Third pass: contracts the type inference cannot see in the signature.
1597 # A routine can document behaviour in its body that changes what the
1598 # harness may assert: an abstract method always dies, a routine returning
1599 # a list must be called in list context, one that returns nothing has no
1600 # output to validate, and a parameter checked against a pattern rejects
1601 # values outside it. A routine that reads a file it is given cannot be
1602 # fuzzed with a synthesized value at all.
1603 313 foreach my $method (@{$methods}) {
1604 813 my $schema = $schemas{$method->{name}};
1605 813 ▶ next unless($schema && ref($schema->{output}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless $schema and ref $schema->{"output"} eq "HASH" 0 813 813
Condition33%1/3
$schema and ref $schema->{"output"} eq "HASH"
!ll&&!rl&&r
00813
MC/DC0%0/2
$schema and ref $schema->{"output"} eq "HASH"
$schema ref $schema->{"output"} eq "HASH"
Truth table33%1/3
A and B
A= $schemaB= ref $schema->{"output"} eq "HASH"
ABresult
0X0
100
111
1606 # A comment is prose, not code: an example signature written in one
1607 # must not change how the routine is called.
1608 813 ▶ my $body = _strip_comments($method->{body} // '', $method->{node});
Condition50%1/2
$method->{"body"} // ""
l!l
8130
MC/DC0%0/1
$method->{"body"} // ""
$method->{"body"}
Truth table50%1/2
A
A= $method->{"body"}
Aresult
11
00
1609  
1610 # A routine that takes a variadic list or an argument hash receives its
1611 # documentation as named arguments: a table row's order is not a call
1612 # order for it. Database::Join::aggregate forwards @args to get_params,
1613 # and calling it positionally tested the argument count instead of the
1614 # documented keys. Scalar arguments before the slurpy are positional
1615 # (`my ($password, %opts) = @_`), and the documentation after them
1616 # describes the named options passed after those arguments, as
1617 # Database::BI::Password::hash_password does.
1618 # A plain positional signature has no slurpy and keeps every
1619 # position: only the arguments left over after a slurpy are named.
1620 813 ▶ if($body =~ /\bmy\s*(?:\(([^)]*)\)|(%\w+))\s*=\s*\@_/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\bmy\s*(?:\(([^)]*)\)|(%\w+))\s*=\s*\@_/s) 440 373 813
1621 440 my $declared = $1;
1622 440 ▶ if(!defined($declared) || ($declared =~ /[\@%]\w+/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $declared or $declared =~ /[\@%]\w+/) 81 359 440
Condition100%3/3
not defined $declared or $declared =~ /[\@%]\w+/
l!l&&r!l&&!r
279359
MC/DC100%2/2
not defined $declared or $declared =~ /[\@%]\w+/
not defined $declared $declared =~ /[\@%]\w+/
Truth table100%3/3
not(A) or B
A= not defined $declaredB= $declared =~ /[\@%]\w+/
ABresult
1X1
011
000
1623 81 my @prefix;
1624 81 ▶ for my $decl (split(/\s*,\s*/, $declared // '')) {
Condition100%2/2
$declared // ""
l!l
792
MC/DC100%1/1
$declared // ""
$declared
Truth table100%2/2
A
A= $declared
Aresult
11
00
1625 156 ▶ last if($decl =~ /^\s*[\@%]/);
Branch: 1 branch
BranchTrueFalseTotal
if $decl =~ /^\s*[\@%]/ 79 77 156
1626 77 ▶ push @prefix, $1 if($decl =~ /^\s*\$(?!self\b|class\b)(\w+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $decl =~ /^\s*\$(?!self\b|class\b)(\w+)/ 2 75 77
1627 }
1628  
1629 # Keep the leading scalars positional (by name where the merge
1630 # found one, by position otherwise) and say that every remaining
1631 # documented name is a named option after them.
1632 81 my %keep = map { $prefix[$_] => $_ } 0 .. $#prefix;
1633 81 ▶ for my $param (keys %{ $schema->{input} // {} }) {
Condition50%1/2
$schema->{"input"} // {}
l!l
810
MC/DC0%0/1
$schema->{"input"} // {}
$schema->{"input"}
Truth table50%1/2
A
A= $schema->{"input"}
Aresult
11
00
1634 162 my $spec = $schema->{input}{$param};
1635 162 ▶ my $pos = exists($keep{$param}) ? delete($keep{$param}) : $spec->{position};
Branch: 1 branch
BranchTrueFalseTotal
exists $keep{$param} ? : 2 160 162
1636 162 ▶ if(defined($pos) && ($pos < @prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos and $pos < @prefix) { } 2 160 162
Condition100%3/3
defined $pos and $pos < @prefix
!ll&&!rl&&r
147132
MC/DC100%2/2
defined $pos and $pos < @prefix
defined $pos $pos < @prefix
Truth table100%3/3
A and B
A= defined $posB= $pos < @prefix
ABresult
0X0
100
111
1637 2 $spec->{position} = $pos;
1638 2 delete $spec->{named};
1639 } else {
1640 160 delete $spec->{position};
1641 160 ▶ $spec->{named} = 1 if @prefix;
Branch: 1 branch
BranchTrueFalseTotal
if @prefix 3 157 160
1642 }
1643 }
1644 }
1645 }
1646  
1647 813 ▶ if($body =~ /\bcroak\b[^;\n]*must be implemented by subclass/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($body =~ /\bcroak\b[^;\n]*must be implemented by subclass/) { } 7 806 813
elsif ($body =~ /\breturn\s+\@\{/ and ($method->{'pod'} // '') =~ /\b(?:a\s+)?list\s+of\b|\breturns?\s+a\s+list\b/i) { } 10 796 806
elsif ($body =~ /\breturn\s*;/ and not $body =~ /\breturn\s+[^\s;]/) { } 13 783 796
elsif ($body =~ /\breturn\s+\\?[A-Z]\w*(?:::\w+)+(?:->)?\w*\s*\(/) { } 3 780 783
1648 7 $schema->{output}{_STATUS} = 'DIES';
1649 ▶ } elsif(($body =~ /\breturn\s+@\{/) && (($method->{pod} // '') =~ /\b(?:a\s+)?list\s+of\b|\breturns?\s+a\s+list\b/i)) {
Condition80%4/5
$body =~ /\breturn\s+\@\{/ and ($method->{"pod"} // "") =~ /\b(?:a\s+)?list\s+of\b|\breturns?\s+a\s+list\b/i
!ll&&!rl&&r
788810
$method->{"pod"} // ""
l!l
180
MC/DC50%1/2
$body =~ /\breturn\s+\@\{/ and ($method->{"pod"} // "") =~ /\b(?:a\s+)?list\s+of\b|\breturns?\s+a\s+list\b/i
$body =~ /\breturn\s+\@\{/ $method->{"pod"}
Truth table66%2/3
A and B
A= $body =~ /\breturn\s+\@\{/B= $method->{"pod"}
ABresult
0X0
100
111
1650 # Called in scalar context a list return yields its count, so the
1651 # harness has to capture the list. Only when the POD says the
1652 # routine returns a list, not an arrayref.
1653 10 $schema->{output}{type} = 'array';
1654 ▶ } elsif(($body =~ /\breturn\s*;/) && ($body !~ /\breturn\s+[^\s;]/)) {
Condition100%3/3
$body =~ /\breturn\s*;/ and not $body =~ /\breturn\s+[^\s;]/
!ll&&!rl&&r
782113
MC/DC100%2/2
$body =~ /\breturn\s*;/ and not $body =~ /\breturn\s+[^\s;]/
$body =~ /\breturn\s*;/ not $body =~ /\breturn\s+[^\s;]/
Truth table100%3/3
A and not(B)
A= $body =~ /\breturn\s*;/B= not $body =~ /\breturn\s+[^\s;]/
ABresult
0X0
100
111
1655 # The routine returns nothing: say so, rather than omitting the
1656 # type. 'void' is the documented way to describe that (see
1657 # _detect_void_context), and the harness validates no return
1658 # value for it.
1659 13 $schema->{output}{type} = 'void';
1660 } elsif($body =~ /\breturn\s+\\?[A-Z]\w*(?:::\w+)+(?:->)?\w*\s*\(/) {
1661 # Delegated to another package: its result type is not visible here.
1662 3 delete $schema->{output}{type};
1663 }
1664  
1665 # A routine that only croaks is unsupported by design: every call is
1666 # an error, so the harness expects a death for each one. Database::Join
1667 # documents query(), group_by() and execute() that way.
1668 813 ▶ if(_always_croaks($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_always_croaks($body)) 8 805 813
1669 8 $schema->{output} = { _STATUS => 'DIES' };
1670 }
1671  
1672 # A routine that hands a database handle back to its caller, or leaves
1673 # it on the object's SQLite cache for the next call, holds a
1674 # descriptor on purpose, so the check that nothing leaked does not
1675 # apply (the harness reads config 'check_fds').
1676 813 ▶ if(_keeps_database_handle($body, $method->{pod} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_keeps_database_handle($body, $method->{"pod"} // "")) 2 811 813
Condition50%1/2
$method->{"pod"} // ""
l!l
8130
MC/DC0%0/1
$method->{"pod"} // ""
$method->{"pod"}
Truth table50%1/2
A
A= $method->{"pod"}
Aresult
11
00
1677 2 $schema->{_analysis}{side_effects}{keeps_fd} = 1;
1678 }
1679  
1680 813 ▶ if(my $skip = _requires_input_file($body, $schema->{input})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $skip = _requires_input_file($body, $schema->{"input"})) 18 795 813
1681 18 $schema->{skip} = $skip;
1682 }
1683  
1684 # A hashref whose POD gives the whole value a shape it must have
1685 # ("hashref of { country code => place rank }", "the existing model
1686 # store") is a precondition, not just a type: a generated hash has
1687 # arbitrary keys, so check_ranks rejects the invented country code
1688 # and the model-store routines are handed a string where their
1689 # documented store has a hash. A hashref option that merely
1690 # describes what some entries mean (resolve_values' cache) is not
1691 # this: arbitrary keys are legal there, so it is still fuzzed.
1692 813 ▶ if((my $pod = $method->{pod} // '') =~ /(?:HASHREF|hashref)\s+of\s*\{|(?:HASHREF|hashref)[^\n]*\bmodel store\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pod = $method->{"pod"} // "") =~ /(?:HASHREF|hashref)\s+of\s*\{|(?:HASHREF|hashref)[^\n]*\bmodel store\b/i) 2 811 813
Condition50%1/2
$method->{"pod"} // ""
l!l
8130
MC/DC0%0/1
$method->{"pod"} // ""
$method->{"pod"}
Truth table50%1/2
A
A= $method->{"pod"}
Aresult
11
00
1693 2 ▶ $schema->{skip} //= 'needs a hashref in the shape its POD documents; a generated hash has arbitrary keys';
Condition50%1/2
$schema->{"skip"} //= "needs a hashref in the shape its POD documents; a generated hash has arbitrary keys"
l!l
02
MC/DC0%0/1
$schema->{"skip"} //= "needs a hashref in the shape its POD documents; a generated hash has arbitrary keys"
$schema->{"skip"}
Truth table50%1/2
A
A= $schema->{"skip"}
Aresult
11
00
1694 }
1695  
1696 # Settings that name a database, a user and a password describe a
1697 # live server, not a value a fixture can carry: there is no database
1698 # here to connect to, and the routine croaks when it cannot.
1699 # DataSource::mariadb_tables documents exactly that settings hash.
1700 813 ▶ if((my $pod = $method->{pod} // '') =~ /HASHREF[^\n]*\{[^}]*\bdatabase\b[^}]*\b(?:username|password)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pod = $method->{"pod"} // "") =~ /HASHREF[^\n]*\{[^}]*\bdatabase\b[^}]*\b(?:username|password)\b/i) 1 812 813
Condition50%1/2
$method->{"pod"} // ""
l!l
8130
MC/DC0%0/1
$method->{"pod"} // ""
$method->{"pod"}
Truth table50%1/2
A
A= $method->{"pod"}
Aresult
11
00
1701 1 ▶ $schema->{skip} //= 'needs a live database connection, which a fuzzer cannot provide';
Condition50%1/2
$schema->{"skip"} //= "needs a live database connection, which a fuzzer cannot provide"
l!l
01
MC/DC0%0/1
$schema->{"skip"} //= "needs a live database connection, which a fuzzer cannot provide"
$schema->{"skip"}
Truth table50%1/2
A
A= $schema->{"skip"}
Aresult
11
00
1702 }
1703  
1704 # A method that reads required keys out of a constructor hashref cannot
1705 # be given a working object: the harness has no schema to put there.
1706 813 ▶ if(ref($schema->{new}) eq 'HASH' && %{$schema->{new}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"new"} eq "HASH" and %{$schema->{"new"};}) 60 753 813
Condition100%3/3
ref $schema->{"new"} eq "HASH" and %{$schema->{"new"};}
!ll&&!rl&&r
65110260
MC/DC100%2/2
ref $schema->{"new"} eq "HASH" and %{$schema->{"new"};}
ref $schema->{"new"} eq "HASH" %{$schema->{"new"};}
Truth table100%3/3
A and B
A= ref $schema->{"new"} eq "HASH"B= %{$schema->{"new"};}
ABresult
0X0
100
111
1707 60 foreach my $param (keys %{$schema->{new}}) {
1708 102 my $p = quotemeta($param);
1709 # Read directly, or through a local copy of the hashref.
1710 102 ▶ my $reads_keys = ($body =~ /\$self->\{\s*['"]?$p['"]?\s*\}->\{/)
Condition66%4/6
$body =~ /\$self->\{\s*['"]?${p}['"]?\s*\}->\{/ || $body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?${p}['"]?\s*\}/ && $body =~ /\$$1->\{/
l!l&&r!l&&!r
0894
$body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?${p}['"]?\s*\}/ and $body =~ /\$$1->\{/
!ll&&!rl&&r
9408
MC/DC33%1/3
$body =~ /\$self->\{\s*['"]?${p}['"]?\s*\}->\{/ || $body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?${p}['"]?\s*\}/ && $body =~ /\$$1->\{/
$body =~ /\$self->\{\s*['"]?${p}['"]?\s*\}->\{/ $body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?${p}['"]?\s*\}/ $body =~ /\$$1->\{/
Truth table50%2/4
A || B and C
A= $body =~ /\$self->\{\s*['"]?${p}['"]?\s*\}->\{/B= $body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?${p}['"]?\s*\}/C= $body =~ /\$$1->\{/
ABCresult
1XX1
0111
00X0
0100
1711 || ($body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?$p['"]?\s*\}/ && $body =~ /\$$1->\{/);
1712 102 ▶ next unless($reads_keys && ($body =~ /\bcroak\b/));
Branch: 1 branch
BranchTrueFalseTotal
unless $reads_keys and $body =~ /\bcroak\b/ 97 5 102
Condition100%3/3
$reads_keys and $body =~ /\bcroak\b/
!ll&&!rl&&r
9435
MC/DC100%2/2
$reads_keys and $body =~ /\bcroak\b/
$reads_keys $body =~ /\bcroak\b/
Truth table100%3/3
A and B
A= $reads_keysB= $body =~ /\bcroak\b/
ABresult
0X0
100
111
1713 5 ▶ $schema->{new} = $self->{_package_name} if($self->{_package_name});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_package_name"} 5 0 5
1714 5 last;
1715 }
1716 }
1717  
1718 813 ▶ _next_parameter: foreach my $param_name (keys %{$schema->{input} // {}}) {
Condition100%2/2
$schema->{"input"} // {}
l!l
77736
MC/DC100%1/1
$schema->{"input"} // {}
$schema->{"input"}
Truth table100%2/2
A
A= $schema->{"input"}
Aresult
11
00
1719 741 my $spec = $schema->{input}{$param_name};
1720 741 ▶ next unless(ref($spec) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 0 741 741
1721 741 my $p = quotemeta($param_name);
1722  
1723 741 ▶ if($body =~ /ref\(\s*\$args\{\s*['"]?$p['"]?\s*\}\s*\)\s*(?:ne|eq)\s*['"]CODE['"]/
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /ref\(\s*\$args\{\s*['"]?${p}['"]?\s*\}\s*\)\s*(?:ne|eq)\s*['"]CODE['"]/ or $body =~ /\$args\{\s*['"]?${p}['"]?\s*\}\s*->\s*\(/) 4 737 741
Condition100%3/3
$body =~ /ref\(\s*\$args\{\s*['"]?${p}['"]?\s*\}\s*\)\s*(?:ne|eq)\s*['"]CODE['"]/ or $body =~ /\$args\{\s*['"]?${p}['"]?\s*\}\s*->\s*\(/
l!l&&r!l&&!r
31737
MC/DC100%2/2
$body =~ /ref\(\s*\$args\{\s*['"]?${p}['"]?\s*\}\s*\)\s*(?:ne|eq)\s*['"]CODE['"]/ or $body =~ /\$args\{\s*['"]?${p}['"]?\s*\}\s*->\s*\(/
$body =~ /ref\(\s*\$args\{\s*['"]?${p}['"]?\s*\}\s*\)\s*(?:ne|eq)\s*['"]CODE['"]/ $body =~ /\$args\{\s*['"]?${p}['"]?\s*\}\s*->\s*\(/
Truth table100%3/3
A or B
A= $body =~ /ref\(\s*\$args\{\s*['"]?${p}['"]?\s*\}\s*\)\s*(?:ne|eq)\s*['"]CODE['"]/B= $body =~ /\$args\{\s*['"]?${p}['"]?\s*\}\s*->\s*\(/
ABresult
1X1
011
000
1724 || $body =~ /\$args\{\s*['"]?$p['"]?\s*\}\s*->\s*\(/) {
1725 4 $spec->{type} = 'coderef';
1726 4 next _next_parameter;
1727 }
1728  
1729 # The value is checked against a file-level constant set: the module
1730 # member list is the documented domain, e.g. add_evidence's category.
1731 737 ▶ if($body =~ /my\s+\$(\w+)\s*=\s*\$args\{\s*['"]?$p['"]?\s*\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /my\s+\$(\w+)\s*=\s*\$args\{\s*['"]?${p}['"]?\s*\}/) 13 724 737
1732 13 my $var = $1;
1733 13 ▶ if($body =~ /[\$%](\w+)\s*\{\s*\$$var\s*\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /[\$%](\w+)\s*\{\s*\$$var\s*\}/) 7 6 13
1734 7 my $const = $1;
1735 7 ▶ my $source = $self->{_document} ? $self->{_document}->serialize : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_document'} ? : 7 0 7
1736 7 ▶ if($source =~ /%$const\s*(?:=|=>)\s*.*?qw\s*[\(\{\[]([^\)\}\]]+)[\)\}\]]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /%$const\s*(?:=|=>)\s*.*?qw\s*[\(\{\[]([^\)\}\]]+)[\)\}\]]/s) 7 0 7
1737 7 my @values = split(' ', $1);
1738 7 ▶ $spec->{memberof} = \@values if(@values);
Branch: 1 branch
BranchTrueFalseTotal
if @values 7 0 7
1739 }
1740 }
1741 }
1742  
1743 # A routine that accepts a value by matching it against patterns and
1744 # croaks when none of them match is a validator for that parameter:
1745 # the union of the patterns is the accepted domain, so a value
1746 # outside it is expected to die. parse_zone's UTC, local and
1747 # offset alternatives are three such patterns.
1748 737 my @accepted;
1749 737 my @subjects = (
1750 qr/\$$p\b/,
1751 qr/\$args\{\s*['"]?$p['"]?\s*\}/,
1752 );
1753 737 foreach my $subject (@subjects) {
1754 1474 ▶ while($body =~ /$subject\s*=~\s*\/([^\/\n]+)\//g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ m[$subject\s*=~\s*/([^/\n]+)/]g) 33 1474 1507
1755 33 my $pattern = $1;
1756 33 my $at = $-[0];
1757 33 my $from = rindex($body, ';', $at);
1758 33 ▶ $from = 0 if($from < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $from < 0 0 33 33
1759 33 my $prefix = substr($body, $from, $at - $from);
1760 # Only a pattern that guards a returned value (or a croak
1761 # unless it matches) says the value is accepted. A test
1762 # used for anything else, such as deciding whether to append
1763 # a word, describes no domain. `return X unless match`
1764 # returns for every value that does NOT match, so there the
1765 # pattern names a rejection, not an accepted domain.
1766 33 ▶ my $accepts = $prefix =~ /\breturn\b[^;]*\bunless\b/
Branch: 1 branch
BranchTrueFalseTotal
$prefix =~ /\breturn\b[^;]*\bunless\b/ ? : 5 28 33
Condition66%2/3
$prefix =~ /\breturn\b/ or $prefix =~ /\b(?:croak|die|confess)\b[^;]*\bunless\b/
l!l&&r!l&&!r
7210
MC/DC0%0/2
$prefix =~ /\breturn\b/ or $prefix =~ /\b(?:croak|die|confess)\b[^;]*\bunless\b/
$prefix =~ /\breturn\b/ $prefix =~ /\b(?:croak|die|confess)\b[^;]*\bunless\b/
Truth table66%2/3
A or B
A= $prefix =~ /\breturn\b/B= $prefix =~ /\b(?:croak|die|confess)\b[^;]*\bunless\b/
ABresult
1X1
011
000
1767 ? 0
1768 : ($prefix =~ /\breturn\b/ || $prefix =~ /\b(?:croak|die|confess)\b[^;]*\bunless\b/);
1769 33 ▶ if(!$accepts && ($prefix =~ /\bif\s*\([^;{}]*$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $accepts and $prefix =~ /\bif\s*\([^;{}]*$/) 0 33 33
Condition66%2/3
not $accepts and $prefix =~ /\bif\s*\([^;{}]*$/
!ll&&!rl&&r
2850
MC/DC0%0/2
not $accepts and $prefix =~ /\bif\s*\([^;{}]*$/
not $accepts $prefix =~ /\bif\s*\([^;{}]*$/
Truth table66%2/3
not(A) and B
A= not $acceptsB= $prefix =~ /\bif\s*\([^;{}]*$/
ABresult
0X0
100
111
1770 # A block opened by an if whose condition is the match,
1771 # e.g. `if (my ($s, $h) = $spec =~ /.../) { return ... }`.
1772 0 my $brace = index($body, '{', $+[0]);
1773 0 ▶ if($brace >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($brace >= 0) 0 0 0
1774 0 my $close = index($body, "\n\t}", $brace);
1775 0 ▶ my $block = substr($body, $brace,
Branch: 1 branch
BranchTrueFalseTotal
$close >= 0 ? : 0 0 0
1776 (($close >= 0) ? $close - $brace : 400));
1777 0 ▶ $accepts = ($block =~ /\breturn\b/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$block =~ /\breturn\b/ ? : 0 0 0
1778 }
1779 }
1780 33 ▶ next unless($accepts);
Branch: 1 branch
BranchTrueFalseTotal
unless $accepts 5 28 33
1781 # A pattern in a croak's own condition names a rejected
1782 # value, not an accepted one.
1783 28 ▶ next if($prefix =~ /\b(?:croak|die|confess)\b[^;]*\bif\b/);
Branch: 1 branch
BranchTrueFalseTotal
if $prefix =~ /\b(?:croak|die|confess)\b[^;]*\bif\b/ 0 28 28
1784 28 ▶ if(!_pattern_is_self_contained($pattern)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_pattern_is_self_contained($pattern)) 0 28 28
1785 0 $self->_log(" CODE: $param_name matches '$pattern', which depends on run-time variables; not recorded");
1786 0 next;
1787 }
1788 28 push @accepted, $pattern;
1789 }
1790 }
1791  
1792 737 my $croaks_about_param = 0;
1793 737 foreach my $subject (@subjects) {
1794 1284 ▶ next unless($body =~ /\b(?:croak|die|confess)\b[^;]*$subject/s);
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /\b(?:croak|die|confess)\b[^;]*$subject/s 1052 232 1284
1795 232 $croaks_about_param = 1;
1796 232 last;
1797 }
1798  
1799 737 my $pattern;
1800 737 ▶ if($body =~ /\$args\{\s*['"]?$p['"]?\s*\}\s*=~\s*\/([^\/\n]+)\//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body =~ m[\$args\{\s*['"]?${p}['"]?\s*\}\s*=~\s*/([^/\n]+)/]) { } 4 733 737
elsif ($body =~ m[\$$p\b\s*=~\s*/([^/\n]+)/]) { } 27 706 733
1801 4 $pattern = $1;
1802 } elsif($body =~ /\$$p\b\s*=~\s*\/([^\/\n]+)\//) {
1803 27 $pattern = $1;
1804 }
1805 737 ▶ if(@accepted && $croaks_about_param) {
Branch: 2 branches
BranchTrueFalseTotal
if (@accepted and $croaks_about_param) { } 22 715 737
elsif (defined $pattern and _pattern_is_self_contained($pattern) and $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i) { } 0 1425 1425
Condition100%3/3
@accepted and $croaks_about_param
!ll&&!rl&&r
711422
MC/DC100%2/2
@accepted and $croaks_about_param
@accepted $croaks_about_param
Truth table100%3/3
A and B
A= @acceptedB= $croaks_about_param
ABresult
0X0
100
111
1806 22 my %seen;
1807 22 @accepted = grep { !$seen{$_}++ } @accepted;
1808 $spec->{matches} = (@accepted == 1)
1809 ? $accepted[0]
1810 22 ▶ : join('|', map { "(?:$_)" } @accepted);
Branch: 1 branch
BranchTrueFalseTotal
@accepted == 1 ? : 21 1 22
1811 22 $spec->{_source} = SOURCE_VALIDATOR;
1812 ▶ } elsif(defined($pattern) && _pattern_is_self_contained($pattern)
Condition83%5/6
defined $pattern and _pattern_is_self_contained($pattern) and $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
!ll&&!rl&&r
71050
defined $pattern and _pattern_is_self_contained($pattern)
!ll&&!rl&&r
70645
MC/DC0%0/3
defined $pattern and _pattern_is_self_contained($pattern) and $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
defined $pattern _pattern_is_self_contained($pattern) $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
Truth table75%3/4
A and B and C
A= defined $patternB= _pattern_is_self_contained($pattern)C= $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
ABCresult
0XX0
10X0
1100
1111
1813 && ($body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i)) {
1814 0 $spec->{matches} = $pattern;
1815 0 $spec->{_source} = SOURCE_VALIDATOR;
1816 }
1817 }
1818 }
1819  
1820 # A constructor that parses a file it is given cannot be built by the
1821 # harness either, so its instance methods get new: ~ like any other
1822 # unbuildable class.
1823 313 ▶ if($schemas{new} && $schemas{new}{skip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schemas{"new"} and $schemas{"new"}{"skip"}) 13 300 313
Condition100%3/3
$schemas{"new"} and $schemas{"new"}{"skip"}
!ll&&!rl&&r
2158513
MC/DC100%2/2
$schemas{"new"} and $schemas{"new"}{"skip"}
$schemas{"new"} $schemas{"new"}{"skip"}
Truth table100%3/3
A and B
A= $schemas{"new"}B= $schemas{"new"}{"skip"}
ABresult
0X0
100
111
1824 13 for my $method_name (keys %schemas) {
1825 51 ▶ next if($method_name eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "new" 13 38 51
1826 38 my $schema = $schemas{$method_name};
1827 38 ▶ next unless(exists($schema->{new}) && defined($schema->{new}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"new"} and defined $schema->{"new"} 11 27 38
Condition66%2/3
exists $schema->{"new"} and defined $schema->{"new"}
!ll&&!rl&&r
11027
MC/DC50%1/2
exists $schema->{"new"} and defined $schema->{"new"}
exists $schema->{"new"} defined $schema->{"new"}
Truth table66%2/3
A and B
A= exists $schema->{"new"}B= defined $schema->{"new"}
ABresult
0X0
100
111
1828 27 $schema->{new} = $package_name;
1829 27 $self->_log(" UNBUILDABLE: $method_name new: ~ ($package_name constructor is not auto-buildable)");
1830 }
1831 }
1832  
1833 # A routine whose whole body is `return $self->other(\@_)` has
1834 # exactly other()'s input contract. CGI::Info::get_cookie is such a
1835 # forwarder, and without this it looked like it took no parameters at all,
1836 # so the harness called it with none and died on the required cookie_name.
1837 313 for my $method_name (sort keys %delegates) {
1838 3 my $target = $delegates{$method_name};
1839 3 ▶ next if $method_name eq $target;
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq $target 0 3 3
1840 3 my $target_schema = $schemas{$target};
1841 3 ▶ next unless $target_schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_schema 1 2 3
1842  
1843 2 my $schema = $schemas{$method_name};
1844 2 ▶ next unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 2 2
1845  
1846 # The forwarded call's result is this routine's result, so the
1847 # documented output type is the target's too (selectall_hashref is a
1848 # deprecated alias of selectall_arrayref), whether or not the target
1849 # takes arguments.
1850 2 ▶ if(ref($target_schema->{output}) eq 'HASH' && %{$target_schema->{output}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target_schema->{"output"} eq "HASH" and %{$target_schema->{"output"};}) 2 0 2
Condition33%1/3
ref $target_schema->{"output"} eq "HASH" and %{$target_schema->{"output"};}
!ll&&!rl&&r
002
MC/DC0%0/2
ref $target_schema->{"output"} eq "HASH" and %{$target_schema->{"output"};}
ref $target_schema->{"output"} eq "HASH" %{$target_schema->{"output"};}
Truth table33%1/3
A and B
A= ref $target_schema->{"output"} eq "HASH"B= %{$target_schema->{"output"};}
ABresult
0X0
100
111
1851 2 $schema->{output} = { %{$target_schema->{output}} };
1852 }
1853  
1854 next unless ref($target_schema->{input}) eq 'HASH'
1855 2 ▶ && scalar(keys %{$target_schema->{input}});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $target_schema->{"input"} eq "HASH" and scalar keys %{$$target_schema{"input"};} 1 1 2
Condition50%1/2
ref $target_schema->{"input"} eq "HASH" and scalar keys %{$$target_schema{"input"};}
!ll
02
MC/DC0%0/1
ref $target_schema->{"input"} eq "HASH" and scalar keys %{$$target_schema{"input"};}
ref $target_schema->{"input"} eq "HASH"
Truth table50%1/2
A
A= ref $target_schema->{"input"} eq "HASH"
Aresult
00
11
1856 1 ▶ next if ref($schema->{input}) eq 'HASH' && scalar(keys %{$schema->{input}});
Branch: 1 branch
BranchTrueFalseTotal
if ref $schema->{"input"} eq "HASH" and scalar keys %{$$schema{"input"};} 0 1 1
Condition50%1/2
ref $schema->{"input"} eq "HASH" and scalar keys %{$$schema{"input"};}
!ll
01
MC/DC0%0/1
ref $schema->{"input"} eq "HASH" and scalar keys %{$$schema{"input"};}
ref $schema->{"input"} eq "HASH"
Truth table50%1/2
A
A= ref $schema->{"input"} eq "HASH"
Aresult
00
11
1857  
1858 $schema->{input} = {
1859 1 map { $_ => { %{$target_schema->{input}{$_}} } } sort keys %{$target_schema->{input}}
1860 };
1861 $schema->{_confidence}{input} = {
1862 1 'level' => 'medium',
1863 'factors' => [ "Forwarded to $target()" ]
1864 };
1865 1 push @{$schema->{_notes}}, "input inherited from $target(), which this routine forwards \@_ to";
1866 1 $self->_log(" DELEGATE: $method_name input inherited from $target");
1867 }
1868  
1869 # A multi-package file is analysed as its primary package, but it is not
1870 # fuzzed: the helper package's routines are deliberately excluded, and
1871 # the primary package's routines may not be fuzzable without it. Mark
1872 # them skipped rather than letting the harness fail on every routine.
1873 313 ▶ if($self->{_multiple_packages}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_multiple_packages"}) 1 312 313
1874 1 for my $method_name (keys %schemas) {
1875 $schemas{$method_name}{skip} //=
1876 1 ▶ "multi-package file; only the primary package $package_name is extracted";
Condition50%1/2
$schemas{$method_name}{"skip"} //= "multi-package file; only the primary package $package_name is extracted"
l!l
01
MC/DC0%0/1
$schemas{$method_name}{"skip"} //= "multi-package file; only the primary package $package_name is extracted"
$schemas{$method_name}{"skip"}
Truth table50%1/2
A
A= $schemas{$method_name}{"skip"}
Aresult
11
00
1877 }
1878 }
1879  
1880 313 ▶ unless ($params->{no_write}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"no_write"}) 207 106 313
1881 207 for my $method_name (keys %schemas) {
1882 534 $self->_write_schema($method_name, $schemas{$method_name});
1883 }
1884 }
1885  
1886 313 return \%schemas;
1887 }
1888  
1889 # --------------------------------------------------
1890 # _delegated_method
1891 #
1892 # Purpose: Detect a routine whose whole body
1893 # forwards its arguments to another
1894 # method of the same object.
1895 #
1896 # Entry: $code - the subroutine's source text.
1897 #
1898 # Exit: Returns the name of the method it
1899 # forwards to, or undef when the routine
1900 # does anything else.
1901 #
1902 # Side effects: None.
1903 #
1904 # Notes: Only the bare `return $self->other(\@_)`
1905 # shape counts. Such a forwarder has
1906 # exactly the target's input contract,
1907 # which extract_all() copies so that the
1908 # harness passes the right arguments
1909 # (CGI::Info::get_cookie -> cookie).
1910 # --------------------------------------------------
1911 813 ▶sub _delegated_method {
Subroutine _delegated_method: called CC=3 SCAR=11.0
1912 813 my ($self, $code) = @_;
1913  
1914 813 ▶ return unless defined($code);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 813 813
1915  
1916 813 (my $body = $code) =~ s/\A\s*sub\s+\w+\s*//;
1917 813 $body =~ s/\s+/ /g;
1918  
1919 # A deprecated alias warns first, then forwards; the warning does not
1920 # change what the routine returns (Database::Join::selectall_hashref).
1921 813 ▶ return $body =~ /\A\{ (?:my \$self = shift; )?(?:carp\s+(?:'[^']*'|"[^"]*")\s*; )?return \$self->(\w+)\(\s*\\?\@_\s*\); \}\z/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$body =~ /\A\{ (?:my \$self = shift; )?(?:carp\s+(?:'[^']*'|"[^"]*")\s*; )?return \$self->(\w+)\(\s*\\?\@_\s*\); \}\z/ ? : 3 810 813
1922 }
1923  
1924 # --------------------------------------------------
1925 # _extract_package_name
1926 #
1927 # Purpose: Extract the Perl package name from a
1928 # PPI document, or from the cached value
1929 # stored at construction time.
1930 #
1931 # Entry: $document - a PPI::Document, or undef
1932 # to use $self->{_document}.
1933 #
1934 # Exit: Returns the package namespace string,
1935 # or an empty string if no package
1936 # statement is found.
1937 #
1938 # Side effects: Stores the package name in
1939 # $self->{_package_name} if not already
1940 # set.
1941 #
1942 # Notes: A file may declare more than one package (a
1943 # main class plus a small error class). The
1944 # first package is the primary one; routines
1945 # owned by later packages are ignored rather
1946 # than aborting the extraction.
1947 # --------------------------------------------------
1948 379 ▶sub _extract_package_name {
Subroutine _extract_package_name: called CC=7 SCAR=19.5
1949 379 my ($self, $document) = @_;
1950  
1951 379 ▶ if(!defined($document)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $document) 62 317 379
1952 62 $document = $self->{_document};
1953 }
1954 379 ▶ my $pkgs = $document->find('PPI::Statement::Package') || [];
Condition100%2/2
$document->find('PPI::Statement::Package') || []
l!l
3718
MC/DC100%1/1
$document->find('PPI::Statement::Package') || []
$document->find('PPI::Statement::Package')
Truth table100%2/2
A
A= $document->find('PPI::Statement::Package')
Aresult
11
00
1955 379 ▶ if(@$pkgs == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pkgs == 0) 8 371 379
1956 8 my $package_stmt = $document->find_first('PPI::Statement::Package');
1957 8 ▶ return $package_stmt ? $package_stmt->namespace() : '';
Branch: 1 branch
BranchTrueFalseTotal
$package_stmt ? : 0 8 8
1958 }
1959 371 ▶ $self->{_package_name} //= $pkgs->[0]->namespace();
Condition66%2/3
$self->{"_package_name"} //= $pkgs->[0]->namespace
l!l&&r!l&&!r
633080
MC/DC0%0/2
$self->{"_package_name"} //= $pkgs->[0]->namespace
$self->{"_package_name"} $pkgs->[0]->namespace
Truth table66%2/3
A //= B
A= $self->{"_package_name"}B= $pkgs->[0]->namespace
ABresult
1X1
011
000
1960 371 ▶ if(@$pkgs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pkgs > 1) 1 370 371
1961 1 $self->{_multiple_packages} = 1;
1962 1 $self->_log(" Multiple packages; analyzing " . $pkgs->[0]->namespace() . " only");
1963 }
1964 371 return $pkgs->[0]->namespace();
1965 }
1966  
1967 # True when a node belongs to the primary (first) package of the document.
1968 # PPI nodes carry a location, so each routine belongs to the last package
1969 # declaration at or before its line. A single-package file trivially passes.
1970 1568 ▶sub _in_primary_package {
Subroutine _in_primary_package: called CC=8 SCAR=21.2
1971 1568 my ($self, $node, $pkgs) = @_;
1972  
1973 1568 ▶ return 1 unless($pkgs && @$pkgs > 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $pkgs and @$pkgs > 1 1565 3 1568
Condition66%2/3
$pkgs and @$pkgs > 1
!ll&&!rl&&r
015653
MC/DC50%1/2
$pkgs and @$pkgs > 1
$pkgs @$pkgs > 1
Truth table66%2/3
A and B
A= $pkgsB= @$pkgs > 1
ABresult
0X0
100
111
1974  
1975 3 my $loc = eval { $node->location };
1976 3 ▶ return 1 unless(ref($loc) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $loc eq "ARRAY" 0 3 3
1977  
1978 3 my $owner = $pkgs->[0];
1979 3 for my $pkg (@$pkgs) {
1980 6 my $pkg_loc = eval { $pkg->location };
1981 6 ▶ next unless(ref($pkg_loc) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pkg_loc eq "ARRAY" 0 6 6
1982 6 ▶ last if($pkg_loc->[0] > $loc->[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg_loc->[0] > $loc->[0] 1 5 6
1983 5 $owner = $pkg;
1984 }
1985  
1986 3 ▶ my $primary = $self->{_package_name} // $pkgs->[0]->namespace();
Condition33%1/3
$self->{"_package_name"} // $pkgs->[0]->namespace
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"_package_name"} // $pkgs->[0]->namespace
$self->{"_package_name"} $pkgs->[0]->namespace
Truth table33%1/3
A // B
A= $self->{"_package_name"}B= $pkgs->[0]->namespace
ABresult
1X1
011
000
1987 3 return $owner->namespace() eq $primary;
1988 }
1989  
1990 # --------------------------------------------------
1991 # _find_methods
1992 #
1993 # Purpose: Locate all subroutine and method
1994 # declarations in a PPI document,
1995 # including Moose-style method modifiers
1996 # and Perl 5.38 class/method syntax.
1997 #
1998 # Entry: $document - a PPI::Document.
1999 #
2000 # Exit: Returns an arrayref of method hashrefs,
2001 # each containing: name, node, body, pod,
2002 # type, and optionally modifier, class,
2003 # and fields keys.
2004 # Private methods (names beginning with
2005 # _) are excluded unless include_private
2006 # was set in new(), except for _new,
2007 # _init, and _build which are always
2008 # included.
2009 #
2010 # Side effects: Logs progress and warnings to stdout
2011 # when verbose is set.
2012 #
2013 # Notes: Duplicate method names are silently
2014 # deduplicated — the second occurrence
2015 # is dropped with a verbose warning.
2016 # Class/method detection is regex-based
2017 # and may misbehave on complex code.
2018 # --------------------------------------------------
2019 320 ▶sub _find_methods {
Subroutine _find_methods: called CC=22 SCAR=34.0
2020 320 my ($self, $document) = @_;
2021  
2022 320 ▶ my $subs = $document->find('PPI::Statement::Sub') || [];
Condition100%2/2
$document->find('PPI::Statement::Sub') || []
l!l
3173
MC/DC100%1/1
$document->find('PPI::Statement::Sub') || []
$document->find('PPI::Statement::Sub')
Truth table100%2/2
A
A= $document->find('PPI::Statement::Sub')
Aresult
11
00
2023 320 ▶ my $pkgs = $document->find('PPI::Statement::Package') || [];
Condition100%2/2
$document->find('PPI::Statement::Package') || []
l!l
3128
MC/DC100%1/1
$document->find('PPI::Statement::Package') || []
$document->find('PPI::Statement::Package')
Truth table100%2/2
A
A= $document->find('PPI::Statement::Package')
Aresult
11
00
2024 # Only fetch statements that begin with a Moose modifier keyword —
2025 # fetching ALL PPI::Statement nodes on a large file returns thousands
2026 # of nodes and then discards nearly all of them in the loop below.
2027 467104 ▶ my $sub_decls = $document->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2028 467104 ▶ $_[1]->isa('PPI::Statement')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement") and $_[1]->content =~ /^\s*(?:before|after|around)\b/ 47565 419539 467104
2029 && $_[1]->content =~ /^\s*(?:before|after|around)\b/
2030 320 ▶ }) || [];
Condition100%2/2
$document->find(sub { $_[1]->isa('PPI::Statement') and $_[1]->content =~ /^\s*(?:before|after|around)\b/; } ) || []
l!l
3317
MC/DC100%1/1
$document->find(sub { $_[1]->isa('PPI::Statement') and $_[1]->content =~ /^\s*(?:before|after|around)\b/; } ) || []
$document->find(sub { $_[1]->isa('PPI::Statement') and $_[1]->content =~ /^\s*(?:before|after|around)\b/; } )
Truth table100%2/2
A
A= $document->find(sub { $_[1]->isa('PPI::Statement') and $_[1]->content =~ /^\s*(?:before|after|around)\b/; } )
Aresult
11
00
2031  
2032 320 my @methods;
2033 320 foreach my $sub (@$subs) {
2034 1565 my $name = $sub->name();
2035  
2036 1565 ▶ next unless defined $name; # Skip anonymous routines
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1565 1565
2037 # A file may define helper packages after the main one; their subs
2038 # are not part of the module under test.
2039 1565 ▶ next unless $self->_in_primary_package($sub, $pkgs);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_primary_package($sub, $pkgs) 2 1563 1565
2040 1563 ▶ next if $name =~ /^(BEGIN|END|DESTROY|AUTOLOAD|CHECK|INIT|UNITCHECK)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(BEGIN|END|DESTROY|AUTOLOAD|CHECK|INIT|UNITCHECK)$/ 8 1555 1563
2041 1555 ▶ next if $name =~ /::/; # cross-package sub (e.g. sub DB::DB { })
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::/ 3 1552 1555
2042  
2043 # Skip private methods unless explicitly included, or they're special
2044 1552 ▶ if ($name =~ /^_/ && $name !~ /^_(new|init|build)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/ and not $name =~ /^_(new|init|build)/) 727 825 1552
Condition100%3/3
$name =~ /^_/ and not $name =~ /^_(new|init|build)/
!ll&&!rl&&r
80718727
MC/DC100%2/2
$name =~ /^_/ and not $name =~ /^_(new|init|build)/
$name =~ /^_/ not $name =~ /^_(new|init|build)/
Truth table100%3/3
A and not(B)
A= $name =~ /^_/B= not $name =~ /^_(new|init|build)/
ABresult
0X0
100
111
2045 727 ▶ next unless $self->{include_private};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"include_private"} 725 2 727
2046 }
2047  
2048 # Get the POD before this sub
2049 827 my $pod = $self->_extract_pod_before($sub);
2050  
2051 827 push @methods, {
2052 name => $name,
2053 node => $sub,
2054 body => $sub->content(),
2055 pod => $pod,
2056 type => 'sub',
2057 };
2058 }
2059  
2060 # Look for class { method } syntax (Perl 5.38+). The regex scans the
2061 # whole document, so it is only safe when there is a single package.
2062 320 my $content = $document->content();
2063 320 ▶ if (@$pkgs <= 1 && $content =~ /\bclass\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pkgs <= 1 and $content =~ /\bclass\b/) 108 212 320
Condition100%3/3
@$pkgs <= 1 and $content =~ /\bclass\b/
!ll&&!rl&&r
1211108
MC/DC100%2/2
@$pkgs <= 1 and $content =~ /\bclass\b/
@$pkgs <= 1 $content =~ /\bclass\b/
Truth table100%3/3
A and B
A= @$pkgs <= 1B= $content =~ /\bclass\b/
ABresult
0X0
100
111
2064 108 $self->_log(' Detecting class/method syntax...');
2065 # Strip POD blocks and line comments before the regex scan so that
2066 # patterns like class Name { inside documentation examples or
2067 # the comment # find "class Name {" blocks don't produce
2068 # spurious method names (e.g. the keyword 'if').
2069 108 (my $code_only = $content) =~ s/^=\w[^\n]*.*?^=cut[^\n]*\n//gms;
2070 108 $code_only =~ s/\s*#[^\n]*//g;
2071 108 $self->_extract_class_methods($code_only, \@methods);
2072 }
2073  
2074 # Process method modifiers (Moose)
2075 320 foreach my $decl (@$sub_decls) {
2076 3 ▶ next unless $self->_in_primary_package($decl, $pkgs);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_primary_package($decl, $pkgs) 0 3 3
2077 3 my $content = $decl->content;
2078 3 ▶ if ($content =~ /^\s*(before|after|around)\s+['"]?(\w+)['"]?\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\s*(before|after|around)\s+['"]?(\w+)['"]?\b/) 0 3 3
2079 0 my ($modifier, $method_name) = ($1, $2);
2080 0 my $full_name = "${modifier}_$method_name";
2081  
2082 # Look for the actual sub definition that follows
2083 0 my $next_sib = $decl->next_sibling;
2084 0 ▶ while ($next_sib && !$next_sib->isa('PPI::Statement::Sub')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_sib and not $next_sib->isa("PPI::Statement::Sub")) 0 0 0
Condition0%0/3
$next_sib and not $next_sib->isa("PPI::Statement::Sub")
!ll&&!rl&&r
000
MC/DC0%0/2
$next_sib and not $next_sib->isa("PPI::Statement::Sub")
$next_sib not $next_sib->isa("PPI::Statement::Sub")
Truth table0%0/3
A and not(B)
A= $next_sibB= not $next_sib->isa("PPI::Statement::Sub")
ABresult
0X0
100
111
2085 0 $next_sib = $next_sib->next_sibling;
2086 }
2087  
2088 0 ▶ if ($next_sib && $next_sib->isa('PPI::Statement::Sub')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_sib and $next_sib->isa("PPI::Statement::Sub")) 0 0 0
Condition0%0/3
$next_sib and $next_sib->isa("PPI::Statement::Sub")
!ll&&!rl&&r
000
MC/DC0%0/2
$next_sib and $next_sib->isa("PPI::Statement::Sub")
$next_sib $next_sib->isa("PPI::Statement::Sub")
Truth table0%0/3
A and B
A= $next_sibB= $next_sib->isa("PPI::Statement::Sub")
ABresult
0X0
100
111
2089 0 my $pod = $self->_extract_pod_before($decl); # POD might be before modifier
2090 0 push @methods, {
2091 name => $full_name,
2092 node => $next_sib,
2093 body => $next_sib->content,
2094 pod => $pod,
2095 type => 'modifier',
2096 original_method => $method_name,
2097 modifier => $modifier,
2098 };
2099 0 $self->_log(" Found method modifier: $full_name");
2100 }
2101 }
2102 }
2103  
2104 # Prevent silent duplicate method overwrites
2105 320 my %seen;
2106 @methods = grep {
2107 320 my $n = $_->{name};
2108 827 ▶ if ($seen{$n}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$n}++) { } 1 826 827
2109 1 $self->_log(" WARNING: duplicate method '$n' ignored");
2110 1 0;
2111 } else {
2112 826 1;
2113 }
2114 } @methods;
2115  
2116 320 return \@methods;
2117 }
2118  
2119 # --------------------------------------------------
2120 # _extract_class_methods
2121 #
2122 # Purpose: Extract method declarations from
2123 # Perl 5.38 class { method {} } syntax
2124 # by regex-based scanning of the class
2125 # body content.
2126 #
2127 # Entry: $content - full document source string.
2128 # $methods - arrayref to push discovered
2129 # method hashrefs onto
2130 # (modified in place).
2131 #
2132 # Exit: Returns nothing. Appends to $methods.
2133 #
2134 # Side effects: Logs class and method discoveries
2135 # to stdout when verbose is set.
2136 #
2137 # Notes: This is experimental — regex-based
2138 # class body parsing may misbehave on
2139 # complex or nested class declarations.
2140 # Class body boundaries are tracked by
2141 # simple brace counting, which will
2142 # fail on unbalanced braces in strings
2143 # or heredocs.
2144 # --------------------------------------------------
2145 108 ▶sub _extract_class_methods {
Subroutine _extract_class_methods: called CC=8 SCAR=40.4
2146 108 my ($self, $content, $methods) = @_;
2147  
2148 # EXPERIMENTAL: regex-based parsing, may misbehave on complex code
2149  
2150 # Simple pattern: find "class Name {" blocks
2151 # This won't handle all edge cases but will work for simple classes
2152 108 ▶ while ($content =~ /class\s+(\w+)\s*\{/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /class\s+(\w+)\s*\{/g) 0 108 108
2153 0 my $class_name = $1;
2154 0 my $start_pos = pos($content);
2155  
2156 # Find the matching closing brace. $start_pos is just after the
2157 # opening '{' consumed by the regex above, so back up one
2158 # character to hand the brace itself to extract_bracketed.
2159 0 require Text::Balanced;
2160 0 my $extracted = Text::Balanced::extract_bracketed(substr($content, $start_pos - 1), '{}');
2161  
2162 0 ▶ next unless defined $extracted; # unbalanced braces, skip class
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 0 0
2163  
2164 0 my $class_body = substr($extracted, 1, length($extracted) - 2);
2165  
2166 0 $self->_log(" Found class $class_name");
2167  
2168 # Extract field declarations from class
2169 0 my $fields = $self->_extract_field_declarations($class_body);
2170  
2171 # Find methods in the class body
2172 0 ▶ while ($class_body =~ /method\s+(\w+)\s*(\([^)]*\))?\s*\{/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_body =~ /method\s+(\w+)\s*(\([^)]*\))?\s*\{/g) 0 0 0
2173 0 ▶ my ($method_name, $sig_with_parens) = ($1, $2 || '()');
Condition0%0/2
$2 || '()'
l!l
00
MC/DC0%0/1
$2 || '()'
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
2174  
2175 # Skip private unless configured
2176 0 ▶ if ($method_name =~ /^_/ && $method_name !~ /^_(new|init|build)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^_/ and not $method_name =~ /^_(new|init|build)/) 0 0 0
Condition0%0/3
$method_name =~ /^_/ and not $method_name =~ /^_(new|init|build)/
!ll&&!rl&&r
000
MC/DC0%0/2
$method_name =~ /^_/ and not $method_name =~ /^_(new|init|build)/
$method_name =~ /^_/ not $method_name =~ /^_(new|init|build)/
Truth table0%0/3
A and not(B)
A= $method_name =~ /^_/B= not $method_name =~ /^_(new|init|build)/
ABresult
0X0
100
111
2177 0 ▶ next unless $self->{include_private};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"include_private"} 0 0 0
2178 }
2179  
2180 # Reconstruct as sub for analysis
2181 0 my $signature = $sig_with_parens;
2182 0 $signature =~ s/^\(//;
2183 0 $signature =~ s/\)$//;
2184  
2185 # Build a fake sub declaration
2186 0 my $fake_sub = "sub $method_name($signature) { }";
2187  
2188 0 push @$methods, {
2189 name => $method_name,
2190 node => undef,
2191 body => $fake_sub, # Just the signature for now
2192 is_stub => 1,
2193 pod => '',
2194 type => 'method',
2195 class => $class_name,
2196 fields => $fields,
2197 };
2198  
2199 0 $self->_log(" Found method $method_name in class $class_name");
2200 }
2201 }
2202 }
2203  
2204 # --------------------------------------------------
2205 # _extract_pod_before
2206 #
2207 # Purpose: Collect the POD documentation that
2208 # appears immediately before a
2209 # subroutine in the PPI document, by
2210 # walking backwards through siblings.
2211 #
2212 # Entry: $sub - a PPI node (typically a
2213 # PPI::Statement::Sub).
2214 #
2215 # Exit: Returns a string containing all POD
2216 # content found before the sub, with
2217 # inline parameter comments converted
2218 # to =item format. Returns an empty
2219 # string if no POD is found.
2220 #
2221 # Side effects: None.
2222 #
2223 # Notes: Stops walking backwards on the first
2224 # non-POD, non-whitespace, non-separator,
2225 # non-include node encountered.
2226 # Walking is capped at $POD_WALK_LIMIT
2227 # steps to prevent runaway processing
2228 # on pathological documents.
2229 # --------------------------------------------------
2230 827 ▶sub _extract_pod_before {
Subroutine _extract_pod_before: called CC=34 SCAR=36.4
2231 827 my ($self, $sub) = @_;
2232  
2233 827 ▶ my $name = eval { $sub->can('name') ? $sub->name : undef };
Branch: 1 branch
BranchTrueFalseTotal
$sub->can('name') ? : 827 0 827
2234 827 my $pod = '';
2235 827 my $current = $sub->previous_sibling();
2236 827 my $skipped_declaration = 0;
2237 827 my $steps = 0;
2238  
2239 # Walk backwards collecting POD.
2240 # Stop after the first pod token so that a =cut before =head1 METHODS
2241 # prevents class-level POD from being mistaken for method-specific POD.
2242 827 ▶ while($current && $steps++ < $POD_WALK_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current and $steps++ < $POD_WALK_LIMIT) 2386 5 2391
Condition66%2/3
$current and $steps++ < $POD_WALK_LIMIT
!ll&&!rl&&r
502386
MC/DC50%1/2
$current and $steps++ < $POD_WALK_LIMIT
$current $steps++ < $POD_WALK_LIMIT
Truth table66%2/3
A and B
A= $currentB= $steps++ < $POD_WALK_LIMIT
ABresult
0X0
100
111
2243 2386 ▶ if ($current->isa('PPI::Token::Pod')) {
Branch: 5 branches
BranchTrueFalseTotal
if ($current->isa('PPI::Token::Pod')) { } 547 1839 2386
elsif ($current->isa('PPI::Token::Comment')) { } 437 1402 1839
elsif ($current->isa('PPI::Token::Whitespace') or $current->isa('PPI::Token::Separator')) { } 1058 344 1402
elsif ($current->isa('PPI::Statement::Include')) { } 65 279 344
elsif ($current->isa('PPI::Statement::Variable') and $current->content =~ /\A\s*(?:my|our)\b/ and not $current->content =~ /\bsub\b/) { } 4 275 279
2244 547 my $content = $current->content();
2245 # A file-scoped declaration may sit between a routine's POD and
2246 # its sub (Mutation::Feedback declares a regex constant there).
2247 # Accept such a POD block only when it documents this routine.
2248 547 ▶ if(!$skipped_declaration
Branch: 1 branch
BranchTrueFalseTotal
if (not $skipped_declaration or defined $name and $content =~ /^=head[1-6]\s+\Q$name\E\b/m) 547 0 547
Condition50%3/6
not $skipped_declaration or defined $name and $content =~ /^=head[1-6]\s+\Q$name\E\b/m
l!l&&r!l&&!r
54430
defined $name and $content =~ /^=head[1-6]\s+\Q$name\E\b/m
!ll&&!rl&&r
003
MC/DC0%0/3
not $skipped_declaration or defined $name and $content =~ /^=head[1-6]\s+\Q$name\E\b/m
not $skipped_declaration defined $name $content =~ /^=head[1-6]\s+\Q$name\E\b/m
Truth table50%2/4
not(A) or B and C
A= not $skipped_declarationB= defined $nameC= $content =~ /^=head[1-6]\s+\Q$name\E\b/m
ABCresult
1XX1
0111
00X0
0100
2249 || (defined($name) && $content =~ /^=head[1-6]\s+\Q$name\E\b/m)) {
2250 547 $pod = $content . $pod;
2251 }
2252 547 last; # Only take the immediately adjacent pod block
2253 } elsif ($current->isa('PPI::Token::Comment')) {
2254 # Include comments that might contain parameter info
2255 437 my $comment = $current->content();
2256 437 ▶ if ($comment =~ /#\s*(?:param|arg|input)\s+\$(\w+)\s*:\s*(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment =~ /#\s*(?:param|arg|input)\s+\$(\w+)\s*:\s*(.+)/i) 0 437 437
2257 0 $pod .= "=item \$$1\n$2\n\n";
2258 }
2259 ▶ } elsif ($current->isa('PPI::Token::Whitespace') ||
Condition66%2/3
$current->isa("PPI::Token::Whitespace") or $current->isa("PPI::Token::Separator")
l!l&&r!l&&!r
10580344
MC/DC50%1/2
$current->isa("PPI::Token::Whitespace") or $current->isa("PPI::Token::Separator")
$current->isa("PPI::Token::Whitespace") $current->isa("PPI::Token::Separator")
Truth table66%2/3
A or B
A= $current->isa("PPI::Token::Whitespace")B= $current->isa("PPI::Token::Separator")
ABresult
1X1
011
000
2260 $current->isa('PPI::Token::Separator')) {
2261 # Skip whitespace and separators
2262 } elsif ($current->isa('PPI::Statement::Include')) {
2263 # allow 'use strict', 'use warnings' between POD and sub
2264 ▶ } elsif ($current->isa('PPI::Statement::Variable')
Condition66%4/6
$current->isa("PPI::Statement::Variable") and $current->content =~ /\A\s*(?:my|our)\b/ and not $current->content =~ /\bsub\b/
!ll&&!rl&&r
27504
$current->isa("PPI::Statement::Variable") and $current->content =~ /\A\s*(?:my|our)\b/
!ll&&!rl&&r
27504
MC/DC33%1/3
$current->isa("PPI::Statement::Variable") and $current->content =~ /\A\s*(?:my|our)\b/ and not $current->content =~ /\bsub\b/
$current->isa("PPI::Statement::Variable") $current->content =~ /\A\s*(?:my|our)\b/ not $current->content =~ /\bsub\b/
Truth table50%2/4
A and B and not(C)
A= $current->isa("PPI::Statement::Variable")B= $current->content =~ /\A\s*(?:my|our)\b/C= not $current->content =~ /\bsub\b/
ABCresult
0XX0
10X0
1100
1111
2265 && $current->content() =~ /\A\s*(?:my|our)\b/
2266 && $current->content() !~ /\bsub\b/) {
2267 # A simple file-scoped declaration: the POD is still this
2268 # routine's documentation.
2269 4 $skipped_declaration = 1;
2270 } else {
2271 # Hit non-POD, non-whitespace - stop
2272 275 last;
2273 }
2274 1564 $current = $current->previous_sibling();
2275 }
2276  
2277 # A POD token can span several routines (and the module-level sections
2278 # before the first one). Keep the section that documents this routine,
2279 # stopping at the next heading that belongs to another method. Headings
2280 # such as SYNOPSIS or NOTES belong to the routine and must be kept.
2281 827 ▶ if($pod && defined($name) && $pod =~ /^=head([1-6])\s+\Q$name\E\b/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod and defined $name and $pod =~ /^=head([1-6])\s+\Q$name\E\b/m) 546 281 827
Condition83%5/6
$pod and defined $name and $pod =~ /^=head([1-6])\s+\Q$name\E\b/m
!ll&&!rl&&r
2801546
$pod and defined $name
!ll&&!rl&&r
2800547
MC/DC66%2/3
$pod and defined $name and $pod =~ /^=head([1-6])\s+\Q$name\E\b/m
$pod defined $name $pod =~ /^=head([1-6])\s+\Q$name\E\b/m
Truth table75%3/4
A and B and C
A= $podB= defined $nameC= $pod =~ /^=head([1-6])\s+\Q$name\E\b/m
ABCresult
0XX0
10X0
1100
1111
2282 546 my ($level, $start) = ($1, $-[0]);
2283 546 my $section = substr($pod, $start);
2284  
2285 546 my $method_names = {};
2286 546 ▶ if(my $doc = eval { $sub->document }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = eval { do { $sub->document } }) 546 0 546
2287 546 ▶ $method_names = $self->{_pod_method_names}{refaddr($doc)} ||= do {
Condition66%2/3
$self->{"_pod_method_names"}{builtin::refaddr($doc)} ||= do { my %names; my $subs = $doc->find("PPI::Statement::Sub"); foreach my $method (@{$subs or [];}) { my $method_name = eval { do { $method->name } }; $names{$method_name} = 1 if defined $method_name; } ; \%names }
l!l&&r!l&&!r
3012450
MC/DC0%0/2
$self->{"_pod_method_names"}{builtin::refaddr($doc)} ||= do { my %names; my $subs = $doc->find("PPI::Statement::Sub"); foreach my $method (@{$subs or [];}) { my $method_name = eval { do { $method->name } }; $names{$method_name} = 1 if defined $method_name; } ; \%names }
$self->{"_pod_method_names"}{builtin::refaddr($doc)} do { my %names; my $subs = $doc->find("PPI::Statement::Sub"); foreach my $method (@{$subs or [];}) { my $method_name = eval { do { $method->name } }; $names{$method_name} = 1 if defined $method_name; } ; \%names }
Truth table66%2/3
A ||= B
A= $self->{"_pod_method_names"}{builtin::refaddr($doc)}B= do { my %names; my $subs = $doc->find("PPI::Statement::Sub"); foreach my $method (@{$subs or [];}) { my $method_name = eval { do { $method->name } }; $names{$method_name} = 1 if defined $method_name; } ; \%names }
ABresult
1X1
011
000
2288 245 my %names;
2289 245 my $subs = $doc->find('PPI::Statement::Sub');
2290 245 ▶ for my $method (@{ $subs || [] }) {
Condition50%1/2
$subs || []
l!l
2450
MC/DC0%0/1
$subs || []
$subs
Truth table50%1/2
A
A= $subs
Aresult
11
00
2291 1332 my $method_name = eval { $method->name };
2292 1332 ▶ $names{$method_name} = 1 if defined($method_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $method_name 1332 0 1332
2293 }
2294 245 \%names;
2295 };
2296 }
2297  
2298 546 my $cut;
2299 546 ▶ while($section =~ /^=head([1-6])\s+(.+?)\s*$/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /^=head([1-6])\s+(.+?)\s*$/gm) 2418 540 2958
2300 2418 my ($heading_level, $title) = ($1, $2);
2301 2418 ▶ next if($-[0] == 0); # the routine's own heading
Branch: 1 branch
BranchTrueFalseTotal
if $-[0] == 0 546 1872 2418
2302 1872 ▶ if($heading_level < $level
Branch: 1 branch
BranchTrueFalseTotal
if ($heading_level < $level or $heading_level == $level and $method_names->{$title}) 6 1866 1872
Condition83%5/6
$heading_level < $level or $heading_level == $level and $method_names->{$title}
l!l&&r!l&&!r
061866
$heading_level == $level and $method_names->{$title}
!ll&&!rl&&r
186336
MC/DC66%2/3
$heading_level < $level or $heading_level == $level and $method_names->{$title}
$heading_level < $level $heading_level == $level $method_names->{$title}
Truth table75%3/4
A or B and C
A= $heading_level < $levelB= $heading_level == $levelC= $method_names->{$title}
ABCresult
1XX1
0111
00X0
0100
2303 || ($heading_level == $level && $method_names->{$title})) {
2304 6 $cut = $-[0];
2305 6 last;
2306 }
2307 }
2308 546 ▶ if(defined($cut)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cut) 6 540 546
2309 6 $section = substr($section, 0, $cut);
2310 6 $section =~ s/\s+\z//;
2311 }
2312 546 $pod = $section;
2313 }
2314  
2315 # A helper documented in its own later block rather than above its sub
2316 # (`=head2 Date normalisation (standalone helper)` followed by
2317 # `date_normalise($value, $pattern) -> ...`) is documented just the same.
2318 827 ▶ if(defined($name) && $pod !~ /^=head[1-6]\s+\Q$name\E\b/m) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and not $pod =~ /^=head[1-6]\s+\Q$name\E\b/m) 281 546 827
Condition66%2/3
defined $name and not $pod =~ /^=head[1-6]\s+\Q$name\E\b/m
!ll&&!rl&&r
0546281
MC/DC50%1/2
defined $name and not $pod =~ /^=head[1-6]\s+\Q$name\E\b/m
defined $name not $pod =~ /^=head[1-6]\s+\Q$name\E\b/m
Truth table66%2/3
A and not(B)
A= defined $nameB= not $pod =~ /^=head[1-6]\s+\Q$name\E\b/m
ABresult
0X0
100
111
2319 281 my $named = $self->_pod_section_naming_routine($sub, $name);
2320 281 ▶ $pod = $named if defined($named) && length($named);
Branch: 1 branch
BranchTrueFalseTotal
if defined $named and length $named 35 246 281
Condition66%2/3
defined $named and length $named
!ll&&!rl&&r
246035
MC/DC50%1/2
defined $named and length $named
defined $named length $named
Truth table66%2/3
A and B
A= defined $namedB= length $named
ABresult
0X0
100
111
2321 }
2322  
2323 827 return $pod;
2324 }
2325  
2326 # --------------------------------------------------
2327 # _pod_section_naming_routine
2328 #
2329 # Purpose: Find a POD section that documents
2330 # a routine in its own text rather than
2331 # in its heading.
2332 #
2333 # Entry: $sub - the PPI statement for the routine.
2334 # $name - the routine's name.
2335 #
2336 # Exit: Returns the section text, from its heading
2337 # to the next heading at the same or a higher
2338 # level, or undef when no section names the
2339 # routine.
2340 #
2341 # Side effects: None.
2342 #
2343 # Notes: A section counts when a line starts with
2344 # the routine name and its argument list, which
2345 # is how these helper blocks show the call.
2346 # --------------------------------------------------
2347 281 ▶sub _pod_section_naming_routine {
Subroutine _pod_section_naming_routine: called CC=11 SCAR=24.0
2348 281 my ($self, $sub, $name) = @_;
2349  
2350 281 ▶ my $doc = eval { $sub->document } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = eval { do { $sub->document } } or return undef 281 0 281
2351 281 ▶ my $tokens = $doc->find('PPI::Token::Pod') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tokens = $doc->find("PPI::Token::Pod") or return undef 82 199 281
2352  
2353 82 my $text = join('', map { $_->content . "\n" } @$tokens);
2354  
2355 # Every heading with its offset, so a section runs to the next heading
2356 # at the same or a higher level.
2357 82 my @heads;
2358 82 ▶ while($text =~ /^=head([1-6])\s+(.+?)\s*$/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^=head([1-6])\s+(.+?)\s*$/gm) 2890 82 2972
2359 2890 push @heads, [ $-[0], $1 + 0, $2 ];
2360 }
2361  
2362 82 for my $i (0 .. $#heads) {
2363 2372 my ($start, $level) = @{ $heads[$i] };
2364 2372 my $end = length($text);
2365 2372 for my $j ($i + 1 .. $#heads) {
2366 6408 ▶ next unless $heads[$j][1] <= $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $heads[$j][1] <= $level 4122 2286 6408
2367 2286 $end = $heads[$j][0];
2368 2286 last;
2369 }
2370 2372 my $section = substr($text, $start, $end - $start);
2371  
2372 # The routine's call stands at the top of its section, before any
2373 # subsection; a passing mention deeper in another section's examples
2374 # does not make that section its documentation.
2375 # A section headed with the routine's own name documents it (modules
2376 # that keep every method's POD in one block after the subs have no
2377 # room for a synopsis line); otherwise the call line must stand at
2378 # the top of the section.
2379 2372 my ($synopsis) = $section =~ /\A(.*?)(?=^=head[3-6]\b|\z)/ms;
2380 2372 ▶ next unless (($heads[$i][2] // '') =~ /\A\Q$name\E\b/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($heads[$i][2] // "") =~ /\A\Q$name\E\b/ or defined $synopsis and $synopsis =~ /^\s*\Q$name\E\s*\(/m 2337 35 2372
Condition75%6/8
($heads[$i][2] // "") =~ /\A\Q$name\E\b/ or defined $synopsis and $synopsis =~ /^\s*\Q$name\E\s*\(/m
l!l&&r!l&&!r
3412337
$heads[$i][2] // ""
l!l
23720
defined $synopsis and $synopsis =~ /^\s*\Q$name\E\s*\(/m
!ll&&!rl&&r
023371
MC/DC0%0/3
($heads[$i][2] // "") =~ /\A\Q$name\E\b/ or defined $synopsis and $synopsis =~ /^\s*\Q$name\E\s*\(/m
$heads[$i][2] defined $synopsis $synopsis =~ /^\s*\Q$name\E\s*\(/m
Truth table25%1/4
A or B and C
A= $heads[$i][2]B= defined $synopsisC= $synopsis =~ /^\s*\Q$name\E\s*\(/m
ABCresult
1XX1
0111
00X0
0100
2381 || (defined($synopsis) && $synopsis =~ /^\s*\Q$name\E\s*\(/m);
2382  
2383 35 $section =~ s/\s+\z//;
2384 35 return $section;
2385 }
2386  
2387 47 return undef;
2388 }
2389  
2390 # --------------------------------------------------
2391 # _pod_documents_file_write
2392 #
2393 # Purpose: Decide whether a routine's POD documents
2394 # creating or writing a file as part of
2395 # its contract, so the generated schema can
2396 # turn off the harness's leftover-file
2397 # check for it.
2398 # Entry: $pod - the routine's raw POD text, or
2399 # undef when it has none.
2400 # Exit: Returns 1 if the SIDE EFFECTS section
2401 # says a file is created, written,
2402 # overwritten or updated; 0 otherwise.
2403 # Side effects: None.
2404 # Notes: Only the SIDE EFFECTS section is examined.
2405 # A file mentioned while describing an
2406 # argument or an example is not a
2407 # documented side effect.
2408 # --------------------------------------------------
2409 816 ▶sub _pod_documents_file_write {
Subroutine _pod_documents_file_write: called CC=7 SCAR=19.5
2410 816 my ($self, $pod) = @_;
2411  
2412 816 ▶ return 0 unless defined($pod) && length($pod);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod and length $pod 240 576 816
Condition100%3/3
defined $pod and length $pod
!ll&&!rl&&r
1239576
MC/DC100%2/2
defined $pod and length $pod
defined $pod length $pod
Truth table100%3/3
A and B
A= defined $podB= length $pod
ABresult
0X0
100
111
2413  
2414 576 my ($section) = $pod =~ /^=head[1-4]\s+SIDE\s+EFFECTS\b(.*?)(?=^=head[1-4]\b|\z)/mis;
2415 576 ▶ return 0 unless defined($section) && length($section);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section and length $section 508 68 576
Condition66%2/3
defined $section and length $section
!ll&&!rl&&r
508068
MC/DC50%1/2
defined $section and length $section
defined $section length $section
Truth table66%2/3
A and B
A= defined $sectionB= length $section
ABresult
0X0
100
111
2416  
2417 68 ▶ return 1 if $section =~ /\b(?:creates?|writes?|rewrites?|overwrites?|updates?)\b[^.\n]*\bfiles?\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $section =~ /\b(?:creates?|writes?|rewrites?|overwrites?|updates?)\b[^.\n]*\bfiles?\b/i 10 58 68
2418 58 ▶ return 1 if $section =~ /\bfiles?\b[^.\n]*\b(?:is|are)\s+(?:created|written|rewritten|overwritten|updated)\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $section =~ /\bfiles?\b[^.\n]*\b(?:is|are)\s+(?:created|written|rewritten|overwritten|updated)\b/i 0 58 58
2419  
2420 58 return 0;
2421 }
2422  
2423 # --------------------------------------------------
2424 # _analyze_method
2425 #
2426 # Purpose: Perform full multi-source analysis of
2427 # a single method and produce a complete
2428 # schema hashref, combining POD analysis,
2429 # code pattern detection, signature
2430 # analysis, validator schema extraction,
2431 # confidence scoring, relationship
2432 # detection, and modern Perl feature
2433 # extraction.
2434 #
2435 # Entry: $method - a method hashref as produced
2436 # by _find_methods, containing
2437 # at minimum: name, body, pod.
2438 #
2439 # Exit: Returns a schema hashref containing:
2440 # function, input, output, _confidence,
2441 # _analysis, _notes, and optionally:
2442 # new, accessor, relationships,
2443 # _yamltest_hints, _attributes,
2444 # _modern_features, _fields, _model,
2445 # _low_confidence.
2446 #
2447 # Side effects: Logs progress to stdout when verbose
2448 # is set. May carp or croak if
2449 # strict_pod is enabled and POD/code
2450 # disagreements are found.
2451 #
2452 # Notes: This is the central analysis entry
2453 # point — it orchestrates all other
2454 # analysis helpers and merges their
2455 # results. The non-invasive reasoning
2456 # layer (Model::Method, Analyzer::*)
2457 # runs after the main schema is built
2458 # and attaches metadata only.
2459 # --------------------------------------------------
2460 817 ▶sub _analyze_method {
Subroutine _analyze_method: called CC=60 SCAR=42.8
2461 817 my ($self, $method) = @_;
2462 817 my $code = $method->{body};
2463 817 my $pod = $method->{pod};
2464  
2465 # Extract modern features
2466 817 my $attributes = $self->_extract_subroutine_attributes($code);
2467 817 my $postfix_derefs = $self->_analyze_postfix_dereferencing($code);
2468 817 my $fields = $self->_extract_field_declarations($code);
2469  
2470 # If this method came from a class, use those field declarations
2471 0 ▶ if ($method->{fields} && keys %{$method->{fields}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"fields"} and keys %{$$method{"fields"};}) 0 817 817
Condition33%1/3
$method->{"fields"} and keys %{$$method{"fields"};}
!ll&&!rl&&r
81700
MC/DC0%0/2
$method->{"fields"} and keys %{$$method{"fields"};}
$method->{"fields"} keys %{$$method{"fields"};}
Truth table33%1/3
A and B
A= $method->{"fields"}B= keys %{$$method{"fields"};}
ABresult
0X0
100
111
2472 0 $fields = $method->{fields};
2473 }
2474  
2475 my $schema = {
2476 function => $method->{name},
2477 817 _confidence => {
2478 'input' => {},
2479 'output' => {}
2480 },
2481 input => {},
2482 output => {},
2483 setup => undef,
2484 transforms => {},
2485 };
2486  
2487 # Analyze different sources
2488 817 my $pod_params = $self->_analyze_pod($pod, $method->{name});
2489 817 my $code_params = $self->_analyze_code($code, $method);
2490  
2491 # A POD INPUT section that documents $class describes a class method, so
2492 # the harness has to pass the class as the invocant rather than as an
2493 # ordinary first argument.
2494 817 ▶ $schema->{invocant} = 'class' if $self->_pod_documents_class_invocant($pod);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_pod_documents_class_invocant($pod) 4 813 817
2495  
2496 # Neither analysis may hand a reference string to the schema as a parameter.
2497 817 $self->_sanitize_parameter_names($pod_params, 'POD analysis');
2498 817 $self->_sanitize_parameter_names($code_params, 'code analysis');
2499  
2500 # Validate POD/code agreement if strict mode is enabled.
2501 # Skip when there is no POD at all — strict_pod checks accuracy of
2502 # existing documentation, not whether every method is documented.
2503 817 ▶ if ($self->{strict_pod} && $pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict_pod"} and $pod) 403 414 817
Condition100%3/3
$self->{"strict_pod"} and $pod
!ll&&!rl&&r
38232403
MC/DC100%2/2
$self->{"strict_pod"} and $pod
$self->{"strict_pod"} $pod
Truth table100%3/3
A and B
A= $self->{"strict_pod"}B= $pod
ABresult
0X0
100
111
2504 my @validation_errors = $self->_validate_pod_code_agreement(
2505 $pod_params,
2506 $code_params,
2507 $method->{name},
2508 {
2509 403 ignore_self => 1,
2510 allow_renames => 1,
2511 }
2512 );
2513  
2514 403 ▶ if (@validation_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@validation_errors) 30 373 403
2515 30 my $error_msg = "POD/Code disagreement in method '$method->{name}':\n " .
2516 join("\n ", @validation_errors);
2517  
2518 # Add to schema for reference even if we croak
2519 30 $schema->{_pod_validation_errors} = \@validation_errors;
2520  
2521 # Either croak immediately or log based on configuration
2522 30 ▶ if($self->{strict_pod} == $STRICT_POD_FATAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'strict_pod'} == $STRICT_POD_FATAL) { } 4 26 30
2523 4 croak("[POD STRICT] $error_msg");
2524 } else { # 1 = warnings
2525 26 carp("[POD STRICT] $error_msg");
2526 # Continue with analysis, but mark as problematic
2527 26 $schema->{_pod_disagreement} = 1;
2528 }
2529 }
2530 399 $schema->{_strict_pod_level} = $self->{strict_pod};
2531 }
2532  
2533 813 my $validator_params = $self->_extract_validator_schema($code);
2534  
2535 813 ▶ if ($validator_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator_params) { } 16 797 813
2536 16 $schema->{input} = $validator_params->{input};
2537 16 $schema->{input_style} = 'hash';
2538 16 $schema->{_confidence}{input} = { 'factors' => [ 'Determined from validator' ], 'level' => 'high' };
2539 $schema->{_analysis}{confidence_factors}{input} = [
2540 16 'Input schema extracted from validator'
2541 ];
2542 # =head4 Input spec overrides take highest priority — apply them on top of
2543 # the validator schema so authors can tune test constraints (optional,
2544 # memberof, matches) without altering the runtime validation call.
2545 16 for my $name (keys %$pod_params) {
2546 25 ▶ next unless ($pod_params->{$name}{_source} // '') eq SOURCE_INPUT_SPEC;
Branch: 1 branch
BranchTrueFalseTotal
unless ($pod_params->{$name}{"_source"} // "") eq "input_spec" 0 25 25
Condition50%1/2
$pod_params->{$name}{"_source"} // ""
l!l
250
MC/DC0%0/1
$pod_params->{$name}{"_source"} // ""
$pod_params->{$name}{"_source"}
Truth table50%1/2
A
A= $pod_params->{$name}{"_source"}
Aresult
11
00
2547 25 my $pod_p = $pod_params->{$name};
2548 25 ▶ $schema->{input}{$name} //= {};
Condition50%1/2
$schema->{"input"}{$name} //= {}
l!l
250
MC/DC0%0/1
$schema->{"input"}{$name} //= {}
$schema->{"input"}{$name}
Truth table50%1/2
A
A= $schema->{"input"}{$name}
Aresult
11
00
2549 25 ▶ $schema->{input}{$name}{optional} = $pod_p->{optional} if defined $pod_p->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"optional"} 25 0 25
2550 25 ▶ $schema->{input}{$name}{memberof} = $pod_p->{memberof} if defined $pod_p->{memberof};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"memberof"} 0 25 25
2551 25 ▶ $schema->{input}{$name}{matches} = $pod_p->{matches} if defined $pod_p->{matches};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"matches"} 0 25 25
2552 25 ▶ $schema->{input}{$name}{type} = $pod_p->{type} if defined $pod_p->{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"type"} 25 0 25
2553 25 ▶ $schema->{input}{$name}{min} = $pod_p->{min} if defined $pod_p->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"min"} 3 22 25
2554 25 ▶ $schema->{input}{$name}{max} = $pod_p->{max} if defined $pod_p->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"max"} 3 22 25
2555 # A documented element or value shape is structural, not a
2556 # constraint the validator states.
2557 25 ▶ $schema->{input}{$name}{schema} = $pod_p->{schema} if ref($pod_p->{schema}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $pod_p->{"schema"} eq "HASH" 2 23 25
2558  
2559 # These constraints are documented, even though the field's
2560 # _source stays the type-checking marker (SOURCE_VALIDATOR).
2561 25 for my $key (qw(min max memberof matches nomatch notmemberof)) {
2562 150 ▶ next unless defined $pod_p->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod_p->{$key} 144 6 150
2563 6 $schema->{input}{$name}{_constraint_source}{$key} = SOURCE_INPUT_SPEC;
2564 }
2565 }
2566  
2567 # Not every validator extractor fills in per-parameter provenance, so
2568 # stamp the one source value here too. The harness treats a
2569 # 'validator' source as proof that the routine checks types; POD and
2570 # code-derived sources describe intent only.
2571 16 ▶ $schema->{input}{$_}{_source} //= SOURCE_VALIDATOR for keys %{$schema->{input}};
Condition50%1/2
$schema->{"input"}{$_}{"_source"} //= "validator"
l!l
049
MC/DC0%0/1
$schema->{"input"}{$_}{"_source"} //= "validator"
$schema->{"input"}{$_}{"_source"}
Truth table50%1/2
A
A= $schema->{"input"}{$_}{"_source"}
Aresult
11
00
2572 } else {
2573 # Merge field declarations into code_params before merging analyses
2574 797 ▶ if (keys %$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$fields) 0 797 797
2575 0 $self->_merge_field_declarations($code_params, $fields);
2576 }
2577  
2578 # A lone documented input parameter may be the whole argument list
2579 # under another name; the helper decides from the body and folds the
2580 # code's facts about that argument onto the documented name.
2581 797 $self->_positional_pod_input($pod_params, $code_params, $code);
2582  
2583 # Merge analyses
2584 797 $schema->{input} = $self->_merge_parameter_analyses(
2585 $pod_params,
2586 $code_params,
2587 );
2588 }
2589  
2590 # ----------------------------------------
2591 # Legacy Output Analysis (unchanged)
2592 # ----------------------------------------
2593  
2594 $schema->{output} = $self->_analyze_output(
2595 $method->{pod},
2596 $method->{body},
2597 $method->{name}
2598 813 );
2599  
2600  
2601 # Detect accessor methods
2602 813 $self->_detect_accessor_methods($method, $schema);
2603  
2604 # Detect if this is an instance method that needs object instantiation
2605 # Constructors never require object instantiation
2606 813 my $needs_object = $self->_needs_object_instantiation($method->{name}, $method->{body}, $method);
2607 813 ▶ if($method->{name} ne 'new' && $needs_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"name"} ne "new" and $needs_object) 396 417 813
Condition100%3/3
$method->{"name"} ne "new" and $needs_object
!ll&&!rl&&r
98319396
MC/DC100%2/2
$method->{"name"} ne "new" and $needs_object
$method->{"name"} ne "new" $needs_object
Truth table100%3/3
A and B
A= $method->{"name"} ne "new"B= $needs_object
ABresult
0X0
100
111
2608 396 $schema->{new} = $needs_object;
2609 396 $self->_log(" NEW: Method requires object instantiation: $needs_object");
2610 }
2611  
2612 # Calculate confidences
2613 813 my $input_confidence = $schema->{_confidence}{'input'};
2614 813 ▶ if(!ref($input_confidence)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $input_confidence) 0 813 813
2615 0 $input_confidence = $schema->{_confidence}{'input'} = $self->_calculate_input_confidence($schema->{input});
2616 }
2617 813 my $output_confidence = $schema->{_confidence}{'output'} = $self->_calculate_output_confidence($schema->{output});
2618  
2619 # Add metadata
2620 813 $schema->{_notes} = $self->_generate_notes($schema->{input});
2621  
2622 # Add analytics
2623 813 ▶ $schema->{_analysis} ||= {};
Condition100%2/2
$schema->{"_analysis"} ||= {}
l!l
16797
MC/DC100%1/1
$schema->{"_analysis"} ||= {}
$schema->{"_analysis"}
Truth table100%2/2
A
A= $schema->{"_analysis"}
Aresult
11
00
2624 813 $schema->{_analysis}{input_confidence} = $input_confidence->{level};
2625 813 $schema->{_analysis}{output_confidence} = $output_confidence->{level};
2626 813 ▶ $schema->{_analysis}{confidence_factors} ||= {};
Condition100%2/2
$schema->{"_analysis"}{"confidence_factors"} ||= {}
l!l
16797
MC/DC100%1/1
$schema->{"_analysis"}{"confidence_factors"} ||= {}
$schema->{"_analysis"}{"confidence_factors"}
Truth table100%2/2
A
A= $schema->{"_analysis"}{"confidence_factors"}
Aresult
11
00
2627 813 ▶ $schema->{_analysis}{confidence_factors}{input} ||= $input_confidence->{factors};
Condition100%3/3
$schema->{"_analysis"}{"confidence_factors"}{"input"} ||= $input_confidence->{"factors"}
l!l&&r!l&&!r
166791
MC/DC100%2/2
$schema->{"_analysis"}{"confidence_factors"}{"input"} ||= $input_confidence->{"factors"}
$schema->{"_analysis"}{"confidence_factors"}{"input"} $input_confidence->{"factors"}
Truth table100%3/3
A ||= B
A= $schema->{"_analysis"}{"confidence_factors"}{"input"}B= $input_confidence->{"factors"}
ABresult
1X1
011
000
2628 813 ▶ $schema->{_analysis}{confidence_factors}{output} ||= $output_confidence->{factors};
Condition33%1/3
$schema->{"_analysis"}{"confidence_factors"}{"output"} ||= $output_confidence->{"factors"}
l!l&&r!l&&!r
08130
MC/DC0%0/2
$schema->{"_analysis"}{"confidence_factors"}{"output"} ||= $output_confidence->{"factors"}
$schema->{"_analysis"}{"confidence_factors"}{"output"} $output_confidence->{"factors"}
Truth table33%1/3
A ||= B
A= $schema->{"_analysis"}{"confidence_factors"}{"output"}B= $output_confidence->{"factors"}
ABresult
1X1
011
000
2629  
2630 813 foreach my $mode('input', 'output') {
2631 1626 $self->_set_defaults($schema, $mode);
2632 }
2633  
2634 # Optionally store detailed per-parameter analysis
2635 813 ▶ if ($input_confidence->{per_parameter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_confidence->{"per_parameter"}) 0 813 813
2636 0 $schema->{_analysis}{per_parameter_scores} = $input_confidence->{per_parameter};
2637 }
2638  
2639 # Calculate overall confidence (for backward compatibility)
2640 813 my $input_level = $input_confidence->{level};
2641 813 my $output_level = $output_confidence->{level};
2642  
2643 813 my %level_rank = (
2644 none => 0,
2645 very_low => 1,
2646 low => 2,
2647 medium => 3,
2648 high => 4
2649 );
2650  
2651 # Overall is the lower of input and output
2652 813 ▶ $input_level //= 'none';
Condition100%2/2
$input_level //= "none"
l!l
96717
MC/DC100%1/1
$input_level //= "none"
$input_level
Truth table100%2/2
A
A= $input_level
Aresult
11
00
2653 813 ▶ $output_level //= 'none';
Condition50%1/2
$output_level //= "none"
l!l
8130
MC/DC0%0/1
$output_level //= "none"
$output_level
Truth table50%1/2
A
A= $output_level
Aresult
11
00
2654 813 ▶ my $overall = $level_rank{$input_level} < $level_rank{$output_level} ? $input_level : $output_level;
Branch: 1 branch
BranchTrueFalseTotal
$level_rank{$input_level} < $level_rank{$output_level} ? : 709 104 813
2655  
2656 813 $schema->{_analysis}{overall_confidence} = $overall;
2657  
2658 # Analyze parameter relationships
2659 813 my $relationships = $self->_analyze_relationships($method);
2660 813 ▶ if ($relationships && @{$relationships}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relationships and @{$relationships;}) 7 806 813
Condition66%2/3
$relationships and @{$relationships;}
!ll&&!rl&&r
08067
MC/DC50%1/2
$relationships and @{$relationships;}
$relationships @{$relationships;}
Truth table66%2/3
A and B
A= $relationshipsB= @{$relationships;}
ABresult
0X0
100
111
2661 7 $schema->{relationships} = $relationships;
2662 7 $self->_log(" Found " . scalar(@$relationships) . " parameter relationships");
2663 }
2664  
2665 # Store modern feature info in schema
2666 813 ▶ $schema->{_attributes} = $attributes if keys %$attributes;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$attributes 0 813 813
2667 813 ▶ $schema->{_modern_features}{postfix_dereferencing} = $postfix_derefs if keys %$postfix_derefs;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$postfix_derefs 0 813 813
2668 813 ▶ $schema->{_fields} = $fields if keys %$fields;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$fields 0 813 813
2669  
2670 # Store class info if this is a class method
2671 813 ▶ if ($method->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"class"}) 0 813 813
2672 0 $schema->{_class} = $method->{class};
2673 }
2674  
2675 813 my $hints = $self->_extract_test_hints($method, $schema);
2676 813 $self->_extract_pod_examples($pod, $hints);
2677  
2678 813 for my $k (qw(boundary_values invalid_inputs valid_inputs equivalence_classes)) {
2679 3252 my %seen;
2680 $hints->{$k} = [
2681 213 ▶ grep { !$seen{ defined $_ ? $_ : '__undef__' }++ }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 213 0 213
2682 3252 @{ $hints->{$k} }
2683 ];
2684 }
2685  
2686 # --------------------------------------------------
2687 # YAML test hints: numeric boundaries
2688 # --------------------------------------------------
2689 813 ▶ if ($self->_method_has_numeric_intent($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_method_has_numeric_intent($schema)) 184 629 813
2690 184 ▶ $schema->{_yamltest_hints} ||= {};
Condition50%1/2
$schema->{"_yamltest_hints"} ||= {}
l!l
0184
MC/DC0%0/1
$schema->{"_yamltest_hints"} ||= {}
$schema->{"_yamltest_hints"}
Truth table50%1/2
A
A= $schema->{"_yamltest_hints"}
Aresult
11
00
2691  
2692 # Do not override existing hints
2693 184 ▶ $schema->{_yamltest_hints}{boundary_values} ||= [];
Condition50%1/2
$schema->{"_yamltest_hints"}{"boundary_values"} ||= []
l!l
0184
MC/DC0%0/1
$schema->{"_yamltest_hints"}{"boundary_values"} ||= []
$schema->{"_yamltest_hints"}{"boundary_values"}
Truth table50%1/2
A
A= $schema->{"_yamltest_hints"}{"boundary_values"}
Aresult
11
00
2694  
2695 0 ▶ my %seen = map { (defined $_ ? $_ : '__undef__') => 1 }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
2696 184 @{ $schema->{_yamltest_hints}{boundary_values} };
2697  
2698 184 foreach my $v (@{ $self->_numeric_boundary_values }) {
2699 919 ▶ my $key = defined $v ? $v : '__undef__';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 918 1 919
2700 919 ▶ push @{ $schema->{_yamltest_hints}{boundary_values} }, $v unless $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$key}++ 919 0 919
2701 }
2702  
2703 184 $self->_log(' HINTS: Added numeric boundary values');
2704 }
2705  
2706 813 ▶ if (keys %$hints) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$hints) 813 0 813
2707 813 ▶ $schema->{_yamltest_hints} ||= {};
Condition100%2/2
$schema->{"_yamltest_hints"} ||= {}
l!l
184629
MC/DC100%1/1
$schema->{"_yamltest_hints"} ||= {}
$schema->{"_yamltest_hints"}
Truth table100%2/2
A
A= $schema->{"_yamltest_hints"}
Aresult
11
00
2708 813 foreach my $k (keys %$hints) {
2709 $schema->{_yamltest_hints}{$k} = $hints->{$k}
2710 3252 ▶ unless exists $schema->{_yamltest_hints}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"_yamltest_hints"}{$k} 3068 184 3252
2711 }
2712 }
2713  
2714 813 ▶ if(($level_rank{$overall} < $level_rank{$LEVEL_MEDIUM}) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($level_rank{$overall} < $level_rank{$LEVEL_MEDIUM} and $level_rank{$overall} < $self->{"confidence_threshold"} * 4) 718 95 813
Condition100%3/3
$level_rank{$overall} < $level_rank{$LEVEL_MEDIUM} and $level_rank{$overall} < $self->{"confidence_threshold"} * 4
!ll&&!rl&&r
1184718
MC/DC100%2/2
$level_rank{$overall} < $level_rank{$LEVEL_MEDIUM} and $level_rank{$overall} < $self->{"confidence_threshold"} * 4
$level_rank{$overall} < $level_rank{$LEVEL_MEDIUM} $level_rank{$overall} < $self->{"confidence_threshold"} * 4
Truth table100%3/3
A and B
A= $level_rank{$overall} < $level_rank{$LEVEL_MEDIUM}B= $level_rank{$overall} < $self->{"confidence_threshold"} * 4
ABresult
0X0
100
111
2715 ($level_rank{$overall} < ($self->{confidence_threshold} * 4))) {
2716 718 $schema->{_low_confidence} = 1
2717 }
2718  
2719 # ----------------------------------------
2720 # Non-invasive reasoning layer
2721 # ----------------------------------------
2722  
2723 my $method_model = App::Test::Generator::Model::Method->new(
2724 name => $method->{name},
2725 source => $method->{body},
2726 813 );
2727  
2728 813 my $return_analyzer = App::Test::Generator::Analyzer::Return->new();
2729 813 $return_analyzer->analyze($method_model);
2730  
2731 # Let model learn from finalized schema
2732 813 ▶ if ($schema->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}) 813 0 813
2733 813 $method_model->absorb_legacy_output($schema->{output});
2734 }
2735  
2736 813 $method_model->resolve_return_type();
2737 813 $method_model->resolve_classification();
2738 813 $method_model->resolve_confidence();
2739  
2740 # Attach only metadata
2741 $schema->{_model} = {
2742 813 classification => $method_model->classification,
2743 confidence => $method_model->confidence,
2744 };
2745  
2746 # ----------------------------------------
2747 # Return Meta Analysis (Non-invasive)
2748 # ----------------------------------------
2749  
2750 813 my $meta = App::Test::Generator::Analyzer::ReturnMeta->new();
2751 813 my $analysis = $meta->analyze($schema);
2752  
2753 813 $schema->{_analysis}{stability_score} = $analysis->{stability_score};
2754 813 $schema->{_analysis}{consistency_score} = $analysis->{consistency_score};
2755 813 $schema->{_analysis}{risk_flags} = $analysis->{risk_flags};
2756  
2757 # ----------------------------------------
2758 # Side Effect Analysis (Non-invasive)
2759 # ----------------------------------------
2760  
2761 813 my $se = App::Test::Generator::Analyzer::SideEffect->new();
2762  
2763 813 my $effects = $se->analyze($method);
2764  
2765 813 $schema->{_analysis}{side_effects} = $effects;
2766  
2767 # A routine whose documented side effects include creating or writing a
2768 # file is doing its job when it leaves that file behind, so the harness's
2769 # leftover-file check must not report it. Record the contract here for
2770 # _write_schema to emit as check_files: 0.
2771 813 ▶ if($self->_pod_documents_file_write($pod)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pod_documents_file_write($pod)) 9 804 813
2772 9 $schema->{_analysis}{side_effects}{writes_file} = 1;
2773 }
2774  
2775 # ----------------------------------------
2776 # Complexity Analysis (Non-invasive)
2777 # ----------------------------------------
2778  
2779 813 my $cx = App::Test::Generator::Analyzer::Complexity->new();
2780 813 my $complexity = $cx->analyze($method);
2781  
2782 813 $schema->{_analysis}{complexity} = $complexity;
2783  
2784 813 return $schema;
2785 }
2786  
2787 # --------------------------------------------------
2788 # _method_has_numeric_intent
2789 #
2790 # Purpose: Determine whether a method schema
2791 # has numeric intent — either a numeric
2792 # output type or at least one required
2793 # numeric input parameter — to decide
2794 # whether to add standard numeric
2795 # boundary hint values.
2796 #
2797 # Entry: $schema - schema hashref as built by
2798 # _analyze_method.
2799 #
2800 # Exit: Returns 1 if numeric intent is
2801 # detected, 0 otherwise.
2802 #
2803 # Side effects: None.
2804 # --------------------------------------------------
2805 813 ▶sub _method_has_numeric_intent {
Subroutine _method_has_numeric_intent: called CC=9 SCAR=22.0
2806 813 my ($self, $schema) = @_;
2807  
2808 # Numeric output
2809 813 ▶ return 1 if ($schema->{output} && $schema->{output}{type} && $schema->{output}{type} =~ /^(number|integer)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"output"} and $schema->{"output"}{"type"} and $schema->{"output"}{"type"} =~ /^(number|integer)$/ 159 654 813
Condition83%5/6
$schema->{"output"} and $schema->{"output"}{"type"} and $schema->{"output"}{"type"} =~ /^(number|integer)$/
!ll&&!rl&&r
26628159
$schema->{"output"} and $schema->{"output"}{"type"}
!ll&&!rl&&r
026787
MC/DC66%2/3
$schema->{"output"} and $schema->{"output"}{"type"} and $schema->{"output"}{"type"} =~ /^(number|integer)$/
$schema->{"output"} $schema->{"output"}{"type"} $schema->{"output"}{"type"} =~ /^(number|integer)$/
Truth table75%3/4
A and B and C
A= $schema->{"output"}B= $schema->{"output"}{"type"}C= $schema->{"output"}{"type"} =~ /^(number|integer)$/
ABCresult
0XX0
10X0
1100
1111
2810  
2811 # Numeric inputs
2812 654 ▶ foreach my $p (values %{ $schema->{input} || {} }) {
Condition100%2/2
$schema->{'input'} || {}
l!l
61836
MC/DC100%1/1
$schema->{'input'} || {}
$schema->{'input'}
Truth table100%2/2
A
A= $schema->{'input'}
Aresult
11
00
2813 659 ▶ next if $p->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"optional"} 342 317 659
2814 317 ▶ return 1 if ($p->{type} && $p->{type} =~ /^(number|integer)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} and $p->{"type"} =~ /^(number|integer)$/ 25 292 317
Condition66%2/3
$p->{"type"} and $p->{"type"} =~ /^(number|integer)$/
!ll&&!rl&&r
029225
MC/DC50%1/2
$p->{"type"} and $p->{"type"} =~ /^(number|integer)$/
$p->{"type"} $p->{"type"} =~ /^(number|integer)$/
Truth table66%2/3
A and B
A= $p->{"type"}B= $p->{"type"} =~ /^(number|integer)$/
ABresult
0X0
100
111
2815 }
2816  
2817 629 return 0;
2818 }
2819  
2820 # --------------------------------------------------
2821 # _numeric_boundary_values
2822 #
2823 # Purpose: Return the standard set of numeric
2824 # boundary values used as test hints
2825 # for methods with numeric intent.
2826 #
2827 # Entry: None.
2828 #
2829 # Exit: Returns an arrayref of boundary
2830 # values: [-1, 0, 1, 2, 100].
2831 #
2832 # Side effects: None.
2833 # --------------------------------------------------
2834 183 ▶sub _numeric_boundary_values {
Subroutine _numeric_boundary_values: called CC=1 SCAR=0.0
2835 183 return [ -1, 0, 1, 2, 100 ];
2836 }
2837  
2838 # --------------------------------------------------
2839 # _detect_accessor_methods
2840 #
2841 # Purpose: Detect whether a method is a getter,
2842 # setter, or combined getter/setter
2843 # accessor by analysing assignment and
2844 # return patterns involving $self->{...}.
2845 #
2846 # Entry: $method - method hashref containing
2847 # at minimum 'body' and
2848 # optionally 'pod'.
2849 # $schema - schema hashref (modified
2850 # in place).
2851 #
2852 # Exit: Returns nothing. Modifies $schema in
2853 # place, setting accessor, input,
2854 # input_style, output, and _confidence
2855 # keys as appropriate.
2856 #
2857 # Side effects: Croaks if a getter/setter has more
2858 # than one argument, or if a setter
2859 # returns non-self data.
2860 # Logs detections to stdout when
2861 # verbose is set.
2862 #
2863 # Notes: Four accessor patterns are detected
2864 # in order: (1) combined getter/setter
2865 # with shift, (2) combined getter/setter
2866 # with validated input, (3) getter only,
2867 # (4) setter that returns $self. Methods
2868 # accessing multiple $self fields are
2869 # skipped immediately.
2870 # --------------------------------------------------
2871 813 ▶sub _detect_accessor_methods {
Subroutine _detect_accessor_methods: called CC=64 SCAR=50.8
2872 813 my ($self, $method, $schema) = @_;
2873  
2874 # A constructor is not an accessor, even when it stores one field: the
2875 # harness may call new() with no arguments and let the defaults apply,
2876 # and its argument types come from the validator, not from the one
2877 # assignment the body happens to make. Geocode::new stores only
2878 # _geocoder on the conditional path, and the accessor reading typed its
2879 # whole parameter list as a single string, so instance methods were
2880 # built with new(geocoder => 'test') and the constructor rightly refused.
2881 813 ▶ return if(($method->{name} // '') eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"name"} // "") eq "new" 98 715 813
Condition50%1/2
$method->{"name"} // ""
l!l
8130
MC/DC0%0/1
$method->{"name"} // ""
$method->{"name"}
Truth table50%1/2
A
A= $method->{"name"}
Aresult
11
00
2882  
2883 715 my $body = $method->{body};
2884  
2885 # Normalize whitespace for regex sanity
2886 715 my $code = $body;
2887 715 $code =~ s/\s+/ /g;
2888  
2889 # If a method touches more than one $self->{...}, it’s not an accessor.
2890 715 my %fields_seen;
2891 715 ▶ while ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}/g) 398 715 1113
2892 398 $fields_seen{$1}++;
2893 }
2894 715 ▶ if (keys(%fields_seen) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %fields_seen > 1) 43 672 715
2895 43 $self->_log(" Skipping accessor detection: multiple fields accessed");
2896 43 return;
2897 }
2898  
2899 # A store guarded by the presence of an argument is the same get/set
2900 # idiom as `if (@_)`, just written against a named lexical:
2901 #
2902 # my $status = shift;
2903 # return $self->{status} = $status if(defined($status));
2904 # ...
2905 # return $self->{status} || 200;
2906 #
2907 # CGI::Info::status is this shape, and it was being typed as a plain
2908 # function because every branch below only looked for `if (@_)`.
2909 672 my $conditional_arg = 0;
2910 672 ▶ while($code =~ /\bmy\s+\$(\w+)\s*=\s*shift\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\bmy\s+\$(\w+)\s*=\s*shift\b/g) 78 651 729
2911 78 my $arg = $1;
2912 # The invocant is shifted too, but a test on $self says nothing
2913 # about whether an argument was supplied.
2914 78 ▶ next if($arg eq 'self');
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "self" 28 50 78
2915 50 ▶ if($code =~ /(?:if|unless)\s*\(?\s*(?:defined\s*\(?\s*)?\$\Q$arg\E\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:if|unless)\s*\(?\s*(?:defined\s*\(?\s*)?\$\Q$arg\E\b/) 21 29 50
2916 21 $conditional_arg = 1;
2917 21 last;
2918 }
2919 }
2920 # `if (@_)` stays the strongest signal; a named-argument guard is
2921 # weaker evidence and must not turn a heuristic miss into a croak.
2922 # The guard must test the argument itself: CGI::Info::protocol reads
2923 # if($ENV{'SCRIPT_URI'}) and stores a value it computed, which says
2924 # nothing about how the method is called. Reading any `if($x)` as an
2925 # argument guard typed it as a getset accessor, and the harness then
2926 # set a value the routine never looks at.
2927 672 ▶ my $arg_guard = ($code =~ /\bif\s*\(\s*(?:\@_|scalar\s*\(\s*\@_\s*\))/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /\bif\s*\(\s*(?:\@_|scalar\s*\(\s*\@_\s*\))/ ? : 7 665 672
2928  
2929 # -------------------------------
2930 # Getter/Setter combo
2931 # -------------------------------
2932 672 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/ and $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/ and $code =~ /if\s*\(\s*\@_/) { } 0 672 672
elsif ($arg_guard || $conditional_arg and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x and $code =~ /return\b/) { } 6 666 672
elsif ($code =~ /return\s+\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*;/) { } 39 627 666
elsif ($code =~ /return\s+\$self\b/ and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/) { } 6 1220 1226
Condition66%4/6
$code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/ and $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/ and $code =~ /if\s*\(\s*\@_/
!ll&&!rl&&r
67110
$code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/ and $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/
!ll&&!rl&&r
67101
MC/DC0%0/3
$code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/ and $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/ and $code =~ /if\s*\(\s*\@_/
$code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/ $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/ $code =~ /if\s*\(\s*\@_/
Truth table50%2/4
A and B and C
A= $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/B= $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/C= $code =~ /if\s*\(\s*\@_/
ABCresult
0XX0
10X0
1100
1111
2933 # Require get/set of the same property
2934 $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/ &&
2935 $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/ &&
2936 $code =~ /if\s*\(\s*\@_/
2937 ) {
2938 0 my $property = $1;
2939  
2940 0 ▶ if(!defined($property)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $property) 0 0 0
2941 0 ▶ if($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/) 0 0 0
2942 0 $property = $1;
2943 }
2944 }
2945  
2946 $schema->{accessor} = {
2947 0 type => 'getset',
2948 property => $property,
2949 };
2950  
2951 0 $self->_log(" Detected getter/setter accessor for property: $property");
2952  
2953 0 ▶ $schema->{input} ||= { value => { type => 'string', optional => 1 } };
Condition0%0/2
$schema->{"input"} ||= {"value", {"type", "string", "optional", 1}}
l!l
00
MC/DC0%0/1
$schema->{"input"} ||= {"value", {"type", "string", "optional", 1}}
$schema->{"input"}
Truth table0%0/2
A
A= $schema->{"input"}
Aresult
11
00
2954  
2955 0 $schema->{input_style} = 'hash';
2956  
2957 $schema->{_confidence}{input} = {
2958 0 level => 'high',
2959 factors => ['Detected combined getter/setter accessor'],
2960 };
2961 0 ▶ if (my $pod = $method->{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pod = $method->{"pod"}) 0 0 0
2962 0 ▶ if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) 0 0 0
2963 0 my $class = $1;
2964 $schema->{output} = {
2965 0 type => 'object',
2966 isa => $class,
2967 };
2968 0 $schema->{input}{$property} = {
2969 type => 'object',
2970 isa => $class,
2971 optional => 1,
2972 };
2973  
2974 $schema->{_confidence}{output} = {
2975 0 level => 'high',
2976 factors => ['POD specifies UserAgent object'],
2977 };
2978 }
2979 }
2980 ▶ } elsif(($arg_guard || $conditional_arg) &&
Condition88%8/9
$arg_guard || $conditional_arg and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x and $code =~ /return\b/
!ll&&!rl&&r
66606
$arg_guard || $conditional_arg and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x
!ll&&!rl&&r
644226
$arg_guard or $conditional_arg
l!l&&r!l&&!r
721644
MC/DC75%3/4
$arg_guard || $conditional_arg and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x and $code =~ /return\b/
$arg_guard $conditional_arg $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x $code =~ /return\b/
Truth table71%5/7
A or B and C and D
A= $arg_guardB= $conditional_argC= $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/xD= $code =~ /return\b/
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
2981 $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x &&
2982 $code =~ /return\b/
2983 ) {
2984 # -------------------------------
2985 # Getter/Setter (validated input)
2986 # -------------------------------
2987 6 my $property = $1;
2988  
2989 # A named-argument guard is weaker evidence than `if (@_)`, so when
2990 # the method declares more than one parameter assume this is not an
2991 # accessor rather than croaking further down on an ambiguous match.
2992 6 ▶ if($conditional_arg && !$arg_guard &&
Branch: 1 branch
BranchTrueFalseTotal
if ($conditional_arg and not $arg_guard and ref $schema->{"input"} eq "HASH" and scalar keys %{$$schema{"input"};} > 1) 0 6 6
Condition66%6/9
$conditional_arg and not $arg_guard and ref $schema->{"input"} eq "HASH" and scalar keys %{$$schema{"input"};} > 1
!ll&&!rl&&r
420
$conditional_arg and not $arg_guard and ref $schema->{"input"} eq "HASH"
!ll&&!rl&&r
402
$conditional_arg and not $arg_guard
!ll&&!rl&&r
402
MC/DC0%0/4
$conditional_arg and not $arg_guard and ref $schema->{"input"} eq "HASH" and scalar keys %{$$schema{"input"};} > 1
$conditional_arg not $arg_guard ref $schema->{"input"} eq "HASH" scalar keys %{$$schema{"input"};} > 1
Truth table40%2/5
A and not(B) and C and D
A= $conditional_argB= not $arg_guardC= ref $schema->{"input"} eq "HASH"D= scalar keys %{$$schema{"input"};} > 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2993 2 (ref($schema->{input}) eq 'HASH') && (scalar(keys %{$schema->{input}}) > 1)) {
2994 0 $self->_log(" Skipping conditional accessor: more than one argument");
2995 0 return;
2996 }
2997  
2998 6 ▶ if(!defined($property)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $property) 6 0 6
2999 6 ▶ if($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=/) 6 0 6
3000 6 $property = $1;
3001 }
3002 }
3003 6 ▶ if ($code =~ /validate_strict/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /validate_strict/) { } 2 4 6
3004 2 push @{ $schema->{_confidence}{input}{factors} }, 'Setter uses Params::Validate::Strict';
3005 } else {
3006 # ---------------------------------------
3007 # Detect object input via blessed($arg)
3008 # ---------------------------------------
3009 4 ▶ if ($code =~ /blessed\s*\(\s*\$(\w+)\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /blessed\s*\(\s*\$(\w+)\s*\)/) { } 0 4 4
3010 0 my $param = $1;
3011  
3012 0 $self->_log(" Detected object input via blessed(\$$param)");
3013  
3014 $schema->{input} = {
3015 0 $param => {
3016 type => 'object',
3017 optional => 1,
3018 }
3019 };
3020  
3021 $schema->{_confidence}{input} = {
3022 0 level => 'high',
3023 factors => ['Input validated by Scalar::Util::blessed'],
3024 };
3025 } else {
3026 # fallback ONLY if nothing known
3027 $schema->{input} ||= {
3028 4 ▶ value => { type => 'string', optional => 1 },
Condition50%1/2
$schema->{"input"} ||= {"value", {"type", "string", "optional", 1}}
l!l
40
MC/DC0%0/1
$schema->{"input"} ||= {"value", {"type", "string", "optional", 1}}
$schema->{"input"}
Truth table50%1/2
A
A= $schema->{"input"}
Aresult
11
00
3029 };
3030 }
3031 };
3032 $schema->{accessor} = {
3033 6 type => 'getset',
3034 property => $property,
3035 };
3036  
3037 6 $self->_log(" Detected getter/setter accessor for property: $property");
3038 6 ▶ if (my $pod = $method->{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pod = $method->{"pod"}) 2 4 6
3039 2 ▶ if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) 0 2 2
3040 0 my $class = $1;
3041 $schema->{output} = {
3042 0 type => 'object',
3043 isa => $class,
3044 };
3045 0 $schema->{input}{$property} = {
3046 type => 'object',
3047 isa => $class,
3048 optional => 1,
3049 };
3050  
3051 $schema->{_confidence}{output} = {
3052 0 level => 'high',
3053 factors => ['POD specifies UserAgent object'],
3054 };
3055 }
3056 }
3057 # Set position 0 on whichever single input parameter already exists.
3058 # The parameter may be named differently from the stored property
3059 # (e.g. $dir for the logdir property), so find the existing key
3060 # rather than creating a new entry keyed by $property — doing the
3061 # latter produces a duplicate-position collision between the real
3062 # param and the spurious $property key.
3063 6 ▶ if(ref($schema->{input}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"input"} eq "HASH") 6 0 6
3064 6 my @input_keys = keys %{$schema->{input}};
3065 6 ▶ if(scalar @input_keys > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @input_keys > 1) { } 0 6 6
elsif (@input_keys == 1) { } 6 0 6
3066 0 croak(__PACKAGE__, ': A getset accessor function can have at most one argument');
3067 } elsif(@input_keys == 1) {
3068 6 $schema->{input}{$input_keys[0]}{position} = 0;
3069 } else {
3070 0 $schema->{input}{$property}{position} = 0;
3071 }
3072 }
3073 } elsif ($code =~ /return\s+\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*;/) {
3074 # -------------------------------
3075 # Getter
3076 # -------------------------------
3077 39 my $property = $1;
3078  
3079 # Don't flag mutators like
3080 # sub foo {
3081 # my $self = shift;
3082 # $self->{bar} = shift;
3083 # return $self->{bar};
3084 # }
3085 # Only exclude if the property is being set FROM EXTERNAL INPUT
3086 39 ▶ if($code !~ /\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*=\s*(?:shift|\$\w+\s*=\s*shift|\@_|\$_\[\d+\])/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code =~ /\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*=\s*(?:shift|\$\w+\s*=\s*shift|\@_|\$_\[\d+\])/) 38 1 39
3087 38 my @returns = $code =~ /return\b/g;
3088 38 my @self_returns = $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}/g;
3089 # it's a getter
3090 38 ▶ if (scalar(@returns) == scalar(@self_returns)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @returns == scalar @self_returns) 36 2 38
3091 # all returns are returning $self->{$property}, so it's a getter
3092 $schema->{accessor} = {
3093 36 type => 'getter',
3094 property => $property,
3095 };
3096  
3097 36 $self->_log(" Detected getter accessor for property: $property");
3098  
3099 # A memoised property's initializer is the only evidence of
3100 # what the getter returns:
3101 # $self->{proposals} //= [ map { ... } @{ $self->survivors } ];
3102 # Without it the expression looks like a scalar, and the
3103 # harness validates the returned arrayref as a string.
3104 36 ▶ $schema->{output} ||= {};
Condition50%1/2
$schema->{"output"} ||= {}
l!l
360
MC/DC0%0/1
$schema->{"output"} ||= {}
$schema->{"output"}
Truth table50%1/2
A
A= $schema->{"output"}
Aresult
11
00
3105 36 ▶ if($code =~ /\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*\/\/=\s*\[/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ m(\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*//=\s*\[)) { } 3 33 36
elsif ($code =~ m[\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*//=\s*\{]) { } 0 33 33
3106 3 $schema->{output}{type} = 'arrayref';
3107 } elsif($code =~ /\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*\/\/=\s*\{/) {
3108 0 $schema->{output}{type} = 'hashref';
3109 }
3110  
3111 $schema->{_confidence}{output} = {
3112 36 level => 'high',
3113 factors => ['Detected getter method'],
3114 };
3115 36 delete $schema->{input};
3116 }
3117 }
3118 ▶ } elsif (
Condition100%3/3
$code =~ /return\s+\$self\b/ and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/
!ll&&!rl&&r
599226
MC/DC100%2/2
$code =~ /return\s+\$self\b/ and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/
$code =~ /return\s+\$self\b/ $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/
Truth table100%3/3
A and B
A= $code =~ /return\s+\$self\b/B= $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/
ABresult
0X0
100
111
3119 $code =~ /return\s+\$self\b/ &&
3120 $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/
3121 ) {
3122 # -------------------------------
3123 # Setter
3124 # -------------------------------
3125 6 my ($property, $param) = ($1, $2);
3126  
3127 $schema->{accessor} = {
3128 6 type => 'setter',
3129 property => $property,
3130 param => $param,
3131 };
3132  
3133 6 $self->_log(" Detected setter accessor for property: $property");
3134  
3135 # A setter that hands the argument to something else before storing
3136 # it, such as CGI::Info::set_logger's
3137 #
3138 # $self->{'logger'} = Log::Abstraction->new($logger);
3139 #
3140 # does not constrain what the argument may be, so the 'string' default
3141 # would invent cases like "a hash ref must die" that the routine
3142 # rightly accepts. Claim 'string' only when every assignment stores the
3143 # argument itself.
3144 6 my $param_type = 'string';
3145 6 ▶ while($code =~ /\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*=\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*=\s*([^;]+);/g) 7 5 12
3146 7 ▶ if($1 =~ /(?:->|::)\w+\s*\([^)]*\$\Q$param\E\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 =~ /(?:->|::)\w+\s*\([^)]*\$\Q$param\E\b/) 1 6 7
3147 1 $param_type = 'any';
3148 1 last;
3149 }
3150 }
3151  
3152 $schema->{input} = {
3153 6 $param => { type => $param_type }, # safe default
3154 };
3155 6 $schema->{input_style} = 'hash';
3156  
3157 $schema->{_confidence}{input} = {
3158 6 level => 'high',
3159 factors => ['Detected setter/accessor method'],
3160 };
3161 6 ▶ if($schema->{output}{_returns_self}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($schema->{'output'}{'_returns_self'}) { } 6 0 6
elsif (scalar keys %{$$schema{'output'};} != 0) { } 0 0 0
3162 6 ▶ if($schema->{output}{type} ne 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"type"} ne "object") 0 6 6
3163 0 croak 'Setter can not return data other than $self';
3164 }
3165 6 ▶ if($schema->{output}{isa} ne $self->{_package_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"isa"} ne $self->{"_package_name"}) 0 6 6
3166 0 croak 'Setter can not return data other than $self';
3167 }
3168 0 } elsif(scalar(keys %{$schema->{output}}) != 0) {
3169 $self->_analysis_error(
3170 method => $method->{name},
3171 0 message => "Setter cannot return data",
3172 );
3173 }
3174 }
3175  
3176 672 ▶ if(exists($schema->{accessor})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"accessor"}) 48 624 672
3177 48 ▶ if($schema->{accessor}{type} && $schema->{accessor}{type} =~ /setter|getset/ && $schema->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"accessor"}{"type"} and $schema->{"accessor"}{"type"} =~ /setter|getset/ and $schema->{"input"}) 12 36 48
Condition66%4/6
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"type"} =~ /setter|getset/ and $schema->{"input"}
!ll&&!rl&&r
36012
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"type"} =~ /setter|getset/
!ll&&!rl&&r
03612
MC/DC33%1/3
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"type"} =~ /setter|getset/ and $schema->{"input"}
$schema->{"accessor"}{"type"} $schema->{"accessor"}{"type"} =~ /setter|getset/ $schema->{"input"}
Truth table50%2/4
A and B and C
A= $schema->{"accessor"}{"type"}B= $schema->{"accessor"}{"type"} =~ /setter|getset/C= $schema->{"input"}
ABCresult
0XX0
10X0
1100
1111
3178 12 for my $param (keys %{ $schema->{input} }) {
3179 12 my $in = $schema->{input}{$param};
3180  
3181 12 ▶ if ($in->{type} && ($in->{type} eq 'object')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->{"type"} and $in->{"type"} eq "object") 2 10 12
Condition100%3/3
$in->{"type"} and $in->{"type"} eq "object"
!ll&&!rl&&r
282
MC/DC100%2/2
$in->{"type"} and $in->{"type"} eq "object"
$in->{"type"} $in->{"type"} eq "object"
Truth table100%3/3
A and B
A= $in->{"type"}B= $in->{"type"} eq "object"
ABresult
0X0
100
111
3182 $schema->{output} = {
3183 type => 'object',
3184 2 ▶ ($in->{isa} ? (isa => $in->{isa}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$in->{'isa'} ? : 0 2 2
3185 };
3186  
3187 $schema->{_confidence}{output} = {
3188 2 level => 'high',
3189 factors => ['Output type propagated from setter input'],
3190 };
3191 }
3192 }
3193 }
3194  
3195 48 ▶ if($schema->{accessor}{type} && $schema->{accessor}{property} && ($schema->{accessor}{type} =~ /getter|getset/) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"accessor"}{"type"} and $schema->{"accessor"}{"property"} and $schema->{"accessor"}{"type"} =~ /getter|getset/ and !defined($schema->{"output"}{"type"}) || $schema->{"output"}{"type"} eq "string") 29 19 48
Condition66%8/12
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"property"} and $schema->{"accessor"}{"type"} =~ /getter|getset/ and !defined($schema->{"output"}{"type"}) || $schema->{"output"}{"type"} eq "string"
!ll&&!rl&&r
61329
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"property"} and $schema->{"accessor"}{"type"} =~ /getter|getset/
!ll&&!rl&&r
0642
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"property"}
!ll&&!rl&&r
0048
not defined $schema->{"output"}{"type"} or $schema->{"output"}{"type"} eq "string"
l!l&&r!l&&!r
02913
MC/DC40%2/5
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"property"} and $schema->{"accessor"}{"type"} =~ /getter|getset/ and !defined($schema->{"output"}{"type"}) || $schema->{"output"}{"type"} eq "string"
$schema->{"accessor"}{"type"} $schema->{"accessor"}{"property"} $schema->{"accessor"}{"type"} =~ /getter|getset/ not defined $schema->{"output"}{"type"} $schema->{"output"}{"type"} eq "string"
Truth table50%3/6
A and B and C and not(D) or E
A= $schema->{"accessor"}{"type"}B= $schema->{"accessor"}{"property"}C= $schema->{"accessor"}{"type"} =~ /getter|getset/D= not defined $schema->{"output"}{"type"}E= $schema->{"output"}{"type"} eq "string"
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
3196 ((!defined($schema->{output}{type})) || ($schema->{output}{type} eq 'string'))) {
3197 29 ▶ if (my $pod = $method->{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pod = $method->{"pod"}) 17 12 29
3198 # POD says "UserAgent object"
3199 17 ▶ if ($pod =~ /\bUser[- ]?Agent\b.*\bobject\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\bUser[- ]?Agent\b.*\bobject\b/i) 1 16 17
3200 1 $schema->{output}{type} = 'object';
3201 1 $schema->{output}{isa} = 'LWP::UserAgent';
3202  
3203 1 push @{ $schema->{_confidence}{output}{factors} }, 'POD indicates UserAgent object';
3204  
3205 1 $schema->{_confidence}{output}{level} = 'high';
3206 }
3207 }
3208 }
3209 }
3210 }
3211  
3212 # --------------------------------------------------
3213 # _analysis_error
3214 #
3215 # Purpose: Report a fatal analysis error with
3216 # module, method, and file context,
3217 # then croak.
3218 #
3219 # Entry: Named args:
3220 # method - method name string.
3221 # message - error description string.
3222 #
3223 # Exit: Does not return — always croaks.
3224 #
3225 # Side effects: None beyond the croak.
3226 # --------------------------------------------------
3227 2 ▶sub _analysis_error {
Subroutine _analysis_error: called CC=5 SCAR=16.1
3228 2 my ($self, %args) = @_;
3229  
3230 2 ▶ my $method = $args{method} // 'UNKNOWN';
Condition100%2/2
$args{"method"} // "UNKNOWN"
l!l
11
MC/DC100%1/1
$args{"method"} // "UNKNOWN"
$args{"method"}
Truth table100%2/2
A
A= $args{"method"}
Aresult
11
00
3231 2 ▶ my $msg = $args{message} // 'Analysis error';
Condition100%2/2
$args{"message"} // "Analysis error"
l!l
11
MC/DC100%1/1
$args{"message"} // "Analysis error"
$args{"message"}
Truth table100%2/2
A
A= $args{"message"}
Aresult
11
00
3232  
3233 2 ▶ my $module = $self->{_package_name} // 'UNKNOWN';
Condition100%2/2
$self->{"_package_name"} // "UNKNOWN"
l!l
11
MC/DC100%1/1
$self->{"_package_name"} // "UNKNOWN"
$self->{"_package_name"}
Truth table100%2/2
A
A= $self->{"_package_name"}
Aresult
11
00
3234 2 ▶ my $file = $self->{input_file} // 'UNKNOWN';
Condition100%2/2
$self->{"input_file"} // "UNKNOWN"
l!l
11
MC/DC100%1/1
$self->{"input_file"} // "UNKNOWN"
$self->{"input_file"}
Truth table100%2/2
A
A= $self->{"input_file"}
Aresult
11
00
3235  
3236 2 croak join "\n",
3237 $msg,
3238 " Module: $module",
3239 " Method: $method",
3240 " File: $file",
3241 '';
3242 }
3243  
3244 # --------------------------------------------------
3245 # _extract_validator_schema
3246 #
3247 # Purpose: Try each supported validator extractor
3248 # in priority order and return the first
3249 # schema that yields a non-empty input
3250 # spec. Used to detect explicit
3251 # parameter validation declarations
3252 # before falling back to heuristic
3253 # code analysis.
3254 #
3255 # Entry: $code - method body source string.
3256 #
3257 # Exit: Returns a schema hashref on success,
3258 # or undef if no supported validator
3259 # call is detected.
3260 #
3261 # Side effects: None.
3262 #
3263 # Notes: Extractors tried in order:
3264 # Params::Validate::Strict,
3265 # Params::Validate,
3266 # MooseX::Params::Validate,
3267 # Type::Params.
3268 # --------------------------------------------------
3269 813 ▶sub _extract_validator_schema {
Subroutine _extract_validator_schema: called CC=6 SCAR=18.3
3270 813 my ($self, $code) = @_;
3271  
3272 813 for my $extractor ('_extract_pvs_schema', '_extract_pv_schema', '_extract_moosex_params_schema', '_extract_type_params_schema') {
3273 3213 my $res = $self->$extractor($code);
3274 3213 ▶ return $res if ($res && ref($res) eq 'HASH' && keys %{ $res->{input} || {} });
Branch: 1 branch
BranchTrueFalseTotal
if $res and ref $res eq "HASH" and keys %{$res->{"input"} or {};} 16 3197 3213
Condition62%5/8
$res and ref $res eq "HASH" and keys %{$res->{"input"} or {};}
!ll&&!rl&&r
3197016
$res and ref $res eq "HASH"
!ll&&!rl&&r
3197016
$res->{'input'} || {}
l!l
160
MC/DC33%1/3
$res and ref $res eq "HASH" and keys %{$res->{"input"} or {};}
$res ref $res eq "HASH" keys %{$res->{"input"} or {};}
MC/DC0%0/1
$res->{'input'} || {}
$res->{'input'}
Truth table50%2/4
A and B and C
A= $resB= ref $res eq "HASH"C= keys %{$res->{"input"} or {};}
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $res->{'input'}
Aresult
11
00
3275 }
3276  
3277 797 return;
3278 }
3279  
3280 # --------------------------------------------------
3281 # _parse_schema_hash
3282 #
3283 # Purpose: Parse a PPI block node representing
3284 # a validator schema hash literal and
3285 # return a normalised schema structure
3286 # suitable for use as input spec.
3287 #
3288 # Entry: $hash - a PPI node with a children()
3289 # method, typically a
3290 # PPI::Structure::Block from
3291 # a validate_strict call.
3292 #
3293 # Exit: Returns a hashref with keys:
3294 # input - hashref of param specs
3295 # input_style - 'hash'
3296 # _confidence - confidence hashref
3297 # or undef if parsing fails.
3298 #
3299 # Side effects: None.
3300 # --------------------------------------------------
3301 0 ▶sub _parse_schema_hash {
Subroutine _parse_schema_hash: not called CC=27 SCAR=66.3
3302 0 my ($self, $hash) = @_;
3303  
3304 0 my %result;
3305  
3306 0 for my $child ($hash->children) {
3307 # skip whitespace and operators
3308 0 ▶ if ($child->isa('PPI::Statement') || $child->isa('PPI::Statement::Expression')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Statement") or $child->isa("PPI::Statement::Expression")) 0 0 0
Condition0%0/3
$child->isa("PPI::Statement") or $child->isa("PPI::Statement::Expression")
l!l&&r!l&&!r
000
MC/DC0%0/2
$child->isa("PPI::Statement") or $child->isa("PPI::Statement::Expression")
$child->isa("PPI::Statement") $child->isa("PPI::Statement::Expression")
Truth table0%0/3
A or B
A= $child->isa("PPI::Statement")B= $child->isa("PPI::Statement::Expression")
ABresult
1X1
011
000
3309 0 my ($key, $val);
3310  
3311 my @tokens = grep {
3312 0 ▶ !$_->isa('PPI::Token::Whitespace') &&
Condition0%0/3
$_->isa('PPI::Token::Whitespace') || $_->isa('PPI::Token::Operator')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Token::Whitespace') || $_->isa('PPI::Token::Operator')
$_->isa('PPI::Token::Whitespace') $_->isa('PPI::Token::Operator')
Truth table0%0/3
A || B
A= $_->isa('PPI::Token::Whitespace')B= $_->isa('PPI::Token::Operator')
ABresult
1X1
011
000
3313 !$_->isa('PPI::Token::Operator')
3314 } $child->children;
3315  
3316 0 ▶ for (my $i = 0; $i < @tokens - 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tokens - 1) 0 0 0
3317 0 ▶ if(($tokens[$i]->isa('PPI::Token::Word') || $tokens[$i]->isa('PPI::Token::Quote')) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens[$i]->isa("PPI::Token::Word") || $tokens[$i]->isa("PPI::Token::Quote") and $tokens[$i + 1]->isa("PPI::Structure::Constructor")) 0 0 0
Condition0%0/6
$tokens[$i]->isa("PPI::Token::Word") || $tokens[$i]->isa("PPI::Token::Quote") and $tokens[$i + 1]->isa("PPI::Structure::Constructor")
!ll&&!rl&&r
000
$tokens[$i]->isa("PPI::Token::Word") or $tokens[$i]->isa("PPI::Token::Quote")
l!l&&r!l&&!r
000
MC/DC0%0/3
$tokens[$i]->isa("PPI::Token::Word") || $tokens[$i]->isa("PPI::Token::Quote") and $tokens[$i + 1]->isa("PPI::Structure::Constructor")
$tokens[$i]->isa("PPI::Token::Word") $tokens[$i]->isa("PPI::Token::Quote") $tokens[$i + 1]->isa("PPI::Structure::Constructor")
Truth table0%0/5
A or B and C
A= $tokens[$i]->isa("PPI::Token::Word")B= $tokens[$i]->isa("PPI::Token::Quote")C= $tokens[$i + 1]->isa("PPI::Structure::Constructor")
ABCresult
00X0
1X00
0100
1X11
0111
3318 $tokens[$i+1]->isa('PPI::Structure::Constructor')) {
3319 0 $key = $tokens[$i]->content;
3320 0 $key =~ s/^['"]|['"]$//g;
3321 0 $val = $tokens[$i+1];
3322 0 last;
3323 }
3324 }
3325  
3326 0 ▶ next unless $key && $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $val 0 0 0
Condition0%0/3
$key and $val
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $val
$key $val
Truth table0%0/3
A and B
A= $keyB= $val
ABresult
0X0
100
111
3327  
3328 0 my %param;
3329 0 for my $inner ($val->children) {
3330 0 ▶ next unless $inner->isa('PPI::Statement') || $inner->isa('PPI::Statement::Expression');
Branch: 1 branch
BranchTrueFalseTotal
unless $inner->isa("PPI::Statement") or $inner->isa("PPI::Statement::Expression") 0 0 0
Condition0%0/3
$inner->isa("PPI::Statement") or $inner->isa("PPI::Statement::Expression")
l!l&&r!l&&!r
000
MC/DC0%0/2
$inner->isa("PPI::Statement") or $inner->isa("PPI::Statement::Expression")
$inner->isa("PPI::Statement") $inner->isa("PPI::Statement::Expression")
Truth table0%0/3
A or B
A= $inner->isa("PPI::Statement")B= $inner->isa("PPI::Statement::Expression")
ABresult
1X1
011
000
3331  
3332 my ($k, undef, $v) = grep {
3333 0 ▶ !$_->isa('PPI::Token::Whitespace') &&
Condition0%0/3
$_->isa('PPI::Token::Whitespace') || $_->isa('PPI::Token::Operator')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Token::Whitespace') || $_->isa('PPI::Token::Operator')
$_->isa('PPI::Token::Whitespace') $_->isa('PPI::Token::Operator')
Truth table0%0/3
A || B
A= $_->isa('PPI::Token::Whitespace')B= $_->isa('PPI::Token::Operator')
ABresult
1X1
011
000
3334 !$_->isa('PPI::Token::Operator')
3335 } $inner->children;
3336  
3337 0 ▶ next unless $k && $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $k and $v 0 0 0
Condition0%0/3
$k and $v
!ll&&!rl&&r
000
MC/DC0%0/2
$k and $v
$k $v
Truth table0%0/3
A and B
A= $kB= $v
ABresult
0X0
100
111
3338  
3339 0 my $keyname = $k->content;
3340 0 ▶ my $value = $v->can('content') ? $v->content : undef;
Branch: 1 branch
BranchTrueFalseTotal
$v->can('content') ? : 0 0 0
3341 0 ▶ $value =~ s/^['"]|['"]$//g if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
3342  
3343 0 ▶ if ($keyname eq 'type') {
Branch: 4 branches
BranchTrueFalseTotal
if ($keyname eq 'type') { } 0 0 0
elsif ($keyname eq 'optional') { } 0 0 0
elsif ($keyname =~ /^(min|max)$/ and &looks_like_number($value)) { } 0 0 0
elsif ($keyname eq 'matches') { } 0 0 0
3344 0 $param{type} = lc($value);
3345 } elsif ($keyname eq 'optional') {
3346 0 ▶ $param{optional} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
3347 ▶ } elsif ($keyname =~ /^(min|max)$/ && looks_like_number($value)) {
Condition0%0/3
$keyname =~ /^(min|max)$/ and &looks_like_number($value)
!ll&&!rl&&r
000
MC/DC0%0/2
$keyname =~ /^(min|max)$/ and &looks_like_number($value)
$keyname =~ /^(min|max)$/ &looks_like_number($value)
Truth table0%0/3
A and B
A= $keyname =~ /^(min|max)$/B= &looks_like_number($value)
ABresult
0X0
100
111
3348 0 $param{$keyname} = 0 + $value;
3349 } elsif ($keyname eq 'matches') {
3350 0 $param{matches} = qr/$value/;
3351 }
3352 }
3353  
3354 0 ▶ $param{type} //= 'string';
Condition0%0/2
$param{"type"} //= "string"
l!l
00
MC/DC0%0/1
$param{"type"} //= "string"
$param{"type"}
Truth table0%0/2
A
A= $param{"type"}
Aresult
11
00
3355 0 ▶ $param{optional} //= 0;
Condition0%0/2
$param{"optional"} //= 0
l!l
00
MC/DC0%0/1
$param{"optional"} //= 0
$param{"optional"}
Truth table0%0/2
A
A= $param{"optional"}
Aresult
11
00
3356  
3357 0 $result{$key} = \%param;
3358 }
3359 }
3360  
3361 return {
3362 0 input => \%result,
3363 input_style => 'hash',
3364 _confidence => {
3365 input => {
3366 level => 'high',
3367 factors => ['Input schema extracted from validator'],
3368 },
3369 },
3370 };
3371 }
3372  
3373 # --------------------------------------------------
3374 # _ppi
3375 #
3376 # Purpose: Return a PPI::Document for a code
3377 # string, using a per-instance cache
3378 # to avoid re-parsing the same string
3379 # multiple times during a single
3380 # analysis pass.
3381 #
3382 # Entry: $code - either a string of Perl source
3383 # code, or an object that
3384 # already has a find() method
3385 # (returned as-is).
3386 #
3387 # Exit: Returns a PPI::Document, or the
3388 # original object if it already
3389 # supports find().
3390 #
3391 # Side effects: Populates $self->{_ppi_cache}.
3392 # --------------------------------------------------
3393 18 ▶sub _ppi {
Subroutine _ppi: called CC=5 SCAR=16.2
3394 18 my ($self, $code) = @_;
3395  
3396 18 ▶ return $code if ref($code) && $code->can('find');
Branch: 1 branch
BranchTrueFalseTotal
if ref $code and $code->can("find") 1 17 18
Condition66%2/3
ref $code and $code->can("find")
!ll&&!rl&&r
1701
MC/DC50%1/2
ref $code and $code->can("find")
ref $code $code->can("find")
Truth table66%2/3
A and B
A= ref $codeB= $code->can("find")
ABresult
0X0
100
111
3397  
3398 17 ▶ $self->{_ppi_cache} ||= {};
Condition100%2/2
$self->{"_ppi_cache"} ||= {}
l!l
215
MC/DC100%1/1
$self->{"_ppi_cache"} ||= {}
$self->{"_ppi_cache"}
Truth table100%2/2
A
A= $self->{"_ppi_cache"}
Aresult
11
00
3399 17 ▶ return $self->{_ppi_cache}{$code} //= PPI::Document->new(\$code);
Condition66%2/3
$self->{"_ppi_cache"}{$code} //= "PPI::Document"->new(\$code)
l!l&&r!l&&!r
1160
MC/DC0%0/2
$self->{"_ppi_cache"}{$code} //= "PPI::Document"->new(\$code)
$self->{"_ppi_cache"}{$code} "PPI::Document"->new(\$code)
Truth table66%2/3
A //= B
A= $self->{"_ppi_cache"}{$code}B= "PPI::Document"->new(\$code)
ABresult
1X1
011
000
3400 }
3401  
3402 # --------------------------------------------------
3403 # _extract_pvs_schema
3404 #
3405 # Purpose: Detect and extract a parameter schema
3406 # from a Params::Validate::Strict
3407 # validate_strict() call in the method
3408 # body.
3409 #
3410 # Entry: $code - method body source string.
3411 #
3412 # Exit: Returns a schema hashref with input,
3413 # style, and source keys on success,
3414 # or undef if no validate_strict call
3415 # is found or parsing fails.
3416 #
3417 # Side effects: None.
3418 # --------------------------------------------------
3419 813 ▶sub _extract_pvs_schema {
Subroutine _extract_pvs_schema: called CC=15 SCAR=29.7
3420 813 my ($self, $code) = @_;
3421  
3422 813 ▶ return unless $code =~ /\bvalidate_strict\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\bvalidate_strict\s*\(/ 800 13 813
3423  
3424 13 ▶ my $doc = $self->_ppi($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->_ppi($code) or return 13 0 13
3425  
3426 24586 ▶ my $calls = $doc->find(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
3427 24586 ▶ $_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validate_strict' || $_[1]->content eq 'Params::Validate::Strict::validate_strict')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and $_[1]->content eq "validate_strict" || $_[1]->content eq "Params::Validate::Strict::validate_strict" 2069 22517 24586
Condition100%6/6
$_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validate_strict' || $_[1]->content eq 'Params::Validate::Strict::validate_strict')
!ll&&!rl&&r
22517205613
$_[1]->content eq "validate_strict" or $_[1]->content eq "Params::Validate::Strict::validate_strict"
l!l&&r!l&&!r
492056
MC/DC100%3/3
$_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validate_strict' || $_[1]->content eq 'Params::Validate::Strict::validate_strict')
$_[1]->isa('PPI::Token::Word') $_[1]->content eq "validate_strict" $_[1]->content eq "Params::Validate::Strict::validate_strict"
Truth table100%4/4
A && B or C
A= $_[1]->isa('PPI::Token::Word')B= $_[1]->content eq "validate_strict"C= $_[1]->content eq "Params::Validate::Strict::validate_strict"
ABCresult
0XX0
1000
11X1
1011
3428 13 ▶ }) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $calls = $doc->find(sub { $_[1]->isa("PPI::Token::Word") and $_[1]->content eq "validate_strict" || $_[1]->content eq "Params::Validate::Strict::validate_strict"; } ) or return 13 0 13
3429  
3430 13 for my $call (@$calls) {
3431 13 my $list = $call->parent();
3432 13 ▶ while ($list && !$list->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and not $list->isa("PPI::Structure::List")) 66 13 79
Condition66%2/3
$list and not $list->isa("PPI::Structure::List")
!ll&&!rl&&r
13066
MC/DC50%1/2
$list and not $list->isa("PPI::Structure::List")
$list not $list->isa("PPI::Structure::List")
Truth table66%2/3
A and not(B)
A= $listB= not $list->isa("PPI::Structure::List")
ABresult
0X0
100
111
3433 66 $list = $list->parent();
3434 }
3435 13 ▶ if(!defined($list)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 13 0 13
3436 13 my $next = $call->next_sibling();
3437 13 ▶ next unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 0 13 13
3438 13 ▶ if($next->content() =~ /schema\s*=>\s*(\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->content =~ /schema\s*=>\s*(\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})/s) 13 0 13
3439 13 my $schema_text = $1;
3440 13 ▶ next if $schema_text =~ $UNSAFE_KEYWORD_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $schema_text =~ /$UNSAFE_KEYWORD_RE/ 0 13 13
3441 13 my $compartment = Safe->new();
3442 13 $compartment->permit_only(qw(:base_core :base_mem :base_orig));
3443  
3444 13 my $schema_str = "my \$schema = $schema_text";
3445 13 my $schema = $compartment->reval($schema_str);
3446 13 ▶ if($self->_usable_validator_schema($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_usable_validator_schema($schema)) 12 1 13
3447 return {
3448 12 input => $schema,
3449 style => 'hash',
3450 source => SOURCE_VALIDATOR
3451 }
3452 }
3453 }
3454 }
3455 1 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 1 0 1
3456  
3457 0 my ($schema_block) = grep { $_->isa('PPI::Structure::Block') } $list->children;
3458  
3459 0 ▶ next unless $schema_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_block 0 0 0
3460  
3461 0 my $schema = $self->_extract_schema_hash_from_block($schema_block);
3462 0 ▶ return $self->_normalize_validator_schema($schema) if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 0 0
3463 }
3464  
3465 1 return;
3466 }
3467  
3468 # --------------------------------------------------
3469 # _extract_pv_schema
3470 #
3471 # Purpose: Detect and extract a parameter schema
3472 # from a Params::Validate validate()
3473 # call in the method body.
3474 #
3475 # Entry: $code - method body source string.
3476 #
3477 # Exit: Returns a schema hashref with input,
3478 # style, and source keys on success,
3479 # or undef if no validate() call is
3480 # found or parsing fails.
3481 #
3482 # Side effects: None.
3483 # --------------------------------------------------
3484 801 ▶sub _extract_pv_schema {
Subroutine _extract_pv_schema: called CC=18 SCAR=33.7
3485 801 my ($self, $code) = @_;
3486  
3487 801 ▶ return unless $code =~ /\bvalidate\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\bvalidate\s*\(/ 800 1 801
3488  
3489 1 ▶ my $doc = $self->_ppi($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->_ppi($code) or return 1 0 1
3490  
3491 263 ▶ my $calls = $doc->find(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
3492 263 ▶ $_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validate' || $_[1]->content eq 'Params::Validate::validate')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and $_[1]->content eq "validate" || $_[1]->content eq "Params::Validate::validate" 28 235 263
Condition83%5/6
$_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validate' || $_[1]->content eq 'Params::Validate::validate')
!ll&&!rl&&r
235271
$_[1]->content eq "validate" or $_[1]->content eq "Params::Validate::validate"
l!l&&r!l&&!r
1027
MC/DC66%2/3
$_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validate' || $_[1]->content eq 'Params::Validate::validate')
$_[1]->isa('PPI::Token::Word') $_[1]->content eq "validate" $_[1]->content eq "Params::Validate::validate"
Truth table75%3/4
A && B or C
A= $_[1]->isa('PPI::Token::Word')B= $_[1]->content eq "validate"C= $_[1]->content eq "Params::Validate::validate"
ABCresult
0XX0
1000
11X1
1011
3493 1 ▶ }) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $calls = $doc->find(sub { $_[1]->isa("PPI::Token::Word") and $_[1]->content eq "validate" || $_[1]->content eq "Params::Validate::validate"; } ) or return 1 0 1
3494  
3495 1 for my $call (@$calls) {
3496 1 my $list = $call->parent;
3497 1 ▶ while ($list && !$list->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and not $list->isa("PPI::Structure::List")) 4 1 5
Condition66%2/3
$list and not $list->isa("PPI::Structure::List")
!ll&&!rl&&r
104
MC/DC50%1/2
$list and not $list->isa("PPI::Structure::List")
$list not $list->isa("PPI::Structure::List")
Truth table66%2/3
A and not(B)
A= $listB= not $list->isa("PPI::Structure::List")
ABresult
0X0
100
111
3498 4 $list = $list->parent;
3499 }
3500 1 ▶ if(!defined($list)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 1 0 1
3501 1 my $next = $call->next_sibling();
3502 1 my ($arglist, $schema_text) = $self->_parse_pv_call($next);
3503  
3504 1 ▶ if($schema_text && $schema_text !~ $UNSAFE_KEYWORD_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_text and not $schema_text =~ /$UNSAFE_KEYWORD_RE/) 1 0 1
Condition33%1/3
$schema_text and not $schema_text =~ /$UNSAFE_KEYWORD_RE/
!ll&&!rl&&r
001
MC/DC0%0/2
$schema_text and not $schema_text =~ /$UNSAFE_KEYWORD_RE/
$schema_text not $schema_text =~ /$UNSAFE_KEYWORD_RE/
Truth table33%1/3
A and not(B)
A= $schema_textB= not $schema_text =~ /$UNSAFE_KEYWORD_RE/
ABresult
0X0
100
111
3505 1 my $compartment = Safe->new();
3506 1 $compartment->permit_only(qw(:base_core :base_mem :base_orig));
3507  
3508 1 my $schema_str = "my \$schema = $schema_text";
3509 1 my $schema = $compartment->reval($schema_str);
3510  
3511 1 ▶ if($self->_usable_validator_schema($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_usable_validator_schema($schema)) 1 0 1
3512 1 foreach my $arg(keys %{$schema}) {
3513 3 my $field = $schema->{$arg};
3514 3 ▶ if(my $type = $field->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $field->{"type"}) 3 0 3
3515 3 ▶ if($type eq 'ARRAYREF') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAYREF') { } 1 2 3
elsif ($type eq 'SCALAR') { } 2 0 2
3516 1 $field->{'type'} = 'arrayref';
3517 } elsif($type eq 'SCALAR') {
3518 2 $field->{'type'} = 'string';
3519 }
3520 }
3521 3 delete $field->{'callbacks'};
3522 }
3523  
3524 return {
3525 1 input => $schema,
3526 style => 'hash',
3527 source => SOURCE_VALIDATOR
3528 }
3529 }
3530 }
3531 }
3532 0 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
3533  
3534 0 my ($schema_block) = grep { $_->isa('PPI::Structure::Block') } $list->children;
3535  
3536 0 ▶ next unless $schema_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_block 0 0 0
3537  
3538 0 my $schema = $self->_extract_schema_hash_from_block($schema_block);
3539 0 ▶ return $self->_normalize_validator_schema($schema) if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 0 0
3540 }
3541  
3542 0 return;
3543 }
3544  
3545 # --------------------------------------------------
3546 # _parse_pv_call
3547 #
3548 # Purpose: Split a Params::Validate call argument
3549 # string into its two components: the
3550 # first argument (typically \@_) and
3551 # the schema hash string.
3552 #
3553 # Entry: $string - the raw argument string
3554 # from the validate() call,
3555 # including outer parentheses.
3556 #
3557 # Exit: Returns a two-element list:
3558 # ($first_arg, $hash_str)
3559 # or an empty list if no comma is found
3560 # at brace depth zero (malformed call).
3561 #
3562 # Side effects: None.
3563 # --------------------------------------------------
3564 2 ▶sub _parse_pv_call {
Subroutine _parse_pv_call: called CC=6 SCAR=19.0
3565 2 my ($self, $string) = @_;
3566  
3567 # Remove outer parentheses and whitespace
3568 2 $string =~ s/^\s*\(\s*//;
3569 2 $string =~ s/\s*\)\s*$//;
3570  
3571 # Find the first comma at brace-depth 0, jumping over each balanced
3572 # {...} block in one step via extract_bracketed rather than
3573 # counting depth character by character
3574 2 require Text::Balanced;
3575 2 my $rest = $string;
3576 2 my $comma_pos = 0;
3577 2 my $found_comma = 0;
3578  
3579 2 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 10 0 10
3580 10 ▶ if (substr($rest, 0, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) eq "{") 0 10 10
3581 # extract_bracketed advances $rest past the extracted block
3582 # in place, so $rest must not be re-truncated afterwards
3583 0 my $extracted = Text::Balanced::extract_bracketed($rest, '{}');
3584 0 ▶ return unless defined $extracted; # Broken source code
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 0 0
3585 0 $comma_pos += length $extracted;
3586 0 next;
3587 }
3588 10 ▶ if (substr($rest, 0, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) eq ",") 2 8 10
3589 2 $found_comma = 1;
3590 2 last;
3591 }
3592 8 $comma_pos++;
3593 8 $rest = substr($rest, 1);
3594 }
3595  
3596 2 ▶ return unless $found_comma;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_comma 0 2 2
3597  
3598 2 my $first_arg = substr($string, 0, $comma_pos);
3599 2 my $hash_str = substr($string, $comma_pos + 1);
3600  
3601 # Trim whitespace
3602 2 $first_arg =~ s/^\s+|\s+$//g;
3603 2 $hash_str =~ s/^\s+|\s+$//g;
3604  
3605 2 return ($first_arg, $hash_str);
3606 }
3607  
3608 # --------------------------------------------------
3609 # _extract_moosex_params_schema
3610 #
3611 # Purpose: Detect and extract a parameter schema
3612 # from a MooseX::Params::Validate
3613 # validated_hash() call in the method
3614 # body.
3615 #
3616 # Entry: $code - method body source string.
3617 #
3618 # Exit: Returns a schema hashref with input,
3619 # style, and source keys on success,
3620 # or undef if no validated_hash() call
3621 # is found or parsing fails.
3622 #
3623 # Side effects: None.
3624 # --------------------------------------------------
3625 sub _extract_moosex_params_schema
3626 800 ▶{
Subroutine _extract_moosex_params_schema: called CC=23 SCAR=36.9
3627 800 my ($self, $code) = @_;
3628  
3629 800 ▶ return unless $code =~ /\bvalidated_hash\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\bvalidated_hash\s*\(/ 799 1 800
3630  
3631 1 ▶ my $doc = $self->_ppi($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->_ppi($code) or return 1 0 1
3632  
3633 212 ▶ my $calls = $doc->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
3634 212 ▶ $_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validated_hash')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and $_[1]->content eq "validated_hash" 27 185 212
3635 1 ▶ }) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $calls = $doc->find(sub { $_[1]->isa("PPI::Token::Word") and $_[1]->content eq "validated_hash"; } ) or return 1 0 1
3636  
3637 1 for my $call (@$calls) {
3638 1 my $list = $call->parent();
3639 1 ▶ while ($list && !$list->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and not $list->isa("PPI::Structure::List")) 4 1 5
Condition66%2/3
$list and not $list->isa("PPI::Structure::List")
!ll&&!rl&&r
104
MC/DC50%1/2
$list and not $list->isa("PPI::Structure::List")
$list not $list->isa("PPI::Structure::List")
Truth table66%2/3
A and not(B)
A= $listB= not $list->isa("PPI::Structure::List")
ABresult
0X0
100
111
3640 4 $list = $list->parent;
3641 }
3642 1 ▶ if(!defined($list)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 1 0 1
3643 1 my $next = $call->next_sibling();
3644 1 my ($arglist, $schema_text) = $self->_parse_pv_call($next);
3645  
3646 1 ▶ if($schema_text && $schema_text !~ $UNSAFE_KEYWORD_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_text and not $schema_text =~ /$UNSAFE_KEYWORD_RE/) 1 0 1
Condition33%1/3
$schema_text and not $schema_text =~ /$UNSAFE_KEYWORD_RE/
!ll&&!rl&&r
001
MC/DC0%0/2
$schema_text and not $schema_text =~ /$UNSAFE_KEYWORD_RE/
$schema_text not $schema_text =~ /$UNSAFE_KEYWORD_RE/
Truth table33%1/3
A and not(B)
A= $schema_textB= not $schema_text =~ /$UNSAFE_KEYWORD_RE/
ABresult
0X0
100
111
3647 1 my $compartment = Safe->new();
3648 1 $compartment->permit_only(qw(:base_core :base_mem :base_orig));
3649  
3650 1 my $schema_str = "my \$schema = { $schema_text }";
3651 1 $schema_str =~ s/ArrayRef\[(.+?)\]/arrayref, element_type => $1/g;
3652 1 my $schema = $compartment->reval($schema_str);
3653  
3654 1 ▶ if($self->_usable_validator_schema($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_usable_validator_schema($schema)) 1 0 1
3655 1 foreach my $arg(keys %{$schema}) {
3656 3 my $field = $schema->{$arg};
3657 3 ▶ if(my $isa = delete $field->{'isa'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $isa = delete $field->{"isa"}) 3 0 3
3658 3 $field->{'type'} = $isa;
3659 }
3660 3 ▶ if(exists($field->{'required'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{'required'}) { } 1 2 3
3661 1 my $required = delete $field->{'required'};
3662 1 ▶ $field->{'optional'} = $required ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$required ? : 1 0 1
3663 } else {
3664 2 $field->{'optional'} = 1;
3665 }
3666 3 ▶ if(ref($field->{'default'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field->{"default"} eq "CODE") 1 2 3
3667 1 delete $field->{'default'}; # TODO
3668 }
3669 }
3670  
3671 1 foreach my $arg(keys %{$schema}) {
3672 3 my $field = $schema->{$arg};
3673 3 ▶ if(my $type = $field->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $field->{"type"}) 3 0 3
3674 3 ▶ if($type eq 'ARRAYREF') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAYREF') { } 0 3 3
elsif ($type eq 'SCALAR') { } 0 3 3
3675 0 $field->{'type'} = 'arrayref';
3676 } elsif($type eq 'SCALAR') {
3677 0 $field->{'type'} = 'string';
3678 }
3679 }
3680 3 delete $field->{'callbacks'};
3681 }
3682  
3683 return {
3684 1 input => $schema,
3685 style => 'hash',
3686 source => SOURCE_VALIDATOR
3687 }
3688 }
3689 }
3690 }
3691 0 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
3692  
3693 0 my ($schema_block) = grep { $_->isa('PPI::Structure::Block') } $list->children;
3694  
3695 0 ▶ next unless $schema_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_block 0 0 0
3696  
3697 0 my $schema = $self->_extract_schema_hash_from_block($schema_block);
3698 0 ▶ return $self->_normalize_validator_schema($schema) if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 0 0
3699 }
3700  
3701 0 return;
3702 }
3703  
3704 # --------------------------------------------------
3705 # _extract_schema_hash_from_block
3706 #
3707 # Purpose: Extract a parameter schema hashref from
3708 # a PPI::Structure::Block node representing
3709 # the schema argument to a validator call
3710 # such as validate_strict({ ... }).
3711 #
3712 # Entry: $block - a PPI::Structure::Block node.
3713 #
3714 # Exit: Returns a hashref of parameter name to
3715 # spec hashref, or undef if parsing fails.
3716 #
3717 # Side effects: None.
3718 #
3719 # Notes: Delegates to _parse_schema_hash which
3720 # expects a PPI node with a children()
3721 # method. This method exists to provide
3722 # a clear semantic name at the call site.
3723 # --------------------------------------------------
3724 0 ▶sub _extract_schema_hash_from_block {
Subroutine _extract_schema_hash_from_block: not called CC=6 SCAR=37.4
3725 0 my ($self, $block) = @_;
3726  
3727 0 ▶ return unless $block && $block->can('children');
Branch: 1 branch
BranchTrueFalseTotal
unless $block and $block->can("children") 0 0 0
Condition0%0/3
$block and $block->can("children")
!ll&&!rl&&r
000
MC/DC0%0/2
$block and $block->can("children")
$block $block->can("children")
Truth table0%0/3
A and B
A= $blockB= $block->can("children")
ABresult
0X0
100
111
3728  
3729 0 my $result = $self->_parse_schema_hash($block);
3730  
3731 0 ▶ return unless $result && ref($result) eq 'HASH' && $result->{input};
Branch: 1 branch
BranchTrueFalseTotal
unless $result and ref $result eq "HASH" and $result->{"input"} 0 0 0
Condition0%0/6
$result and ref $result eq "HASH" and $result->{"input"}
!ll&&!rl&&r
000
$result and ref $result eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$result and ref $result eq "HASH" and $result->{"input"}
$result ref $result eq "HASH" $result->{"input"}
Truth table0%0/4
A and B and C
A= $resultB= ref $result eq "HASH"C= $result->{"input"}
ABCresult
0XX0
10X0
1100
1111
3732  
3733 0 return $result->{input};
3734 }
3735  
3736 # --------------------------------------------------
3737 # _usable_validator_schema
3738 #
3739 # Purpose: Decide whether a schema hash evaluated
3740 # from source code is a real parameter
3741 # schema, or the remains of a runtime
3742 # expression that was never meant to be
3743 # evaluated at all.
3744 #
3745 # Entry: $schema - value returned by
3746 # Safe->reval() on the schema
3747 # text found in the call.
3748 #
3749 # Exit: Returns true when the schema is a
3750 # non-empty hash of named parameters to
3751 # spec hashes, false otherwise.
3752 #
3753 # Side effects: None.
3754 #
3755 # Notes: CGI::Info::params validates each
3756 # parameter inside a loop with
3757 #
3758 # validate_strict({
3759 # schema => { $key => $schema },
3760 # args => { $key => $value },
3761 # });
3762 #
3763 # Evaluating { $key => $schema } with
3764 # both lexicals undefined yields
3765 # { '' => undef }. Accepting that as the
3766 # method's input gave the schema an empty
3767 # parameter name, which the harness
3768 # generator rightly refuses to turn into
3769 # a Perl variable, so no harness could be
3770 # generated for the routine at all.
3771 # --------------------------------------------------
3772 15 ▶sub _usable_validator_schema {
Subroutine _usable_validator_schema: called CC=6 SCAR=18.3
3773 15 my ($self, $schema) = @_;
3774  
3775 15 ▶ return 0 unless ref($schema) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 15 15
3776  
3777 15 my @names = keys %$schema;
3778 15 ▶ return 0 unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 15 15
3779  
3780 15 for my $name (@names) {
3781 46 ▶ return 0 unless length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 1 45 46
3782 45 ▶ return 0 unless ref($schema->{$name}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema->{$name} eq "HASH" 0 45 45
3783 }
3784  
3785 14 return 1;
3786 }
3787  
3788 # --------------------------------------------------
3789 # _normalize_validator_schema
3790 #
3791 # Purpose: Normalise a raw validator schema
3792 # hashref (as extracted from PPI) into
3793 # the standard input spec format used
3794 # throughout the extractor.
3795 #
3796 # Entry: $schema - hashref of parameter name
3797 # to raw spec hashref, as
3798 # produced by
3799 # _extract_schema_hash_from_block.
3800 #
3801 # Exit: Returns a hashref with keys:
3802 # input_style - 'hash'
3803 # input - normalised param specs
3804 # Each param spec gains an explicit
3805 # optional key and _source / _type_confidence
3806 # metadata.
3807 #
3808 # Side effects: None.
3809 # --------------------------------------------------
3810 0 ▶sub _normalize_validator_schema {
Subroutine _normalize_validator_schema: not called CC=3 SCAR=24.8
3811 0 my ($self, $schema) = @_;
3812  
3813 0 my %input;
3814  
3815 0 for my $name (keys %$schema) {
3816 0 my $spec = $schema->{$name};
3817  
3818 $input{$name} = {
3819 %$spec,
3820 0 ▶ optional => exists $spec->{optional} ? $spec->{optional} : 0,
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{'optional'} ? : 0 0 0
3821 _source => SOURCE_VALIDATOR,
3822 _type_confidence => 'high',
3823 };
3824 }
3825  
3826 return {
3827 0 input_style => 'hash',
3828 input => \%input,
3829 };
3830 }
3831  
3832 # --------------------------------------------------
3833 # _extract_type_params_schema
3834 #
3835 # Purpose: Detect and extract a parameter schema
3836 # from a Type::Params signature_for()
3837 # declaration for the current method,
3838 # located in the module-level document.
3839 #
3840 # Entry: $code - method body source string
3841 # (used to extract the function
3842 # name for lookup).
3843 #
3844 # Exit: Returns a schema hashref on success,
3845 # or undef if no signature_for
3846 # declaration is found or compilation
3847 # fails.
3848 #
3849 # Side effects: May fork a child process to compile
3850 # the signature in isolation.
3851 # --------------------------------------------------
3852 802 ▶sub _extract_type_params_schema {
Subroutine _extract_type_params_schema: called CC=6 SCAR=18.0
3853 802 my ($self, $code) = @_;
3854  
3855 802 ▶ my $function = $self->_extract_function_name($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $function = $self->_extract_function_name($code) or return 802 0 802
3856  
3857 802 ▶ my $doc = $self->{_document} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->{"_document"} or return 801 1 802
3858 801 ▶ my $stmt = $self->_find_signature_statement($doc, $function) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $self->_find_signature_statement($doc, $function) or return 4 797 801
3859  
3860 4 ▶ my $signature_expr = $self->_extract_signature_expression($stmt, $function) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $signature_expr = $self->_extract_signature_expression($stmt, $function) or return 4 0 4
3861  
3862 4 ▶ my $meta = $self->_compile_signature_isolated($function, $signature_expr) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->_compile_signature_isolated($function, $signature_expr) or return 3 1 4
3863  
3864 3 return $self->_build_schema_from_meta($meta);
3865 }
3866  
3867 # --------------------------------------------------
3868 # _extract_function_name
3869 #
3870 # Purpose: Extract the subroutine name from the
3871 # start of a method body string, used
3872 # to look up its Type::Params signature.
3873 #
3874 # Entry: $code - method body source string.
3875 #
3876 # Exit: Returns the subroutine name string,
3877 # or undef if no 'sub name' declaration
3878 # is found.
3879 #
3880 # Side effects: None.
3881 # --------------------------------------------------
3882 803 ▶sub _extract_function_name {
Subroutine _extract_function_name: called CC=2 SCAR=6.9
3883 803 my ($self, $code) = @_;
3884 803 ▶ return $1 if $code =~ /^\s*sub\s+([a-zA-Z0-9_]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /^\s*sub\s+([a-zA-Z0-9_]+)/ 801 2 803
3885 2 return;
3886 }
3887  
3888 # --------------------------------------------------
3889 # _find_signature_statement
3890 #
3891 # Purpose: Search a PPI document for a
3892 # signature_for statement that
3893 # corresponds to a named function.
3894 #
3895 # Entry: $doc - PPI::Document to search.
3896 # $function - function name string.
3897 #
3898 # Exit: Returns the matching PPI::Statement
3899 # node, or undef if none is found.
3900 #
3901 # Side effects: None.
3902 # --------------------------------------------------
3903 799 ▶sub _find_signature_statement {
Subroutine _find_signature_statement: called CC=4 SCAR=14.0
3904 799 my ($self, $doc, $function) = @_;
3905  
3906 my $statements = $doc->find(
3907 2278627 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
3908 2278627 ▶ $_[1]->isa('PPI::Statement') && $_[1]->content =~ /^\s*signature_for\b/
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement") and $_[1]->content =~ /^\s*signature_for\b/ 234531 2044096 2278627
3909 }
3910 799 ▶ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $statements = $doc->find(sub { $_[1]->isa("PPI::Statement") and $_[1]->content =~ /^\s*signature_for\b/; } ) or return 3 796 799
3911  
3912 3 foreach my $stmt (@$statements) {
3913 3 my $content = $stmt->content;
3914 3 ▶ if ($content =~ /^\s*signature_for\s+\Q$function\E\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\s*signature_for\s+\Q$function\E\b/) 3 0 3
3915 3 return $stmt;
3916 }
3917 }
3918  
3919 0 return;
3920 }
3921  
3922 # --------------------------------------------------
3923 # _extract_signature_expression
3924 #
3925 # Purpose: Extract the Type::Params signature
3926 # expression (everything after =>) from
3927 # a signature_for statement node.
3928 #
3929 # Entry: $stmt - PPI::Statement node.
3930 # $function - function name string,
3931 # used in the match pattern.
3932 #
3933 # Exit: Returns the signature expression
3934 # string, or undef if the pattern
3935 # does not match.
3936 #
3937 # Side effects: None.
3938 # --------------------------------------------------
3939 3 ▶sub _extract_signature_expression {
Subroutine _extract_signature_expression: called CC=2 SCAR=7.4
3940 3 my ($self, $stmt, $function) = @_;
3941  
3942 3 my $content = $stmt->content;
3943  
3944 3 ▶ if ($content =~ /^\s*signature_for\s+\Q$function\E\s*=>\s*(.+?);?\s*$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\s*signature_for\s+\Q$function\E\s*=>\s*(.+?);?\s*$/s) 3 0 3
3945 3 return $1;
3946 }
3947  
3948 0 return;
3949 }
3950  
3951 # --------------------------------------------------
3952 # _compile_signature_isolated
3953 #
3954 # Purpose: Compile and evaluate a Type::Params
3955 # signature expression in an isolated
3956 # process to extract parameter metadata
3957 # without polluting the current process.
3958 #
3959 # Only runs when the caller passed
3960 # allow_signature_exec => 1 to new().
3961 # Extracting parameter types from a
3962 # Type::Params signature_for() declaration
3963 # requires actually building the type
3964 # objects at runtime -- there is no purely
3965 # static way to do it -- so this is real
3966 # execution of an excerpt of the target
3967 # module's own source. Every other code
3968 # path in this module is static (PPI-only)
3969 # analysis that never runs the target's
3970 # code, so this one feature must be opted
3971 # into explicitly rather than triggered
3972 # implicitly by extract_all().
3973 #
3974 # A Safe compartment was previously tried
3975 # first as a "fast path" before falling
3976 # back to this subprocess unconditionally.
3977 # It was removed: Type::Params and
3978 # Types::Common pull in XS modules (e.g.
3979 # B.pm via Type::Params), and Safe cannot
3980 # host XS/dynamic loading at all, so the
3981 # compartment never succeeded for any real
3982 # signature_for() declaration -- it was
3983 # dead code that gave a false impression of
3984 # sandboxing while every real call fell
3985 # through to the unconditional subprocess
3986 # below.
3987 #
3988 # Entry: $function - function name string.
3989 # $signature_expr - Type::Params
3990 # signature expression
3991 # string.
3992 #
3993 # Exit: Returns a decoded JSON hashref
3994 # containing parameters and returns
3995 # metadata on success.
3996 # Returns undef without running anything if
3997 # allow_signature_exec was not enabled.
3998 # Croaks on unsafe expressions, timeout,
3999 # or compile errors.
4000 #
4001 # Side effects: May fork a child process with a
4002 # memory limit applied via
4003 # BSD::Resource if available.
4004 # Memory limiting is best-effort and
4005 # silently skipped on platforms where
4006 # BSD::Resource is unavailable.
4007 # --------------------------------------------------
4008 12 ▶sub _compile_signature_isolated {
Subroutine _compile_signature_isolated: called CC=13 SCAR=26.2
4009 12 my ($self, $function, $signature_expr) = @_;
4010  
4011 12 ▶ unless ($self->{allow_signature_exec}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"allow_signature_exec"}) 2 10 12
4012 carp "Skipping Type::Params signature_for($function) extraction: ",
4013 'allow_signature_exec => 1 was not passed to new() ',
4014 '(this would execute code from the target module)'
4015 2 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
4016 2 return;
4017 }
4018  
4019 # Remove comments
4020 10 $signature_expr =~ s/#.*$//mg;
4021  
4022 # Reject obviously dangerous constructs. This is defense in depth
4023 # only, not a real security boundary -- it is a denylist of literal
4024 # tokens and cannot catch e.g. a symbolic-ref call built by string
4025 # concatenation. The actual control here is the allow_signature_exec
4026 # opt-in above: this code must never run against a module the caller
4027 # has not already decided to trust enough to execute.
4028 # Both checks unified into one croak so the message and class are consistent
4029 10 ▶ if ($signature_expr =~ $UNSAFE_KEYWORD_RE || $signature_expr =~ $UNSAFE_CHAR_RE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature_expr =~ /$UNSAFE_KEYWORD_RE/ or $signature_expr =~ /$UNSAFE_CHAR_RE/) 0 10 10
Condition33%1/3
$signature_expr =~ /$UNSAFE_KEYWORD_RE/ or $signature_expr =~ /$UNSAFE_CHAR_RE/
l!l&&r!l&&!r
0010
MC/DC0%0/2
$signature_expr =~ /$UNSAFE_KEYWORD_RE/ or $signature_expr =~ /$UNSAFE_CHAR_RE/
$signature_expr =~ /$UNSAFE_KEYWORD_RE/ $signature_expr =~ /$UNSAFE_CHAR_RE/
Truth table33%1/3
A or B
A= $signature_expr =~ /$UNSAFE_KEYWORD_RE/B= $signature_expr =~ /$UNSAFE_CHAR_RE/
ABresult
1X1
011
000
4030 0 croak 'Unsafe signature expression -- rejected to prevent code execution';
4031 }
4032  
4033 10 my $payload = <<'PERL';
4034 use strict;
4035 use warnings;
4036 use Type::Params -sigs;
4037 use Types::Common -types;
4038 use JSON::MaybeXS;
4039  
4040 # Apply address-space limit passed from parent via env. Done here (in the
4041 # child) rather than in the parent so the parent's memory is never capped.
4042 if (my $limit = $ENV{_ATG_RLIMIT_AS}) {
4043 eval {
4044 require BSD::Resource;
4045 BSD::Resource::setrlimit(BSD::Resource::RLIMIT_AS(), $limit, $limit);
4046 };
4047 }
4048  
4049 # Stub sub so Perl can parse it
4050 sub FUNCTION_NAME {}
4051  
4052 # Create the Type::Params signature object
4053 my $sig = signature_for FUNCTION_NAME => SIGNATURE_EXPR;
4054  
4055 # Extract parameters — guard against older Type::Params (< 2.x) where
4056 # signature_for() installs the constraint but returns undef rather than
4057 # the signature object, so ->parameters() would die.
4058 my @sig_params = (defined $sig && ref $sig && $sig->can('parameters'))
4059 ? @{ $sig->parameters || [] }
4060 : ();
4061 my $pos = 0;
4062 my @params;
4063  
4064 # if ($sig->method) {
4065 # The $self value
4066 # push @params, {
4067 # name => 'arg0',
4068 # optional => 0,
4069 # position => $pos++,
4070 # };
4071 # }
4072  
4073 for my $p (@sig_params) {
4074 my $name = ($p->can('name') && defined($p->name) && length($p->name))
4075 ? $p->name
4076 : "arg$pos";
4077 push @params, {
4078 name => $name,
4079 optional => $p->optional ? 1 : 0,
4080 position => $pos,
4081 type => $p->type->name
4082 };
4083 $pos++;
4084 }
4085  
4086 # Extract return type
4087 my $returns;
4088 if (my $r = $sig->returns_scalar) {
4089 $returns = {
4090 context => 'scalar',
4091 type => $r ? $r->name : 'unknown',
4092 };
4093 } elsif ($r = $sig->returns_list) {
4094 $returns = {
4095 context => 'list',
4096 type => $r ? $r->name : 'unknown',
4097 };
4098 }
4099  
4100 print encode_json({
4101 parameters => \@params,
4102 returns => $returns,
4103 });
4104 PERL
4105  
4106 # Substitute function name and signature expression
4107 10 $payload =~ s/FUNCTION_NAME/$function/g;
4108 10 $payload =~ s/SIGNATURE_EXPR/$signature_expr/;
4109  
4110 # Run in an isolated Perl process
4111 10 my ($wtr, $rdr, $err) = (undef, undef, gensym);
4112 10 local %ENV;
4113  
4114 # Pass the memory limit to the child via env so the child can apply
4115 # setrlimit on itself after exec. Must NOT call setrlimit here in the
4116 # parent: setrlimit(RLIMIT_AS) constrains the parent's own address
4117 # space, and when the parent later tries to allocate memory for test
4118 # framework teardown it would OOM and crash inside the Test::Builder
4119 # subtest context, producing a "context destroyed" error.
4120 10 $ENV{_ATG_RLIMIT_AS} = $MEMORY_LIMIT_BYTES;
4121  
4122 10 my $pid = open3($wtr, $rdr, $err, $^X, '-T');
4123  
4124 10 print $wtr $payload;
4125 10 close $wtr;
4126  
4127 0 ▶ local $SIG{ALRM} = sub { croak 'Signature compile timeout' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
4128 10 eval { alarm($SIGNATURE_TIMEOUT_SECS) }; # no-op on Windows
4129  
4130 10 my $stdout = do { local $/; <$rdr> };
4131 10 my $stderr = do { local $/; <$err> };
4132  
4133 10 eval { alarm 0 };
4134  
4135 10 waitpid($pid, 0);
4136  
4137 10 ▶ if ($stderr && length $stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr and length $stderr) 1 9 10
Condition66%2/3
$stderr and length $stderr
!ll&&!rl&&r
901
MC/DC50%1/2
$stderr and length $stderr
$stderr length $stderr
Truth table66%2/3
A and B
A= $stderrB= length $stderr
ABresult
0X0
100
111
4138 1 ▶ carp "Error compiling signature:\n$stderr" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
4139 1 return;
4140 }
4141  
4142 # Child may be killed by the kernel OOM killer (SIGKILL) before it can
4143 # write anything to stdout or stderr. Guard both cases so we degrade
4144 # gracefully rather than croaking with "malformed JSON".
4145 9 ▶ if (!defined($stdout) || !length($stdout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stdout and length $stdout) 4 5 9
Condition66%2/3
defined $stdout and length $stdout
!ll&&!rl&&r
045
MC/DC50%1/2
defined $stdout and length $stdout
defined $stdout length $stdout
Truth table66%2/3
A and B
A= defined $stdoutB= length $stdout
ABresult
0X0
100
111
4146 4 ▶ carp 'Signature subprocess produced no output' if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 4 4
4147 4 return;
4148 }
4149  
4150 5 my $result = eval { decode_json($stdout) };
4151 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 2 5
4152 3 ▶ carp "Error decoding signature output: $@" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 3 3
4153 3 return;
4154 }
4155 2 return $result;
4156 }
4157  
4158 # --------------------------------------------------
4159 # _build_schema_from_meta
4160 #
4161 # Purpose: Convert the parameter and return type
4162 # metadata produced by
4163 # _compile_signature_isolated into a
4164 # standard schema hashref.
4165 #
4166 # Entry: $meta - hashref with 'parameters'
4167 # arrayref and optional
4168 # 'returns' hashref, as decoded
4169 # from the isolated compile
4170 # JSON output.
4171 #
4172 # Exit: Returns a schema hashref with input,
4173 # output, style, source, _notes, and
4174 # _confidence keys.
4175 #
4176 # Side effects: None.
4177 #
4178 # Notes: Unknown Type::Params type names are
4179 # mapped to 'string' with a note added
4180 # and confidence downgraded to 'medium'.
4181 # --------------------------------------------------
4182 2 ▶sub _build_schema_from_meta {
Subroutine _build_schema_from_meta: called CC=9 SCAR=24.3
4183 2 my ($self, $meta) = @_;
4184  
4185 2 my %type_map = (
4186 Num => 'number',
4187 Int => 'integer',
4188 Str => 'string',
4189 Bool => 'boolean',
4190 Object => 'object',
4191 ArrayRef => 'array',
4192 HashRef => 'object',
4193 );
4194  
4195 2 my $input;
4196 2 my $position = 0;
4197 2 my $confidence = 'high';
4198 2 my @notes = ('Type::Params detected');
4199  
4200 2 ▶ foreach my $p (@{ $meta->{parameters} || [] }) {
Condition50%1/2
$meta->{'parameters'} || []
l!l
20
MC/DC0%0/1
$meta->{'parameters'} || []
$meta->{'parameters'}
Truth table50%1/2
A
A= $meta->{'parameters'}
Aresult
11
00
4201 4 ▶ my $type = $type_map{ $p->{type} } // 'string';
Condition50%1/2
$type_map{$p->{"type"}} // "string"
l!l
40
MC/DC0%0/1
$type_map{$p->{"type"}} // "string"
$type_map{$p->{"type"}}
Truth table50%1/2
A
A= $type_map{$p->{"type"}}
Aresult
11
00
4202  
4203 4 ▶ if (!exists $type_map{$p->{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $type_map{$p->{"type"}}) 0 4 4
4204 0 push @notes, "Unknown type $p->{type}, defaulting to string";
4205 0 $confidence = 'medium';
4206 }
4207  
4208 $input->{"arg$position"} = {
4209 type => $type,
4210 position => $position,
4211 4 ▶ optional => $p->{optional} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$p->{'optional'} ? : 0 4 4
4212 };
4213  
4214 4 $position++;
4215 }
4216  
4217 2 my $output;
4218  
4219 2 ▶ if (my $ret = $meta->{returns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = $meta->{"returns"}) 2 0 2
4220 2 ▶ my $type = $type_map{ $ret->{type} } // 'string';
Condition50%1/2
$type_map{$ret->{"type"}} // "string"
l!l
20
MC/DC0%0/1
$type_map{$ret->{"type"}} // "string"
$type_map{$ret->{"type"}}
Truth table50%1/2
A
A= $type_map{$ret->{"type"}}
Aresult
11
00
4221  
4222 2 ▶ if (!exists $type_map{$ret->{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $type_map{$ret->{"type"}}) 0 2 2
4223 0 push @notes, "Unknown return type $ret->{type}, defaulting to string";
4224 0 $confidence = 'medium';
4225 }
4226  
4227 $output = {
4228 2 type => $type,
4229 "_$ret->{context}_context" => { type => $type },
4230 };
4231 }
4232  
4233 return {
4234 2 input => $input,
4235 output => $output,
4236 style => 'hash',
4237 source => SOURCE_VALIDATOR,
4238 _notes => \@notes,
4239 _confidence => {
4240 input => $confidence,
4241 },
4242 };
4243 }
4244  
4245 # --------------------------------------------------
4246 # _analyze_pod
4247 #
4248 # Purpose: Parse POD documentation for a method
4249 # and extract parameter names, types,
4250 # constraints, and optionality from
4251 # multiple POD patterns.
4252 #
4253 # Entry: $pod - string of POD content as
4254 # returned by _extract_pod_before.
4255 # May be undef or empty.
4256 #
4257 # Exit: Returns a hashref of parameter name
4258 # to parameter spec hashref. Returns an
4259 # empty hashref if no POD is provided
4260 # or no parameters are found.
4261 #
4262 # Side effects: Carps when a semantic type is
4263 # detected, advising the caller to
4264 # set config->properties.
4265 # Logs progress to stdout when
4266 # verbose is set.
4267 #
4268 # Notes: Three pattern strategies are tried
4269 # in order: (1) named Parameters section,
4270 # (2) inline $name - type format,
4271 # (3) =over/=item list. Parameters found
4272 # earlier take precedence over later
4273 # discoveries. Default values from POD
4274 # are merged in last.
4275 # --------------------------------------------------
4276 817 ▶sub _analyze_pod {
Subroutine _analyze_pod: called CC=117 SCAR=60.1
4277 817 my ($self, $pod, $routine) = @_;
4278  
4279 817 ▶ return {} unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 239 578 817
4280  
4281 578 my %params;
4282 578 my $position_counter = 0;
4283  
4284 # Check for positional arguments in method signature
4285 # Pattern: =head2 method_name($arg1, $arg2, $arg3)
4286 578 ▶ if ($pod =~ /=head2\s+\w+\s*\(([^)]+)\)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=head2\s+\w+\s*\(([^)]+)\)/s) 39 539 578
4287 39 my $sig = $1;
4288 # Extract parameter names in order
4289 39 my @sig_params = $sig =~ /\$(\w+)/g;
4290  
4291 # Skip $self or $class
4292 39 ▶ shift @sig_params if @sig_params && $sig_params[0] =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if @sig_params and $sig_params[0] =~ /^(self|class|pkg|proto|klass)$/i 0 39 39
Condition33%1/3
@sig_params and $sig_params[0] =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
0390
MC/DC0%0/2
@sig_params and $sig_params[0] =~ /^(self|class|pkg|proto|klass)$/i
@sig_params $sig_params[0] =~ /^(self|class|pkg|proto|klass)$/i
Truth table33%1/3
A and B
A= @sig_paramsB= $sig_params[0] =~ /^(self|class|pkg|proto|klass)$/i
ABresult
0X0
100
111
4293  
4294 # Assign positions
4295 39 foreach my $param (@sig_params) {
4296 51 ▶ $params{$param}{position} //= $position_counter;
Condition33%1/3
$params{$param}{"position"} //= $position_counter
l!l&&r!l&&!r
0510
MC/DC0%0/2
$params{$param}{"position"} //= $position_counter
$params{$param}{"position"} $position_counter
Truth table33%1/3
A //= B
A= $params{$param}{"position"}B= $position_counter
ABresult
1X1
011
000
4297 51 $self->_log(" POD: $param has position $params{$param}{position}");
4298 51 $position_counter++;
4299 }
4300 }
4301  
4302 578 $self->_log(" POD: Found $position_counter unnamed parameters to add to the position list");
4303  
4304 # Pattern 1: Parse line-by-line in Parameters section
4305 # First, extract the Parameters section
4306 578 my $param_section;
4307 578 ▶ if($pod =~ /(?:Parameters?|Arguments?|Inputs?):?\s*\n((?:\s*\$.*\n)+)/si) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pod =~ /(?:Parameters?|Arguments?|Inputs?):?\s*\n((?:\s*\$.*\n)+)/is) { } 57 521 578
elsif ($pod =~ /^=head\d+\s+(?:Parameters?|Arguments?|Inputs?)\b.*?\n(.*?)(?=^=head|\Z)/ism) { } 437 84 521
4308 57 $param_section = $1;
4309 } elsif ($pod =~ /^=head\d+\s+(?:Parameters?|Arguments?|Inputs?)\b.*?\n(.*?)(?=^=head|\Z)/msi) {
4310 437 $param_section = $1;
4311 }
4312 578 ▶ if($param_section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_section) 494 84 578
4313 494 my $param_order = 0;
4314  
4315 494 $self->_log(" POD: Scan for named parameters in '$param_section'");
4316 # Now parse each line that starts with $varname
4317 494 foreach my $line (split /\n/, $param_section) {
4318 3636 ▶ if ($line =~ /C<\$(\w+)>\s*\((Required|Mandatory)\)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /C<\$(\w+)>\s*\((Required|Mandatory)\)/i) 0 3636 3636
4319 0 $params{$1}{optional} = 0;
4320 0 $self->_log(" POD: $1 marked required from item header");
4321 }
4322  
4323 # Match: $name - type (constraints), description
4324 # or: $name - type, description
4325 # or: $name - type
4326 3636 ▶ if(($line =~ /^\s*\$(\w+)\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\$(\w+)\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i or $line =~ /^\s*C<\$(\w+)>\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i) 83 3553 3636
Condition100%3/3
$line =~ /^\s*\$(\w+)\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i or $line =~ /^\s*C<\$(\w+)>\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i
l!l&&r!l&&!r
7583553
MC/DC100%2/2
$line =~ /^\s*\$(\w+)\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i or $line =~ /^\s*C<\$(\w+)>\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i
$line =~ /^\s*\$(\w+)\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i $line =~ /^\s*C<\$(\w+)>\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i
Truth table100%3/3
A or B
A= $line =~ /^\s*\$(\w+)\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/iB= $line =~ /^\s*C<\$(\w+)>\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i
ABresult
1X1
011
000
4327 ($line =~ /^\s*C<\$(\w+)>\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i)) {
4328 83 my ($name, $type, $constraint, $desc) = ($1, lc($2), $3, $4);
4329  
4330 # Clean up
4331 83 ▶ $desc =~ s/^\s+|\s+$//g if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 75 8 83
4332  
4333 # Skip common non-parameters
4334 83 ▶ next if $name =~ /^(self|class|pkg|proto|klass|return|returns?)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass|return|returns?)$/i 0 83 83
4335  
4336 83 ▶ $params{$name} ||= { _source => SOURCE_POD };
Condition100%2/2
$params{$name} ||= {"_source", "pod"}
l!l
2855
MC/DC100%1/1
$params{$name} ||= {"_source", "pod"}
$params{$name}
Truth table100%2/2
A
A= $params{$name}
Aresult
11
00
4337  
4338 # If we haven't already assigned a position from the signature, use order in Parameters section
4339 83 ▶ unless (exists $params{$name}{position}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{$name}{"position"}) 55 28 83
4340 55 $params{$name}{position} = $param_order++;
4341 55 $self->_log(" POD: $name has position $params{$name}{position} (from Parameters order)");
4342 }
4343  
4344 # Normalize type names
4345 83 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 83 83
4346 83 ▶ $type = 'number' if $type eq 'num' || $type eq 'float';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "num" or $type eq "float" 0 83 83
Condition33%1/3
$type eq "num" or $type eq "float"
l!l&&r!l&&!r
0083
MC/DC0%0/2
$type eq "num" or $type eq "float"
$type eq "num" $type eq "float"
Truth table33%1/3
A or B
A= $type eq "num"B= $type eq "float"
ABresult
1X1
011
000
4347 83 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 83 83
4348 83 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 1 82 83
4349 83 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 83 83
4350  
4351 83 $params{$name}{type} = $type;
4352  
4353 # Parse constraints
4354 83 ▶ if($constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint) 9 74 83
4355 9 $self->_parse_constraints($params{$name}, $constraint);
4356 }
4357  
4358 # Check for optional/required in description OR constraint.
4359 # Use word boundaries to avoid matching "optionally" as "optional".
4360 83 ▶ my $full_text = ($constraint || '') . ' ' . ($desc || '');
Condition100%4/4
$constraint || ''
l!l
974
$desc || ''
l!l
758
MC/DC100%1/1
$constraint || ''
$constraint
MC/DC100%1/1
$desc || ''
$desc
Truth table100%2/2
A
A= $constraint
Aresult
11
00
Truth table100%2/2
A
A= $desc
Aresult
11
00
4361 83 ▶ if ($full_text =~ /\boptional\b/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($full_text =~ /\boptional\b/i) { } 10 73 83
elsif ($full_text =~ /required|mandatory/i) { } 4 69 73
4362 10 $params{$name}{optional} = 1;
4363 10 $self->_log(" POD: $name marked as optional");
4364 } elsif ($full_text =~ /required|mandatory/i) {
4365 4 $params{$name}{optional} = 0;
4366 4 $self->_log(" POD: $name marked as required");
4367 }
4368  
4369 # Detect semantic types:
4370 83 ▶ if ($desc =~ /\b(email|url|uri|path|filename)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /\b(email|url|uri|path|filename)\b/i) 4 79 83
4371 # TODO: ensure properties is set to 1 in $config
4372 4 carp('Manually set config->properties to 1 in ', $self->{'input_file'});
4373 4 $params{$name}{semantic} = lc($1);
4374 }
4375  
4376 # Look for regex patterns
4377 83 ▶ if ($desc && $desc =~ m{matches?\s+(/[^/]+/|qr/.+?/)}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc and $desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i) 6 77 83
Condition100%3/3
$desc and $desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i
!ll&&!rl&&r
8696
MC/DC100%2/2
$desc and $desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i
$desc $desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i
Truth table100%3/3
A and B
A= $descB= $desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i
ABresult
0X0
100
111
4378 6 $params{$name}{matches} = $1;
4379 6 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4380 }
4381  
4382 83 ▶ if(my $values = $self->_prose_memberof($desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($desc)) 0 83 83
4383 0 $params{$name}{memberof} = $values;
4384 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4385 0 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4386 }
4387  
4388 83 ▶ $self->_log(" POD: Found parameter '$name' in parameters section, type=$type" .
Branch: 2 branches
BranchTrueFalseTotal
$constraint ? : 9 74 83
$desc ? : 75 8 83
4389 ($constraint ? " ($constraint)" : '') .
4390 ($desc ? " - $desc" : ''));
4391 }
4392 }
4393 }
4394  
4395 # Pattern 2: Also try the inline format in case Parameters: section wasn't found
4396 578 ▶ while ($pod =~ /\$(\w+)\s*-\s*(string|integer|int|number|num|float|boolean|bool|arrayref|array|hashref|hash|object|any)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/gim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\$(\w+)\s*-\s*(string|integer|int|number|num|float|boolean|bool|arrayref|array|hashref|hash|object|any)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/mig) 61 578 639
4397 61 my ($name, $type, $constraint, $desc) = ($1, lc($2), $3, $4);
4398  
4399 # Only process if we haven't already found this param in the Parameters section
4400 61 ▶ next if exists $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$name} 61 0 61
4401  
4402 # Clean up description - remove leading/trailing whitespace
4403 0 ▶ $desc =~ s/^\s+|\s+$//g if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 0 0 0
4404  
4405 # Skip common words that aren't parameters
4406 0 ▶ next if $name =~ /^(self|class|pkg|proto|klass|return|returns?)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass|return|returns?)$/i 0 0 0
4407  
4408 0 ▶ $params{$name} ||= { _source => SOURCE_POD };
Condition0%0/2
$params{$name} ||= {"_source", "pod"}
l!l
00
MC/DC0%0/1
$params{$name} ||= {"_source", "pod"}
$params{$name}
Truth table0%0/2
A
A= $params{$name}
Aresult
11
00
4409  
4410 # Normalize type names
4411 0 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 0 0
4412 0 ▶ $type = 'number' if $type eq 'num' || $type eq 'float';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "num" or $type eq "float" 0 0 0
Condition0%0/3
$type eq "num" or $type eq "float"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "num" or $type eq "float"
$type eq "num" $type eq "float"
Truth table0%0/3
A or B
A= $type eq "num"B= $type eq "float"
ABresult
1X1
011
000
4413 0 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 0 0
4414 0 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 0 0
4415 0 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 0 0
4416  
4417 0 $params{$name}{type} = $type;
4418  
4419 # Parse constraints
4420 0 ▶ if ($constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint) 0 0 0
4421 0 $self->_parse_constraints($params{$name}, $constraint);
4422 }
4423  
4424 # Check for optional/required in description.
4425 # Use word boundaries to avoid matching "optionally" as "optional".
4426 0 ▶ if ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 0 0 0
4427 0 ▶ if ($desc =~ /\boptional\b/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($desc =~ /\boptional\b/i) { } 0 0 0
elsif ($desc =~ /required|mandatory/i) { } 0 0 0
4428 0 $params{$name}{optional} = 1;
4429 } elsif ($desc =~ /required|mandatory/i) {
4430 0 $params{$name}{optional} = 0;
4431 }
4432  
4433 # Look for regex patterns in description
4434 0 ▶ if ($desc =~ m{matches?\s+(/[^/]+/|qr/.+?/)}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i) 0 0 0
4435 0 $params{$name}{matches} = $1;
4436 }
4437 }
4438  
4439 0 ▶ if(my $values = $self->_prose_memberof($desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($desc)) 0 0 0
4440 0 $params{$name}{memberof} = $values;
4441 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4442 0 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4443 }
4444  
4445 0 ▶ $self->_log(" POD: Found parameter '$name' in the inline documentation, type=$type" .
Branch: 1 branch
BranchTrueFalseTotal
$constraint ? : 0 0 0
4446 ($constraint ? " ($constraint)" : ''));
4447 }
4448  
4449 # Pattern 3: Parse =over /=item list of arguments, e.g. `=item * C<$name>`.
4450 # The sigil is required here: `=item * C<field>` without one usually
4451 # documents a returned key or an internal field, not an argument.
4452 #
4453 # Inside an Arguments section the sigil is optional: some modules write
4454 # `=item * C<mutation_results>` for a real argument. Add the sigil for
4455 # those items only, so a Returns list elsewhere still cannot name an
4456 # argument.
4457 578 my $item_pod = $pod;
4458 578 $item_pod =~ s{
4459 (?:^=head[2-6]\s+arguments?\b)
4460 (.*?)
4461 (?=^=head[1-6]\b|\z)
4462 }{
4463 293 my $section = $&;
4464 293 $section =~ s{^=item(\s+\*\s*)C<\s*([A-Za-z_]\w*)\s*>}{
4465 146 '=item' . $1 . 'C<$' . $2 . '>'
4466 }gme;
4467 293 $section;
4468 }msgeix;
4469  
4470 578 ▶ while ($item_pod =~ /=item\s+(?:\*\s*)?(?:C<)?\$(\w+)\b(?:>)?\s*(?:-.*)?\n?(.*?)(?==item|\=back|\=head)/sig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_pod =~ /=item\s+(?:\*\s*)?(?:C<)?\$(\w+)\b(?:>)?\s*(?:-.*)?\n?(.*?)(?==item|\=back|\=head)/sig) 347 578 925
4471 347 my $name = $1;
4472 347 my $desc = $2;
4473  
4474 # Never allow empty or undefined parameter names
4475 347 ▶ next unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 347 347
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
00347
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
4476  
4477 347 $desc =~ s/^\s+|\s+$//g;
4478  
4479 # Skip common non-parameters
4480 347 ▶ next if $name =~ /^(self|class|pkg|proto|klass|return|returns?)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass|return|returns?)$/i 12 335 347
4481  
4482 335 ▶ $params{$name} ||= { _source => SOURCE_POD };
Condition100%2/2
$params{$name} ||= {"_source", "pod"}
l!l
2333
MC/DC100%1/1
$params{$name} ||= {"_source", "pod"}
$params{$name}
Truth table100%2/2
A
A= $params{$name}
Aresult
11
00
4483  
4484 # Explicit typed form only:
4485 # $param - type (constraints)
4486 335 ▶ if ($desc =~ /^\s*(string|integer|int|number|num|float|boolean|bool|array|arrayref|hash|hashref|any)\b(?:\s*\(([^)]+)\))?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /^\s*(string|integer|int|number|num|float|boolean|bool|array|arrayref|hash|hashref|any)\b(?:\s*\(([^)]+)\))?/i) { } 44 291 335
4487 44 my $type = lc($1);
4488 44 my $constraint = $2;
4489  
4490 # Normalize type names
4491 44 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 44 44
4492 44 ▶ $type = 'number' if $type eq 'num' || $type eq 'float';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "num" or $type eq "float" 0 44 44
Condition33%1/3
$type eq "num" or $type eq "float"
l!l&&r!l&&!r
0044
MC/DC0%0/2
$type eq "num" or $type eq "float"
$type eq "num" $type eq "float"
Truth table33%1/3
A or B
A= $type eq "num"B= $type eq "float"
ABresult
1X1
011
000
4493 44 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 44 44
4494 44 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 44 44
4495 44 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 44 44
4496  
4497 44 $params{$name}{type} = $type;
4498  
4499 44 ▶ if ($constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint) 12 32 44
4500 12 $self->_parse_constraints($params{$name}, $constraint);
4501 }
4502  
4503 44 $self->_log(" POD: Explicit type '$type' for $name");
4504 } else {
4505 # Heuristic inference from description text
4506 291 ▶ if ($desc =~ /\bstring\b/i) {
Branch: 5 branches
BranchTrueFalseTotal
if ($desc =~ /\bstring\b/i) { } 27 264 291
elsif ($desc =~ /\b(int|integer)\b/i) { } 1 263 264
elsif ($desc =~ /\b(num|number|float)\b/i) { } 6 257 263
elsif ($desc =~ /\b(bool|boolean)\b/i) { } 0 257 257
elsif ($desc =~ /\bhashref\b/i) { } 69 188 257
4507 27 $params{$name}{type} = 'string';
4508 } elsif ($desc =~ /\b(int|integer)\b/i) {
4509 1 $params{$name}{type} = 'integer';
4510 } elsif ($desc =~ /\b(num|number|float)\b/i) {
4511 6 $params{$name}{type} = 'number';
4512 } elsif ($desc =~ /\b(bool|boolean)\b/i) {
4513 0 $params{$name}{type} = 'boolean';
4514 } elsif ($desc =~ /\bhashref\b/i) {
4515 # "either a path or an already decoded hashref": the harness
4516 # can build a hashref, so prefer it over the default string.
4517 69 $params{$name}{type} = 'hashref';
4518 }
4519 }
4520  
4521 # Check for optional/required in description.
4522 # Use word boundaries to avoid matching "optionally" as "optional".
4523 # Ignore code spans: a description that mentions the parameter name
4524 # C<optional> (CoverageGuidedFuzzer's schema) is not saying the
4525 # parameter itself is optional, and can still say "Required."
4526 335 my $optionality_text = $desc;
4527 335 $optionality_text =~ s/\b[CBIL]<[^>]*>//g;
4528 335 ▶ if ($optionality_text =~ /\boptional\b/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($optionality_text =~ /\boptional\b/i) { } 91 244 335
elsif ($optionality_text =~ /\b(?:required|mandatory)\b/i) { } 104 140 244
4529 91 $params{$name}{optional} = 1;
4530 } elsif ($optionality_text =~ /\b(?:required|mandatory)\b/i) {
4531 104 $params{$name}{optional} = 0;
4532 }
4533  
4534 # Look for regex patterns
4535 335 ▶ if ($desc =~ m{matches?\s+(/[^/]+/|qr/.+?/)}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i) 0 335 335
4536 0 $params{$name}{matches} = $1;
4537 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4538 }
4539  
4540 335 ▶ if(my $values = $self->_prose_memberof($desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($desc)) 8 327 335
4541 8 $params{$name}{memberof} = $values;
4542 8 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4543 8 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4544 }
4545  
4546 335 $self->_log(" POD: Found parameter '$name' from =item list");
4547 }
4548  
4549 # Extract default values from POD
4550 578 my $pod_defaults = $self->_extract_defaults_from_pod($pod);
4551 578 foreach my $param (keys %$pod_defaults) {
4552 21 ▶ if (exists $params{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$param}) 15 6 21
4553 15 $params{$param}{_default} = $pod_defaults->{$param};
4554 15 ▶ $params{$param}{optional} = 1 unless defined $params{$param}{optional};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$param}{"optional"} 7 8 15
4555 $self->_log(sprintf(" POD: %s has default value: %s",
4556 $param,
4557 15 ▶ defined($pod_defaults->{$param}) ? $pod_defaults->{$param} : 'undef'
Branch: 1 branch
BranchTrueFalseTotal
defined $pod_defaults->{$param} ? : 14 1 15
4558 ));
4559 }
4560 }
4561  
4562 # Default undocumented optionality: documented params are REQUIRED unless stated otherwise
4563 578 for my $name (keys %params) {
4564 439 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 0 439 439
4565  
4566 # TODO: if optionality was never explicitly set, assume required.
4567 # Currently disabled as it breaks some schemas — revisit in a future pass.
4568 # if (!exists $params{$name}{optional}) {
4569 # $params{$name}{optional} = 0;
4570 # $self->_log(" POD: $name assumed required (no optional/default specified)");
4571 # }
4572 }
4573  
4574 # Pattern 0: =head3|4 Input formal spec — highest-priority type source.
4575 # Runs last so positional matching can use positions set by earlier patterns.
4576 # Accepts positional array format: [ {type=>'...'}, ... ]
4577 # and named hash format: { name => {type=>'...'}, ... }
4578 578 ▶ if ($pod =~ /=head[34]\s+Input\b(.*?)(?==head|\z)/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=head[34]\s+Input\b(.*?)(?==head|\z)/is) 412 166 578
4579 412 my $block = $1;
4580 412 $block =~ s/\A\s+//;
4581 # The spec may follow introductory prose and comment lines: start at
4582 # the first line that opens the structure.
4583 412 ▶ $block =~ s/\A.*?^[ \t]+(?=[\[\{])//ms unless($block =~ /\A[\[\{]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $block =~ /\A[\[\{]/ 29 383 412
4584  
4585 412 ▶ if ($block =~ /\A\[/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block =~ /\A\[/) { } 4 408 412
elsif ($block =~ /\A\{/) { } 380 28 408
4586 # Positional format: each {…} maps to the param at that array index.
4587 4 my $idx = 0;
4588 4 ▶ while ($block =~ /\{([^}]*)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /\{([^}]*)\}/g) 6 4 10
4589 6 my $spec = $1;
4590 0 ▶ my ($name) = grep { ($params{$_}{position} // -1) == $idx }
Condition0%0/2
$params{$_}{"position"} // -1
l!l
00
MC/DC0%0/1
$params{$_}{"position"} // -1
$params{$_}{"position"}
Truth table0%0/2
A
A= $params{$_}{"position"}
Aresult
11
00
4591 keys %params;
4592 6 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 6 6
4593 0 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4594 0 ▶ if (my $t = $self->_map_formal_input_type($spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->_map_formal_input_type($spec)) 0 0 0
4595 0 $params{$name}{type} = $t;
4596 0 $self->_log(" POD: $name type '$t' from =head Input (positional $idx)");
4597 }
4598 0 ▶ if(defined(my $opt = _spec_optionality($spec))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $opt = _spec_optionality($spec))) 0 0 0
4599 0 $params{$name}{optional} = $opt;
4600 }
4601 }
4602 6 $idx++;
4603 }
4604 } elsif ($block =~ /\A\{/) {
4605 # Named format: each 'name => {…}' entry maps directly by name.
4606 380 ▶ while ($block =~ /\b(\w+)\s*=>\s*\{([^}]*)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /\b(\w+)\s*=>\s*\{([^}]*)\}/g) 609 380 989
4607 609 my ($name, $spec) = ($1, $2);
4608 609 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 220 389 609
4609 389 ▶ $params{$name} //= { _source => SOURCE_INPUT_SPEC };
Condition100%2/2
$params{$name} //= {"_source", "input_spec"}
l!l
29693
MC/DC100%1/1
$params{$name} //= {"_source", "input_spec"}
$params{$name}
Truth table100%2/2
A
A= $params{$name}
Aresult
11
00
4610 389 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4611 389 ▶ if (my $t = $self->_map_formal_input_type($spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->_map_formal_input_type($spec)) 389 0 389
4612 389 $params{$name}{type} = $t;
4613 389 $self->_log(" POD: $name type '$t' from =head Input (named)");
4614 }
4615 389 ▶ if(defined(my $opt = _spec_optionality($spec))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $opt = _spec_optionality($spec))) 206 183 389
4616 206 $params{$name}{optional} = $opt;
4617 }
4618 389 ▶ if ($spec =~ /\bmemberof\s*=>\s*\[([^\]]*)\]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bmemberof\s*=>\s*\[([^\]]*)\]/i) 4 385 389
4619 4 my $list_str = $1;
4620 4 my @vals;
4621 4 ▶ while ($list_str =~ /['"]([^'"]*)['"]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_str =~ /['"]([^'"]*)['"]/g) 8 4 12
4622 8 push @vals, $1;
4623 }
4624 4 ▶ if(@vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 4 0 4
4625 4 $params{$name}{memberof} = \@vals;
4626 # The list is documentation: the code may accept more.
4627 4 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4628 }
4629 }
4630 389 ▶ if ($spec =~ /\bmin\s*=>\s*(\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bmin\s*=>\s*(\d+)/i) 46 343 389
4631 46 $params{$name}{min} = $1 + 0;
4632 }
4633 389 ▶ if ($spec =~ /\bmax\s*=>\s*(\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bmax\s*=>\s*(\d+)/i) 29 360 389
4634 29 $params{$name}{max} = $1 + 0;
4635 }
4636 # A documented size is a claim about valid input, not a check
4637 # the routine has been seen to make: NameCase's spec says
4638 # min => 1 while its code accepts []. The harness still uses
4639 # the bound to build valid data, but generates no outside case.
4640 389 ▶ if(exists($params{$name}{min}) || exists($params{$name}{max})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$name}{"min"} or exists $params{$name}{"max"}) 59 330 389
Condition100%3/3
exists $params{$name}{"min"} or exists $params{$name}{"max"}
l!l&&r!l&&!r
4613330
MC/DC100%2/2
exists $params{$name}{"min"} or exists $params{$name}{"max"}
exists $params{$name}{"min"} exists $params{$name}{"max"}
Truth table100%3/3
A or B
A= exists $params{$name}{"min"}B= exists $params{$name}{"max"}
ABresult
1X1
011
000
4641 59 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4642 }
4643 389 ▶ if ($spec =~ /\bisa\s*=>\s*['"]([^'"]+)['"]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bisa\s*=>\s*['"]([^'"]+)['"]/i) 36 353 389
4644 36 $params{$name}{isa} = $1;
4645 }
4646 }
4647 # Two or more documented names signal a hash/named API, and
4648 # positional info from signature analysis is then not meaningful:
4649 # it causes "param X missing position" errors when params are
4650 # mixed. A lone name may instead label the first positional
4651 # argument, which _positional_pod_input decides once the body is
4652 # known.
4653 380 delete $params{$_}{position} for keys %params;
4654 }
4655  
4656 # A human-readable table names each parameter once, as
4657 # `lat : number -90 .. 90` or `$path SCALAR a readable .json file`.
4658 # It is a formal specification like the structures above, so the type
4659 # it gives is documented rather than a check the code was seen to make.
4660 412 $self->_apply_pod_input_table($block, \%params, $routine);
4661  
4662 # Some input specs are a bare list of `name => { ... }` entries with no
4663 # outer braces, as Database::Join's new() has. Parsing the whole bare
4664 # list would read unrelated examples in the block, so only the
4665 # unambiguous required flag is taken from it; the parameter itself is
4666 # described by the validator, signature or prose. A structured spec
4667 # was read above, and only top-level entries count: a nested
4668 # properties entry's required flag belongs to that property.
4669 412 ▶ unless($block =~ /\A[\[\{]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block =~ /\A[\[\{]/) 28 384 412
4670 28 for my $entry (_top_level_hash_entries($block)) {
4671 6 my ($bare_name, $bare_spec) = @{$entry};
4672 6 ▶ next if $bare_name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $bare_name =~ /^(self|class|pkg|proto|klass)$/i 0 6 6
4673 6 my $own = _own_spec_text($bare_spec);
4674 6 ▶ next unless $own =~ /\brequired\s*=>\s*(0|1)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $own =~ /\brequired\s*=>\s*(0|1)/i 4 2 6
4675 2 ▶ next if $own =~ /\boptional\s*=>\s*(?:0|1)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $own =~ /\boptional\s*=>\s*(?:0|1)/i 0 2 2
4676 2 ▶ $params{$bare_name} //= { _source => SOURCE_INPUT_SPEC };
Condition50%1/2
$params{$bare_name} //= {"_source", "input_spec"}
l!l
20
MC/DC0%0/1
$params{$bare_name} //= {"_source", "input_spec"}
$params{$bare_name}
Truth table50%1/2
A
A= $params{$bare_name}
Aresult
11
00
4677 2 $params{$bare_name}{optional} = 1 - ($1 + 0);
4678  
4679 # The comment lines under the entry describe its shape.
4680 2 my ($desc) = $block =~ /(?:^|\n)[ \t]*\Q$bare_name\E\s*=>[^\n]*(?:\n[ \t]+[^\n#][^\n]*)*\n((?:[ \t]*#[^\n]*(?:\n|\z))+)/;
4681 2 my ($bare_type) = $own =~ /\btype\s*=>\s*'?(\w+)/;
4682 2 ▶ if(my $shape = _pod_nested_input_shape($bare_type // '', $desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $shape = _pod_nested_input_shape($bare_type // "", $desc)) 0 2 2
Condition50%1/2
$bare_type // ""
l!l
20
MC/DC0%0/1
$bare_type // ""
$bare_type
Truth table50%1/2
A
A= $bare_type
Aresult
11
00
4683 0 $params{$bare_name}{schema} = $shape;
4684 }
4685 }
4686 }
4687 }
4688  
4689 # Documented value lists in =item prose, e.g.
4690 # =item * C<$colour> - one of red, green or blue
4691 # Run after the formal =head Input spec, which can be the only place a
4692 # parameter is named (strict_pod is such a parameter): the prose list
4693 # then still reaches it. Only parameters already found are updated:
4694 # a Returns section also uses =item, and its entries are outputs.
4695 578 ▶ while ($pod =~ /=item\s+(?:\*\s*)?(?:C<\$?(\w+)>|\$(\w+))\s*-?\s*((?:[^\n]*\n)*?)(?==item|\=back|\=head)/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=item\s+(?:\*\s*)?(?:C<\$?(\w+)>|\$(\w+))\s*-?\s*((?:[^\n]*\n)*?)(?==item|\=back|\=head)/gi) 466 578 1044
4696 466 ▶ my ($name, $text) = ($1 // $2, $3);
Condition66%2/3
$1 // $2
l!l&&r!l&&!r
46510
MC/DC0%0/2
$1 // $2
$1 $2
Truth table66%2/3
A // B
A= $1B= $2
ABresult
1X1
011
000
4697 466 ▶ next unless $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{$name} 127 339 466
4698 339 ▶ next if defined($params{$name}{memberof});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{$name}{"memberof"} 8 331 339
4699  
4700 331 ▶ if(my $values = $self->_prose_memberof($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($text)) 0 331 331
4701 0 $params{$name}{memberof} = $values;
4702 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4703 0 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4704 }
4705 }
4706  
4707 578 $self->_sanitize_parameter_names(\%params, 'POD analysis');
4708 578 return \%params;
4709 }
4710  
4711 # --------------------------------------------------
4712 # _map_formal_input_type
4713 #
4714 # Purpose: Extract and normalise the type string
4715 # from a parameter spec fragment such as
4716 # "type => 'scalar | scalarref'".
4717 # Handles union types by returning the
4718 # canonical ATG type for the first
4719 # recognised alternative.
4720 #
4721 # Entry: $spec - text content of a { } block
4722 # from a =head3|4 Input spec.
4723 #
4724 # Exit: Canonical type string, or undef when
4725 # no 'type' key is present or the value
4726 # is not a recognised type name.
4727 # --------------------------------------------------
4728 394 ▶sub _map_formal_input_type {
Subroutine _map_formal_input_type: called CC=5 SCAR=16.1
4729 394 my ($self, $spec) = @_;
4730 # Accept both quoted type => 'scalar' and unquoted Params::Validate
4731 # constants type => OBJECT (no quotes around the constant name).
4732 394 ▶ return undef unless $spec =~ /\btype\s*=>\s*(?:['"]([^'"]+)['"]|([A-Z_]+))/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ /\btype\s*=>\s*(?:['"]([^'"]+)['"]|([A-Z_]+))/i 1 393 394
4733 393 ▶ my $raw = lc(defined($1) ? $1 : $2);
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 148 245 393
4734 393 $raw =~ s/\s+//g;
4735  
4736 393 my %map = (
4737 scalar => 'string',
4738 scalarref => 'string',
4739 str => 'string',
4740 string => 'string',
4741 int => 'integer',
4742 integer => 'integer',
4743 num => 'number',
4744 number => 'number',
4745 float => 'number',
4746 bool => 'boolean',
4747 boolean => 'boolean',
4748 array => 'arrayref',
4749 arrayref => 'arrayref',
4750 hash => 'hashref',
4751 hashref => 'hashref',
4752 object => 'object',
4753 any => 'any',
4754 undef => 'undef',
4755 coderef => 'coderef',
4756 );
4757  
4758 393 for my $t (split /\|/, $raw) {
4759 393 ▶ return $map{$t} if exists $map{$t};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map{$t} 392 1 393
4760 }
4761 1 return undef;
4762 }
4763  
4764 # --------------------------------------------------
4765 # _apply_pod_input_table
4766 #
4767 # Purpose: Read a =head3|4 Input section written as a table, one
4768 # `name : type ...` (or `$name SCALAR ...`) line per parameter,
4769 # and record the type each one documents.
4770 #
4771 # Entry: $block - text of the Input section.
4772 # $params - parameters found so far; updated in place.
4773 #
4774 # Exit: Returns the number of parameters the table describes.
4775 #
4776 # Notes: The table is a formal input specification, so its type is
4777 # documented: the harness builds valid data from it instead of
4778 # expecting a wrong-typed value to die. A range in the entry,
4779 # such as `-90 .. 90`, is a documented bound, and "optional" or
4780 # "default" makes the parameter optional.
4781 # --------------------------------------------------
4782 # --------------------------------------------------
4783 # _pod_documents_class_invocant
4784 #
4785 # Purpose: Say whether a method's POD INPUT section documents a $class
4786 # invocant, which makes the routine a class method.
4787 #
4788 # Entry: $pod - the method's POD text.
4789 #
4790 # Exit: Returns 1 when the INPUT section names $class, 0 otherwise.
4791 # --------------------------------------------------
4792 817 ▶sub _pod_documents_class_invocant {
Subroutine _pod_documents_class_invocant: called CC=5 SCAR=16.1
4793 817 my ($self, $pod) = @_;
4794  
4795 817 ▶ return 0 unless defined($pod) && length($pod);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod and length $pod 239 578 817
Condition66%2/3
defined $pod and length $pod
!ll&&!rl&&r
0239578
MC/DC50%1/2
defined $pod and length $pod
defined $pod length $pod
Truth table66%2/3
A and B
A= defined $podB= length $pod
ABresult
0X0
100
111
4796 578 ▶ return 0 unless $pod =~ /=head[34]\s+Input\b(.*?)(?==head|\z)/si;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod =~ /=head[34]\s+Input\b(.*?)(?==head|\z)/is 166 412 578
4797  
4798 # `$class SCALAR the class (called as a class method)` and
4799 # `class : SCALAR` are both written this way.
4800 412 ▶ return (($1 =~ /^\s*\$?class\b/mi) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$1 =~ /^\s*\$?class\b/im ? : 4 408 412
4801 }
4802  
4803 # --------------------------------------------------
4804 # _strip_literals
4805 #
4806 # Purpose: Remove quoted strings and regex literals from a code fragment
4807 # before looking for arithmetic operators in it. A '-' in
4808 # 'Period-over-period', a '%' in sprintf '%04d', or a '/' in
4809 # /[0-9%]/ is text, not arithmetic, and reading it as an operator
4810 # typed string arguments and string-returning routines as numbers.
4811 #
4812 # Entry: $code - source fragment (a parameter body or a return value).
4813 #
4814 # Exit: The fragment with each quoted string and regex literal body
4815 # emptied. Undef is treated as an empty fragment.
4816 # --------------------------------------------------
4817 1432 ▶sub _strip_literals {
Subroutine _strip_literals: called CC=2 SCAR=7.0
4818 1432 my ($code) = @_;
4819  
4820 1432 ▶ return '' unless defined($code);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 1432 1432
4821  
4822 1432 $code =~ s/'(?:\\.|[^'\\])*'/''/g;
4823 1432 $code =~ s/"(?:\\.|[^"\\])*"/""/g;
4824  
4825 # m{...}, qr{...} and s{...}{...}, which a '/' pattern would miss.
4826 1432 $code =~ s{\b(?:qr|m|s|tr|y)\s*\{(?:\\.|[^{}\\]|\{[^{}]*\})*\}
4827 (?:\s*[a-z]*\s*\{(?:\\.|[^{}\\]|\{[^{}]*\})*\})?}{''}gx;
4828 1432 $code =~ s{\b(?:qr|m|s|tr|y)\s*/(?:\\.|[^/\\])*/}{''}g;
4829 1432 $code =~ s{([=!]~\s*)/(?:\\.|[^/\\])*/}{"$1"}g;
4830  
4831 1432 return $code;
4832 }
4833  
4834 # --------------------------------------------------
4835 # _strip_comments
4836 #
4837 # Purpose: Remove Perl comments from a code fragment before it is scanned
4838 # for parameters. A comment may quote an example signature;
4839 # reading it as code invents parameters the routine does not take
4840 # (an example in extract_all() once added $password to its schema).
4841 #
4842 # Entry: $code - source fragment (a method body).
4843 # $node - optional PPI node for the same code. When given, its
4844 # token stream is used, so a '#' inside a quoted string
4845 # is kept.
4846 #
4847 # Exit: The fragment with each comment removed. Undef is treated as an
4848 # empty fragment.
4849 # --------------------------------------------------
4850 1632 ▶sub _strip_comments {
Subroutine _strip_comments: called CC=5 SCAR=16.2
4851 1632 my ($code, $node) = @_;
4852  
4853 1632 ▶ return '' unless defined($code);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 1632 1632
4854  
4855 1632 ▶ if($node && $node->can('tokens')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node and $node->can("tokens")) 1630 2 1632
Condition66%2/3
$node and $node->can("tokens")
!ll&&!rl&&r
201630
MC/DC50%1/2
$node and $node->can("tokens")
$node $node->can("tokens")
Truth table66%2/3
A and B
A= $nodeB= $node->can("tokens")
ABresult
0X0
100
111
4856 return join '', map {
4857 1630 ▶ $_->isa('PPI::Token::Comment') ? '' : $_->content
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('PPI::Token::Comment') ? : 4357 203941 208298
4858 } $node->tokens;
4859 }
4860  
4861 # No PPI node (a synthesized class method): fall back to line comments.
4862 2 $code =~ s/[ \t]*\#[^\n]*//g;
4863 2 return $code;
4864 }
4865  
4866 # --------------------------------------------------
4867 # _pod_input_prose_type
4868 #
4869 # Purpose: Name the type a one-line description of an input value gives,
4870 # for the `$value # string` form of an Input table.
4871 #
4872 # Entry: $text - the description after the '#'.
4873 #
4874 # Exit: Canonical type string, or undef when it names none.
4875 # --------------------------------------------------
4876 8 ▶sub _pod_input_prose_type {
Subroutine _pod_input_prose_type: called CC=8 SCAR=22.1
4877 8 my ($text) = @_;
4878  
4879 8 ▶ return undef unless defined($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 8 8
4880 8 ▶ return 'arrayref' if $text =~ /\barrayref\b|\barray\s+of\b|\blist\s+of\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\barrayref\b|\barray\s+of\b|\blist\s+of\b/i 0 8 8
4881 8 ▶ return 'hashref' if $text =~ /\bhashref\b|\bhash\s+of\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\bhashref\b|\bhash\s+of\b/i 0 8 8
4882 8 ▶ return 'boolean' if $text =~ /\bboolean\b|\bbool\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\bboolean\b|\bbool\b/i 0 8 8
4883 8 ▶ return 'integer' if $text =~ /\binteger\b|\bint\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\binteger\b|\bint\b/i 0 8 8
4884 8 ▶ return 'number' if $text =~ /\bnumber\b|\bfloat\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\bnumber\b|\bfloat\b/i 0 8 8
4885 8 ▶ return 'string' if $text =~ /\bstring\b|\btext\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\bstring\b|\btext\b/i 3 5 8
4886 5 return undef;
4887 }
4888  
4889 # --------------------------------------------------
4890 # _pod_input_hash_dump
4891 #
4892 # Purpose: Read an Input section written as the Perl
4893 # hash it documents, one outer key per
4894 # parameter:
4895 #
4896 # {
4897 # name => { type => 'string', optional => 1 },
4898 # strategy => { type => HASHREF, optional => 0 },
4899 # }
4900 #
4901 # Only the outer keys are parameters; an inner
4902 # key describes its parameter. Lines outside
4903 # those hashes ("Valid and invalid input, by
4904 # partition:") are prose.
4905 #
4906 # Entry: $block - the Input section's text.
4907 #
4908 # Exit: A list of [ $name, \%fields ] in document
4909 # order, or an empty list when the section is
4910 # not a hash dump.
4911 # --------------------------------------------------
4912 412 ▶sub _pod_input_hash_dump {
Subroutine _pod_input_hash_dump: called CC=6 SCAR=17.9
4913 412 my ($block) = @_;
4914  
4915 412 my @rows;
4916 412 my ($name, $body, $depth) = (undef, '', 0);
4917  
4918 412 for my $line (split(/\n/, $block)) {
4919 1407 my $added;
4920 1407 ▶ if(!defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name) { } 1329 78 1407
4921 1329 ▶ next unless $line =~ /^\s*(\w+)\s*=>\s*\{(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s*(\w+)\s*=>\s*\{(.*)$/ 807 522 1329
4922 522 ($name, $body, $depth) = ($1, '', 1);
4923 522 $added = $2;
4924 } else {
4925 78 $added = $line;
4926 }
4927  
4928 600 $body .= ' ' . $added;
4929 600 $depth += ($added =~ tr/{//) - ($added =~ tr/}//);
4930 600 ▶ next if $depth > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 0 78 522 600
4931  
4932 522 ▶ push @rows, [ $name, _pod_input_hash_fields($body) ] if $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 522 0 522
4933 522 undef $name;
4934 522 $body = '';
4935 522 $depth = 0;
4936 }
4937  
4938 412 return @rows;
4939 }
4940  
4941 522 ▶sub _pod_input_hash_fields {
Subroutine _pod_input_hash_fields: called CC=4 SCAR=15.5
4942 522 my ($body) = @_;
4943  
4944 522 my %fields;
4945 # Keys are often quoted ('type' => 'boolean'), so the quote before =>
4946 # belongs to the key.
4947 522 ▶ while($body =~ /'?(\w+)'?\s*=>\s*'?([A-Za-z0-9_:.]+)'?/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /'?(\w+)'?\s*=>\s*'?([A-Za-z0-9_:.]+)'?/g) 975 522 1497
4948 975 $fields{$1} = $2;
4949 }
4950 # An enum names the documented values; the bare-word scan above cannot
4951 # read the list, so record it separately.
4952 522 ▶ if($body =~ /\benum\s*=>\s*\[([^\]]*)\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\benum\s*=>\s*\[([^\]]*)\]/) 0 522 522
4953 0 my @values = $1 =~ /['"]([^'"]+)['"]/g;
4954 0 ▶ $fields{enum} = \@values if(@values);
Branch: 1 branch
BranchTrueFalseTotal
if @values 0 0 0
4955 }
4956  
4957 522 return \%fields;
4958 }
4959  
4960 # The shape of a documented nested argument, read from the comment lines
4961 # under its Input entry: "Arrayref of {col, op, val} predicate hashrefs" is
4962 # an array of hashes with those keys, and "index => criteria hashref" is a
4963 # hash whose values are hashes. A type-less element spec maps key names to
4964 # their specs, which is how the harness builds a fixed-key hash.
4965 407 ▶sub _pod_nested_input_shape {
Subroutine _pod_nested_input_shape: called CC=16 SCAR=27.9
4966 407 my ($type, $desc) = @_;
4967  
4968 407 ▶ return undef unless(defined($type) && length($type));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and length $type 5 402 407
Condition66%2/3
defined $type and length $type
!ll&&!rl&&r
50402
MC/DC50%1/2
defined $type and length $type
defined $type length $type
Truth table66%2/3
A and B
A= defined $typeB= length $type
ABresult
0X0
100
111
4969 402 ▶ return undef unless(defined($desc) && !ref($desc) && length($desc));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc and not ref $desc and length $desc 369 33 402
Condition66%4/6
defined $desc and not ref $desc and length $desc
!ll&&!rl&&r
369033
defined $desc and not ref $desc
!ll&&!rl&&r
369033
MC/DC33%1/3
defined $desc and not ref $desc and length $desc
defined $desc not ref $desc length $desc
Truth table50%2/4
A and not(B) and C
A= defined $descB= not ref $descC= length $desc
ABCresult
0XX0
10X0
1100
1111
4970  
4971 33 ▶ if(($type eq 'arrayref') && $desc =~ /\bof\s+\{([^}]+)\}/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "arrayref" and $desc =~ /\bof\s+\{([^}]+)\}/i) 1 32 33
Condition100%3/3
$type eq "arrayref" and $desc =~ /\bof\s+\{([^}]+)\}/i
!ll&&!rl&&r
2571
MC/DC100%2/2
$type eq "arrayref" and $desc =~ /\bof\s+\{([^}]+)\}/i
$type eq "arrayref" $desc =~ /\bof\s+\{([^}]+)\}/i
Truth table100%3/3
A and B
A= $type eq "arrayref"B= $desc =~ /\bof\s+\{([^}]+)\}/i
ABresult
0X0
100
111
4972 1 my %element;
4973 1 for my $key ($1 =~ /(\w+)/g) {
4974 3 my %spec = (type => 'string');
4975 # "op -- one of: eq, ne, ..." is the documented domain.
4976 3 ▶ if($desc =~ /\b\Q$key\E\s*--\s*one\s+of:\s*([^\n]+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /\b\Q$key\E\s*--\s*one\s+of:\s*([^\n]+)/i) 1 2 3
4977 1 my @values = map { s/\A\s+|\s+\z//gr } split /,/, $1;
4978 1 ▶ $spec{memberof} = \@values if(@values);
Branch: 1 branch
BranchTrueFalseTotal
if @values 1 0 1
4979 }
4980 3 $element{$key} = \%spec;
4981 }
4982 1 ▶ return { %element } if(%element);
Branch: 1 branch
BranchTrueFalseTotal
if %element 1 0 1
4983 }
4984  
4985 # "Arrayref of insight hashrefs" (Database::BI::Summary's insights) holds
4986 # hashes, not the strings a bare arrayref would otherwise be given.
4987 32 ▶ if(($type eq 'arrayref') && $desc =~ /\bhashrefs?\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "arrayref" and $desc =~ /\bhashrefs?\b/i) 2 30 32
Condition100%3/3
$type eq "arrayref" and $desc =~ /\bhashrefs?\b/i
!ll&&!rl&&r
2552
MC/DC100%2/2
$type eq "arrayref" and $desc =~ /\bhashrefs?\b/i
$type eq "arrayref" $desc =~ /\bhashrefs?\b/i
Truth table100%3/3
A and B
A= $type eq "arrayref"B= $desc =~ /\bhashrefs?\b/i
ABresult
0X0
100
111
4988 2 return { type => 'hashref' };
4989 }
4990  
4991 30 ▶ return { type => 'hashref' }
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hashref" and $desc =~ /=>\s*\w+\s+hashref\b/i 1 29 30
Condition100%3/3
$type eq "hashref" and $desc =~ /=>\s*\w+\s+hashref\b/i
!ll&&!rl&&r
2631
MC/DC100%2/2
$type eq "hashref" and $desc =~ /=>\s*\w+\s+hashref\b/i
$type eq "hashref" $desc =~ /=>\s*\w+\s+hashref\b/i
Truth table100%3/3
A and B
A= $type eq "hashref"B= $desc =~ /=>\s*\w+\s+hashref\b/i
ABresult
0X0
100
111
4992 if(($type eq 'hashref') && $desc =~ /=>\s*\w+\s+hashref\b/i);
4993  
4994 29 return undef;
4995 }
4996  
4997 # `10_000` is the number 10000 written for a reader, not a string with
4998 # underscores in it.
4999 9 ▶sub _undigitised {
Subroutine _undigitised: called CC=2 SCAR=7.0
5000 9 my ($number) = @_;
5001  
5002 9 ▶ return undef unless defined($number);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 9 9
5003 9 $number =~ s/_//g;
5004 9 return $number + 0;
5005 }
5006  
5007 412 ▶sub _apply_pod_input_table {
Subroutine _apply_pod_input_table: called CC=103 SCAR=63.9
5008 412 my ($self, $block, $params, $routine) = @_;
5009  
5010 412 my %types = (
5011 scalar => 'string',
5012 str => 'string',
5013 string => 'string',
5014 int => 'integer',
5015 integer => 'integer',
5016 num => 'number',
5017 number => 'number',
5018 float => 'number',
5019 bool => 'boolean',
5020 boolean => 'boolean',
5021 array => 'arrayref',
5022 arrayref => 'arrayref',
5023 hash => 'hashref',
5024 hashref => 'hashref',
5025 object => 'object',
5026 coderef => 'coderef',
5027 code => 'coderef',
5028 any => 'any',
5029 );
5030  
5031 # The section may document the interface as the Perl hash it is:
5032 #
5033 # {
5034 # name => { type => 'string', optional => 1 },
5035 # strategy => { type => HASHREF, optional => 0 },
5036 # }
5037 #
5038 # Its outer keys are the parameters, in order, and their type and
5039 # optionality are stated rather than inferred. Prose elsewhere in the
5040 # section is not a row.
5041 412 ▶ if(my @dump = _pod_input_hash_dump($block)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@dump) = _pod_input_hash_dump($block)) 252 160 412
5042 252 my $count = 0;
5043 252 for my $row (@dump) {
5044 522 my ($name, $fields) = @{$row};
5045 522 ▶ next if $name =~ /^(?:self|class|pkg|proto|klass|returns?)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(?:self|class|pkg|proto|klass|returns?)$/i 150 372 522
5046  
5047 # A hash documents named arguments, not call order: whether the
5048 # routine takes them positionally is the code's business, so the
5049 # call positions (if any) come from its signature.
5050 372 ▶ my $p = $params->{$name} //= {};
Condition100%2/2
$params->{$name} //= {}
l!l
3666
MC/DC100%1/1
$params->{$name} //= {}
$params->{$name}
Truth table100%2/2
A
A= $params->{$name}
Aresult
11
00
5051  
5052 372 ▶ if(defined($fields->{type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fields->{"type"}) 372 0 372
5053 372 my $type = $types{lc($fields->{type})};
5054 372 ▶ $p->{type} = $type if defined($type);
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 372 0 372
5055 }
5056 # A parameter the hash documents is required unless it says
5057 # otherwise, as the table parser treats a bare row.
5058 372 ▶ $p->{optional} = ($fields->{optional} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$fields->{'optional'} ? : 143 229 372
5059 $p->{isa} = $fields->{isa}
5060 372 ▶ if defined($fields->{isa}) && $fields->{isa} !~ /\Aundef\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields->{"isa"} and not $fields->{"isa"} =~ /\Aundef\z/i 36 336 372
Condition66%2/3
defined $fields->{"isa"} and not $fields->{"isa"} =~ /\Aundef\z/i
!ll&&!rl&&r
336036
MC/DC50%1/2
defined $fields->{"isa"} and not $fields->{"isa"} =~ /\Aundef\z/i
defined $fields->{"isa"} not $fields->{"isa"} =~ /\Aundef\z/i
Truth table66%2/3
A and not(B)
A= defined $fields->{"isa"}B= not $fields->{"isa"} =~ /\Aundef\z/i
ABresult
0X0
100
111
5061 # The documented values are the domain: a generated string outside
5062 # them is an error the routine promises to reject.
5063 $p->{memberof} = $fields->{enum}
5064 0 ▶ if ref($fields->{enum}) eq 'ARRAY' && @{$fields->{enum}};
Branch: 1 branch
BranchTrueFalseTotal
if ref $fields->{"enum"} eq "ARRAY" and @{$fields->{"enum"};} 0 372 372
Condition33%1/3
ref $fields->{"enum"} eq "ARRAY" and @{$fields->{"enum"};}
!ll&&!rl&&r
37200
MC/DC0%0/2
ref $fields->{"enum"} eq "ARRAY" and @{$fields->{"enum"};}
ref $fields->{"enum"} eq "ARRAY" @{$fields->{"enum"};}
Truth table33%1/3
A and B
A= ref $fields->{"enum"} eq "ARRAY"B= @{$fields->{"enum"};}
ABresult
0X0
100
111
5065 $p->{min} = $fields->{min} + 0
5066 372 ▶ if defined($fields->{min}) && $fields->{min} =~ /\A-?\d/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields->{"min"} and $fields->{"min"} =~ /\A-?\d/ 45 327 372
Condition66%2/3
defined $fields->{"min"} and $fields->{"min"} =~ /\A-?\d/
!ll&&!rl&&r
327045
MC/DC50%1/2
defined $fields->{"min"} and $fields->{"min"} =~ /\A-?\d/
defined $fields->{"min"} $fields->{"min"} =~ /\A-?\d/
Truth table66%2/3
A and B
A= defined $fields->{"min"}B= $fields->{"min"} =~ /\A-?\d/
ABresult
0X0
100
111
5067 $p->{max} = $fields->{max} + 0
5068 372 ▶ if defined($fields->{max}) && $fields->{max} =~ /\A-?\d/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields->{"max"} and $fields->{"max"} =~ /\A-?\d/ 29 343 372
Condition66%2/3
defined $fields->{"max"} and $fields->{"max"} =~ /\A-?\d/
!ll&&!rl&&r
343029
MC/DC50%1/2
defined $fields->{"max"} and $fields->{"max"} =~ /\A-?\d/
defined $fields->{"max"} $fields->{"max"} =~ /\A-?\d/
Truth table66%2/3
A and B
A= defined $fields->{"max"}B= $fields->{"max"} =~ /\A-?\d/
ABresult
0X0
100
111
5069 372 $p->{_source} = SOURCE_INPUT_SPEC;
5070  
5071 # The comment lines under the row describe the documented shape.
5072 # "Arrayref of {col, op, val} predicate hashrefs" fixes the element
5073 # keys (Database::Join::new's mandatory_filters), and a hashref
5074 # documented as "index => criteria hashref" holds hashrefs rather
5075 # than the strings a bare hashref would otherwise be given.
5076 372 my ($desc) = $block =~ /(?:^|\n)[ \t]*\Q$name\E\s*=>[^\n]*\n((?:[ \t]*#[^\n]*(?:\n|\z))+)/;
5077 372 ▶ if(my $shape = _pod_nested_input_shape($p->{type}, $desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $shape = _pod_nested_input_shape($p->{"type"}, $desc)) 2 370 372
5078 2 $p->{schema} = $shape;
5079 }
5080  
5081 $self->_log(' POD: ' . $name . ' type '
5082 372 ▶ . (defined($p->{type}) ? "'$p->{type}'" : '(none)')
Branch: 1 branch
BranchTrueFalseTotal
defined $p->{'type'} ? : 372 0 372
5083 . ' from the Input hash');
5084 372 $count++;
5085 }
5086  
5087 252 ▶ return $count if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 228 24 252
5088 }
5089  
5090 184 my @found;
5091 my %row_text;
5092  
5093 # The table lists the arguments in call order, so it gives their positions.
5094 184 my $position = 0;
5095 184 my $row_routine;
5096  
5097 # A row can name several parameters at once: `$spec, $mtime, $size # as
5098 # for path`. Each is an argument of the routine, in this order, described
5099 # by the rest of the row, so the row is read once per name.
5100 my @lines;
5101 184 for my $line (split(/\n/, $block)) {
5102 283 ▶ if($line =~ /^(\s{0,8})(\\?[\$\@%]\w+\s*,\s*(?:\\?[\$\@%]\w+\s*,\s*)*\\?[\$\@%]\w+)\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s{0,8})(\\?[\$\@%]\w+\s*,\s*(?:\\?[\$\@%]\w+\s*,\s*)*\\?[\$\@%]\w+)\s*(.*)$/) { } 1 282 283
5103 1 my ($indent, $names, $rest) = ($1, $2, $3);
5104 1 push @lines, map { $indent . $_ . " $rest" } ($names =~ /\\?([\$\@%]\w+)/g);
5105 } else {
5106 282 push @lines, $line;
5107 }
5108 }
5109  
5110 184 for my $line (@lines) {
5111 285 my ($name, $spec, $prose);
5112  
5113 # One block can document several helpers, each row prefixed with the
5114 # routine it belongs to: `compile_pattern: $pattern # string`.
5115 285 ▶ if($line =~ /^\s{0,8}(\w+)\s*:\s*\\?[\$\@%](\w+)\s+#\s*(\S.*)$/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($line =~ /^\s{0,8}(\w+)\s*:\s*\\?[\$\@%](\w+)\s+#\s*(\S.*)$/) { } 2 283 285
elsif ($line =~ /^\s{0,8}\\?[\$\@%]?(\w+)\s*(?::|=>)\s*(\S.*)$/) { } 45 238 283
elsif ($line =~ /^\s{0,8}\\?[\$\@%](\w+)\s+(SCALAR|ARRAYREF|HASHREF|CODEREF|CODE|OBJECT|GLOB)\b\s*,?\s*(.*)$/i) { } 5 233 238
elsif ($line =~ /^\s*\\?[\$\@%](\w+)\s+#\s*(\S.*)$/) { } 6 227 233
elsif ($line =~ /^\s{0,8}\\?[\$\@%](\w+)\s+(\S.*)$/) { } 1 226 227
5116 2 ($row_routine, $name, $spec, $prose) = ($1, $2, $3, 1);
5117 # The section's own indentation is stripped from its first line, so no
5118 # leading space is required. `$name : type`, `$name => type`, and the
5119 # sigil forms `\@rows arrayref ...` / `\%models => hashref ...` all
5120 # name a parameter and give its type; the sigil rows were previously
5121 # skipped, leaving those parameters to the inferred code types.
5122 } elsif($line =~ /^\s{0,8}\\?[\$\@%]?(\w+)\s*(?::|=>)\s*(\S.*)$/) {
5123 45 ($name, $spec) = ($1, $2);
5124 # A prose line can look like a row ("Effect: on the array
5125 # backend ..."); only a value that begins with a documented type
5126 # declares an argument. A type can be written quoted
5127 # ("'string',") or with a sigil first.
5128 45 (my $first = $spec) =~ s/\A['"]//;
5129 45 ($first) = $first =~ /\A([A-Za-z]+)/;
5130 45 ▶ next unless(defined($first) && exists($types{lc($first)}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and exists $types{lc $first} 24 21 45
Condition66%2/3
defined $first and exists $types{lc $first}
!ll&&!rl&&r
24021
MC/DC50%1/2
defined $first and exists $types{lc $first}
defined $first exists $types{lc $first}
Truth table66%2/3
A and B
A= defined $firstB= exists $types{lc $first}
ABresult
0X0
100
111
5131 } elsif($line =~ /^\s{0,8}\\?[\$\@%](\w+)\s+(SCALAR|ARRAYREF|HASHREF|CODEREF|CODE|OBJECT|GLOB)\b\s*,?\s*(.*)$/i) {
5132 5 ($name, $spec) = ($1, "$2 $3");
5133 } elsif($line =~ /^\s*\\?[\$\@%](\w+)\s+#\s*(\S.*)$/) {
5134 # `\t$value # string`, or a wider indent under a routine's
5135 # first row: the comment describes the value.
5136 6 ($name, $spec, $prose) = ($1, $2, 1);
5137 } elsif($line =~ /^\s{0,8}\\?[\$\@%](\w+)\s+(\S.*)$/) {
5138 # `\@rows arrayref of hashrefs # required; ...`: the type
5139 # follows the parameter, in lower case.
5140 1 ($name, $spec, $prose) = ($1, $2, 1);
5141 } else {
5142 226 next;
5143 }
5144  
5145 # Another routine's rows are not this routine's arguments.
5146 35 ▶ next if defined($routine) && defined($row_routine) && $row_routine ne $routine;
Branch: 1 branch
BranchTrueFalseTotal
if defined $routine and defined $row_routine and $row_routine ne $routine 1 34 35
Condition83%5/6
defined $routine and defined $row_routine and $row_routine ne $routine
!ll&&!rl&&r
3311
defined $routine and defined $row_routine
!ll&&!rl&&r
0332
MC/DC66%2/3
defined $routine and defined $row_routine and $row_routine ne $routine
defined $routine defined $row_routine $row_routine ne $routine
Truth table75%3/4
A and B and C
A= defined $routineB= defined $row_routineC= $row_routine ne $routine
ABCresult
0XX0
10X0
1100
1111
5147  
5148 34 ▶ next if $name =~ /^(?:self|class|pkg|proto|klass|returns?)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(?:self|class|pkg|proto|klass|returns?)$/i 1 33 34
5149  
5150 # A quoted type ('arrayref',) is written the same way as a bare one:
5151 # the quote belongs to the value, not to the type's name.
5152 33 (my $typed_spec = $spec) =~ s/\A['"]//;
5153 33 my ($type_word) = $typed_spec =~ /\A([A-Za-z]+)/;
5154 33 my $type;
5155 33 ▶ if($prose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prose) { } 8 25 33
5156 # In a comment the first word is prose, not a type: `$spec # any
5157 # spec string, or undef` documents a string, though its first word
5158 # is "any". The type is the one the description names.
5159 8 $type = _pod_input_prose_type($spec);
5160 8 ▶ $type = $types{lc($type_word)} if(!defined($type) && defined($type_word));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $type and defined $type_word 5 3 8
Condition66%2/3
not defined $type and defined $type_word
!ll&&!rl&&r
305
MC/DC50%1/2
not defined $type and defined $type_word
not defined $type defined $type_word
Truth table66%2/3
not(A) and B
A= not defined $typeB= defined $type_word
ABresult
0X0
100
111
5161 } else {
5162 25 ▶ $type = defined($type_word) ? $types{lc($type_word)} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $type_word ? : 25 0 25
5163 25 ▶ $type = _pod_input_prose_type($spec) if(!defined($type) && $prose);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $type and $prose 0 25 25
Condition33%1/3
not defined $type and $prose
!ll&&!rl&&r
2500
MC/DC0%0/2
not defined $type and $prose
not defined $type $prose
Truth table33%1/3
not(A) and B
A= not defined $typeB= $prose
ABresult
0X0
100
111
5164 }
5165  
5166 # The row documents the argument, so it exists and has this call
5167 # position even when the description names no type the extractor
5168 # recognises; the code's type then stands rather than the row
5169 # claiming a constraint it does not state.
5170 33 ▶ my $p = $params->{$name} //= {};
Condition100%2/2
$params->{$name} //= {}
l!l
132
MC/DC100%1/1
$params->{$name} //= {}
$params->{$name}
Truth table100%2/2
A
A= $params->{$name}
Aresult
11
00
5171 33 ▶ $p->{position} = $position++ unless defined $p->{position};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p->{"position"} 32 1 33
5172 33 ▶ $p->{type} = $type if defined($type);
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 28 5 33
5173 33 $p->{_source} = SOURCE_INPUT_SPEC;
5174 33 $row_text{$name} = $spec;
5175  
5176 # The row's own comment describes the documented shape: an arrayref
5177 # of "insight hashrefs" holds hashes rather than strings.
5178 33 ▶ if(my $shape = _pod_nested_input_shape($type, $spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $shape = _pod_nested_input_shape($type, $spec)) 2 31 33
5179 2 $p->{schema} = $shape;
5180 }
5181  
5182 33 ▶ if($spec =~ /\boptional\b/i || $spec =~ /\bdefault\b/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ /\boptional\b/i or $spec =~ /\bdefault\b/i) { } 13 20 33
elsif ($spec =~ /\b(?:required|mandatory)\b/i) { } 7 13 20
Condition66%2/3
$spec =~ /\boptional\b/i or $spec =~ /\bdefault\b/i
l!l&&r!l&&!r
13020
MC/DC50%1/2
$spec =~ /\boptional\b/i or $spec =~ /\bdefault\b/i
$spec =~ /\boptional\b/i $spec =~ /\bdefault\b/i
Truth table66%2/3
A or B
A= $spec =~ /\boptional\b/iB= $spec =~ /\bdefault\b/i
ABresult
1X1
011
000
5183 13 $p->{optional} = 1;
5184 } elsif($spec =~ /\b(?:required|mandatory)\b/i) {
5185 7 $p->{optional} = 0;
5186 }
5187  
5188 # `one or more` is a size floor, not a spelling: an empty list is
5189 # the documented error, so say so rather than letting the harness
5190 # treat [] as a legal value.
5191 33 ▶ if(defined($type) && $type eq 'arrayref' && $spec =~ /\b(?:one\s+or\s+more|non-empty)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq "arrayref" and $spec =~ /\b(?:one\s+or\s+more|non-empty)\b/i) 2 31 33
Condition100%6/6
defined $type and $type eq "arrayref" and $spec =~ /\b(?:one\s+or\s+more|non-empty)\b/i
!ll&&!rl&&r
2832
defined $type and $type eq "arrayref"
!ll&&!rl&&r
5235
MC/DC100%3/3
defined $type and $type eq "arrayref" and $spec =~ /\b(?:one\s+or\s+more|non-empty)\b/i
defined $type $type eq "arrayref" $spec =~ /\b(?:one\s+or\s+more|non-empty)\b/i
Truth table100%4/4
A and B and C
A= defined $typeB= $type eq "arrayref"C= $spec =~ /\b(?:one\s+or\s+more|non-empty)\b/i
ABCresult
0XX0
10X0
1100
1111
5192 2 $p->{min} = 1;
5193 }
5194  
5195 # "a readable .json file" is a precondition on the path: the harness
5196 # has to supply an existing file, not a generated string.
5197 33 ▶ if(defined($type) && $type eq 'string' && $spec =~ /\b(?:readable|existing)\b[^;:]*\bfile\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq "string" and $spec =~ /\b(?:readable|existing)\b[^;:]*\bfile\b/i) 1 32 33
Condition100%6/6
defined $type and $type eq "string" and $spec =~ /\b(?:readable|existing)\b[^;:]*\bfile\b/i
!ll&&!rl&&r
21111
defined $type and $type eq "string"
!ll&&!rl&&r
51612
MC/DC100%3/3
defined $type and $type eq "string" and $spec =~ /\b(?:readable|existing)\b[^;:]*\bfile\b/i
defined $type $type eq "string" $spec =~ /\b(?:readable|existing)\b[^;:]*\bfile\b/i
Truth table100%4/4
A and B and C
A= defined $typeB= $type eq "string"C= $spec =~ /\b(?:readable|existing)\b[^;:]*\bfile\b/i
ABCresult
0XX0
10X0
1100
1111
5198 1 $p->{semantic} = 'existing_file';
5199 }
5200  
5201 # "an existing directory" is the same kind of precondition, for the
5202 # cache factories that stat the directory they are given. The
5203 # harness makes a temporary directory rather than guessing at a path.
5204 33 ▶ if(defined($type) && $type eq 'string' && $spec =~ /\bdir(?:ectory)?\b/i
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq "string" and $spec =~ /\bdir(?:ectory)?\b/i and $spec =~ /\b(?:existing|readable|writable)\b/i) 1 32 33
Condition88%8/9
defined $type and $type eq "string" and $spec =~ /\bdir(?:ectory)?\b/i and $spec =~ /\b(?:existing|readable|writable)\b/i
!ll&&!rl&&r
3201
defined $type and $type eq "string" and $spec =~ /\bdir(?:ectory)?\b/i
!ll&&!rl&&r
21111
defined $type and $type eq "string"
!ll&&!rl&&r
51612
MC/DC75%3/4
defined $type and $type eq "string" and $spec =~ /\bdir(?:ectory)?\b/i and $spec =~ /\b(?:existing|readable|writable)\b/i
defined $type $type eq "string" $spec =~ /\bdir(?:ectory)?\b/i $spec =~ /\b(?:existing|readable|writable)\b/i
Truth table80%4/5
A and B and C and D
A= defined $typeB= $type eq "string"C= $spec =~ /\bdir(?:ectory)?\b/iD= $spec =~ /\b(?:existing|readable|writable)\b/i
ABCDresult
0XXX0
10XX0
110X0
11100
11111
5205 && $spec =~ /\b(?:existing|readable|writable)\b/i) {
5206 1 $p->{semantic} = 'directory';
5207 }
5208  
5209 # "the SQLite file's contents" is a byte string: a wide character is
5210 # not something a byte-oriented write accepts, so the harness passes
5211 # bytes rather than text (and no BOM probe, which is text).
5212 33 ▶ if($name =~ /\A(?:bytes|blob)\z/i
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\A(?:bytes|blob)\z/i or $spec =~ /\braw\s+bytes\b|file'?s\s+contents|\bbinary\s+(?:data|contents)\b/i) 1 32 33
Condition66%2/3
$name =~ /\A(?:bytes|blob)\z/i or $spec =~ /\braw\s+bytes\b|file'?s\s+contents|\bbinary\s+(?:data|contents)\b/i
l!l&&r!l&&!r
1032
MC/DC50%1/2
$name =~ /\A(?:bytes|blob)\z/i or $spec =~ /\braw\s+bytes\b|file'?s\s+contents|\bbinary\s+(?:data|contents)\b/i
$name =~ /\A(?:bytes|blob)\z/i $spec =~ /\braw\s+bytes\b|file'?s\s+contents|\bbinary\s+(?:data|contents)\b/i
Truth table66%2/3
A or B
A= $name =~ /\A(?:bytes|blob)\z/iB= $spec =~ /\braw\s+bytes\b|file'?s\s+contents|\bbinary\s+(?:data|contents)\b/i
ABresult
1X1
011
000
5213 || $spec =~ /\braw\s+bytes\b|file'?s\s+contents|\bbinary\s+(?:data|contents)\b/i) {
5214 1 $p->{semantic} = 'bytes';
5215 1 ▶ $p->{type} //= 'string';
Condition50%1/2
$p->{"type"} //= "string"
l!l
01
MC/DC0%0/1
$p->{"type"} //= "string"
$p->{"type"}
Truth table50%1/2
A
A= $p->{"type"}
Aresult
11
00
5216 }
5217  
5218 # A documented range, e.g. `lat : number -90 .. 90`
5219 33 ▶ if($spec =~ /(-?\d+(?:\.\d+)?)\s*\.\.\s*(-?\d+(?:\.\d+)?)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ /(-?\d+(?:\.\d+)?)\s*\.\.\s*(-?\d+(?:\.\d+)?)/) { } 2 31 33
elsif ($spec =~ /(-?\d[\d_]*)\s+to\s+(-?\d[\d_]*)/) { } 3 28 31
5220 2 $p->{min} = $1 + 0;
5221 2 $p->{max} = $2 + 0;
5222 # Or with words, as Database::BI::Password documents `rounds`:
5223 # `10_000 to 100_000_000 (default 200_000)`. An underscore is a
5224 # digit separator, not part of the value.
5225 } elsif($spec =~ /(-?\d[\d_]*)\s+to\s+(-?\d[\d_]*)/) {
5226 3 $p->{min} = _undigitised($1);
5227 3 $p->{max} = _undigitised($2);
5228 }
5229  
5230 # The documented default is the value the routine is meant to run
5231 # with, so a maximum far above it is a work factor: a PBKDF2 round
5232 # count near the documented maximum can take minutes per call, which
5233 # no fuzz run can afford. Only then does the default cap the valid
5234 # data; an ordinary bounded option (a port, 1 to 65535 with default
5235 # 8080) must still be generated across its whole range, so the cap
5236 # needs the maximum to be at least ten times the default (and a
5237 # positive default, so a flag whose default is 0 is not pinned at 0).
5238 # max stays the documented bound either way, so a value above it is
5239 # still probed.
5240 33 ▶ if(defined($type) && ($type =~ /\A(?:integer|number)\z/)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type =~ /\A(?:integer|number)\z/ and $spec =~ /\bdefault\b\s*\(?\s*(-?\d[\d_]*)/i) 3 30 33
Condition100%6/6
defined $type and $type =~ /\A(?:integer|number)\z/ and $spec =~ /\bdefault\b\s*\(?\s*(-?\d[\d_]*)/i
!ll&&!rl&&r
2733
defined $type and $type =~ /\A(?:integer|number)\z/
!ll&&!rl&&r
5226
MC/DC100%3/3
defined $type and $type =~ /\A(?:integer|number)\z/ and $spec =~ /\bdefault\b\s*\(?\s*(-?\d[\d_]*)/i
defined $type $type =~ /\A(?:integer|number)\z/ $spec =~ /\bdefault\b\s*\(?\s*(-?\d[\d_]*)/i
Truth table100%4/4
A and B and C
A= defined $typeB= $type =~ /\A(?:integer|number)\z/C= $spec =~ /\bdefault\b\s*\(?\s*(-?\d[\d_]*)/i
ABCresult
0XX0
10X0
1100
1111
5241 && $spec =~ /\bdefault\b\s*\(?\s*(-?\d[\d_]*)/i) {
5242 3 my $default = _undigitised($1);
5243 3 $p->{default} = $default;
5244 $p->{sample_max} = $default
5245 if defined($p->{max}) && ($default > 0)
5246 && ($p->{max} >= 10 * $default)
5247 3 ▶ && (!defined($p->{min}) || ($default >= $p->{min}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"max"} and $default > 0 and $p->{"max"} >= 10 * $default and !defined($p->{"min"}) || $default >= $p->{"min"} 1 2 3
Condition66%8/12
defined $p->{"max"} and $default > 0 and $p->{"max"} >= 10 * $default and !defined($p->{"min"}) || $default >= $p->{"min"}
!ll&&!rl&&r
201
defined $p->{"max"} and $default > 0 and $p->{"max"} >= 10 * $default
!ll&&!rl&&r
111
defined $p->{"max"} and $default > 0
!ll&&!rl&&r
102
not defined $p->{"min"} or $default >= $p->{"min"}
l!l&&r!l&&!r
010
MC/DC40%2/5
defined $p->{"max"} and $default > 0 and $p->{"max"} >= 10 * $default and !defined($p->{"min"}) || $default >= $p->{"min"}
defined $p->{"max"} $default > 0 $p->{"max"} >= 10 * $default not defined $p->{"min"} $default >= $p->{"min"}
Truth table50%3/6
A and B and C and not(D) or E
A= defined $p->{"max"}B= $default > 0C= $p->{"max"} >= 10 * $defaultD= not defined $p->{"min"}E= $default >= $p->{"min"}
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
5248 }
5249  
5250 33 ▶ if(my $values = $self->_prose_memberof($spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($spec)) 0 33 33
5251 0 $p->{memberof} = $values;
5252 }
5253  
5254 33 ▶ $self->_log(' POD: ' . $name . ' type ' . (defined($type) ? "'$type'" : '(none)')
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 28 5 33
5255 . ' from the Input table');
5256 33 push @found, $name;
5257 }
5258  
5259 # A nested "Measure hashref keys:" block documents the shape of the
5260 # hashrefs an arrayref argument holds. Without it the harness invents a
5261 # string element, which the routine rightly rejects; with it the
5262 # documented keys, their allowed values and their patterns are what the
5263 # harness builds.
5264 184 ▶ if($block =~ /^(\w+)\s+hashref\s+keys\s*:\s*\n(.*?)(?=\n\s*\n|\z)/msi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /^(\w+)\s+hashref\s+keys\s*:\s*\n(.*?)(?=\n\s*\n|\z)/ism) 0 184 184
5265 0 my ($kind, $rows) = ($1, $2);
5266 0 my (%element, %alternation);
5267 0 for my $line (split /\n/, $rows) {
5268 0 ▶ next unless($line =~ /^\s{0,8}(\w+)\s*=>\s*(\S.*)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s{0,8}(\w+)\s*=>\s*(\S.*)$/ 0 0 0
5269 0 my ($key, $text) = ($1, $2);
5270 # A key another key's description makes conditional on the
5271 # function ("required for all functions except bare count") is
5272 # left out: the element is built for the function that needs no
5273 # further column.
5274 0 ▶ next if($text =~ /\brequired\b[^;]*\bexcept\b/i);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\brequired\b[^;]*\bexcept\b/i 0 0 0
5275 0 my %spec = (type => 'string');
5276 0 ▶ if($text =~ /\(([^)]*\|[^)]*)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\(([^)]*\|[^)]*)\)/) 0 0 0
5277 0 my @values = map { s/\A\s+|\s+\z//gr } split /\|/, $1;
5278 0 ▶ $spec{memberof} = \@values if(@values);
Branch: 1 branch
BranchTrueFalseTotal
if @values 0 0 0
5279 0 $alternation{$key} = \@values;
5280 }
5281 0 ▶ if($text =~ /match(?:es)?\s+C?<\s*\/(.*?)\/\s*>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ m[match(?:es)?\s+C?<\s*/(.*?)/\s*>]) 0 0 0
5282 0 $spec{matches} = $1;
5283 }
5284 0 $element{$key} = \%spec;
5285 }
5286  
5287 # The description says which listed function needs no further key;
5288 # build the element for that one.
5289 0 ▶ if(my ($except) = $rows =~ /\bexcept\s+(?:bare\s+)?(\w+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($except) = $rows =~ /\bexcept\s+(?:bare\s+)?(\w+)/i) 0 0 0
5290 0 for my $key (keys %alternation) {
5291 0 ▶ next unless(grep { $_ eq $except } @{$alternation{$key}});
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $except;} @{$alternation{$key};} 0 0 0
5292 0 $element{$key}{memberof} = [ $except ];
5293 }
5294 }
5295  
5296 0 ▶ if(%element) {
Branch: 1 branch
BranchTrueFalseTotal
if (%element) 0 0 0
5297 0 for my $name (keys %{$params}) {
5298 0 my $p = $params->{$name};
5299 0 ▶ next unless(($p->{_source} // '') eq SOURCE_INPUT_SPEC);
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"_source"} // "") eq "input_spec" 0 0 0
Condition0%0/2
$p->{"_source"} // ""
l!l
00
MC/DC0%0/1
$p->{"_source"} // ""
$p->{"_source"}
Truth table0%0/2
A
A= $p->{"_source"}
Aresult
11
00
5300 0 ▶ next unless((($p->{type} // '') =~ /^array/));
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"} // "") =~ /^array/ 0 0 0
Condition0%0/2
$p->{"type"} // ""
l!l
00
MC/DC0%0/1
$p->{"type"} // ""
$p->{"type"}
Truth table0%0/2
A
A= $p->{"type"}
Aresult
11
00
5301 next unless(defined($row_text{$name})
5302 0 ▶ && $row_text{$name} =~ /arrayref\s+of\s+\Q$kind\E\s+hashrefs/i);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row_text{$name} and $row_text{$name} =~ /arrayref\s+of\s+\Q$kind\E\s+hashrefs/i 0 0 0
Condition0%0/3
defined $row_text{$name} and $row_text{$name} =~ /arrayref\s+of\s+\Q$kind\E\s+hashrefs/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row_text{$name} and $row_text{$name} =~ /arrayref\s+of\s+\Q$kind\E\s+hashrefs/i
defined $row_text{$name} $row_text{$name} =~ /arrayref\s+of\s+\Q$kind\E\s+hashrefs/i
Truth table0%0/3
A and B
A= defined $row_text{$name}B= $row_text{$name} =~ /arrayref\s+of\s+\Q$kind\E\s+hashrefs/i
ABresult
0X0
100
111
5303 0 $p->{schema} = { %element };
5304 }
5305 }
5306 }
5307  
5308 # A section documenting "Reserved keys extracted before the rest is treated
5309 # as WHERE criteria" describes a variadic named API (Database::Join's
5310 # aggregate): only the key it marks required is required, and an optional
5311 # reference it gives no element shape for cannot be built validly, so it
5312 # is left out rather than fuzzed with invented contents.
5313 184 ▶ if($block =~ /\bReserved keys\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /\bReserved keys\b/i) 0 184 184
5314 0 for my $name (keys %{$params}) {
5315 0 my $p = $params->{$name};
5316 0 ▶ next unless(($p->{_source} // '') eq SOURCE_INPUT_SPEC);
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"_source"} // "") eq "input_spec" 0 0 0
Condition0%0/2
$p->{"_source"} // ""
l!l
00
MC/DC0%0/1
$p->{"_source"} // ""
$p->{"_source"}
Truth table0%0/2
A
A= $p->{"_source"}
Aresult
11
00
5317  
5318 0 ▶ $p->{optional} = 1 unless(defined($p->{optional}) && !$p->{optional});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p->{"optional"} and not $p->{"optional"} 0 0 0
Condition0%0/3
defined $p->{"optional"} and not $p->{"optional"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p->{"optional"} and not $p->{"optional"}
defined $p->{"optional"} not $p->{"optional"}
Truth table0%0/3
A and not(B)
A= defined $p->{"optional"}B= not $p->{"optional"}
ABresult
0X0
100
111
5319  
5320 0 ▶ my $type = $p->{type} // '';
Condition0%0/2
$p->{"type"} // ""
l!l
00
MC/DC0%0/1
$p->{"type"} // ""
$p->{"type"}
Truth table0%0/2
A
A= $p->{"type"}
Aresult
11
00
5321 0 ▶ next unless(($type eq 'arrayref') || ($type eq 'array') || ($type eq 'hashref'));
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "arrayref" or $type eq "array" or $type eq "hashref" 0 0 0
Condition0%0/6
$type eq "arrayref" or $type eq "array" or $type eq "hashref"
l!l&&r!l&&!r
000
$type eq "arrayref" or $type eq "array"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "arrayref" or $type eq "array" or $type eq "hashref"
$type eq "arrayref" $type eq "array" $type eq "hashref"
Truth table0%0/4
A or B or C
A= $type eq "arrayref"B= $type eq "array"C= $type eq "hashref"
ABCresult
1XX1
01X1
0011
0000
5322 0 ▶ next if(ref($p->{schema}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $p->{"schema"} eq "HASH" 0 0 0
5323 0 ▶ next if(ref($p->{memberof}) eq 'ARRAY' && @{$p->{memberof}});
Branch: 1 branch
BranchTrueFalseTotal
if ref $p->{"memberof"} eq "ARRAY" and @{$p->{"memberof"};} 0 0 0
Condition0%0/3
ref $p->{"memberof"} eq "ARRAY" and @{$p->{"memberof"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $p->{"memberof"} eq "ARRAY" and @{$p->{"memberof"};}
ref $p->{"memberof"} eq "ARRAY" @{$p->{"memberof"};}
Truth table0%0/3
A and B
A= ref $p->{"memberof"} eq "ARRAY"B= @{$p->{"memberof"};}
ABresult
0X0
100
111
5324  
5325 0 ▶ delete $params->{$name} if($p->{optional});
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"optional"} 0 0 0
5326 }
5327 }
5328  
5329 184 return scalar @found;
5330 }
5331  
5332 # --------------------------------------------------
5333 # _analyze_output
5334 #
5335 # Purpose: Orchestrate analysis of a method's
5336 # return value by combining POD return
5337 # section parsing, code return statement
5338 # analysis, boolean detection, context
5339 # detection, void detection, chaining
5340 # detection, and error convention
5341 # detection.
5342 #
5343 # Entry: $pod - POD string for the method.
5344 # $code - method body source string.
5345 # $method_name - name of the method being
5346 # analysed, used for
5347 # boolean heuristics.
5348 #
5349 # Exit: Returns a hashref describing the
5350 # output type and behaviour, or an empty
5351 # hashref if nothing could be determined.
5352 # Keys include: type, value, isa, and
5353 # various _* metadata keys.
5354 #
5355 # Side effects: Logs progress to stdout when
5356 # verbose is set.
5357 # --------------------------------------------------
5358 815 ▶sub _analyze_output {
Subroutine _analyze_output: called CC=3 SCAR=11.0
5359 815 my ($self, $pod, $code, $method_name) = @_;
5360  
5361 815 my %output;
5362  
5363 815 $self->_analyze_output_from_pod(\%output, $pod);
5364 815 $self->_analyze_output_from_code(\%output, $code, $method_name);
5365 815 $self->_enhance_boolean_detection(\%output, $pod, $code, $method_name);
5366 815 $self->_detect_list_context(\%output, $code);
5367 815 $self->_detect_void_context(\%output, $code, $method_name);
5368 815 $self->_detect_chaining_pattern(\%output, $code);
5369 815 $self->_detect_error_conventions(\%output, $code);
5370  
5371 815 ▶ $self->_validate_output(\%output) if keys %output;
Branch: 1 branch
BranchTrueFalseTotal
if keys %output 793 22 815
5372  
5373 # Don't return empty output
5374 815 ▶ return (keys %output) ? \%output : {};
Branch: 1 branch
BranchTrueFalseTotal
keys %output ? : 793 22 815
5375 }
5376  
5377 # --------------------------------------------------
5378 # _analyze_output_from_pod
5379 #
5380 # Purpose: Parse the POD documentation for a
5381 # method's return value and populate
5382 # an output hashref with type, value,
5383 # and behaviour information.
5384 #
5385 # Entry: $output - hashref to populate
5386 # (modified in place).
5387 # $pod - POD string for the method.
5388 #
5389 # Exit: Returns nothing. Modifies $output
5390 # in place.
5391 #
5392 # Side effects: Logs detections to stdout when
5393 # verbose is set.
5394 #
5395 # Notes: Two patterns are tried: (1) a
5396 # 'Returns:' section of up to 3 lines,
5397 # and (2) an inline 'returns X' phrase.
5398 # The section pattern takes precedence.
5399 # --------------------------------------------------
5400 813 ▶sub _analyze_output_from_pod {
Subroutine _analyze_output_from_pod: called CC=65 SCAR=54.0
5401 813 my ($self, $output, $pod) = @_;
5402 813 my %VALID_OUTPUT_TYPES = map { $_ => 1 }
5403 qw(string integer number float boolean arrayref hashref object coderef void undef);
5404  
5405 813 ▶ if ($pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod) 574 239 813
5406 # Pattern 0: =head4 Output formal spec (highest priority — explicit over heuristic)
5407 # The outer container shape determines the return type:
5408 # (...) — list/array of items
5409 # [...] — arrayref (bare [] = empty/void, skip)
5410 # {...} — hashref spec; look for type => inside, or isa => for object
5411 574 ▶ if($pod =~ /=head4\s+Output\b(.*?)(?==head|\z)/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=head4\s+Output\b(.*?)(?==head|\z)/is) 396 178 574
5412 396 my $block = $1;
5413 396 $block =~ s/^\s+//;
5414 396 ▶ if($block =~ /^\(/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($block =~ /^\(/) { } 2 394 396
elsif ($block =~ /^\[/) { } 0 394 394
elsif ($block =~ /^\{/) { } 389 5 394
elsif ($block =~ /\A(?:(?:Always|Usually)\s+)?(?:An?\s+)?(Hashref|Hash|Arrayref|Array|List)(?=\s*(?:of\b|:|\(|\[|\.|;|,|\z))/i) { } 3 2 5
5415 2 $output->{type} = 'array';
5416 2 $self->_log(" OUTPUT: type 'array' from =head4 Output list notation");
5417 } elsif($block =~ /^\[/) {
5418 0 ▶ unless($block =~ /^\[\s*\]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block =~ /^\[\s*\]/) 0 0 0
5419 0 $output->{type} = 'arrayref';
5420 0 $self->_log(" OUTPUT: type 'arrayref' from =head4 Output arrayref notation");
5421 }
5422 } elsif($block =~ /^\{/) {
5423 389 ▶ if($block =~ /type\s*=>\s*['"]?(\w[\w:]*?)['"]?\s*[,}]/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($block =~ /type\s*=>\s*['"]?(\w[\w:]*?)['"]?\s*[,}]/i) { } 382 7 389
elsif ($block =~ /\bisa\s*=>/) { } 0 7 7
elsif ($block =~ /^\{[^}]*\}/ and $block =~ /=>/) { } 1 6 7
5424 382 my $type = lc($1);
5425 382 ▶ $type = 'string' if $type eq 'scalar' || $type eq 'scalarref' || $type eq 'str';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "scalar" or $type eq "scalarref" or $type eq "str" 109 273 382
Condition83%5/6
$type eq "scalar" or $type eq "scalarref" or $type eq "str"
l!l&&r!l&&!r
1090273
$type eq "scalar" or $type eq "scalarref"
l!l&&r!l&&!r
1063273
MC/DC66%2/3
$type eq "scalar" or $type eq "scalarref" or $type eq "str"
$type eq "scalar" $type eq "scalarref" $type eq "str"
Truth table75%3/4
A or B or C
A= $type eq "scalar"B= $type eq "scalarref"C= $type eq "str"
ABCresult
1XX1
01X1
0011
0000
5426 382 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 382 382
5427 382 ▶ $type = 'number' if $type eq 'float' || $type eq 'num';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "float" or $type eq "num" 0 382 382
Condition33%1/3
$type eq "float" or $type eq "num"
l!l&&r!l&&!r
00382
MC/DC0%0/2
$type eq "float" or $type eq "num"
$type eq "float" $type eq "num"
Truth table33%1/3
A or B
A= $type eq "float"B= $type eq "num"
ABresult
1X1
011
000
5428 382 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 382 382
5429 382 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 382 382
5430 382 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 382 382
5431 382 ▶ if($VALID_OUTPUT_TYPES{$type}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($VALID_OUTPUT_TYPES{$type}) { } 382 0 382
elsif ($block =~ /\bisa\s*=>/) { } 0 0 0
5432 382 $output->{type} = $type;
5433 382 $self->_log(" OUTPUT: type '$type' from =head4 Output formal spec");
5434 } elsif($block =~ /\bisa\s*=>/) {
5435 0 $output->{type} = 'object';
5436 0 $self->_log(" OUTPUT: type 'object' from =head4 Output isa spec");
5437 }
5438 } elsif($block =~ /\bisa\s*=>/) {
5439 0 $output->{type} = 'object';
5440 0 $self->_log(" OUTPUT: type 'object' from =head4 Output isa spec");
5441 ▶ } elsif($block =~ /^\{[^}]*\}/ && $block =~ /=>/) {
Condition66%2/3
$block =~ /^\{[^}]*\}/ and $block =~ /=>/
!ll&&!rl&&r
061
MC/DC50%1/2
$block =~ /^\{[^}]*\}/ and $block =~ /=>/
$block =~ /^\{[^}]*\}/ $block =~ /=>/
Truth table66%2/3
A and B
A= $block =~ /^\{[^}]*\}/B= $block =~ /=>/
ABresult
0X0
100
111
5442 # A record such as
5443 # `{ n => 'integer', unit => 'string' } | undef`
5444 # documents a hashref return.
5445 1 $output->{type} = 'hashref';
5446 1 $self->_log(" OUTPUT: type 'hashref' from =head4 Output record notation");
5447 }
5448 } elsif($block =~ /\A(?:(?:Always|Usually)\s+)?(?:An?\s+)?(Hashref|Hash|Arrayref|Array|List)(?=\s*(?:of\b|:|\(|\[|\.|;|,|\z))/i) {
5449 # A documented shape such as "Hashref: column_name => {...}",
5450 # "Arrayref of hashrefs" or "Always an arrayref of hashrefs"
5451 # names the container first. "The hash string." names a
5452 # string, so an article alone does not make it a container.
5453 3 my $word = lc($1);
5454 3 ▶ $output->{type} = ($word =~ /^array|^list/) ? 'arrayref' : 'hashref';
Branch: 1 branch
BranchTrueFalseTotal
$word =~ /^array|^list/ ? : 3 0 3
5455 3 $self->_log(" OUTPUT: type '$output->{type}' from =head4 Output shape");
5456 }
5457 }
5458  
5459 # Pattern 1: Returns: section
5460 # Up to 3 lines
5461 574 ▶ if ($pod =~ /Returns?:\s+([^\n]+(?:\n[^\n]+){0,2})/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /Returns?:\s+([^\n]+(?:\n[^\n]+){0,2})/is) 30 544 574
5462 30 my $returns_desc = $1;
5463 30 $returns_desc =~ s/^\s+|\s+$//g;
5464  
5465 30 $self->_log(" OUTPUT: Found Returns section: $returns_desc");
5466  
5467 # Try to infer type from description (skip if Pattern 0 already set type)
5468 30 ▶ if (!$output->{type} && $returns_desc =~ /\b(string|text)\b/i) {
Branch: 8 branches
BranchTrueFalseTotal
if (not $output->{'type'} and $returns_desc =~ /\b(string|text)\b/i) { } 3 27 30
elsif (not $output->{'type'} and $returns_desc =~ /\b(integer|int|count)\b/i) { } 6 21 27
elsif (not $output->{'type'} and $returns_desc =~ /\b(float|decimal|number)\b/i) { } 2 19 21
elsif (not $output->{'type'} and $returns_desc =~ /\b(boolean|true|false)\b/i) { } 7 12 19
elsif (not $output->{'type'} and $returns_desc =~ /\b(array|list)\b/i) { } 0 12 12
elsif (not $output->{'type'} and $returns_desc =~ /\b(hash|hashref|dictionary)\b/i) { } 1 11 12
elsif (not $output->{'type'} and $returns_desc =~ /\b(object|instance)\b/i) { } 2 9 11
elsif (not $output->{'type'} and $returns_desc =~ /\bundef\b/i) { } 0 15 15
Condition100%3/3
not $output->{"type"} and $returns_desc =~ /\b(string|text)\b/i
!ll&&!rl&&r
6213
MC/DC100%2/2
not $output->{"type"} and $returns_desc =~ /\b(string|text)\b/i
not $output->{"type"} $returns_desc =~ /\b(string|text)\b/i
Truth table100%3/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\b(string|text)\b/i
ABresult
0X0
100
111
5469 3 $output->{type} = 'string';
5470 ▶ } elsif (!$output->{type} && $returns_desc =~ /\b(integer|int|count)\b/i) {
Condition100%3/3
not $output->{"type"} and $returns_desc =~ /\b(integer|int|count)\b/i
!ll&&!rl&&r
6156
MC/DC100%2/2
not $output->{"type"} and $returns_desc =~ /\b(integer|int|count)\b/i
not $output->{"type"} $returns_desc =~ /\b(integer|int|count)\b/i
Truth table100%3/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\b(integer|int|count)\b/i
ABresult
0X0
100
111
5471 6 $output->{type} = 'integer';
5472 ▶ } elsif (!$output->{type} && $returns_desc =~ /\b(float|decimal|number)\b/i) {
Condition100%3/3
not $output->{"type"} and $returns_desc =~ /\b(float|decimal|number)\b/i
!ll&&!rl&&r
6132
MC/DC100%2/2
not $output->{"type"} and $returns_desc =~ /\b(float|decimal|number)\b/i
not $output->{"type"} $returns_desc =~ /\b(float|decimal|number)\b/i
Truth table100%3/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\b(float|decimal|number)\b/i
ABresult
0X0
100
111
5473 2 $output->{type} = 'number';
5474 ▶ } elsif (!$output->{type} && $returns_desc =~ /\b(boolean|true|false)\b/i) {
Condition100%3/3
not $output->{"type"} and $returns_desc =~ /\b(boolean|true|false)\b/i
!ll&&!rl&&r
667
MC/DC100%2/2
not $output->{"type"} and $returns_desc =~ /\b(boolean|true|false)\b/i
not $output->{"type"} $returns_desc =~ /\b(boolean|true|false)\b/i
Truth table100%3/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\b(boolean|true|false)\b/i
ABresult
0X0
100
111
5475 7 $output->{type} = 'boolean';
5476 ▶ } elsif (!$output->{type} && $returns_desc =~ /\b(array|list)\b/i) {
Condition66%2/3
not $output->{"type"} and $returns_desc =~ /\b(array|list)\b/i
!ll&&!rl&&r
660
MC/DC0%0/2
not $output->{"type"} and $returns_desc =~ /\b(array|list)\b/i
not $output->{"type"} $returns_desc =~ /\b(array|list)\b/i
Truth table66%2/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\b(array|list)\b/i
ABresult
0X0
100
111
5477 0 $output->{type} = 'arrayref';
5478 ▶ } elsif (!$output->{type} && $returns_desc =~ /\b(hash|hashref|dictionary)\b/i) {
Condition100%3/3
not $output->{"type"} and $returns_desc =~ /\b(hash|hashref|dictionary)\b/i
!ll&&!rl&&r
651
MC/DC100%2/2
not $output->{"type"} and $returns_desc =~ /\b(hash|hashref|dictionary)\b/i
not $output->{"type"} $returns_desc =~ /\b(hash|hashref|dictionary)\b/i
Truth table100%3/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\b(hash|hashref|dictionary)\b/i
ABresult
0X0
100
111
5479 1 $output->{type} = 'hashref';
5480 ▶ } elsif (!$output->{type} && $returns_desc =~ /\b(object|instance)\b/i) {
Condition100%3/3
not $output->{"type"} and $returns_desc =~ /\b(object|instance)\b/i
!ll&&!rl&&r
632
MC/DC100%2/2
not $output->{"type"} and $returns_desc =~ /\b(object|instance)\b/i
not $output->{"type"} $returns_desc =~ /\b(object|instance)\b/i
Truth table100%3/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\b(object|instance)\b/i
ABresult
0X0
100
111
5481 2 $output->{type} = 'object';
5482 ▶ } elsif (!$output->{type} && $returns_desc =~ /\bundef\b/i) {
Condition66%2/3
not $output->{"type"} and $returns_desc =~ /\bundef\b/i
!ll&&!rl&&r
630
MC/DC0%0/2
not $output->{"type"} and $returns_desc =~ /\bundef\b/i
not $output->{"type"} $returns_desc =~ /\bundef\b/i
Truth table66%2/3
not(A) and B
A= not $output->{"type"}B= $returns_desc =~ /\bundef\b/i
ABresult
0X0
100
111
5483 0 $output->{type} = 'undef';
5484 }
5485  
5486 # Look for specific values
5487 30 ▶ if ($returns_desc =~ /\b1\s+(?:on\s+success|if\s+successful)\b/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($returns_desc =~ /\b1\s+(?:on\s+success|if\s+successful)\b/i) { } 0 30 30
elsif ($returns_desc =~ /\b0\s+(?:on\s+failure|if\s+fail)\b/i) { } 0 30 30
elsif ($returns_desc =~ /dies\s+on\s+(?:error|failure)/i) { } 0 30 30
5488 0 $output->{value} = 1;
5489 0 ▶ if(defined($output->{'type'}) && ($output->{type} eq 'scalar')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output->{'type'} and $output->{'type'} eq 'scalar') { } 0 0 0
Condition0%0/3
defined $output->{"type"} and $output->{"type"} eq "scalar"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $output->{"type"} and $output->{"type"} eq "scalar"
defined $output->{"type"} $output->{"type"} eq "scalar"
Truth table0%0/3
A and B
A= defined $output->{"type"}B= $output->{"type"} eq "scalar"
ABresult
0X0
100
111
5490 0 $output->{type} = 'boolean';
5491 } else {
5492 0 ▶ $output->{type} ||= 'boolean';
Condition0%0/2
$output->{"type"} ||= "boolean"
l!l
00
MC/DC0%0/1
$output->{"type"} ||= "boolean"
$output->{"type"}
Truth table0%0/2
A
A= $output->{"type"}
Aresult
11
00
5493 }
5494 0 $self->_log(" OUTPUT: Returns 1 on success");
5495 } elsif ($returns_desc =~ /\b0\s+(?:on\s+failure|if\s+fail)\b/i) {
5496 0 $output->{alt_value} = 0;
5497 } elsif ($returns_desc =~ /dies\s+on\s+(?:error|failure)/i) {
5498 0 $output->{_STATUS} = 'LIVES';
5499 0 $self->_log(' OUTPUT: Should not die on success');
5500 }
5501 30 ▶ if ($returns_desc =~ /\b(true|false)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returns_desc =~ /\b(true|false)\b/i) 3 27 30
5502 3 ▶ $output->{type} ||= 'boolean';
Condition50%1/2
$output->{"type"} ||= "boolean"
l!l
30
MC/DC0%0/1
$output->{"type"} ||= "boolean"
$output->{"type"}
Truth table50%1/2
A
A= $output->{"type"}
Aresult
11
00
5503 }
5504 30 ▶ if ($returns_desc =~ /\bundef\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returns_desc =~ /\bundef\b/i) 0 30 30
5505 0 $output->{optional} = 1;
5506 }
5507 }
5508  
5509 # Pattern 2: Inline "returns X"
5510 574 ▶ if((!$output->{type}) && ($pod =~ /returns?\s+(?:an?\s+)?(\w+)/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $output->{"type"} and $pod =~ /returns?\s+(?:an?\s+)?(\w+)/i) 52 522 574
Condition100%3/3
not $output->{"type"} and $pod =~ /returns?\s+(?:an?\s+)?(\w+)/i
!ll&&!rl&&r
40911352
MC/DC100%2/2
not $output->{"type"} and $pod =~ /returns?\s+(?:an?\s+)?(\w+)/i
not $output->{"type"} $pod =~ /returns?\s+(?:an?\s+)?(\w+)/i
Truth table100%3/3
not(A) and B
A= not $output->{"type"}B= $pod =~ /returns?\s+(?:an?\s+)?(\w+)/i
ABresult
0X0
100
111
5511 52 my $type = lc($1);
5512  
5513 52 ▶ $type = 'boolean' if $type =~ /^(true|false|bool)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(true|false|bool)$/ 10 42 52
5514 # Skip if it's just a number (like "returns 1")
5515 52 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 52 52
5516 52 ▶ $type = 'number' if $type =~ /^(num|float)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(num|float)$/ 0 52 52
5517 52 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 52 52
5518 52 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 52 52
5519  
5520 52 ▶ if($type =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^\d+$/) 0 52 52
5521 0 ▶ if($type eq '1' || $type eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '1' or $type eq '0') { } 0 0 0
Condition0%0/3
$type eq 1 or $type eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq 1 or $type eq 0
$type eq 1 $type eq 0
Truth table0%0/3
A or B
A= $type eq 1B= $type eq 0
ABresult
1X1
011
000
5522 # Try hard to guess if the result is a boolean
5523 0 ▶ if($pod =~ /1 on success.+0 (on|if) /i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pod =~ /1 on success.+0 (on|if) /i) { } 0 0 0
elsif ($pod =~ /return 0 .+ 1 on success/) { } 0 0 0
5524 0 $type = 'boolean';
5525 } elsif($pod =~ /return 0 .+ 1 on success/) {
5526 0 $type = 'boolean';
5527 } else {
5528 0 $type = 'integer';
5529 }
5530 } else {
5531 0 $type = 'integer';
5532 }
5533 }
5534  
5535 52 ▶ $type = 'arrayref' if !$type && $pod =~ /returns?\s+.+\slist\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if not $type and $pod =~ /returns?\s+.+\slist\b/i 0 52 52
Condition33%1/3
not $type and $pod =~ /returns?\s+.+\slist\b/i
!ll&&!rl&&r
5200
MC/DC0%0/2
not $type and $pod =~ /returns?\s+.+\slist\b/i
not $type $pod =~ /returns?\s+.+\slist\b/i
Truth table33%1/3
not(A) and B
A= not $typeB= $pod =~ /returns?\s+.+\slist\b/i
ABresult
0X0
100
111
5536 # $output->{type} = $type if $type && $type !~ /^\d+$/;
5537 52 ▶ if ($VALID_OUTPUT_TYPES{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VALID_OUTPUT_TYPES{$type}) { } 14 38 52
5538 14 $output->{type} = $type;
5539 14 $self->_log(" OUTPUT: Inferred type from POD: $type");
5540 } else {
5541 38 $self->_log(" OUTPUT: POD return type '$type' is not a valid type, ignoring");
5542 }
5543 }
5544 }
5545 }
5546  
5547 # --------------------------------------------------
5548 # _extract_defaults_from_pod
5549 #
5550 # Purpose: Extract default values for parameters
5551 # from POD documentation using multiple
5552 # pattern strategies.
5553 #
5554 # Entry: $pod - POD string for the method.
5555 # May be undef or empty.
5556 #
5557 # Exit: Returns a hashref of parameter name
5558 # to cleaned default value. Returns an
5559 # empty hashref if no POD is provided
5560 # or no defaults are found.
5561 #
5562 # Side effects: None.
5563 #
5564 # Notes: Three strategies are tried: (1) lines
5565 # containing 'Default:' or 'Defaults to:',
5566 # (2) lines containing 'Optional, default',
5567 # (3) inline $name - type, default value
5568 # format. Parameter names are inferred
5569 # by scanning backwards from the default
5570 # phrase to the nearest $variable.
5571 # --------------------------------------------------
5572 584 ▶sub _extract_defaults_from_pod {
Subroutine _extract_defaults_from_pod: called CC=9 SCAR=22.1
5573 584 my ($self, $pod) = @_;
5574  
5575 584 ▶ return {} unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 1 583 584
5576  
5577 583 my %defaults;
5578  
5579 # Pattern 1: Default: 'value' or Defaults to: 'value'
5580 583 ▶ while ($pod =~ /(?:Default(?:s? to)?|default(?:s? to)?)[:]\s*([^\n\r]+)/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /(?:Default(?:s? to)?|default(?:s? to)?)[:]\s*([^\n\r]+)/gi) 38 583 621
5581 38 my $default_text = $1;
5582 38 my $match_pos = pos($pod);
5583 38 $default_text =~ s/^\s+|\s+$//g;
5584  
5585 # Look backwards in the POD to find the parameter name
5586 38 my $context = substr($pod, 0, $match_pos);
5587 38 my @param_matches = ($context =~ /\$(\w+)/g);
5588 38 ▶ my $param = $param_matches[-1] if @param_matches; # Last parameter before default
Branch: 1 branch
BranchTrueFalseTotal
if @param_matches 38 0 38
5589  
5590 38 ▶ if ($param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) 38 0 38
5591 # Always clean the default value - let _clean_default_value handle everything
5592 38 ▶ if ($default_text =~ /(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_text =~ /(\w+)\s*=\s*(.+)$/) { } 0 38 38
5593 # Has explicit param = value format in the default text
5594 0 my ($p, $value) = ($1, $2);
5595 0 $defaults{$p} = $self->_clean_default_value($value);
5596 } else {
5597 # Just a value, associate with the found param
5598 38 $defaults{$param} = $self->_clean_default_value($default_text, 0); # NOT from code
5599 }
5600 }
5601 }
5602  
5603 # Pattern 2: Optional, default 'value'
5604 583 ▶ while ($pod =~ /Optional(?:,)?\s+(?:default|value)\s*[:=]?\s*([^\n\r,;]+)/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /Optional(?:,)?\s+(?:default|value)\s*[:=]?\s*([^\n\r,;]+)/gi) 30 583 613
5605 30 my $default_text = $1;
5606 30 my $match_pos = pos($pod);
5607 30 $default_text =~ s/^\s+|\s+$//g;
5608  
5609 # Look backwards for parameter name
5610 30 my $context = substr($pod, 0, $match_pos);
5611 30 my @param_matches = ($context =~ /\$(\w+)/g);
5612 30 ▶ if (@param_matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param_matches) 29 1 30
5613 29 my $param = $param_matches[-1]; # Last parameter before the default
5614 29 $defaults{$param} = $self->_clean_default_value($default_text, 0);
5615 }
5616 }
5617  
5618 # Pattern 3: In parameter descriptions: $param - type, default 'value'
5619 583 ▶ while ($pod =~ /\$(\w+)\s*-\s*\w+(?:\([^)]*\))?[,\s]+default\s+['"]?([^'",\n]+)['"]?/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\$(\w+)\s*-\s*\w+(?:\([^)]*\))?[,\s]+default\s+['"]?([^'",\n]+)['"]?/gi) 1 583 584
5620 1 my ($param, $value) = ($1, $2);
5621 1 $defaults{$param} = $self->_clean_default_value($value, 0);
5622 }
5623  
5624 583 return \%defaults;
5625 }
5626  
5627 # --------------------------------------------------
5628 # _analyze_output_from_code
5629 #
5630 # Purpose: Analyse return statements in a method
5631 # body to infer the output type by
5632 # counting and classifying each return
5633 # expression.
5634 #
5635 # Entry: $output - hashref to populate
5636 # (modified in place).
5637 # $code - method body source string.
5638 # $method_name - method name string.
5639 #
5640 # Exit: Returns nothing. Modifies $output
5641 # in place.
5642 #
5643 # Side effects: Logs detections to stdout when
5644 # verbose is set.
5645 # --------------------------------------------------
5646 sub _analyze_output_from_code
5647 813 ▶{
Subroutine _analyze_output_from_code: called CC=82 SCAR=49.0
5648 813 my ($self, $output, $code, $method_name) = @_;
5649  
5650 813 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 813 0 813
5651 # Early boolean detection - check for consistent 1/0 returns
5652 813 my @all_returns = $code =~ /return\s+([^;]+);/g;
5653 813 ▶ if (@all_returns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@all_returns) 714 99 813
5654 714 my $boolean_count = 0;
5655 714 my $total_count = scalar(@all_returns);
5656  
5657 714 foreach my $ret (@all_returns) {
5658 1003 $ret =~ s/^\s+|\s+$//g;
5659 # Match 0 or 1, even with conditions
5660 1003 ▶ $boolean_count++ if ($ret =~ /^(?:0|1)(?:\s|$)/);
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /^(?:0|1)(?:\s|$)/ 117 886 1003
5661 }
5662  
5663 # If most returns are 0 or 1, strongly suggest boolean
5664 714 ▶ if ($boolean_count >= 2 && $boolean_count >= $total_count * 0.8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean_count >= 2 and $boolean_count >= $total_count * "0.8") 15 699 714
Condition100%3/3
$boolean_count >= 2 and $boolean_count >= $total_count * "0.8"
!ll&&!rl&&r
6851415
MC/DC100%2/2
$boolean_count >= 2 and $boolean_count >= $total_count * "0.8"
$boolean_count >= 2 $boolean_count >= $total_count * "0.8"
Truth table100%3/3
A and B
A= $boolean_count >= 2B= $boolean_count >= $total_count * "0.8"
ABresult
0X0
100
111
5665 15 ▶ unless ($output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->{"type"}) 5 10 15
5666 5 $output->{type} = 'boolean';
5667 5 $self->_log(" OUTPUT: Early detection - $boolean_count/$total_count returns are 0/1, setting boolean");
5668 }
5669 }
5670 }
5671  
5672 813 my @return_statements;
5673  
5674 813 ▶ if ($code =~ /return\s+bless\s*\{[^}]*\}\s*,\s*['"]?(\w+)['"]?/s) {
Branch: 6 branches
BranchTrueFalseTotal
if ($code =~ /return\s+bless\s*\{[^}]*\}\s*,\s*['"]?(\w+)['"]?/s) { } 15 798 813
elsif ($code =~ /return\s+bless/s) { } 65 733 798
elsif ($code =~ /return\s*\(\s*[^)]+\s*,\s*[^)]+\s*\)\s*;/) { } 1 732 733
elsif ($code =~ /return\s+bless[^,]+,\s*__PACKAGE__/) { } 0 732 732
elsif ($code =~ /return\s*\(([^)]+)\)/) { } 12 720 732
elsif ($code =~ /return\s+\$self\s*;/ and $code =~ /\$self\s*->\s*\{[^}]+\}\s*=/) { } 13 1412 1425
5675 # Detect blessed refs
5676 15 $output->{type} = 'object';
5677 15 ▶ if($method_name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name eq 'new') { } 14 1 15
5678 # If we found the new() method, the object we're returning should be a sensible one
5679 14 ▶ if($self->{_document} && (my $package_stmt = $self->{_document}->find_first('PPI::Statement::Package'))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"} and my $package_stmt = $self->{"_document"}->find_first("PPI::Statement::Package")) 14 0 14
Condition33%1/3
$self->{"_document"} and my $package_stmt = $self->{"_document"}->find_first("PPI::Statement::Package")
!ll&&!rl&&r
0014
MC/DC0%0/2
$self->{"_document"} and my $package_stmt = $self->{"_document"}->find_first("PPI::Statement::Package")
$self->{"_document"} my $package_stmt = $self->{"_document"}->find_first("PPI::Statement::Package")
Truth table33%1/3
A and B
A= $self->{"_document"}B= my $package_stmt = $self->{"_document"}->find_first("PPI::Statement::Package")
ABresult
0X0
100
111
5680 14 $output->{isa} = $package_stmt->namespace();
5681 14 ▶ $self->{_package_name} //= $output->{isa};
Condition33%1/3
$self->{"_package_name"} //= $output->{"isa"}
l!l&&r!l&&!r
1400
MC/DC0%0/2
$self->{"_package_name"} //= $output->{"isa"}
$self->{"_package_name"} $output->{"isa"}
Truth table33%1/3
A //= B
A= $self->{"_package_name"}B= $output->{"isa"}
ABresult
1X1
011
000
5682 }
5683 } else {
5684 1 $output->{isa} = $1;
5685 }
5686 15 $self->_log(" OUTPUT: Bless found, inferring type from code is $output->{isa}");
5687 } elsif ($code =~ /return\s+bless/s) {
5688 65 $output->{type} = 'object';
5689 65 ▶ if($method_name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name eq 'new') { } 62 3 65
5690 62 $output->{isa} = $self->_extract_package_name();
5691 62 $self->_log(" OUTPUT: Bless found, inferring type from code is $output->{isa}");
5692 } else {
5693 3 $self->_log(' OUTPUT: Bless found, inferring type from code is object');
5694 }
5695 } elsif ($code =~ /return\s*\(\s*[^)]+\s*,\s*[^)]+\s*\)\s*;/) {
5696 # Detect array context returns - must end with semicolon to be actual return
5697 1 $output->{type} = 'array'; # Not arrayref - actual array
5698 1 $self->_log(' OUTPUT: Found array contect return');
5699 } elsif ($code =~ /return\s+bless[^,]+,\s*__PACKAGE__/) {
5700 # Detect: bless {}, __PACKAGE__
5701 0 $output->{type} = 'object';
5702 # Get package name from the extractor's stored document
5703 0 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 0 0 0
5704 0 my $pkg = $self->{_document}->find_first('PPI::Statement::Package');
5705 0 ▶ $output->{isa} = $pkg ? $pkg->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 0 0 0
5706 0 ▶ $self->_log(' OUTPUT: Object blessed into __PACKAGE__: ' . ($output->{isa} || 'UNKNOWN'));
Condition0%0/2
$output->{'isa'} || 'UNKNOWN'
l!l
00
MC/DC0%0/1
$output->{'isa'} || 'UNKNOWN'
$output->{'isa'}
Truth table0%0/2
A
A= $output->{'isa'}
Aresult
11
00
5707 0 ▶ $self->{_package_name} //= $output->{isa};
Condition0%0/3
$self->{"_package_name"} //= $output->{"isa"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_package_name"} //= $output->{"isa"}
$self->{"_package_name"} $output->{"isa"}
Truth table0%0/3
A //= B
A= $self->{"_package_name"}B= $output->{"isa"}
ABresult
1X1
011
000
5708 }
5709 } elsif ($code =~ /return\s*\(([^)]+)\)/) {
5710 12 my $content = $1;
5711 12 ▶ if ($content =~ /,/) { # Has comma = multiple values
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /,/) 6 6 12
5712 6 $output->{type} = 'array';
5713 }
5714 ▶ } elsif ($code =~ /return\s+\$self\s*;/ && $code =~ /\$self\s*->\s*\{[^}]+\}\s*=/) {
Condition100%3/3
$code =~ /return\s+\$self\s*;/ and $code =~ /\$self\s*->\s*\{[^}]+\}\s*=/
!ll&&!rl&&r
705213
MC/DC100%2/2
$code =~ /return\s+\$self\s*;/ and $code =~ /\$self\s*->\s*\{[^}]+\}\s*=/
$code =~ /return\s+\$self\s*;/ $code =~ /\$self\s*->\s*\{[^}]+\}\s*=/
Truth table100%3/3
A and B
A= $code =~ /return\s+\$self\s*;/B= $code =~ /\$self\s*->\s*\{[^}]+\}\s*=/
ABresult
0X0
100
111
5715 # Returns $self for chaining
5716 13 $output->{type} = 'object';
5717 13 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 13 0 13
5718 13 my $pkg = $self->{_document}->find_first('PPI::Statement::Package');
5719 13 ▶ $output->{isa} = $pkg ? $pkg->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 13 0 13
5720 13 ▶ $self->_log(' OUTPUT: Object chained into __PACKAGE__: ' . ($output->{isa} || 'UNKNOWN'));
Condition50%1/2
$output->{'isa'} || 'UNKNOWN'
l!l
130
MC/DC0%0/1
$output->{'isa'} || 'UNKNOWN'
$output->{'isa'}
Truth table50%1/2
A
A= $output->{'isa'}
Aresult
11
00
5721 13 ▶ $self->{_package_name} //= $output->{isa};
Condition33%1/3
$self->{"_package_name"} //= $output->{"isa"}
l!l&&r!l&&!r
1300
MC/DC0%0/2
$self->{"_package_name"} //= $output->{"isa"}
$self->{"_package_name"} $output->{"isa"}
Truth table33%1/3
A //= B
A= $self->{"_package_name"}B= $output->{"isa"}
ABresult
1X1
011
000
5722 }
5723 }
5724  
5725 # Find all return statements
5726 813 ▶ while ($code =~ /return\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+([^;]+);/g) 1003 813 1816
5727 1003 my $return_expr = $1;
5728 1003 push @return_statements, $return_expr;
5729 }
5730  
5731 813 ▶ if (@return_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@return_statements) { } 714 99 813
5732 714 $self->_log(' OUTPUT: Found ' . scalar(@return_statements) . ' return statement(s)');
5733  
5734 # Analyze return patterns
5735 714 my %return_types;
5736  
5737 714 ▶ if($output->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"type"}) 393 321 714
5738 393 $return_types{$output->{'type'}} += 3; # Add weighting to what's already been found
5739 }
5740 714 my $min;
5741 my $time_list;
5742 714 foreach my $ret (@return_statements) {
5743 1003 $ret =~ s/^\s+|\s+$//g;
5744  
5745 # Arithmetic operators inside a regex literal or a quoted
5746 # string are not arithmetic. SafeUA::numeric_host returns a
5747 # dotted quad and sprintf('%04d-...') returns a date: neither
5748 # is a number, though /:/ has a '/' and '%04d' a '%'.
5749 1003 my $ops_ret = _strip_literals($ret);
5750  
5751 # Literal values
5752 1003 ▶ if ($ret eq '1' || $ret eq '0') {
Branch: 20 branches
BranchTrueFalseTotal
if ($ret eq '1' or $ret eq '0') { } 72 931 1003
elsif ($ret =~ /^['"]/) { } 64 867 931
elsif ($ret =~ /^-?\d+$/) { } 106 761 867
elsif ($ret =~ /^-?\d+\.\d+$/) { } 0 761 761
elsif ($ret =~ /\A(?:sprintf|join|lc|uc|lcfirst|ucfirst|quotemeta)\b/) { } 53 708 761
elsif ($ret =~ /\Aundef\b/) { } 13 695 708
elsif ($self->_returns_time_list($ret)) { } 1 694 695
elsif ($ret =~ /^\[/) { } 19 675 694
elsif ($ret =~ /^\{/) { } 25 650 675
elsif ($ret =~ /^\\\%/) { } 27 623 650
elsif ($ret =~ /^\\\@/) { } 13 610 623
elsif ($ops_ret =~ m[ # Numeric expressions (heuristic, medium confidence). # `->` is a method call, `//` a defined-or, and `-e` a file # test, not operators. (?: \+ | -(?!>|[A-Za-z]\b) | \* | (?<!\/)\/(?!\/) | (?<!\\)% | \+\+ | -- ) ]x) { } 126 484 610
elsif ($ret =~ /\|\|\s*\d+\b/) { } 1 483 484
elsif ($ret =~ /^length[\s\(]/) { } 2 481 483
elsif ($ret =~ /^pos[\s\(]/) { } 0 481 481
elsif ($ret =~ /^index[\s\(]/) { } 0 481 481
elsif ($ret =~ /^rindex[\s\(]/) { } 0 481 481
elsif ($ret =~ /^ord[\s\(]/) { } 0 481 481
elsif ($ops_ret =~ /=/ and $ops_ret =~ /\$\w+/) { } 107 374 481
elsif ($ret =~ /\$\w+/) { } 341 33 374
Condition100%3/3
$ret eq 1 or $ret eq 0
l!l&&r!l&&!r
4626931
MC/DC100%2/2
$ret eq 1 or $ret eq 0
$ret eq 1 $ret eq 0
Truth table100%3/3
A or B
A= $ret eq 1B= $ret eq 0
ABresult
1X1
011
000
5753 72 $return_types{boolean}++;
5754 } elsif ($ret =~ /^['"]/) {
5755 64 $return_types{string}++;
5756 } elsif ($ret =~ /^-?\d+$/) {
5757 106 $return_types{integer}++;
5758 } elsif ($ret =~ /^-?\d+\.\d+$/) {
5759 0 $return_types{number}++;
5760 } elsif ($ret =~ /\A(?:sprintf|join|lc|uc|lcfirst|ucfirst|quotemeta)\b/) {
5761 # A formatting or case call returns text, whatever arithmetic
5762 # its arguments contain: sprintf '%02d:%02d', $h, $m is a
5763 # timestamp string, not a number.
5764 53 $return_types{string} += 2;
5765 } elsif ($ret =~ /\Aundef\b/) {
5766 # `return undef unless $id` is an undef return with a guard,
5767 # the same error return as a bare `return undef`.
5768 13 $return_types{undef}++;
5769 } elsif ($self->_returns_time_list($ret)) {
5770 # localtime and gmtime give the nine-element time list in
5771 # list context; the arithmetic in their argument does not
5772 # make the return a number.
5773 1 $return_types{array} += 2;
5774 1 $time_list = 1;
5775 } elsif ($ret =~ /^\[/) {
5776 # Data structures
5777 19 $return_types{arrayref}++;
5778 } elsif ($ret =~ /^\{/) {
5779 25 $return_types{hashref}++;
5780 } elsif ($ret =~ /^\\\%/) {
5781 # `return \%checked`: a reference to a lexical hash has no
5782 # $ sigil, so before this it fell through every branch and
5783 # the routine was left with no output type at all.
5784 27 $return_types{hashref}++;
5785 } elsif ($ret =~ /^\\\@/) {
5786 13 $return_types{arrayref}++;
5787 } elsif ($ops_ret =~ m{
5788 # Numeric expressions (heuristic, medium confidence).
5789 # `->` is a method call, `//` a defined-or, and `-e` a file
5790 # test, not operators.
5791 (?:
5792 \+ | -(?!>|[A-Za-z]\b) | \* | (?<!\/)\/(?!\/) | (?<!\\)%
5793 | \+\+ | --
5794 )
5795 }x) {
5796 126 $return_types{number} += 2;
5797 } elsif ($ret =~ /\|\|\s*\d+\b/) {
5798 # Logical-or fallback with numeric literal (e.g. $x || 200)
5799 1 $return_types{integer} += 2;
5800 1 $self->_log(" OUTPUT: Numeric fallback expression detected");
5801 } elsif($ret =~ /^length[\s\(]/) {
5802 2 $return_types{integer}++;
5803 2 $min = 0;
5804 } elsif($ret =~ /^pos[\s\(]/) {
5805 0 $return_types{integer}++;
5806 0 $min = 0;
5807 } elsif($ret =~ /^index[\s\(]/) {
5808 0 $return_types{integer}++;
5809 0 $min = -1;
5810 } elsif($ret =~ /^rindex[\s\(]/) {
5811 0 $return_types{integer}++;
5812 0 $min = -1;
5813 } elsif($ret =~ /^ord[\s\(]/) {
5814 0 $return_types{integer}++;
5815 ▶ } elsif ($ops_ret =~ /=/ && $ops_ret =~ /\$\w+/) {
Condition100%3/3
$ops_ret =~ /=/ and $ops_ret =~ /\$\w+/
!ll&&!rl&&r
3731107
MC/DC100%2/2
$ops_ret =~ /=/ and $ops_ret =~ /\$\w+/
$ops_ret =~ /=/ $ops_ret =~ /\$\w+/
Truth table100%3/3
A and B
A= $ops_ret =~ /=/B= $ops_ret =~ /\$\w+/
ABresult
0X0
100
111
5816 # Assignment returning a value (e.g. $self->{status} = $status)
5817 # If assignment involves a numeric literal or variable, assume numeric intent.
5818 # $1, $2 and friends are regex captures - strings, not numeric
5819 # literals - so drop them before looking for digits. CGI::Info::script_dir
5820 # returns "$self->{script_path} =~ $dir_regex ? $1 : ..." and the
5821 # "1" in $1 was being read as a numeric literal.
5822 107 (my $numeric_ret = $ops_ret) =~ s/\$\d+//g;
5823 107 ▶ if ($numeric_ret =~ /\b\d+\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric_ret =~ /\b\d+\b/) { } 36 71 107
5824 36 $return_types{integer} += 2;
5825 36 $self->_log(" OUTPUT: Assignment with numeric value detected");
5826 } else {
5827 71 $return_types{scalar}++;
5828 }
5829 }
5830 # Variables/expressions
5831 elsif ($ret =~ /\$\w+/) {
5832 # \@_ is this routine's own argument list being passed
5833 # along, not an arrayref being returned. CGI::Info::get_cookie
5834 # returns $self->cookie(\@_) and was typed as returning an
5835 # arrayref because of it.
5836 341 (my $ref_ret = $ret) =~ s/\\\@_\b//g;
5837 341 ▶ if ($ref_ret =~ /\\\@/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($ref_ret =~ /\\\@/) { } 0 341 341
elsif ($ref_ret =~ /\\\%/) { } 6 335 341
elsif ($ret =~ /bless/) { } 22 313 335
elsif ($ret =~ /^\{[^}]*\}$/) { } 0 313 313
elsif ($ret =~ /^\[[^\]]*\]$/) { } 0 313 313
5838 0 $return_types{arrayref}++;
5839 } elsif ($ref_ret =~ /\\\%/) {
5840 6 $return_types{hashref}++;
5841 } elsif ($ret =~ /bless/) {
5842 22 $return_types{object} += 2; # Heigher weight
5843 } elsif ($ret =~ /^\{[^}]*\}$/) {
5844 0 $return_types{hashref}++;
5845 } elsif ($ret =~ /^\[[^\]]*\]$/) {
5846 0 $return_types{arrayref}++;
5847 } else {
5848 313 $return_types{scalar}++;
5849 }
5850 }
5851 }
5852  
5853 # Determine most common return type
5854 714 ▶ if (keys %return_types) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %return_types) 705 9 714
5855 # The undef paths are the documented error return
5856 # (_error_return), not the type of a successful value: a
5857 # routine returning a record or undef returns a record when
5858 # it succeeds, however many guards send it back undef.
5859 705 my %value_types = %return_types;
5860 705 ▶ delete $value_types{undef} if(keys(%value_types) > 1);
Branch: 1 branch
BranchTrueFalseTotal
if keys %value_types > 1 265 440 705
5861 # A tie is broken by name so the same code always yields the
5862 # same schema: hash order is randomised per process, and
5863 # Geo::Coder::Free::geocode_many alternated between string and
5864 # hashref in successive runs.
5865 705 ▶ my ($most_common) = sort { $value_types{$b} <=> $value_types{$a} || $a cmp $b } keys %value_types;
Condition66%2/3
$value_types{$b} <=> $value_types{$a} || $a cmp $b
l!l&&r!l&&!r
307260
MC/DC0%0/2
$value_types{$b} <=> $value_types{$a} || $a cmp $b
$value_types{$b} <=> $value_types{$a} $a cmp $b
Truth table66%2/3
A || B
A= $value_types{$b} <=> $value_types{$a}B= $a cmp $b
ABresult
1X1
011
000
5866 # Prefer integer over scalar if numeric returns dominate
5867 705 ▶ if ($return_types{integer} && (!$return_types{string})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_types{"integer"} and not $return_types{"string"}) 136 569 705
Condition100%3/3
$return_types{"integer"} and not $return_types{"string"}
!ll&&!rl&&r
5663136
MC/DC100%2/2
$return_types{"integer"} and not $return_types{"string"}
$return_types{"integer"} not $return_types{"string"}
Truth table100%3/3
A and not(B)
A= $return_types{"integer"}B= not $return_types{"string"}
ABresult
0X0
100
111
5868 136 ▶ if (!$output->{type} || $output->{type} eq 'scalar') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $output->{"type"} or $output->{"type"} eq "scalar") 105 31 136
Condition66%2/3
not $output->{"type"} or $output->{"type"} eq "scalar"
l!l&&r!l&&!r
105031
MC/DC50%1/2
not $output->{"type"} or $output->{"type"} eq "scalar"
not $output->{"type"} $output->{"type"} eq "scalar"
Truth table66%2/3
not(A) or B
A= not $output->{"type"}B= $output->{"type"} eq "scalar"
ABresult
1X1
011
000
5869 105 $output->{type} = 'integer';
5870 105 $self->_log(" OUTPUT: Numeric returns dominate, forcing integer");
5871 105 ▶ $output->{_type_confidence} ||= 'low';
Condition50%1/2
$output->{"_type_confidence"} ||= "low"
l!l
0105
MC/DC0%0/1
$output->{"_type_confidence"} ||= "low"
$output->{"_type_confidence"}
Truth table50%1/2
A
A= $output->{"_type_confidence"}
Aresult
11
00
5872 105 ▶ if(defined($min)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 2 103 105
5873 2 $output->{min} = $min;
5874 }
5875 }
5876 }
5877 705 ▶ unless ($output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->{"type"}) 207 498 705
5878 207 $output->{type} = $most_common;
5879  
5880 # Assign confidence for inferred numeric expressions
5881 207 ▶ if ($most_common eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($most_common eq "number") 31 176 207
5882 31 ▶ $output->{_type_confidence} ||= 'medium';
Condition50%1/2
$output->{"_type_confidence"} ||= "medium"
l!l
031
MC/DC0%0/1
$output->{"_type_confidence"} ||= "medium"
$output->{"_type_confidence"}
Truth table50%1/2
A
A= $output->{"_type_confidence"}
Aresult
11
00
5883 31 ▶ if(defined($min)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 0 31 31
5884 0 $output->{min} = $min;
5885 }
5886 }
5887  
5888 207 $self->_log(" OUTPUT: Inferred type from code: $most_common");
5889 }
5890  
5891 705 ▶ if($time_list && ($output->{type} // '') eq 'array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_list and ($output->{"type"} // "") eq "array") 1 704 705
Condition60%3/5
$time_list and ($output->{"type"} // "") eq "array"
!ll&&!rl&&r
70401
$output->{"type"} // ""
l!l
10
MC/DC50%1/2
$time_list and ($output->{"type"} // "") eq "array"
$time_list $output->{"type"}
Truth table66%2/3
A and B
A= $time_listB= $output->{"type"}
ABresult
0X0
100
111
5892 # 'number': a time field can be negative (a fixed offset
5893 # before the epoch), and Params::Validate::Strict reads
5894 # the '-' of an integer element as a non-digit.
5895 1 $output->{element_type} = 'number';
5896 }
5897 }
5898  
5899 # Check for consistent single value returns
5900 714 ▶ if (@return_statements == 1 && $return_statements[0] eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (@return_statements == 1 and $return_statements[0] eq 1) 33 681 714
Condition100%3/3
@return_statements == 1 and $return_statements[0] eq 1
!ll&&!rl&&r
11456733
MC/DC100%2/2
@return_statements == 1 and $return_statements[0] eq 1
@return_statements == 1 $return_statements[0] eq 1
Truth table100%3/3
A and B
A= @return_statements == 1B= $return_statements[0] eq 1
ABresult
0X0
100
111
5901 33 $output->{value} = 1;
5902 33 ▶ $output->{type} = 'boolean' if !$output->{type} || $output->{type} eq 'scalar';
Branch: 1 branch
BranchTrueFalseTotal
if not $output->{"type"} or $output->{"type"} eq "scalar" 0 33 33
Condition33%1/3
not $output->{"type"} or $output->{"type"} eq "scalar"
l!l&&r!l&&!r
0033
MC/DC0%0/2
not $output->{"type"} or $output->{"type"} eq "scalar"
not $output->{"type"} $output->{"type"} eq "scalar"
Truth table33%1/3
not(A) or B
A= not $output->{"type"}B= $output->{"type"} eq "scalar"
ABresult
1X1
011
000
5903 33 ▶ $self->_log(" OUTPUT: Type already set to '$output->{type}', overriding with boolean") if($output->{'type'});
Branch: 1 branch
BranchTrueFalseTotal
if $output->{"type"} 33 0 33
5904 }
5905 } else {
5906 # No explicit return - might return nothing or implicit undef
5907 99 $self->_log(" OUTPUT: No explicit return statement found");
5908 }
5909 }
5910 }
5911  
5912 # --------------------------------------------------
5913 # _enhance_boolean_detection
5914 #
5915 # Purpose: Apply additional boolean-specific
5916 # detection heuristics using a weighted
5917 # scoring system, to override weak
5918 # type assignments when there is strong
5919 # evidence of a boolean return.
5920 #
5921 # Entry: $output - output hashref
5922 # (modified in place).
5923 # $pod - POD string.
5924 # $code - method body source string.
5925 # $method_name - method name string.
5926 #
5927 # Exit: Returns nothing. Modifies $output
5928 # in place, setting type to 'boolean'
5929 # if the score reaches
5930 # $BOOLEAN_SCORE_THRESHOLD.
5931 #
5932 # Side effects: Logs scoring details to stdout when
5933 # verbose is set.
5934 #
5935 # Notes: Only fires when output type is
5936 # not yet set or is 'unknown'. Does not
5937 # override explicitly set types.
5938 # --------------------------------------------------
5939 813 ▶sub _enhance_boolean_detection {
Subroutine _enhance_boolean_detection: called CC=22 SCAR=46.4
5940 813 my ($self, $output, $pod, $code, $method_name) = @_;
5941  
5942 813 my $boolean_score = 0; # Track evidence for boolean return
5943  
5944 813 ▶ return unless !$output->{type} || $output->{type} eq 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
unless not $output->{"type"} or $output->{"type"} eq "unknown" 783 30 813
Condition66%2/3
not $output->{"type"} or $output->{"type"} eq "unknown"
l!l&&r!l&&!r
300783
MC/DC50%1/2
not $output->{"type"} or $output->{"type"} eq "unknown"
not $output->{"type"} $output->{"type"} eq "unknown"
Truth table66%2/3
not(A) or B
A= not $output->{"type"}B= $output->{"type"} eq "unknown"
ABresult
1X1
011
000
5945  
5946 # Look for stronger boolean indicators
5947 30 ▶ if ($pod && !$output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod and not $output->{"type"}) 12 18 30
Condition66%2/3
$pod and not $output->{"type"}
!ll&&!rl&&r
18012
MC/DC50%1/2
$pod and not $output->{"type"}
$pod not $output->{"type"}
Truth table66%2/3
A and not(B)
A= $podB= not $output->{"type"}
ABresult
0X0
100
111
5948 # Common boolean return patterns in POD
5949 12 ▶ if ($pod =~ /returns?\s+(?:true|false|1|0)\s+(?:on|for|upon)\s+(?:success|failure|error|valid|invalid)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /returns?\s+(?:true|false|1|0)\s+(?:on|for|upon)\s+(?:success|failure|error|valid|invalid)/i) 0 12 12
5950 0 $boolean_score += 30;
5951 0 $self->_log(' OUTPUT: Strong boolean indicator in POD (+30)');
5952 }
5953  
5954 # Check for method names that suggest boolean returns
5955 12 ▶ if ($pod =~ /(?:method|sub)\s+(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /(?:method|sub)\s+(\w+)/) 0 12 12
5956 0 my $inferred_method_name = $1;
5957 0 ▶ if ($inferred_method_name =~ /^(is_|has_|can_|should_|contains_|exists_)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inferred_method_name =~ /^(is_|has_|can_|should_|contains_|exists_)/) 0 0 0
5958 0 $boolean_score += 20;
5959 0 $self->_log(" OUTPUT: Inferred method name '$inferred_method_name' suggests boolean return (+20)");
5960 }
5961 }
5962 }
5963  
5964 # Analyze code for boolean patterns
5965 30 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 30 0 30
5966 # Count boolean return idioms
5967 30 my $true_returns = () = $code =~ /return\s+1\s*;/g;
5968 30 my $false_returns = () = $code =~ /return\s+0\s*;/g;
5969  
5970 30 ▶ if ($true_returns + $false_returns >= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($true_returns + $false_returns >= 2) { } 0 30 30
elsif ($true_returns + $false_returns == 1) { } 0 30 30
5971 0 $boolean_score += 40;
5972 0 $self->_log(' OUTPUT: Multiple 1/0 returns suggest boolean (+40)');
5973 } elsif ($true_returns + $false_returns == 1) {
5974 0 $boolean_score += 10;
5975 0 $self->_log(' OUTPUT: Single 1/0 return (+10)');
5976 }
5977  
5978 # Ternary operators that return booleans
5979 30 ▶ if ($code =~ /return\s+(?:\w+\s*[!=]=\s*\w+|\w+\s*>\s*\w+|\w+\s*<\s*\w+)\s*\?\s*(?:1|0)\s*:\s*(?:1|0)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+(?:\w+\s*[!=]=\s*\w+|\w+\s*>\s*\w+|\w+\s*<\s*\w+)\s*\?\s*(?:1|0)\s*:\s*(?:1|0)/) 0 30 30
5980 0 $boolean_score += 25;
5981 0 $self->_log(' OUTPUT: Ternary with 1/0 suggests boolean (+25)');
5982 }
5983  
5984 # Check for common boolean method patterns
5985 30 ▶ if ($code =~ /return\s+[!\$\@\%]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+[!\$\@\%]/) 6 24 30
5986 # Returns negation or existence check
5987 6 $boolean_score += 15;
5988 6 $self->_log(' OUTPUT: Returns negation/existence check (+15)');
5989 }
5990 }
5991  
5992 # Check method name for boolean indicators
5993 30 ▶ if ($method_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name) 30 0 30
5994 30 ▶ if ($method_name =~ /^(?:is_|has_|can_|should_|contains_|exists_|check_|verify_|validate_)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^(?:is_|has_|can_|should_|contains_|exists_|check_|verify_|validate_)/) 0 30 30
5995 0 $boolean_score += 25;
5996 0 $self->_log(" OUTPUT: Method name '$method_name' suggests boolean return (+25)");
5997 }
5998 30 ▶ if ($method_name =~ /_ok$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /_ok$/) 0 30 30
5999 0 $boolean_score += 30;
6000 0 $self->_log(" OUTPUT: Method name '$method_name' ends with '_ok' (+30)");
6001 }
6002 }
6003  
6004 # Apply boolean type if we have strong evidence
6005 # Override weak type assignments (like 'array' from false positive)
6006 30 ▶ if($boolean_score >= $BOOLEAN_SCORE_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean_score >= $BOOLEAN_SCORE_THRESHOLD) 0 30 30
6007 0 ▶ if (!$output->{type} || $output->{type} eq 'scalar' || $output->{type} eq 'array' || $output->{type} eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $output->{"type"} or $output->{"type"} eq "scalar" or $output->{"type"} eq "array" or $output->{"type"} eq "undef") 0 0 0
Condition0%0/9
not $output->{"type"} or $output->{"type"} eq "scalar" or $output->{"type"} eq "array" or $output->{"type"} eq "undef"
l!l&&r!l&&!r
000
not $output->{"type"} or $output->{"type"} eq "scalar" or $output->{"type"} eq "array"
l!l&&r!l&&!r
000
not $output->{"type"} or $output->{"type"} eq "scalar"
l!l&&r!l&&!r
000
MC/DC0%0/4
not $output->{"type"} or $output->{"type"} eq "scalar" or $output->{"type"} eq "array" or $output->{"type"} eq "undef"
not $output->{"type"} $output->{"type"} eq "scalar" $output->{"type"} eq "array" $output->{"type"} eq "undef"
Truth table0%0/5
not(A) or B or C or D
A= not $output->{"type"}B= $output->{"type"} eq "scalar"C= $output->{"type"} eq "array"D= $output->{"type"} eq "undef"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
6008 0 ▶ my $old_type = $output->{type} || 'none';
Condition0%0/2
$output->{'type'} || 'none'
l!l
00
MC/DC0%0/1
$output->{'type'} || 'none'
$output->{'type'}
Truth table0%0/2
A
A= $output->{'type'}
Aresult
11
00
6009 0 $output->{type} = 'boolean';
6010 0 $self->_log(" OUTPUT: Boolean score $boolean_score >= $BOOLEAN_SCORE_THRESHOLD, setting type to boolean (was: $old_type)");
6011 }
6012 }
6013 }
6014  
6015 # --------------------------------------------------
6016 # _detect_list_context
6017 #
6018 # Purpose: Detect methods that return different
6019 # values depending on calling context
6020 # via wantarray, and methods that
6021 # return explicit lists.
6022 #
6023 # Entry: $output - output hashref (modified
6024 # in place).
6025 # $code - method body source string.
6026 #
6027 # Exit: Returns nothing. Modifies $output
6028 # in place, setting _context_aware,
6029 # _list_context, _scalar_context,
6030 # _list_return, and/or type keys.
6031 #
6032 # Side effects: Logs detections to stdout when
6033 # verbose is set.
6034 # --------------------------------------------------
6035 813 ▶sub _detect_list_context {
Subroutine _detect_list_context: called CC=16 SCAR=27.9
6036 813 my ($self, $output, $code) = @_;
6037 813 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 813 813
6038  
6039 # Check for wantarray usage
6040 813 ▶ if ($code =~ /wantarray/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /wantarray/) 7 806 813
6041 7 $output->{_context_aware} = 1;
6042 7 $self->_log(' OUTPUT: Method uses wantarray - context sensitive');
6043  
6044 # Debug: show what we're matching against
6045 7 ▶ if ($code =~ /(wantarray[^;]+;)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(wantarray[^;]+;)/s) 6 1 7
6046 6 $self->_log(" DEBUG wantarray line: $1");
6047 }
6048  
6049 7 ▶ if ($code =~ /wantarray\s*\?\s*\(([^)]+)\)\s*:\s*([^;]+)/s) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ /wantarray\s*\?\s*\(([^)]+)\)\s*:\s*([^;]+)/s) { } 1 6 7
elsif ($code =~ /wantarray\s*\?\s*([^:]+?)\s*:\s*([^;]+)/s) { } 5 1 6
elsif ($code =~ /return[^;]*unless\s+wantarray.*?return\s*\(([^)]+)\)/s) { } 1 0 1
6050 # Pattern 1: wantarray ? (list, items) : scalar_value (with parens)
6051 1 my ($list_return, $scalar_return) = ($1, $2);
6052 1 $self->_log(" DEBUG list (with parens): [$list_return], scalar: [$scalar_return]");
6053  
6054 1 $output->{_list_context} = $self->_infer_type_from_expression($list_return);
6055 1 $output->{_scalar_context} = $self->_infer_type_from_expression($scalar_return);
6056 1 $self->_log(' OUTPUT: Detected context-dependent returns (parenthesized)');
6057 } elsif ($code =~ /wantarray\s*\?\s*([^:]+?)\s*:\s*([^;]+)/s) {
6058 # Pattern 2: wantarray ? @array : scalar (no parens around list)
6059 5 my ($list_return, $scalar_return) = ($1, $2);
6060 # Clean up
6061 5 $list_return =~ s/^\s+|\s+$//g;
6062 5 $scalar_return =~ s/^\s+|\s+$//g;
6063  
6064 5 $self->_log(" DEBUG list (no parens): [$list_return], scalar: [$scalar_return]");
6065  
6066 5 $output->{_list_context} = $self->_infer_type_from_expression($list_return);
6067 5 $output->{_scalar_context} = $self->_infer_type_from_expression($scalar_return);
6068 5 $self->_log(' OUTPUT: Detected context-dependent returns (non-parenthesized)');
6069 } elsif ($code =~ /return[^;]*unless\s+wantarray.*?return\s*\(([^)]+)\)/s) {
6070 # Pattern 3: return unless wantarray; return (list);
6071 1 $output->{_list_context} = { type => 'array' };
6072 1 $self->_log(' OUTPUT: Detected list context return after wantarray check');
6073 }
6074 }
6075  
6076 # Detect explicit list returns (multiple values in parentheses)
6077 # Avoid false positives from function calls
6078 813 ▶ if ($code =~ /return\s*\(\s*([^)]+)\s*\)\s*;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s*\(\s*([^)]+)\s*\)\s*;/) 2 811 813
6079 2 my $content = $1;
6080  
6081 # Count commas outside of nested structures, jumping over each
6082 # balanced bracketed block in one step via extract_bracketed
6083 2 require Text::Balanced;
6084 2 my $comma_count = 0;
6085 2 my $rest = $content;
6086 2 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 29 2 31
6087 29 ▶ if (substr($rest, 0, 1) =~ /[(\[{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) =~ /[(\[{]/) 4 25 29
6088 4 my $extracted = Text::Balanced::extract_bracketed($rest, '(){}[]');
6089 4 ▶ last unless defined $extracted; # Unbalanced brackets
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 4 4
6090 4 next;
6091 }
6092 25 ▶ $comma_count++ if substr($rest, 0, 1) eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if substr($rest, 0, 1) eq "," 2 23 25
6093 25 $rest = substr($rest, 1);
6094 }
6095  
6096 2 ▶ if ($comma_count > 0 && $content !~ /\b(?:bless|new)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma_count > 0 and not $content =~ /\b(?:bless|new)\b/) 1 1 2
Condition66%2/3
$comma_count > 0 and not $content =~ /\b(?:bless|new)\b/
!ll&&!rl&&r
101
MC/DC50%1/2
$comma_count > 0 and not $content =~ /\b(?:bless|new)\b/
$comma_count > 0 not $content =~ /\b(?:bless|new)\b/
Truth table66%2/3
A and not(B)
A= $comma_count > 0B= not $content =~ /\b(?:bless|new)\b/
ABresult
0X0
100
111
6097 # Multiple values returned
6098 1 ▶ unless ($output->{type} && $output->{type} eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->{"type"} and $output->{"type"} eq "boolean") 1 0 1
Condition33%1/3
$output->{"type"} and $output->{"type"} eq "boolean"
!ll&&!rl&&r
010
MC/DC0%0/2
$output->{"type"} and $output->{"type"} eq "boolean"
$output->{"type"} $output->{"type"} eq "boolean"
Truth table33%1/3
A and B
A= $output->{"type"}B= $output->{"type"} eq "boolean"
ABresult
0X0
100
111
6099 1 $output->{type} = 'array';
6100 1 $output->{_list_return} = $comma_count + 1;
6101 1 $self->_log(' OUTPUT: Returns list of ' . ($comma_count + 1) . ' values');
6102 }
6103 }
6104 }
6105 }
6106  
6107 # --------------------------------------------------
6108 # _returns_time_list
6109 #
6110 # Purpose: Decide whether a return expression
6111 # gives the nine-element list built by
6112 # localtime or gmtime, rather than a
6113 # scalar derived from one.
6114 #
6115 # Entry: $ret - a return expression.
6116 #
6117 # Exit: Returns true when the whole
6118 # expression, or the arm of a ternary
6119 # that ends it, is a localtime/gmtime
6120 # call.
6121 # --------------------------------------------------
6122 695 ▶sub _returns_time_list {
Subroutine _returns_time_list: called CC=4 SCAR=14.2
6123 695 my ($self, $ret) = @_;
6124  
6125 695 ▶ return 0 unless defined($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 695 695
6126 695 ▶ return 0 unless $ret =~ /(?:\A|[?:])\s*\(*\s*(?:localtime|gmtime)\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret =~ /(?:\A|[?:])\s*\(*\s*(?:localtime|gmtime)\s*\(/ 694 1 695
6127  
6128 1 ▶ return $ret =~ /(?:localtime|gmtime)\s*\([^()]*\)\s*\)*\s*\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ret =~ /(?:localtime|gmtime)\s*\([^()]*\)\s*\)*\s*\z/ ? : 1 0 1
6129 }
6130  
6131 # --------------------------------------------------
6132 # _detect_void_context
6133 #
6134 # Purpose: Detect methods that return nothing
6135 # meaningful (void context), methods
6136 # that always return 1 as a success
6137 # indicator, and methods whose name
6138 # suggests void context (setters,
6139 # mutators, loggers).
6140 #
6141 # Entry: $output - output hashref
6142 # (modified in place).
6143 # $code - method body source string.
6144 # $method_name - method name string.
6145 #
6146 # Exit: Returns nothing. Modifies $output
6147 # in place, setting _void_context,
6148 # _success_indicator, and/or type.
6149 #
6150 # Side effects: Logs detections to stdout when
6151 # verbose is set.
6152 # --------------------------------------------------
6153 813 ▶sub _detect_void_context {
Subroutine _detect_void_context: called CC=17 SCAR=28.3
6154 813 my ($self, $output, $code, $method_name) = @_;
6155 813 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 813 813
6156  
6157 813 $self->_log(" DEBUG _detect_void_context called for $method_name");
6158  
6159 # Methods that typically don't return meaningful values
6160 813 my $void_patterns = {
6161 'setter' => qr/^set_\w+$/,
6162 'mutator' => qr/^(?:add|remove|delete|clear|reset|update)_/,
6163 'logger' => qr/^(?:log|debug|warn|error|info)$/,
6164 'printer' => qr/^(?:print|say|dump)_/,
6165 };
6166  
6167 # Check if method name suggests void context
6168 813 foreach my $type (keys %$void_patterns) {
6169 3219 ▶ if ($method_name =~ $void_patterns->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /$void_patterns->{$type}/) 23 3196 3219
6170 23 $output->{_void_context_hint} = $type;
6171 23 $self->_log(" OUTPUT: Method name suggests $type (typically void context)");
6172 23 last;
6173 }
6174 }
6175  
6176 # Analyze return statements
6177 813 my @returns = $code =~ /return\s*([^;]*);/g;
6178  
6179 813 $self->_log(' DEBUG Found ' . scalar(@returns) . ' return statements');
6180  
6181 # Count different return patterns
6182 813 my $no_value_returns = 0;
6183 813 my $true_returns = 0;
6184 813 my $self_returns = 0;
6185  
6186 813 foreach my $ret (@returns) {
6187 1165 $ret =~ s/^\s+|\s+$//g;
6188 1165 $self->_log(" DEBUG return value: [$ret]");
6189 1165 ▶ $no_value_returns++ if $ret eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq "" 14 1151 1165
6190 1165 ▶ $no_value_returns++ if($ret =~ /^(if|unless)\s/);
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /^(if|unless)\s/ 10 1155 1165
6191 1165 ▶ $true_returns++ if $ret eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq 1 46 1119 1165
6192 1165 ▶ $self_returns++ if $ret eq '$self';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq "\$self" 15 1150 1165
6193 1165 ▶ if ($ret =~ /\?\s*1\s*:\s*0\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret =~ /\?\s*1\s*:\s*0\b/) 28 1137 1165
6194 # Strong boolean signal: ternary returning 1/0
6195 28 $true_returns++;
6196 # $self->_log(" OUTPUT: Ternary 1:0 return detected, treating as boolean (+40)");
6197 28 $self->_log(' OUTPUT: Ternary 1:0 return detected, treating as boolean');
6198 }
6199 }
6200  
6201 813 my $total_returns = scalar(@returns);
6202  
6203 813 $self->_log(" DEBUG no_value=$no_value_returns, true=$true_returns, self=$self_returns, total=$total_returns");
6204  
6205 # Void context indicators
6206 813 ▶ if ($no_value_returns > 0 && $no_value_returns == $total_returns) {
Branch: 2 branches
BranchTrueFalseTotal
if ($no_value_returns > 0 and $no_value_returns == $total_returns) { } 11 802 813
elsif ($true_returns > 0 and $true_returns == $total_returns and $total_returns >= 1) { } 49 1506 1555
Condition100%3/3
$no_value_returns > 0 and $no_value_returns == $total_returns
!ll&&!rl&&r
7911111
MC/DC100%2/2
$no_value_returns > 0 and $no_value_returns == $total_returns
$no_value_returns > 0 $no_value_returns == $total_returns
Truth table100%3/3
A and B
A= $no_value_returns > 0B= $no_value_returns == $total_returns
ABresult
0X0
100
111
6207 11 $output->{_void_context} = 1;
6208 11 $output->{type} = 'void'; # This should override any previous type
6209 11 $self->_log(' OUTPUT: All returns are empty - void context method');
6210 ▶ } elsif ($true_returns > 0 && $true_returns == $total_returns && $total_returns >= 1) {
Condition83%5/6
$true_returns > 0 and $true_returns == $total_returns and $total_returns >= 1
!ll&&!rl&&r
753049
$true_returns > 0 and $true_returns == $total_returns
!ll&&!rl&&r
7282549
MC/DC66%2/3
$true_returns > 0 and $true_returns == $total_returns and $total_returns >= 1
$true_returns > 0 $true_returns == $total_returns $total_returns >= 1
Truth table75%3/4
A and B and C
A= $true_returns > 0B= $true_returns == $total_returnsC= $total_returns >= 1
ABCresult
0XX0
10X0
1100
1111
6211 # Methods that always return true (success indicator)
6212 49 $output->{_success_indicator} = 1;
6213 # Don't override type if already set to boolean
6214 49 ▶ unless ($output->{type} && $output->{type} eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->{"type"} and $output->{"type"} eq "boolean") 13 36 49
Condition66%2/3
$output->{"type"} and $output->{"type"} eq "boolean"
!ll&&!rl&&r
01336
MC/DC50%1/2
$output->{"type"} and $output->{"type"} eq "boolean"
$output->{"type"} $output->{"type"} eq "boolean"
Truth table66%2/3
A and B
A= $output->{"type"}B= $output->{"type"} eq "boolean"
ABresult
0X0
100
111
6215 13 $output->{type} = 'boolean';
6216 }
6217 49 $self->_log(' OUTPUT: Always returns 1 - success indicator pattern');
6218 }
6219 }
6220  
6221 # --------------------------------------------------
6222 # _detect_chaining_pattern
6223 #
6224 # Purpose: Detect methods that return $self for
6225 # fluent interface chaining, by counting
6226 # the proportion of return statements
6227 # that return $self.
6228 #
6229 # Entry: $output - output hashref (modified
6230 # in place).
6231 # $code - method body source string.
6232 #
6233 # Exit: Returns nothing. Modifies $output
6234 # in place, setting type to 'object',
6235 # _returns_self to 1, and isa to the
6236 # current package name when the
6237 # proportion of $self returns is >= 0.8.
6238 #
6239 # Side effects: Logs detection to stdout when
6240 # verbose is set.
6241 # --------------------------------------------------
6242 813 ▶sub _detect_chaining_pattern {
Subroutine _detect_chaining_pattern: called CC=10 SCAR=23.4
6243 813 my ($self, $output, $code) = @_;
6244 813 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 813 813
6245  
6246 # Count returns of $self
6247 813 my $self_returns = 0;
6248 813 my $total_returns = 0;
6249  
6250 813 ▶ while ($code =~ /return\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+([^;]+);/g) 1003 813 1816
6251 1003 my $ret = $1;
6252 1003 $ret =~ s/^\s+|\s+$//g;
6253 1003 $total_returns++;
6254 1003 ▶ $self_returns++ if $ret eq '$self';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq "\$self" 15 988 1003
6255 }
6256  
6257 # If most/all returns are $self, it's a chaining method
6258 813 ▶ if ($self_returns > 0 && $total_returns > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_returns > 0 and $total_returns > 0) 15 798 813
Condition66%2/3
$self_returns > 0 and $total_returns > 0
!ll&&!rl&&r
798015
MC/DC50%1/2
$self_returns > 0 and $total_returns > 0
$self_returns > 0 $total_returns > 0
Truth table66%2/3
A and B
A= $self_returns > 0B= $total_returns > 0
ABresult
0X0
100
111
6259 15 my $ratio = $self_returns / $total_returns;
6260  
6261 15 ▶ if ($ratio >= 0.8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ratio >= "0.8") 13 2 15
6262 13 $output->{type} = 'object';
6263 13 $output->{_returns_self} = 1;
6264  
6265 # Get the class name
6266 13 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 13 0 13
6267 13 my $pkg = $self->{_document}->find_first('PPI::Statement::Package');
6268 13 ▶ $output->{isa} = $pkg ? $pkg->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 13 0 13
6269 13 ▶ $self->{_package_name} //= $output->{isa};
Condition33%1/3
$self->{"_package_name"} //= $output->{"isa"}
l!l&&r!l&&!r
1300
MC/DC0%0/2
$self->{"_package_name"} //= $output->{"isa"}
$self->{"_package_name"} $output->{"isa"}
Truth table33%1/3
A //= B
A= $self->{"_package_name"}B= $output->{"isa"}
ABresult
1X1
011
000
6270 }
6271  
6272 13 $self->_log(" OUTPUT: Chainable method - returns \$self ($self_returns/$total_returns returns)");
6273 }
6274 }
6275 }
6276  
6277 # --------------------------------------------------
6278 # _detect_error_conventions
6279 #
6280 # Purpose: Analyse how a method signals errors
6281 # by detecting patterns such as
6282 # 'return undef if', implicit bare
6283 # returns, empty list returns, 0/1
6284 # boolean error patterns, and eval
6285 # exception handling.
6286 #
6287 # Entry: $output - output hashref (modified
6288 # in place).
6289 # $code - method body source string.
6290 #
6291 # Exit: Returns nothing. Modifies $output
6292 # in place, setting _error_handling,
6293 # _error_return, and
6294 # _success_failure_pattern keys.
6295 #
6296 # Side effects: Logs detections to stdout when
6297 # verbose is set.
6298 # --------------------------------------------------
6299 813 ▶sub _detect_error_conventions {
Subroutine _detect_error_conventions: called CC=21 SCAR=30.5
6300 813 my ($self, $output, $code) = @_;
6301  
6302 813 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 813 813
6303  
6304 813 $self->_log(' DEBUG _detect_error_conventions called');
6305  
6306 813 my %error_patterns;
6307  
6308 # Pattern 1: return undef if/unless condition
6309 813 ▶ while ($code =~ /return\s+undef\s+(?:if|unless)\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+undef\s+(?:if|unless)\s+([^;]+);/g) 9 813 822
6310 9 push @{$error_patterns{undef_on_error}}, $1;
6311 9 $self->_log(" DEBUG Found 'return undef' pattern");
6312 }
6313  
6314 # Pattern 2: return if/unless (implicit undef)
6315 813 ▶ while ($code =~ /return\s+(?:if|unless)\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+(?:if|unless)\s+([^;]+);/g) 13 813 826
6316 13 push @{$error_patterns{implicit_undef}}, $1;
6317 13 $self->_log(" DEBUG Found implicit undef pattern");
6318 }
6319  
6320 # Pattern 3: return () - matches with or without conditions
6321 813 ▶ if ($code =~ /return\s*\(\s*\)\s*(?:if|unless|;)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s*\(\s*\)\s*(?:if|unless|;)/) 3 810 813
6322 3 $error_patterns{empty_list} = 1;
6323 3 $self->_log(" DEBUG Found empty list return");
6324 }
6325  
6326 # Pattern 4: return 0/1 pattern (indicates boolean with error handling)
6327 813 my $zero_returns = 0;
6328 813 my $one_returns = 0;
6329 # Match "return 0" or "return 1" followed by anything (condition or semicolon)
6330 813 ▶ while ($code =~ /return\s+(0|1)\s*(?:;|if|unless)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+(0|1)\s*(?:;|if|unless)/g) 129 813 942
6331 129 ▶ if ($1 eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '0') { } 69 60 129
6332 69 $zero_returns++;
6333 } else {
6334 60 $one_returns++;
6335 }
6336 }
6337  
6338 813 ▶ if ($zero_returns > 0 && $one_returns > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zero_returns > 0 and $one_returns > 0) 16 797 813
Condition100%3/3
$zero_returns > 0 and $one_returns > 0
!ll&&!rl&&r
7732416
MC/DC100%2/2
$zero_returns > 0 and $one_returns > 0
$zero_returns > 0 $one_returns > 0
Truth table100%3/3
A and B
A= $zero_returns > 0B= $one_returns > 0
ABresult
0X0
100
111
6339 16 $error_patterns{zero_on_error} = 1;
6340 16 $self->_log(" DEBUG Found 0/1 return pattern ($zero_returns zeros, $one_returns ones)");
6341 }
6342  
6343 # Pattern 5: Exception handling with eval
6344 813 ▶ if ($code =~ /eval\s*\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /eval\s*\{/) 31 782 813
6345 # Check if there's error handling after eval
6346 31 ▶ if ($code =~ /eval\s*\{.*?\}[^}]*(?:if\s*\(\s*\$\@|catch|return\s+undef)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /eval\s*\{.*?\}[^}]*(?:if\s*\(\s*\$\@|catch|return\s+undef)/s) 20 11 31
6347 20 $error_patterns{exception_handling} = 1;
6348 20 $self->_log(' DEBUG Found exception handling with eval');
6349 }
6350 }
6351  
6352 # Detect success/failure return pattern
6353 813 my @all_returns = $code =~ /return\s+([^;]+);/g;
6354 813 my $has_undef = grep { /^\s*undef\s*(?:if|unless|$)/ } @all_returns;
6355 813 ▶ my $has_value = grep { !/^\s*undef\s*$/ && !/^\s*$/ } @all_returns;
Condition66%2/3
/^\s*undef\s*$/ || /^\s*$/
l!l&&r!l&&!r
101002
MC/DC50%1/2
/^\s*undef\s*$/ || /^\s*$/
/^\s*undef\s*$/ /^\s*$/
Truth table66%2/3
A || B
A= /^\s*undef\s*$/B= /^\s*$/
ABresult
1X1
011
000
6356  
6357 813 ▶ if ($has_undef && $has_value && scalar(@all_returns) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_undef and $has_value and scalar @all_returns >= 2) 6 807 813
Condition66%4/6
$has_undef and $has_value and scalar @all_returns >= 2
!ll&&!rl&&r
80706
$has_undef and $has_value
!ll&&!rl&&r
80706
MC/DC33%1/3
$has_undef and $has_value and scalar @all_returns >= 2
$has_undef $has_value scalar @all_returns >= 2
Truth table50%2/4
A and B and C
A= $has_undefB= $has_valueC= scalar @all_returns >= 2
ABCresult
0XX0
10X0
1100
1111
6358 6 $output->{_success_failure_pattern} = 1;
6359 6 $self->_log(" OUTPUT: Uses success/failure return pattern");
6360 }
6361  
6362 # Store error conventions in output
6363 813 ▶ if(scalar(keys %error_patterns)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %error_patterns) { } 52 761 813
6364 52 $output->{_error_handling} = \%error_patterns;
6365  
6366 # Determine primary error convention
6367 52 ▶ if ($error_patterns{undef_on_error}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($error_patterns{'undef_on_error'}) { } 5 47 52
elsif ($error_patterns{'implicit_undef'}) { } 12 35 47
elsif ($error_patterns{'empty_list'}) { } 3 32 35
elsif ($error_patterns{'zero_on_error'}) { } 16 16 32
6368 5 $output->{_error_return} = 'undef';
6369 5 $self->_log(" OUTPUT: Returns undef on error");
6370 } elsif ($error_patterns{implicit_undef}) {
6371 12 $output->{_error_return} = 'undef';
6372 12 $self->_log(" OUTPUT: Returns implicit undef on error");
6373 } elsif ($error_patterns{empty_list}) {
6374 3 $output->{_error_return} = 'empty_list';
6375 3 $self->_log(" OUTPUT: Returns empty list on error");
6376 } elsif ($error_patterns{zero_on_error}) {
6377 16 $output->{_error_return} = 'false';
6378 16 $self->_log(" OUTPUT: Returns 0/false on error");
6379 }
6380  
6381 52 ▶ if ($error_patterns{exception_handling}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_patterns{"exception_handling"}) 20 32 52
6382 20 $self->_log(" OUTPUT: Has exception handling");
6383 }
6384 } else {
6385 761 delete $output->{_error_handling};
6386 }
6387 }
6388  
6389 # --------------------------------------------------
6390 # _infer_type_from_expression
6391 #
6392 # Purpose: Infer the data type of a return
6393 # expression string by matching it
6394 # against common Perl literal and
6395 # variable patterns.
6396 #
6397 # Entry: $expr - return expression string,
6398 # trimmed of leading and
6399 # trailing whitespace.
6400 # May be undef.
6401 #
6402 # Exit: Returns a type hashref of the form
6403 # { type => '...' } and optionally
6404 # { min => N }. Defaults to
6405 # { type => 'scalar' } when no
6406 # pattern matches.
6407 #
6408 # Side effects: None.
6409 # --------------------------------------------------
6410 28 ▶sub _infer_type_from_expression {
Subroutine _infer_type_from_expression: called CC=25 SCAR=32.3
6411 28 my ($self, $expr) = @_;
6412  
6413 28 ▶ return { type => 'scalar' } unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 1 27 28
6414  
6415 27 $expr =~ s/^\s+|\s+$//g;
6416  
6417 # Check for multiple comma-separated values (indicates array/list)
6418 27 ▶ if ($expr =~ /,/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /,/) 3 24 27
6419 3 require Text::Balanced;
6420 3 my $comma_count = 0;
6421 3 my $rest = $expr;
6422 3 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 35 3 38
6423 35 ▶ if (substr($rest, 0, 1) =~ /[(\[{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) =~ /[(\[{]/) 4 31 35
6424 4 my $extracted = Text::Balanced::extract_bracketed($rest, '(){}[]');
6425 4 ▶ last unless defined $extracted; # Unbalanced brackets
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 4 4
6426 4 next;
6427 }
6428 31 ▶ $comma_count++ if substr($rest, 0, 1) eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if substr($rest, 0, 1) eq "," 3 28 31
6429 31 $rest = substr($rest, 1);
6430 }
6431  
6432 3 ▶ if ($comma_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma_count > 0) 2 1 3
6433 2 return { type => 'array' };
6434 }
6435 }
6436  
6437 # Check for @ prefix (array)
6438 25 ▶ if ($expr =~ /^\@\w+/ || $expr =~ /^qw\(/ || $expr =~ /^\@\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\@\w+/ or $expr =~ /^qw\(/ or $expr =~ /^\@\{/) 7 18 25
Condition100%6/6
$expr =~ /^\@\w+/ or $expr =~ /^qw\(/ or $expr =~ /^\@\{/
l!l&&r!l&&!r
4318
$expr =~ /^\@\w+/ or $expr =~ /^qw\(/
l!l&&r!l&&!r
3121
MC/DC100%3/3
$expr =~ /^\@\w+/ or $expr =~ /^qw\(/ or $expr =~ /^\@\{/
$expr =~ /^\@\w+/ $expr =~ /^qw\(/ $expr =~ /^\@\{/
Truth table100%4/4
A or B or C
A= $expr =~ /^\@\w+/B= $expr =~ /^qw\(/C= $expr =~ /^\@\{/
ABCresult
1XX1
01X1
0011
0000
6439 7 return { type => 'array' };
6440 }
6441  
6442 # Check for scalar() function - returns count
6443 18 ▶ if ($expr =~ /scalar\s*\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /scalar\s*\(/) 2 16 18
6444 2 return { type => 'integer', min => 0 };
6445 }
6446  
6447 # Check for array reference
6448 16 ▶ if ($expr =~ /^\[/ || $expr =~ /^\\\@/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\[/ or $expr =~ /^\\\@/) 3 13 16
Condition100%3/3
$expr =~ /^\[/ or $expr =~ /^\\\@/
l!l&&r!l&&!r
1213
MC/DC100%2/2
$expr =~ /^\[/ or $expr =~ /^\\\@/
$expr =~ /^\[/ $expr =~ /^\\\@/
Truth table100%3/3
A or B
A= $expr =~ /^\[/B= $expr =~ /^\\\@/
ABresult
1X1
011
000
6449 3 return { type => 'arrayref' };
6450 }
6451  
6452 # Check for hash reference
6453 13 ▶ if ($expr =~ /^\{/ || $expr =~ /^\\\%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\{/ or $expr =~ /^\\\%/) 2 11 13
Condition100%3/3
$expr =~ /^\{/ or $expr =~ /^\\\%/
l!l&&r!l&&!r
1111
MC/DC100%2/2
$expr =~ /^\{/ or $expr =~ /^\\\%/
$expr =~ /^\{/ $expr =~ /^\\\%/
Truth table100%3/3
A or B
A= $expr =~ /^\{/B= $expr =~ /^\\\%/
ABresult
1X1
011
000
6454 2 return { type => 'hashref' };
6455 }
6456  
6457 # Check for hash
6458 11 ▶ if ($expr =~ /^\%\w+/ || $expr =~ /^\%\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\%\w+/ or $expr =~ /^\%\{/) 0 11 11
Condition33%1/3
$expr =~ /^\%\w+/ or $expr =~ /^\%\{/
l!l&&r!l&&!r
0011
MC/DC0%0/2
$expr =~ /^\%\w+/ or $expr =~ /^\%\{/
$expr =~ /^\%\w+/ $expr =~ /^\%\{/
Truth table33%1/3
A or B
A= $expr =~ /^\%\w+/B= $expr =~ /^\%\{/
ABresult
1X1
011
000
6459 0 return { type => 'hash' };
6460 }
6461  
6462 # Check for strings
6463 11 ▶ if ($expr =~ /^['"]/ || $expr =~ /['"]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^['"]/ or $expr =~ /['"]$/) 1 10 11
Condition66%2/3
$expr =~ /^['"]/ or $expr =~ /['"]$/
l!l&&r!l&&!r
1010
MC/DC50%1/2
$expr =~ /^['"]/ or $expr =~ /['"]$/
$expr =~ /^['"]/ $expr =~ /['"]$/
Truth table66%2/3
A or B
A= $expr =~ /^['"]/B= $expr =~ /['"]$/
ABresult
1X1
011
000
6464 1 return { type => 'string' };
6465 }
6466  
6467 # Check for booleans first — must come before the integer check
6468 # since /^-?\d+$/ would otherwise match 0 and 1 as integers
6469 10 ▶ if($expr =~ /^[01]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^[01]$/) 2 8 10
6470 2 return { type => 'boolean' };
6471 }
6472  
6473 # Check for integers
6474 8 ▶ if($expr =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^-?\d+$/) 2 6 8
6475 2 return { type => 'integer' };
6476 }
6477  
6478 6 ▶ if ($expr =~ /^-?\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^-?\d+\.\d+$/) 1 5 6
6479 1 return { type => 'number' };
6480 }
6481  
6482 # Check for objects
6483 5 ▶ if ($expr =~ /bless/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /bless/) 0 5 5
6484 0 return { type => 'object' };
6485 }
6486  
6487 5 ▶ if($expr =~ /\blength\s*\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\blength\s*\(/) 1 4 5
6488 1 return { type => 'integer', min => 0 };
6489 }
6490  
6491 # Default to scalar
6492 4 return { type => 'scalar' };
6493 }
6494  
6495 # --------------------------------------------------
6496 # _detect_chaining_from_pod
6497 #
6498 # Purpose: Check POD documentation for explicit
6499 # indications that a method is chainable
6500 # or part of a fluent interface.
6501 #
6502 # Entry: $output - output hashref (modified
6503 # in place).
6504 # $pod - POD string for the method.
6505 #
6506 # Exit: Returns nothing. Sets _returns_self
6507 # in $output if chaining keywords are
6508 # found.
6509 #
6510 # Side effects: Logs detection to stdout when
6511 # verbose is set.
6512 # --------------------------------------------------
6513 0 ▶sub _detect_chaining_from_pod {
Subroutine _detect_chaining_from_pod: not called CC=6 SCAR=37.4
6514 0 my ($self, $output, $pod) = @_;
6515 0 ▶ return unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 0 0 0
6516  
6517 # Look for explicit chaining documentation
6518 0 ▶ if ($pod =~ /returns?\s+(?:\$)?self\b/i ||
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /returns?\s+(?:\$)?self\b/i or $pod =~ /chainable/i or $pod =~ /fluent\s+interface/i or $pod =~ /method\s+chaining/i) 0 0 0
Condition0%0/9
$pod =~ /returns?\s+(?:\$)?self\b/i or $pod =~ /chainable/i or $pod =~ /fluent\s+interface/i or $pod =~ /method\s+chaining/i
l!l&&r!l&&!r
000
$pod =~ /returns?\s+(?:\$)?self\b/i or $pod =~ /chainable/i or $pod =~ /fluent\s+interface/i
l!l&&r!l&&!r
000
$pod =~ /returns?\s+(?:\$)?self\b/i or $pod =~ /chainable/i
l!l&&r!l&&!r
000
MC/DC0%0/4
$pod =~ /returns?\s+(?:\$)?self\b/i or $pod =~ /chainable/i or $pod =~ /fluent\s+interface/i or $pod =~ /method\s+chaining/i
$pod =~ /returns?\s+(?:\$)?self\b/i $pod =~ /chainable/i $pod =~ /fluent\s+interface/i $pod =~ /method\s+chaining/i
Truth table0%0/5
A or B or C or D
A= $pod =~ /returns?\s+(?:\$)?self\b/iB= $pod =~ /chainable/iC= $pod =~ /fluent\s+interface/iD= $pod =~ /method\s+chaining/i
ABCDresult
1XXX1
01XX1
001X1
00011
00000
6519 $pod =~ /chainable/i ||
6520 $pod =~ /fluent\s+interface/i ||
6521 $pod =~ /method\s+chaining/i) {
6522  
6523 0 $output->{_returns_self} = 1;
6524 0 $self->_log(" OUTPUT: POD indicates chainable/fluent interface");
6525 }
6526 }
6527  
6528 # --------------------------------------------------
6529 # _validate_output
6530 #
6531 # Purpose: Apply basic sanity checks to the
6532 # assembled output hashref and warn
6533 # about suspicious type combinations,
6534 # normalising clearly invalid types to
6535 # 'string'.
6536 #
6537 # Entry: $output - output hashref (modified
6538 # in place).
6539 #
6540 # Exit: Returns nothing. May modify type key
6541 # in $output. Logs warnings to stdout
6542 # when verbose is set.
6543 #
6544 # Side effects: None.
6545 # --------------------------------------------------
6546 792 ▶sub _validate_output {
Subroutine _validate_output: called CC=9 SCAR=22.2
6547 792 my ($self, $output) = @_;
6548  
6549 # Warn about suspicious combinations
6550 792 ▶ if (defined $output->{type} && $output->{type} eq 'boolean' && !defined($output->{value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output->{"type"} and $output->{"type"} eq "boolean" and not defined $output->{"value"}) 30 762 792
Condition100%6/6
defined $output->{"type"} and $output->{"type"} eq "boolean" and not defined $output->{"value"}
!ll&&!rl&&r
7293330
defined $output->{"type"} and $output->{"type"} eq "boolean"
!ll&&!rl&&r
572463
MC/DC100%3/3
defined $output->{"type"} and $output->{"type"} eq "boolean" and not defined $output->{"value"}
defined $output->{"type"} $output->{"type"} eq "boolean" not defined $output->{"value"}
Truth table100%4/4
A and B and not(C)
A= defined $output->{"type"}B= $output->{"type"} eq "boolean"C= not defined $output->{"value"}
ABCresult
0XX0
10X0
1100
1111
6551 30 $self->_log(' WARNING Boolean type without value - may want to set value: 1');
6552 }
6553 792 ▶ if ($output->{value} && defined $output->{type} && $output->{type} ne 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"value"} and defined $output->{"type"} and $output->{"type"} ne "boolean") 0 792 792
Condition66%4/6
$output->{"value"} and defined $output->{"type"} and $output->{"type"} ne "boolean"
!ll&&!rl&&r
759330
$output->{"value"} and defined $output->{"type"}
!ll&&!rl&&r
759033
MC/DC0%0/3
$output->{"value"} and defined $output->{"type"} and $output->{"type"} ne "boolean"
$output->{"value"} defined $output->{"type"} $output->{"type"} ne "boolean"
Truth table50%2/4
A and B and C
A= $output->{"value"}B= defined $output->{"type"}C= $output->{"type"} ne "boolean"
ABCresult
0XX0
10X0
1100
1111
6554 0 $self->_log(" WARNING Value set but type is not boolean: $output->{type}");
6555 }
6556 792 my %valid_types = map { $_ => 1 } qw(string integer number boolean array arrayref hashref object void);
6557 792 ▶ if(exists $output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $output->{"type"}) 787 5 792
6558 787 ▶ if(!$valid_types{$output->{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $valid_types{$output->{"type"}}) 118 669 787
6559 118 $self->_log(" WARNING Output value type is unknown: '$output->{type}', setting to string");
6560 118 $output->{type} = 'string';
6561 }
6562 }
6563 }
6564  
6565 # --------------------------------------------------
6566 # _prose_memberof
6567 #
6568 # Purpose: Recognise a documented list of allowed values written as
6569 # prose, such as "one of red, green or blue", "must be one
6570 # of C<low>, C<moderate> or C<high>", "valid values are 1,
6571 # 5 and 111" or "either C<host> or C<port>".
6572 # The list is documentation, not a check the routine was
6573 # seen to make, so callers mark the parameter
6574 # _source => 'pod': the harness uses the values as valid
6575 # data and generates no must-die case outside the set.
6576 # Entry: $text - a parameter description or constraint, possibly
6577 # containing POD formatting codes.
6578 # Exit: Arrayref of the documented values, or undef when the text
6579 # holds no recognisable list. Every value must be a single
6580 # token, so prose like "one of the four recognised keys set
6581 # to a true value also adds ..." is not mistaken for a list.
6582 # --------------------------------------------------
6583 812 ▶sub _prose_memberof {
Subroutine _prose_memberof: called CC=10 SCAR=23.0
6584 812 my ($self, $text) = @_;
6585  
6586 812 ▶ return undef unless defined($text) && length($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 37 775 812
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
037775
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
6587  
6588 775 my $plain = $text;
6589 775 $plain =~ s/\b[CLIB]<([^<>]*)>/$1/g; # drop POD formatting
6590 775 $plain =~ s/\bE<([^<>]*)>/$1/g;
6591 775 $plain =~ s/\A\s+//;
6592 775 $plain =~ s/\s+\z//;
6593  
6594 775 my $list;
6595 775 ▶ if($plain =~ /\b(?:one\s+of|valid\s+values?\s*(?:are|is)?\s*:?|allowed\s+values?\s*(?:are|is)?\s*:?|accepted\s+values?\s*(?:are|is)?\s*:?|permitted\s+values?\s*(?:are|is)?\s*:?)\s*(.+)$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($plain =~ /\b(?:one\s+of|valid\s+values?\s*(?:are|is)?\s*:?|allowed\s+values?\s*(?:are|is)?\s*:?|accepted\s+values?\s*(?:are|is)?\s*:?|permitted\s+values?\s*(?:are|is)?\s*:?)\s*(.+)$/i) { } 7 768 775
elsif ($plain =~ /\b(?:must\s+be\s+)?either\s+(.+)$/i) { } 1 767 768
elsif ($plain =~ /\b(?:also\s+)?accepts\s+the\s+strings?\s+(.+)$/i) { } 4 763 767
6596 7 $list = $1;
6597 } elsif($plain =~ /\b(?:must\s+be\s+)?either\s+(.+)$/i) {
6598 1 $list = $1;
6599 } elsif($plain =~ /\b(?:also\s+)?accepts\s+the\s+strings?\s+(.+)$/i) {
6600 # "Also accepts the strings off, warn, and fatal."
6601 4 $list = $1;
6602 } else {
6603 763 return undef;
6604 }
6605  
6606 12 $list =~ s/\.(?:\s|\z).*\z//s; # stop at the end of the sentence
6607 12 $list =~ s/\s*\.\s*\z//;
6608 12 $list =~ s/\s+\z//;
6609  
6610 12 my @values = grep { length } split(/\s*,\s*(?:(?:and|or)\s+)?|\s+(?:and|or)\s+/i, $list);
6611 12 ▶ return undef if(@values < 2 || @values > 20);
Branch: 1 branch
BranchTrueFalseTotal
if @values < 2 or @values > 20 3 9 12
Condition66%2/3
@values < 2 or @values > 20
l!l&&r!l&&!r
309
MC/DC50%1/2
@values < 2 or @values > 20
@values < 2 @values > 20
Truth table66%2/3
A or B
A= @values < 2B= @values > 20
ABresult
1X1
011
000
6612  
6613 9 for my $value (@values) {
6614 24 $value =~ s/\A\s+|\s+\z//g;
6615 24 $value =~ s/\A['"]|['"]\z//g;
6616 24 ▶ return undef unless $value =~ /\A[A-Za-z0-9_:.+-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[A-Za-z0-9_:.+-]+\z/ 1 23 24
6617 }
6618  
6619 8 return \@values;
6620 }
6621  
6622 # --------------------------------------------------
6623 # _parse_constraints
6624 #
6625 # Purpose: Parse a constraint string extracted
6626 # from POD documentation and populate
6627 # min, max, or other constraint fields
6628 # in a parameter hashref.
6629 #
6630 # Entry: $param - hashref for the parameter
6631 # being annotated (modified
6632 # in place).
6633 # $constraint - the constraint string,
6634 # e.g. '3-50', 'positive',
6635 # '>= 0', 'min 3'.
6636 #
6637 # Exit: Returns nothing. Modifies $param in
6638 # place by setting min and/or max keys.
6639 #
6640 # Side effects: Logs min/max values to stdout when
6641 # verbose is set, and marks the constraints as
6642 # documented (documentation only).
6643 # --------------------------------------------------
6644 30 ▶sub _parse_constraints {
Subroutine _parse_constraints: called CC=23 SCAR=34.6
6645 30 my ($self, $param, $constraint) = @_;
6646  
6647 # A documented list of values, e.g. "(one of red, green)"
6648 30 ▶ if(my $values = $self->_prose_memberof($constraint)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($constraint)) 0 30 30
6649 0 $param->{memberof} = $values;
6650 }
6651  
6652 # Range: "3-50" or "1-100 chars"
6653 30 ▶ if ($constraint =~ /(\d+)\s*-\s*(\d+)/) {
Branch: 7 branches
BranchTrueFalseTotal
if ($constraint =~ /(\d+)\s*-\s*(\d+)/) { } 6 24 30
elsif ($constraint =~ /(\d+)\s*\.\.\s*(\d+)/) { } 1 23 24
elsif ($constraint =~ /(?:min|minimum|at least)\s*(\d+)/i) { } 4 19 23
elsif ($constraint =~ /(?:max|maximum|up to)\s*(\d+)/i) { } 3 16 19
elsif ($constraint =~ /positive/i) { } 2 14 16
elsif ($constraint =~ /non-negative/i) { } 1 13 14
elsif ($constraint =~ /^(\S+)\s+(.+)$/) { } 8 5 13
6654 6 $param->{min} = $1;
6655 6 $param->{max} = $2;
6656 }
6657 elsif ($constraint =~ /(\d+)\s*\.\.\s*(\d+)/) {
6658 # Range: 0..19
6659 1 $param->{min} = $1;
6660 1 $param->{max} = $2;
6661 }
6662 # Minimum: "min 3" or "at least 5"
6663 elsif ($constraint =~ /(?:min|minimum|at least)\s*(\d+)/i) {
6664 4 $param->{min} = $1;
6665 }
6666 # Maximum: "max 50" or "up to 100"
6667 elsif ($constraint =~ /(?:max|maximum|up to)\s*(\d+)/i) {
6668 3 $param->{max} = $1;
6669 }
6670 # Positive
6671 elsif ($constraint =~ /positive/i) {
6672 2 ▶ $param->{min} = 1 if $param->{type} && $param->{type} eq 'integer';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"type"} and $param->{"type"} eq "integer" 1 1 2
Condition66%2/3
$param->{"type"} and $param->{"type"} eq "integer"
!ll&&!rl&&r
011
MC/DC50%1/2
$param->{"type"} and $param->{"type"} eq "integer"
$param->{"type"} $param->{"type"} eq "integer"
Truth table66%2/3
A and B
A= $param->{"type"}B= $param->{"type"} eq "integer"
ABresult
0X0
100
111
6673 2 ▶ $param->{min} = 0.01 if $param->{type} && $param->{type} eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"type"} and $param->{"type"} eq "number" 1 1 2
Condition66%2/3
$param->{"type"} and $param->{"type"} eq "number"
!ll&&!rl&&r
011
MC/DC50%1/2
$param->{"type"} and $param->{"type"} eq "number"
$param->{"type"} $param->{"type"} eq "number"
Truth table66%2/3
A and B
A= $param->{"type"}B= $param->{"type"} eq "number"
ABresult
0X0
100
111
6674 }
6675 # Non-negative
6676 elsif ($constraint =~ /non-negative/i) {
6677 1 $param->{min} = 0;
6678 } elsif($constraint =~ /^(\S+)\s+(.+)$/) {
6679 8 my ($op, $val) = ($1, $2);
6680 8 ▶ if(looks_like_number($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($val)) 0 8 8
6681 0 ▶ if ($op eq '<') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '<') { } 0 0 0
elsif ($op eq '<=') { } 0 0 0
elsif ($op eq '>') { } 0 0 0
elsif ($op eq '>=') { } 0 0 0
6682 0 $param->{max} = $val - 1;
6683 } elsif ($op eq '<=') {
6684 0 $param->{max} = $val;
6685 } elsif ($op eq '>') {
6686 0 $param->{min} = $val + 1;
6687 } elsif ($op eq '>=') {
6688 0 $param->{min} = $val;
6689 }
6690 }
6691 }
6692  
6693 30 ▶ if(defined($param->{max})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"max"}) 10 20 30
6694 10 $self->_log(" Set max to $param->{max}");
6695 }
6696 30 ▶ if(defined($param->{min})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"min"}) 14 16 30
6697 14 $self->_log(" Set min to $param->{min}");
6698 }
6699  
6700 # These constraints come from prose ("3-50 chars", "at least 5"): they
6701 # describe valid input rather than a check the code was seen to make, so
6702 # the harness uses them as data hints and expects no croak outside them.
6703 30 ▶ if(defined($param->{min}) || defined($param->{max}) || $param->{memberof}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"min"} or defined $param->{"max"} or $param->{"memberof"}) 17 13 30
Condition83%5/6
defined $param->{"min"} or defined $param->{"max"} or $param->{"memberof"}
l!l&&r!l&&!r
17013
defined $param->{"min"} or defined $param->{"max"}
l!l&&r!l&&!r
14313
MC/DC66%2/3
defined $param->{"min"} or defined $param->{"max"} or $param->{"memberof"}
defined $param->{"min"} defined $param->{"max"} $param->{"memberof"}
Truth table75%3/4
A or B or C
A= defined $param->{"min"}B= defined $param->{"max"}C= $param->{"memberof"}
ABCresult
1XX1
01X1
0011
0000
6704 17 $param->{_source} = SOURCE_POD;
6705 }
6706 }
6707  
6708 # --------------------------------------------------
6709 # _analyze_code
6710 #
6711 # Purpose: Analyse a method's source code using
6712 # pattern matching to infer parameter
6713 # names, types, constraints, defaults,
6714 # and optionality. Orchestrates all
6715 # per-parameter code analysis helpers.
6716 #
6717 # Entry: $code - method body source string.
6718 # $method - method hashref (used for
6719 # constructor-specific logic
6720 # when extracting parameters
6721 # from @_ patterns).
6722 #
6723 # Exit: Returns a hashref of parameter name
6724 # to parameter spec hashref, with as
6725 # much type and constraint information
6726 # as could be inferred from the code.
6727 #
6728 # Side effects: Logs progress and warnings to stdout
6729 # when verbose is set.
6730 #
6731 # Notes: Analysis is capped at max_parameters
6732 # to prevent runaway processing on
6733 # pathological methods. Falls back to
6734 # classic @_ extraction if signature
6735 # extraction found no parameters.
6736 # --------------------------------------------------
6737 819 ▶sub _analyze_code {
Subroutine _analyze_code: called CC=27 SCAR=38.0
6738 819 my ($self, $code, $method) = @_;
6739  
6740 # A comment is prose, not code. A signature shown in an example comment
6741 # (extract_all's # my ($password, %opts) = @_) must not become a parameter.
6742 819 $code = _strip_comments($code, $method->{node});
6743  
6744 819 my %params;
6745  
6746 # Safety check - limit parameter analysis to prevent runaway processing
6747 819 my $param_count = 0;
6748  
6749 # Extract parameter names from various signature styles
6750 819 $self->_extract_parameters_from_signature(\%params, $code);
6751  
6752 # Params::Get: get_params('key', \@_) passes the param name as a string,
6753 # not as a $var in the signature, so run this unconditionally as a second
6754 # pass after the early-returning signature parsers have finished.
6755 819 ▶ if($code =~ /Params::Get/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /Params::Get/) 17 802 819
6756 17 my $pos = scalar keys %params;
6757 17 ▶ while($code =~ /get_params\s*\(\s*['"](\w+)['"]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /get_params\s*\(\s*['"](\w+)['"]/g) 7 17 24
6758 7 my $name = $1;
6759 7 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 0 7 7
6760 7 ▶ $params{$name} //= { _source => SOURCE_CODE, position => $pos++ };
Condition50%1/2
$params{$name} //= {"_source", "code", "position", $pos++}
l!l
07
MC/DC0%0/1
$params{$name} //= {"_source", "code", "position", $pos++}
$params{$name}
Truth table50%1/2
A
A= $params{$name}
Aresult
11
00
6761 7 $self->_log(" CODE: Found Params::Get parameter '$name'");
6762 }
6763 }
6764  
6765 819 $self->_extract_defaults_from_code(\%params, $code, $method);
6766  
6767 # Infer types from defaults
6768 819 foreach my $param (keys %params) {
6769 550 ▶ if ($params{$param}{_default} && !$params{$param}{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$param}{"_default"} and not $params{$param}{"type"}) 33 517 550
Condition66%2/3
$params{$param}{"_default"} and not $params{$param}{"type"}
!ll&&!rl&&r
517033
MC/DC50%1/2
$params{$param}{"_default"} and not $params{$param}{"type"}
$params{$param}{"_default"} not $params{$param}{"type"}
Truth table66%2/3
A and not(B)
A= $params{$param}{"_default"}B= not $params{$param}{"type"}
ABresult
0X0
100
111
6770 33 my $default = $params{$param}{_default};
6771 33 ▶ if (ref($default) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $default eq 'HASH') { } 6 27 33
elsif (ref $default eq 'ARRAY') { } 2 25 27
6772 6 $params{$param}{type} = 'hashref';
6773 6 $self->_log(" CODE: $param type inferred as hashref from default");
6774 } elsif (ref($default) eq 'ARRAY') {
6775 2 $params{$param}{type} = 'arrayref';
6776 2 $self->_log(" CODE: $param type inferred as arrayref from default");
6777 }
6778 }
6779 }
6780  
6781 819 ▶ if($code =~ /(?:croak|die)\(.*\)\s+if\s*\(\s*scalar\(\@_\)\s*<\s*(\d+)\s*\)/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ /(?:croak|die)\(.*\)\s+if\s*\(\s*scalar\(\@_\)\s*<\s*(\d+)\s*\)/s) { } 0 819 819
elsif ($code =~ /(?:croak|die)\(.*\)\s+if\s*\(\s*scalar\(\@_\)\s*==\s*0\s*\)/s) { } 0 819 819
6782 0 my $required_count = $1;
6783 0 my @param_names = sort { $params{$a}{position} <=> $params{$b}{position} } keys %params;
6784 0 for my $i (0 .. $required_count-1) {
6785 0 $params{$param_names[$i]}{optional} = 0;
6786 0 $self->_log(" CODE: $param_names[$i] marked required due to croak scalar check");
6787 }
6788 } elsif ($code =~ /(?:croak|die)\(.*\)\s+if\s*\(\s*scalar\(\@_\)\s*==\s*0\s*\)/s) {
6789 0 foreach my $param (keys %params) {
6790 0 $params{$param}{optional} = 0;
6791 0 $self->_log(" CODE: $param: all parameters are required due to 'scalar(@_) == 0' check");
6792 }
6793 }
6794  
6795 # Analyze each parameter (with safety limit)
6796 819 foreach my $param (keys %params) {
6797 550 ▶ if ($param_count++ > $self->{max_parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_count++ > $self->{"max_parameters"}) 0 550 550
6798 0 $self->_log(" WARNING: Max parameters ($self->{max_parameters}) exceeded, skipping remaining");
6799 0 last;
6800 }
6801  
6802 550 my $p = \$params{$param};
6803  
6804 550 $self->_analyze_parameter_type($p, $param, $code);
6805 550 $self->_analyze_parameter_constraints($p, $param, $code);
6806 550 $self->_analyze_parameter_validation($p, $param, $code);
6807 550 $self->_analyze_advanced_types($p, $param, $code);
6808  
6809 # Defined checks
6810 550 ▶ if ($code =~ /defined\s*\(\s*\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /defined\s*\(\s*\$$param\s*\)/) 43 507 550
6811 43 $$p->{optional} = 0;
6812 43 $self->_log(" CODE: $param is required (defined check)");
6813 }
6814  
6815 # Determine optional/required and numeric type from code
6816 550 ▶ if ($code =~ /\s*\$$param\s*(?:\/\/|\|\|)=/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ m[\s*\$$param\s*(?://|\|\|)=]) { } 21 529 550
elsif ($code =~ m[\s*\$$param\s*(?!->)(?:[\+\-\*\%]|/(?!/)|(?:\+\+)|(?:--)|(?:[\+\-\*\%]=|/(?!/)=)|\+\$|\$[+-])]) { } 49 480 529
elsif ($code =~ /\$\b$param\b\s*(?:\+0|\*1)/) { } 0 480 480
6817 # e.g. $var //= 5; or $var ||= 5;
6818 21 $$p->{optional} = 1;
6819 21 $self->_log(" CODE: $param is optional (default value assigned in code)");
6820 } elsif ($code =~ /\s*\$$param\s*(?!->)(?:[\+\-\*\%]|\/(?!\/)|(?:\+\+)|(?:--)|(?:[\+\-\*\%]=|\/(?!\/)=)|\+\$|\$[+-])/ ) {
6821 # Covers arithmetic usage:
6822 # $x + $param, $param++, $param--, $x += $param, $x -= $param, etc.
6823 49 $$p->{optional} = 0;
6824 49 ▶ $$p->{type} //= 'number';
Condition100%2/2
$$p->{"type"} //= "number"
l!l
481
MC/DC100%1/1
$$p->{"type"} //= "number"
$$p->{"type"}
Truth table100%2/2
A
A= $$p->{"type"}
Aresult
11
00
6825 49 $self->_log(" CODE: $param is required (used in arithmetic context)");
6826 } elsif ($code =~ /\$\b$param\b\s*(?:\+0|\*1)/) {
6827 # Forces numeric context, e.g., "$param + 0" or "$param * 1"
6828 0 $$p->{optional} = 0;
6829 0 ▶ $$p->{type} //= 'number';
Condition0%0/2
$$p->{"type"} //= "number"
l!l
00
MC/DC0%0/1
$$p->{"type"} //= "number"
$$p->{"type"}
Truth table0%0/2
A
A= $$p->{"type"}
Aresult
11
00
6830 0 $self->_log(" CODE: $param is required (numeric context)");
6831 }
6832  
6833 # Required parameter checks (undef causes error)
6834  
6835 # Style 1: block form
6836 550 ▶ if ($code =~ /if\s*\(\s*!\s*defined\s*\(\s*\$$param\s*\)\s*\)\s*\{([^}]+)\}/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /if\s*\(\s*!\s*defined\s*\(\s*\$$param\s*\)\s*\)\s*\{([^}]+)\}/s) 0 550 550
6837 0 my $block = $1;
6838 0 ▶ if ($block =~ /\b(croak|die|confess)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /\b(croak|die|confess)\b/) 0 0 0
6839 0 $$p->{optional} = 0;
6840 0 $self->_log(" CODE: $param is required (undef causes error)");
6841 }
6842 }
6843  
6844 # Style 2: postfix unless
6845 550 ▶ if ($code =~ /\b(croak|die|confess)\b[^;]*\bunless\s+defined\s*\(\s*\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\b(croak|die|confess)\b[^;]*\bunless\s+defined\s*\(\s*\$$param\s*\)/) 19 531 550
6846 19 $$p->{optional} = 0;
6847 19 $self->_log(" CODE: $param is required (postfix undef check)");
6848 }
6849  
6850 # Exists checks for hash keys
6851 550 ▶ if ($code =~ /exists\s*\(\s*\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /exists\s*\(\s*\$$param\s*\)/) 0 550 550
6852 0 $$p->{type} = 'hashkey';
6853 0 $self->_log(" CODE: $param is a hash key");
6854 }
6855  
6856 # Scalar context for arrays
6857 550 ▶ if ($code =~ /scalar\s*\(\s*\@?\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /scalar\s*\(\s*\@?\$$param\s*\)/) 1 549 550
6858 1 $$p->{type} = 'array';
6859 1 $self->_log(" CODE: $param used in scalar context (array)");
6860 }
6861  
6862 550 $self->_extract_error_constraints($p, $param, $code);
6863 }
6864  
6865 819 $self->_sanitize_parameter_names(\%params, 'code analysis');
6866 819 return \%params;
6867 }
6868  
6869 # --------------------------------------------------
6870 # _analyze_parameter_type
6871 #
6872 # Purpose: Infer the type of a single parameter
6873 # from ref() checks, isa() calls,
6874 # bless patterns, array/hash operations,
6875 # and numeric operator usage in the
6876 # method body.
6877 #
6878 # Entry: $p_ref - reference to the parameter
6879 # hashref (modified in place
6880 # via the referenced hash).
6881 # $param - parameter name string.
6882 # $code - method body source string.
6883 #
6884 # Exit: Returns nothing. Modifies the
6885 # referenced parameter hashref.
6886 #
6887 # Side effects: Logs detections to stdout when
6888 # verbose is set.
6889 # --------------------------------------------------
6890 550 ▶sub _analyze_parameter_type {
Subroutine _analyze_parameter_type: called CC=25 SCAR=32.4
6891 550 my ($self, $p_ref, $param, $code) = @_;
6892 550 my $p = $$p_ref;
6893  
6894 # Type inference from ref() checks
6895 550 ▶ if ($code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"](ARRAY|HASH|SCALAR)['"]/gi) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"](ARRAY|HASH|SCALAR)['"]/gi) { } 41 509 550
elsif ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]([^'"]+)['"]\s*\)/i) { } 3 506 509
elsif ($code =~ /bless\s+.*\$$param/) { } 6 500 506
6896 41 my $reftype = lc($1);
6897 41 ▶ $p->{type} = $reftype eq 'array' ? 'arrayref' :
Branch: 2 branches
BranchTrueFalseTotal
$reftype eq 'array' ? : 5 36 41
$reftype eq 'hash' ? : 36 0 36
6898 $reftype eq 'hash' ? 'hashref' :
6899 'scalar';
6900 41 $self->_log(" CODE: $param is $p->{type} (ref check)");
6901 }
6902 # ISA checks for objects
6903 elsif ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]([^'"]+)['"]\s*\)/i) {
6904 3 $p->{type} = 'object';
6905 3 $p->{isa} = $1;
6906 3 $self->_log(" CODE: $param is object of class $1");
6907 }
6908 # Blessed references
6909 elsif ($code =~ /bless\s+.*\$$param/) {
6910 6 $p->{type} = 'object';
6911 6 $self->_log(" CODE: $param is blessed object");
6912 }
6913 # Array/hash operations
6914 550 ▶ if (!$p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 495 55 550
6915 495 ▶ if ($code =~ /\@\{\s*\$$param\s*\}/ || $code =~ /push\s*\(\s*\@?\$$param/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ /\@\{\s*\$$param\s*\}/ or $code =~ /push\s*\(\s*\@?\$$param/) { } 5 490 495
elsif ($code =~ /\%\{\s*\$$param\s*\}/ or $code =~ /\$$param\s*->\s*\{/) { } 29 461 490
Condition66%2/3
$code =~ /\@\{\s*\$$param\s*\}/ or $code =~ /push\s*\(\s*\@?\$$param/
l!l&&r!l&&!r
50490
MC/DC50%1/2
$code =~ /\@\{\s*\$$param\s*\}/ or $code =~ /push\s*\(\s*\@?\$$param/
$code =~ /\@\{\s*\$$param\s*\}/ $code =~ /push\s*\(\s*\@?\$$param/
Truth table66%2/3
A or B
A= $code =~ /\@\{\s*\$$param\s*\}/B= $code =~ /push\s*\(\s*\@?\$$param/
ABresult
1X1
011
000
6916 5 $p->{type} = 'arrayref';
6917 ▶ } elsif ($code =~ /\%\{\s*\$$param\s*\}/ || $code =~ /\$$param\s*->\s*\{/) {
Condition100%3/3
$code =~ /\%\{\s*\$$param\s*\}/ or $code =~ /\$$param\s*->\s*\{/
l!l&&r!l&&!r
128461
MC/DC100%2/2
$code =~ /\%\{\s*\$$param\s*\}/ or $code =~ /\$$param\s*->\s*\{/
$code =~ /\%\{\s*\$$param\s*\}/ $code =~ /\$$param\s*->\s*\{/
Truth table100%3/3
A or B
A= $code =~ /\%\{\s*\$$param\s*\}/B= $code =~ /\$$param\s*->\s*\{/
ABresult
1X1
011
000
6918 29 $p->{type} = 'hashref';
6919 }
6920 }
6921  
6922 # An argument used only as `$param->method` is an object the caller
6923 # supplies (a controller, a cache, a database handle). The harness
6924 # cannot build one, so it is an object rather than a guessed scalar.
6925 # `$param->{...}` and `$param->[...]` are hash and array access, which
6926 # the check above has already typed.
6927 550 ▶ if (!$p->{type} && $code =~ /\$$param\s*->\s*[A-Za-z_]\w*/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->{"type"} and $code =~ /\$$param\s*->\s*[A-Za-z_]\w*/) 32 518 550
Condition100%3/3
not $p->{"type"} and $code =~ /\$$param\s*->\s*[A-Za-z_]\w*/
!ll&&!rl&&r
8942932
MC/DC100%2/2
not $p->{"type"} and $code =~ /\$$param\s*->\s*[A-Za-z_]\w*/
not $p->{"type"} $code =~ /\$$param\s*->\s*[A-Za-z_]\w*/
Truth table100%3/3
not(A) and B
A= not $p->{"type"}B= $code =~ /\$$param\s*->\s*[A-Za-z_]\w*/
ABresult
0X0
100
111
6928 32 $p->{type} = 'object';
6929 # A method call on undef is an error, so the argument cannot be
6930 # omitted unless the body guards the call (a truthiness test in
6931 # _analyze_parameter_validation makes it optional again).
6932 32 $p->{optional} = 0;
6933 32 $self->_log(" CODE: $param is an object (called as a method holder)");
6934 }
6935  
6936 # Infer type from the default value if type is unknown
6937 550 ▶ if (!$p->{type} && exists $p->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->{"type"} and exists $p->{"_default"}) 32 518 550
Condition100%3/3
not $p->{"type"} and exists $p->{"_default"}
!ll&&!rl&&r
12139732
MC/DC100%2/2
not $p->{"type"} and exists $p->{"_default"}
not $p->{"type"} exists $p->{"_default"}
Truth table100%3/3
not(A) and B
A= not $p->{"type"}B= exists $p->{"_default"}
ABresult
0X0
100
111
6938 32 my $default = $p->{_default};
6939 32 ▶ if (ref($default) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $default eq 'HASH') { } 0 32 32
elsif (ref $default eq 'ARRAY') { } 0 32 32
6940 0 $p->{type} = 'hashref';
6941 0 $self->_log(" CODE: $param type inferred as hashref from default");
6942 } elsif (ref($default) eq 'ARRAY') {
6943 0 $p->{type} = 'arrayref';
6944 0 $self->_log(" CODE: $param type inferred as arrayref from default");
6945 }
6946 }
6947  
6948 # ------------------------------------------------------------
6949 # Heuristic numeric inference (low confidence)
6950 # ------------------------------------------------------------
6951 550 ▶ if (!$p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->{"type"}) 429 121 550
6952 # A '-' or '%' in 'Period-over-period' or "%04d" is text, not
6953 # arithmetic, so the operators are looked for outside the literals.
6954 429 my $ops_code = _strip_literals($code);
6955 # An explicit looks_like_number($param) check is a direct
6956 # numeric-type assertion by the author, stronger evidence than
6957 # incidental arithmetic adjacency (e.g. $param is only ever
6958 # used inside a defined-or default before the arithmetic, so
6959 # the arithmetic-operator check below never sees $param itself
6960 # next to an operator).
6961 429 ▶ if ($code =~ /\blooks_like_number\s*\(\s*\$$param\s*\)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ /\blooks_like_number\s*\(\s*\$$param\s*\)/) { } 13 416 429
elsif ($ops_code =~ m[\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))] or $ops_code =~ m[(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))\s*\$$param] or $ops_code =~ /\bint\s*\(\s*\$$param\s*\)/ or $ops_code =~ /\babs\s*\(\s*\$$param\s*\)/) { } 66 350 416
elsif ($ops_code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ or $ops_code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/) { } 29 321 350
6962 13 $p->{type} = 'number';
6963 13 $p->{_type_confidence} = 'heuristic';
6964 13 $self->_log(" CODE: $param inferred as number (looks_like_number check)");
6965 }
6966 # Numeric operators: + - * / % **
6967 # Use \/(?!\/) to exclude // (defined-or) from matching as division.
6968 # `$c->req` is a method call, not $c minus something, so '-' before
6969 # '>' does not count as an operator.
6970 ▶ elsif (
Condition88%8/9
$ops_code =~ m[\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))] or $ops_code =~ m[(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))\s*\$$param] or $ops_code =~ /\bint\s*\(\s*\$$param\s*\)/ or $ops_code =~ /\babs\s*\(\s*\$$param\s*\)/
l!l&&r!l&&!r
660350
$ops_code =~ m[\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))] or $ops_code =~ m[(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))\s*\$$param] or $ops_code =~ /\bint\s*\(\s*\$$param\s*\)/
l!l&&r!l&&!r
642350
$ops_code =~ m[\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))] or $ops_code =~ m[(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))\s*\$$param]
l!l&&r!l&&!r
3628352
MC/DC75%3/4
$ops_code =~ m[\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))] or $ops_code =~ m[(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))\s*\$$param] or $ops_code =~ /\bint\s*\(\s*\$$param\s*\)/ or $ops_code =~ /\babs\s*\(\s*\$$param\s*\)/
$ops_code =~ m[\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))] $ops_code =~ m[(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))\s*\$$param] $ops_code =~ /\bint\s*\(\s*\$$param\s*\)/ $ops_code =~ /\babs\s*\(\s*\$$param\s*\)/
Truth table80%4/5
A or B or C or D
A= $ops_code =~ m[\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))]B= $ops_code =~ m[(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!/)/(?!/))\s*\$$param]C= $ops_code =~ /\bint\s*\(\s*\$$param\s*\)/D= $ops_code =~ /\babs\s*\(\s*\$$param\s*\)/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
6971 $ops_code =~ /\$$param\s*(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!\/)\/(?!\/))/ ||
6972 $ops_code =~ /(?:[\+\*\%]|-(?!>|[A-Za-z]\b)|(?<!\/)\/(?!\/))\s*\$$param/ ||
6973 $ops_code =~ /\bint\s*\(\s*\$$param\s*\)/ ||
6974 $ops_code =~ /\babs\s*\(\s*\$$param\s*\)/
6975 ) {
6976 66 $p->{type} = 'number';
6977 66 $p->{_type_confidence} = 'heuristic';
6978 66 $self->_log(" CODE: $param inferred as number (numeric operator)");
6979 }
6980 # Numeric comparison
6981 ▶ elsif (
Condition100%3/3
$ops_code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ or $ops_code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
l!l&&r!l&&!r
218321
MC/DC100%2/2
$ops_code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ or $ops_code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
$ops_code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ $ops_code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
Truth table100%3/3
A or B
A= $ops_code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/B= $ops_code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
ABresult
1X1
011
000
6982 $ops_code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ ||
6983 $ops_code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
6984 ) {
6985 29 $p->{type} = 'number';
6986 29 $p->{_type_confidence} = 'heuristic';
6987 29 $self->_log(" CODE: $param inferred as number (numeric comparison)");
6988 }
6989 }
6990 }
6991  
6992 # --------------------------------------------------
6993 # _analyze_advanced_types
6994 #
6995 # Purpose: Apply enhanced type detection to a
6996 # single parameter, checking for
6997 # DateTime objects, file handles,
6998 # coderefs, and enum-like constraints
6999 # beyond what basic type inference
7000 # can determine.
7001 #
7002 # Entry: $p_ref - reference to the parameter
7003 # hashref (modified in place
7004 # via the referenced hash).
7005 # $param - the parameter name string.
7006 # $code - method body source string.
7007 #
7008 # Exit: Returns nothing. Modifies the
7009 # referenced parameter hashref in place.
7010 #
7011 # Side effects: Logs detections to stdout when
7012 # verbose is set.
7013 #
7014 # Notes: Delegates to four specialised
7015 # detectors: _detect_datetime_type,
7016 # _detect_filehandle_type,
7017 # _detect_coderef_type, and
7018 # _detect_enum_type. Each detector
7019 # returns early on first match so
7020 # detectors are implicitly prioritised
7021 # in that order.
7022 # --------------------------------------------------
7023 551 ▶sub _analyze_advanced_types {
Subroutine _analyze_advanced_types: called CC=1 SCAR=0.0
7024 551 my ($self, $p_ref, $param, $code) = @_;
7025  
7026 # Dereference once to get the hash reference
7027 551 my $p = $$p_ref;
7028  
7029 # Now pass the dereferenced hash to the detection methods
7030 551 $self->_detect_datetime_type($p, $param, $code);
7031 551 $self->_detect_filehandle_type($p, $param, $code);
7032 551 $self->_detect_coderef_type($p, $param, $code);
7033 551 $self->_detect_enum_type($p, $param, $code);
7034 }
7035  
7036 # --------------------------------------------------
7037 # _detect_datetime_type
7038 #
7039 # Purpose: Detect DateTime objects, Time::Piece
7040 # objects, date strings, ISO 8601
7041 # strings, and UNIX timestamps by
7042 # analysing code patterns involving
7043 # the parameter.
7044 #
7045 # Entry: $p - parameter hashref (modified
7046 # in place).
7047 # $param - parameter name string.
7048 # $code - method body source string.
7049 #
7050 # Exit: Returns nothing. Modifies $p in place,
7051 # setting type, isa, semantic, min,
7052 # matches, and/or format keys.
7053 # Returns immediately on first match.
7054 #
7055 # Side effects: Logs detections to stdout when
7056 # verbose is set.
7057 # --------------------------------------------------
7058 550 ▶sub _detect_datetime_type {
Subroutine _detect_datetime_type: called CC=15 SCAR=29.3
7059 550 my ($self, $p, $param, $code) = @_;
7060  
7061 # Validate param is just a simple word
7062 550 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 550 550
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00550
MC/DC0%0/2
defined $param and $param =~ /^\w+$/
defined $param $param =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $paramB= $param =~ /^\w+$/
ABresult
0X0
100
111
7063  
7064 # DateTime object detection via isa/UNIVERSAL checks
7065 550 ▶ if ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]DateTime['"]\s*\)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]DateTime['"]\s*\)/i) 1 549 550
7066 1 $p->{type} = 'object';
7067 1 $p->{isa} = 'DateTime';
7068 1 $p->{semantic} = 'datetime_object';
7069 1 $self->_log(" ADVANCED: $param is DateTime object");
7070 1 return;
7071 }
7072  
7073 # Check for DateTime method calls
7074 549 ▶ if ($code =~ /\$$param\s*->\s*(ymd|dmy|mdy|hms|iso8601|epoch|strftime)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*->\s*(ymd|dmy|mdy|hms|iso8601|epoch|strftime)/) 1 548 549
7075 1 $p->{type} = 'object';
7076 1 $p->{isa} = 'DateTime';
7077 1 $p->{semantic} = 'datetime_object';
7078 1 $self->_log(" ADVANCED: $param uses DateTime methods");
7079 1 return;
7080 }
7081  
7082 # Time::Piece detection
7083 548 ▶ if ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]Time::Piece['"]\s*\)/i ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]Time::Piece['"]\s*\)/i or $code =~ /\$$param\s*->\s*(strftime|epoch|year|mon|mday)/) 0 548 548
Condition33%1/3
$code =~ /\$$param\s*->\s*isa\s*\(\s*['"]Time::Piece['"]\s*\)/i or $code =~ /\$$param\s*->\s*(strftime|epoch|year|mon|mday)/
l!l&&r!l&&!r
00548
MC/DC0%0/2
$code =~ /\$$param\s*->\s*isa\s*\(\s*['"]Time::Piece['"]\s*\)/i or $code =~ /\$$param\s*->\s*(strftime|epoch|year|mon|mday)/
$code =~ /\$$param\s*->\s*isa\s*\(\s*['"]Time::Piece['"]\s*\)/i $code =~ /\$$param\s*->\s*(strftime|epoch|year|mon|mday)/
Truth table33%1/3
A or B
A= $code =~ /\$$param\s*->\s*isa\s*\(\s*['"]Time::Piece['"]\s*\)/iB= $code =~ /\$$param\s*->\s*(strftime|epoch|year|mon|mday)/
ABresult
1X1
011
000
7084 $code =~ /\$$param\s*->\s*(strftime|epoch|year|mon|mday)/) {
7085 0 $p->{type} = 'object';
7086 0 $p->{isa} = 'Time::Piece';
7087 0 $p->{semantic} = 'timepiece_object';
7088 0 $self->_log(" ADVANCED: $param is Time::Piece object");
7089 0 return;
7090 }
7091  
7092 # ISO 8601 date pattern. Checked before the looser date-string match
7093 # below, which would otherwise claim a full datetime first.
7094 548 ▶ if ($code =~ /\$$param\s*=~\s*\/([^\/]+)\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[\$$param\s*=~\s*/([^/]+)/]) 28 520 548
7095 28 my $regex = $1;
7096 28 ▶ if ($self->_looks_like_iso8601_regex($regex)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_looks_like_iso8601_regex($regex)) 1 27 28
7097 1 $p->{type} = 'string';
7098 1 $p->{semantic} = 'iso8601_string';
7099 1 $p->{matches} = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z?$/';
7100 1 $self->_log(" ADVANCED: $param validated as ISO 8601 datetime");
7101 1 return;
7102 }
7103 }
7104  
7105 # String date/time patterns via regex matching
7106 547 ▶ if ($code =~ /\$$param\s*=~\s*\/.*?\\d\{4\}.*?\\d\{2\}.*?\\d\{2\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[\$$param\s*=~\s*/.*?\\d\{4\}.*?\\d\{2\}.*?\\d\{2\}]) 1 546 547
7107 1 $p->{type} = 'string';
7108 1 $p->{semantic} = 'date_string';
7109 1 $p->{format} = 'YYYY-MM-DD or similar';
7110 1 $self->_log(" ADVANCED: $param validated as date string pattern");
7111 1 return;
7112 }
7113  
7114 # UNIX timestamp detection (numeric with specific range)
7115 546 ▶ if ($code =~ /\$$param\s*>\s*\d{9,}/ || # UNIX timestamps are 10+ digits
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*>\s*\d{9,}/ or $code =~ /time\(\s*\)\s*-\s*\$$param/ or $code =~ /\$$param\s*-\s*time\(\s*\)/) 1 545 546
Condition66%4/6
$code =~ /\$$param\s*>\s*\d{9,}/ or $code =~ /time\(\s*\)\s*-\s*\$$param/ or $code =~ /\$$param\s*-\s*time\(\s*\)/
l!l&&r!l&&!r
10545
$code =~ /\$$param\s*>\s*\d{9,}/ or $code =~ /time\(\s*\)\s*-\s*\$$param/
l!l&&r!l&&!r
10545
MC/DC33%1/3
$code =~ /\$$param\s*>\s*\d{9,}/ or $code =~ /time\(\s*\)\s*-\s*\$$param/ or $code =~ /\$$param\s*-\s*time\(\s*\)/
$code =~ /\$$param\s*>\s*\d{9,}/ $code =~ /time\(\s*\)\s*-\s*\$$param/ $code =~ /\$$param\s*-\s*time\(\s*\)/
Truth table50%2/4
A or B or C
A= $code =~ /\$$param\s*>\s*\d{9,}/B= $code =~ /time\(\s*\)\s*-\s*\$$param/C= $code =~ /\$$param\s*-\s*time\(\s*\)/
ABCresult
1XX1
01X1
0011
0000
7116 $code =~ /time\(\s*\)\s*-\s*\$$param/ ||
7117 $code =~ /\$$param\s*-\s*time\(\s*\)/) {
7118 1 $p->{type} = 'integer';
7119 1 $p->{semantic} = 'unix_timestamp';
7120 1 $p->{min} = 0;
7121 1 $self->_log(" ADVANCED: $param appears to be UNIX timestamp");
7122 1 return;
7123 }
7124  
7125 # Date parsing with strptime or similar
7126 545 ▶ if ($code =~ /strptime\s*\(\s*\$$param/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /strptime\s*\(\s*\$$param/ or $code =~ /DateTime::Format::\w+\s*->\s*parse_datetime\s*\(\s*\$$param/) 0 545 545
Condition33%1/3
$code =~ /strptime\s*\(\s*\$$param/ or $code =~ /DateTime::Format::\w+\s*->\s*parse_datetime\s*\(\s*\$$param/
l!l&&r!l&&!r
00545
MC/DC0%0/2
$code =~ /strptime\s*\(\s*\$$param/ or $code =~ /DateTime::Format::\w+\s*->\s*parse_datetime\s*\(\s*\$$param/
$code =~ /strptime\s*\(\s*\$$param/ $code =~ /DateTime::Format::\w+\s*->\s*parse_datetime\s*\(\s*\$$param/
Truth table33%1/3
A or B
A= $code =~ /strptime\s*\(\s*\$$param/B= $code =~ /DateTime::Format::\w+\s*->\s*parse_datetime\s*\(\s*\$$param/
ABresult
1X1
011
000
7127 $code =~ /DateTime::Format::\w+\s*->\s*parse_datetime\s*\(\s*\$$param/) {
7128 0 $p->{type} = 'string';
7129 0 $p->{semantic} = 'datetime_parseable';
7130 0 $self->_log(" ADVANCED: $param is parsed as datetime");
7131 0 return;
7132 }
7133 }
7134  
7135 # --------------------------------------------------
7136 # _looks_like_iso8601_regex
7137 #
7138 # Purpose: Decide whether a regex body names an
7139 # ISO 8601 datetime rather than merely
7140 # containing the letters t and z, which
7141 # a timezone alternation such as
7142 # /\A(?:utc|gmt|z)\z/i also does.
7143 #
7144 # Entry: $regex - regex body without its
7145 # delimiters.
7146 #
7147 # Exit: Returns true when the body spells out
7148 # a year, month and day before the time
7149 # separator.
7150 # --------------------------------------------------
7151 28 ▶sub _looks_like_iso8601_regex {
Subroutine _looks_like_iso8601_regex: called CC=5 SCAR=16.1
7152 28 my ($self, $regex) = @_;
7153  
7154 28 ▶ return 0 unless defined $regex && $regex =~ /[Tt]/ && $regex =~ /[Zz]/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regex and $regex =~ /[Tt]/ and $regex =~ /[Zz]/ 26 2 28
Condition83%5/6
defined $regex and $regex =~ /[Tt]/ and $regex =~ /[Zz]/
!ll&&!rl&&r
2422
defined $regex and $regex =~ /[Tt]/
!ll&&!rl&&r
0244
MC/DC66%2/3
defined $regex and $regex =~ /[Tt]/ and $regex =~ /[Zz]/
defined $regex $regex =~ /[Tt]/ $regex =~ /[Zz]/
Truth table75%3/4
A and B and C
A= defined $regexB= $regex =~ /[Tt]/C= $regex =~ /[Zz]/
ABCresult
0XX0
10X0
1100
1111
7155  
7156 2 my $digit = qr/(?:\\d|\[0-9\])/;
7157 2 my $year = qr/(?:$digit\s*\{\s*4\s*\}|$digit$digit$digit$digit)/;
7158 2 my $short = qr/(?:$digit\s*\{\s*[12]\s*\}|$digit$digit)/;
7159  
7160 2 ▶ return $regex =~ /$year[^A-Za-z]{0,3}$short[^A-Za-z]{0,3}$short/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$regex =~ /${year}[^A-Za-z]{0,3}${short}[^A-Za-z]{0,3}$short/ ? : 1 1 2
7161 }
7162  
7163 # --------------------------------------------------
7164 # _detect_filehandle_type
7165 #
7166 # Purpose: Detect file handle parameters and
7167 # file path string parameters by
7168 # analysing I/O operations, file test
7169 # operators, and path manipulation
7170 # patterns involving the parameter.
7171 #
7172 # Entry: $p - parameter hashref (modified
7173 # in place).
7174 # $param - parameter name string.
7175 # $code - method body source string.
7176 #
7177 # Exit: Returns nothing. Modifies $p in place,
7178 # setting type, isa, and semantic keys.
7179 # Returns immediately on first match.
7180 #
7181 # Side effects: Logs detections to stdout when
7182 # verbose is set.
7183 # --------------------------------------------------
7184 550 ▶sub _detect_filehandle_type {
Subroutine _detect_filehandle_type: called CC=13 SCAR=30.5
7185 550 my ($self, $p, $param, $code) = @_;
7186  
7187 550 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 550 550
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00550
MC/DC0%0/2
defined $param and $param =~ /^\w+$/
defined $param $param =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $paramB= $param =~ /^\w+$/
ABresult
0X0
100
111
7188  
7189 # File handle operations. The builtin has to be a name of its own: an
7190 # underscore before it means a call such as _json_read($path), whose
7191 # argument is a path, not a handle.
7192 550 ▶ if ($code =~ /(?<![\w:])(?:open|close|read|print|say|sysread|syswrite)\s*\(?\s*\$$param/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?<![\w:])(?:open|close|read|print|say|sysread|syswrite)\s*\(?\s*\$$param/) 1 549 550
7193 1 $p->{type} = 'object';
7194 1 $p->{isa} = 'IO::Handle';
7195 1 $p->{semantic} = 'filehandle';
7196 1 $self->_log(" ADVANCED: $param is a file handle");
7197 1 return;
7198 }
7199  
7200 # Filehandle-specific operations
7201 549 ▶ if ($code =~ /\$$param\s*->\s*(readline|getline|print|say|close|flush|autoflush)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*->\s*(readline|getline|print|say|close|flush|autoflush)/) 1 548 549
7202 1 $p->{type} = 'object';
7203 1 $p->{isa} = 'IO::Handle';
7204 1 $p->{semantic} = 'filehandle';
7205 1 $self->_log(" ADVANCED: $param uses filehandle methods");
7206 1 return;
7207 }
7208  
7209 # File test operators. A -d test is a directory requirement: the value
7210 # has to name an existing directory, which no spelling pattern can
7211 # capture, so it gets its own semantic rather than a filepath pattern.
7212 548 ▶ if ($code =~ /(?:-[frwxoOeszlpSbctugkTBMAC])\s+\$$param/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:-[frwxoOeszlpSbctugkTBMAC])\s+\$$param/) 2 546 548
7213 2 $p->{type} = 'string';
7214 2 ▶ if ($code =~ /-d\s+\$$param/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /-d\s+\$$param/) { } 1 1 2
7215 1 $p->{semantic} = 'directory';
7216 1 $self->_log(" ADVANCED: $param is tested as a directory");
7217 } else {
7218 1 $p->{semantic} = 'filepath';
7219 1 $self->_log(" ADVANCED: $param is tested as file path");
7220 }
7221 2 return;
7222 }
7223  
7224 # File::Spec operations or path manipulation
7225 546 ▶ if ($code =~ /File::(?:Spec|Basename)::\w+\s*\(\s*\$$param/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /File::(?:Spec|Basename)::\w+\s*\(\s*\$$param/ or $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/) 0 546 546
Condition33%1/3
$code =~ /File::(?:Spec|Basename)::\w+\s*\(\s*\$$param/ or $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/
l!l&&r!l&&!r
00546
MC/DC0%0/2
$code =~ /File::(?:Spec|Basename)::\w+\s*\(\s*\$$param/ or $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/
$code =~ /File::(?:Spec|Basename)::\w+\s*\(\s*\$$param/ $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/
Truth table33%1/3
A or B
A= $code =~ /File::(?:Spec|Basename)::\w+\s*\(\s*\$$param/B= $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/
ABresult
1X1
011
000
7226 $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/) {
7227 0 $p->{type} = 'string';
7228 0 $p->{semantic} = 'filepath';
7229 0 $self->_log(" ADVANCED: $param manipulated as file path");
7230 0 return;
7231 }
7232  
7233 # Path validation patterns
7234 # Only match a literal path assigned or defaulted to this variable
7235 546 ▶ if(defined $p->{_default} && $p->{_default} =~ m{^([A-Za-z]:\\|/|\./|\.\./)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"_default"} and $p->{"_default"} =~ m[^([A-Za-z]:\\|/|\./|\.\./)]) 0 546 546
Condition66%2/3
defined $p->{"_default"} and $p->{"_default"} =~ m[^([A-Za-z]:\\|/|\./|\.\./)]
!ll&&!rl&&r
501450
MC/DC0%0/2
defined $p->{"_default"} and $p->{"_default"} =~ m[^([A-Za-z]:\\|/|\./|\.\./)]
defined $p->{"_default"} $p->{"_default"} =~ m[^([A-Za-z]:\\|/|\./|\.\./)]
Truth table66%2/3
A and B
A= defined $p->{"_default"}B= $p->{"_default"} =~ m[^([A-Za-z]:\\|/|\./|\.\./)]
ABresult
0X0
100
111
7236 0 $p->{type} = 'string';
7237 0 $p->{semantic} = 'filepath';
7238 0 $self->_log(" ADVANCED: $param default looks like a path");
7239 0 return;
7240 }
7241  
7242 # IO::File detection
7243 546 ▶ if ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]IO::File['"]\s*\)/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]IO::File['"]\s*\)/ or $code =~ /IO::File\s*->\s*new\s*\(\s*\$$param/) 0 546 546
Condition33%1/3
$code =~ /\$$param\s*->\s*isa\s*\(\s*['"]IO::File['"]\s*\)/ or $code =~ /IO::File\s*->\s*new\s*\(\s*\$$param/
l!l&&r!l&&!r
00546
MC/DC0%0/2
$code =~ /\$$param\s*->\s*isa\s*\(\s*['"]IO::File['"]\s*\)/ or $code =~ /IO::File\s*->\s*new\s*\(\s*\$$param/
$code =~ /\$$param\s*->\s*isa\s*\(\s*['"]IO::File['"]\s*\)/ $code =~ /IO::File\s*->\s*new\s*\(\s*\$$param/
Truth table33%1/3
A or B
A= $code =~ /\$$param\s*->\s*isa\s*\(\s*['"]IO::File['"]\s*\)/B= $code =~ /IO::File\s*->\s*new\s*\(\s*\$$param/
ABresult
1X1
011
000
7244 $code =~ /IO::File\s*->\s*new\s*\(\s*\$$param/) {
7245 0 $p->{type} = 'object';
7246 0 $p->{isa} = 'IO::File';
7247 0 $p->{semantic} = 'filehandle';
7248 0 $self->_log(" ADVANCED: $param is IO::File object");
7249 0 return;
7250 }
7251 }
7252  
7253 # --------------------------------------------------
7254 # _detect_coderef_type
7255 #
7256 # Purpose: Detect coderef and callback parameters
7257 # by analysing ref() checks, invocation
7258 # patterns, and parameter naming
7259 # conventions.
7260 #
7261 # Entry: $p - parameter hashref (modified
7262 # in place).
7263 # $param - parameter name string.
7264 # $code - method body source string.
7265 #
7266 # Exit: Returns nothing. Modifies $p in place,
7267 # setting type and semantic keys.
7268 # Returns immediately on first match.
7269 #
7270 # Side effects: Logs detections to stdout when
7271 # verbose is set.
7272 # --------------------------------------------------
7273 550 ▶sub _detect_coderef_type {
Subroutine _detect_coderef_type: called CC=11 SCAR=26.0
7274 550 my ($self, $p, $param, $code) = @_;
7275  
7276 550 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 550 550
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00550
MC/DC0%0/2
defined $param and $param =~ /^\w+$/
defined $param $param =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $paramB= $param =~ /^\w+$/
ABresult
0X0
100
111
7277  
7278 # ref() check for CODE, for a plain variable or a named argument
7279 550 ▶ if ($code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i or $code =~ /ref\s*\(\s*\$args\{\s*['"]?${param}['"]?\s*\}\s*\)\s*eq\s*['"]CODE['"]/i) 1 549 550
Condition66%2/3
$code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i or $code =~ /ref\s*\(\s*\$args\{\s*['"]?${param}['"]?\s*\}\s*\)\s*eq\s*['"]CODE['"]/i
l!l&&r!l&&!r
10549
MC/DC50%1/2
$code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i or $code =~ /ref\s*\(\s*\$args\{\s*['"]?${param}['"]?\s*\}\s*\)\s*eq\s*['"]CODE['"]/i
$code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i $code =~ /ref\s*\(\s*\$args\{\s*['"]?${param}['"]?\s*\}\s*\)\s*eq\s*['"]CODE['"]/i
Truth table66%2/3
A or B
A= $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/iB= $code =~ /ref\s*\(\s*\$args\{\s*['"]?${param}['"]?\s*\}\s*\)\s*eq\s*['"]CODE['"]/i
ABresult
1X1
011
000
7280 || $code =~ /ref\s*\(\s*\$args\{\s*['"]?$param['"]?\s*\}\s*\)\s*eq\s*['"]CODE['"]/i) {
7281 1 $p->{type} = 'coderef';
7282 1 $p->{semantic} = 'callback';
7283 1 $self->_log(" ADVANCED: $param is coderef (ref check)");
7284 1 return;
7285 }
7286  
7287 # Invocation as coderef - note the escaped @ in \@_
7288 549 ▶ if ($code =~ /\$$param\s*->\s*\(/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*->\s*\(/ or $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/ or $code =~ /&\s*\{\s*\$$param\s*\}/) 2 547 549
Condition66%4/6
$code =~ /\$$param\s*->\s*\(/ or $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/ or $code =~ /&\s*\{\s*\$$param\s*\}/
l!l&&r!l&&!r
20547
$code =~ /\$$param\s*->\s*\(/ or $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/
l!l&&r!l&&!r
20547
MC/DC33%1/3
$code =~ /\$$param\s*->\s*\(/ or $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/ or $code =~ /&\s*\{\s*\$$param\s*\}/
$code =~ /\$$param\s*->\s*\(/ $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/ $code =~ /&\s*\{\s*\$$param\s*\}/
Truth table50%2/4
A or B or C
A= $code =~ /\$$param\s*->\s*\(/B= $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/C= $code =~ /&\s*\{\s*\$$param\s*\}/
ABCresult
1XX1
01X1
0011
0000
7289 $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/ ||
7290 $code =~ /&\s*\{\s*\$$param\s*\}/) {
7291 2 $p->{type} = 'coderef';
7292 2 $p->{semantic} = 'callback';
7293 2 $self->_log(" ADVANCED: $param invoked as coderef");
7294 2 return;
7295 }
7296  
7297 # Parameter name suggests callback
7298 547 ▶ if ($param =~ /^(?:callback|cb|handler|sub|code|fn|func|on_\w+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(?:callback|cb|handler|sub|code|fn|func|on_\w+)$/i) 3 544 547
7299 3 $p->{type} = 'coderef';
7300 3 $p->{semantic} = 'callback';
7301 3 $self->_log(" ADVANCED: $param name suggests coderef");
7302 3 return;
7303 }
7304  
7305 # Blessed coderef (unusual but valid)
7306 544 ▶ if ($code =~ /blessed\s*\(\s*\$$param\s*\)/ &&
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /blessed\s*\(\s*\$$param\s*\)/ and $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i) 0 544 544
Condition66%2/3
$code =~ /blessed\s*\(\s*\$$param\s*\)/ and $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i
!ll&&!rl&&r
54130
MC/DC0%0/2
$code =~ /blessed\s*\(\s*\$$param\s*\)/ and $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i
$code =~ /blessed\s*\(\s*\$$param\s*\)/ $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i
Truth table66%2/3
A and B
A= $code =~ /blessed\s*\(\s*\$$param\s*\)/B= $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i
ABresult
0X0
100
111
7307 $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i) {
7308 0 $p->{type} = 'object';
7309 0 $p->{isa} = 'blessed_coderef';
7310 0 $p->{semantic} = 'callback';
7311 0 $self->_log(" ADVANCED: $param is blessed coderef");
7312 0 return;
7313 }
7314 }
7315  
7316 # --------------------------------------------------
7317 # _detect_enum_type
7318 #
7319 # Purpose: Detect enum-like parameters whose
7320 # valid values are a fixed set, by
7321 # analysing validation patterns
7322 # including regex alternations, hash
7323 # lookups, grep checks, given/when,
7324 # if/elsif chains, and smart match.
7325 #
7326 # Entry: $p - parameter hashref (modified
7327 # in place).
7328 # $param - parameter name string.
7329 # $code - method body source string.
7330 #
7331 # Exit: Returns nothing. Modifies $p in place,
7332 # setting type, enum, and semantic keys.
7333 # Returns immediately on first match.
7334 #
7335 # Side effects: Logs detections to stdout when
7336 # verbose is set.
7337 #
7338 # Notes: Requires at least 3 if/elsif branches
7339 # for pattern 5 to avoid false positives
7340 # from ordinary conditional code.
7341 # --------------------------------------------------
7342 550 ▶sub _detect_enum_type {
Subroutine _detect_enum_type: called CC=24 SCAR=36.1
7343 550 my ($self, $p, $param, $code) = @_;
7344  
7345 550 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 550 550
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00550
MC/DC0%0/2
defined $param and $param =~ /^\w+$/
defined $param $param =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $paramB= $param =~ /^\w+$/
ABresult
0X0
100
111
7346  
7347 # Pattern 1: die/croak unless value is in list
7348 # die 'Invalid status' unless $status =~ /^(active|inactive|pending)$/;
7349 550 ▶ if ($code =~ /unless\s+\$$param\s*=~\s*\/\^?\(([^)]+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[unless\s+\$$param\s*=~\s*/\^?\(([^)]+)\)]) 6 544 550
7350 6 my $values = $1;
7351 6 my @enum_values = split(/\|/, $values);
7352 6 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 2 4 6
7353 6 $p->{enum} = \@enum_values;
7354 6 $p->{semantic} = 'enum';
7355 6 $self->_log(" ADVANCED: $param is enum with values: " . join(', ', @enum_values));
7356 6 return;
7357 }
7358  
7359 # Pattern 2: Hash lookup for validation
7360 # my %valid = map { $_ => 1 } qw(red green blue);
7361 # die unless $valid{$param};
7362 544 ▶ if ($code =~ /\%(\w+)\s*=.*?qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\%(\w+)\s*=.*?qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) 1 543 544
7363 1 my $hash_name = $1;
7364 1 my $values_str = $2;
7365 1 ▶ if (defined $values_str && $code =~ /\$$hash_name\s*\{\s*\$$param\s*\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values_str and $code =~ /\$$hash_name\s*\{\s*\$$param\s*\}/) 1 0 1
Condition33%1/3
defined $values_str and $code =~ /\$$hash_name\s*\{\s*\$$param\s*\}/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $values_str and $code =~ /\$$hash_name\s*\{\s*\$$param\s*\}/
defined $values_str $code =~ /\$$hash_name\s*\{\s*\$$param\s*\}/
Truth table33%1/3
A and B
A= defined $values_strB= $code =~ /\$$hash_name\s*\{\s*\$$param\s*\}/
ABresult
0X0
100
111
7366 1 my @enum_values = split(/\s+/, $values_str);
7367 1 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 1 0 1
7368 1 $p->{enum} = \@enum_values;
7369 1 $p->{semantic} = 'enum';
7370 1 $self->_log(" ADVANCED: $param validated via hash lookup: " . join(', ', @enum_values));
7371 1 return;
7372 }
7373 }
7374  
7375 # Pattern 3: Array grep validation
7376 # die unless grep { $_ eq $param } qw(foo bar baz);
7377 543 ▶ if ($code =~ /grep\s*\{[^}]*\$$param[^}]*\}\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /grep\s*\{[^}]*\$${param}[^}]*\}\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) 1 542 543
7378 1 my $values_str = $1;
7379 1 my @enum_values = split(/\s+/, $values_str);
7380 1 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 1 0 1
7381 1 $p->{enum} = \@enum_values;
7382 1 $p->{semantic} = 'enum';
7383 1 $self->_log(" ADVANCED: $param validated via grep: " . join(', ', @enum_values));
7384 1 return;
7385 }
7386  
7387 # Pattern 4: Given/when (Perl 5.10+)
7388 542 ▶ if ($code =~ /given\s*\(\s*\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /given\s*\(\s*\$$param\s*\)/) 1 541 542
7389 1 my @enum_values;
7390 1 ▶ while ($code =~ /when\s*\(\s*['"]([^'"]+)['"]\s*\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /when\s*\(\s*['"]([^'"]+)['"]\s*\)/g) 2 1 3
7391 2 push @enum_values, $1;
7392 }
7393 1 ▶ if (@enum_values >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@enum_values >= 2) 1 0 1
7394 1 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 1 0 1
7395 1 $p->{enum} = \@enum_values;
7396 1 $p->{semantic} = 'enum';
7397 1 $self->_log(" ADVANCED: $param has enum values from given/when: " .
7398 join(', ', @enum_values));
7399 1 return;
7400 }
7401 }
7402  
7403 # Pattern 5: Multiple if/elsif checking specific values
7404 541 my @if_values;
7405 541 ▶ while ($code =~ /if\s*\(\s*\$$param\s*eq\s*['"]([^'"]+)['"]\s*\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /if\s*\(\s*\$$param\s*eq\s*['"]([^'"]+)['"]\s*\)/g) 7 541 548
7406 7 push @if_values, $1;
7407 }
7408 541 ▶ while ($code =~ /elsif\s*\(\s*\$$param\s*eq\s*['"]([^'"]+)['"]\s*\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /elsif\s*\(\s*\$$param\s*eq\s*['"]([^'"]+)['"]\s*\)/g) 5 541 546
7409 5 push @if_values, $1;
7410 }
7411 541 ▶ if (@if_values >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@if_values >= 3) 2 539 541
7412 2 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 2 0 2
7413 2 $p->{enum} = \@if_values;
7414 2 $p->{semantic} = 'enum';
7415 2 $self->_log(" ADVANCED: $param appears to be enum from if/elsif: " .
7416 join(', ', @if_values));
7417 2 return;
7418 }
7419  
7420 # Pattern 6: Smart match (~~) with array
7421 539 ▶ if ($code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ or $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) 0 539 539
Condition33%1/3
$code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ or $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/
l!l&&r!l&&!r
00539
MC/DC0%0/2
$code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ or $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/
$code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/
Truth table33%1/3
A or B
A= $code =~ /\$$param\s*~~\s*\[([^\]]+)\]/B= $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/
ABresult
1X1
011
000
7422 $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) {
7423 0 my $values_str = $1;
7424 0 my @enum_values;
7425 0 ▶ if ($values_str =~ /['"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($values_str =~ /['"]/) { } 0 0 0
7426 0 @enum_values = $values_str =~ /['"](.*?)['"]/g;
7427 } else {
7428 0 @enum_values = split(/\s+/, $values_str);
7429 }
7430 0 ▶ if (@enum_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@enum_values) 0 0 0
7431 0 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 0 0 0
7432 0 $p->{enum} = \@enum_values;
7433 0 $p->{semantic} = 'enum';
7434 0 $self->_log(" ADVANCED: $param validated with smart match: " .
7435 join(', ', @enum_values));
7436 0 return;
7437 }
7438 }
7439 }
7440  
7441 # --------------------------------------------------
7442 # _extract_error_constraints
7443 #
7444 # Purpose: Extract invalid-value constraints and
7445 # error messages from die/croak patterns
7446 # referencing a specific parameter, and
7447 # infer numeric bounds from comparisons
7448 # with literals.
7449 #
7450 # Entry: $p_ref - reference to the parameter
7451 # hashref (modified in place).
7452 # $param - parameter name string.
7453 # $code - method body source string.
7454 #
7455 # Exit: Returns nothing. May add _invalid,
7456 # _errors, min, and/or max to the
7457 # referenced parameter hashref.
7458 #
7459 # Side effects: Logs detections to stdout when
7460 # verbose is set.
7461 # --------------------------------------------------
7462 550 ▶sub _extract_error_constraints {
Subroutine _extract_error_constraints: called CC=17 SCAR=28.4
7463 550 my ($self, $p, $param, $code) = @_;
7464  
7465 # Look for die/croak/confess with a condition involving this param
7466 550 ▶ while ($code =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ / (?:die|croak|confess) # error call \s* (?: ["']([^"']+)["'] # captured error message | q[qw]?\s*[\(\[]([^)\]]+)[\)\]] # q(), qq(), qw() )? \s* if\s+ (.+?) # condition \s*; /gsx) 64 550 614
7467 (?:die|croak|confess) # error call
7468 \s*
7469 (?:
7470 ["']([^"']+)["'] # captured error message
7471 |
7472 q[qw]?\s*[\(\[]([^)\]]+)[\)\]] # q(), qq(), qw()
7473 )?
7474 \s*
7475 if\s+
7476 (.+?) # condition
7477 \s*;
7478 /gsx) {
7479  
7480 64 ▶ my $message = $1 || $2;
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
53011
MC/DC50%1/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
7481 64 my $condition = $3;
7482  
7483 # Only keep conditions that reference this parameter
7484 64 ▶ next unless $condition =~ /\$$param\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $condition =~ /\$$param\b/ 25 39 64
7485  
7486 # Initialize storage
7487 39 ▶ $$p->{_invalid} ||= [];
Condition100%2/2
$$p->{"_invalid"} ||= []
l!l
138
MC/DC100%1/1
$$p->{"_invalid"} ||= []
$$p->{"_invalid"}
Truth table100%2/2
A
A= $$p->{"_invalid"}
Aresult
11
00
7488 39 ▶ $$p->{_errors} ||= [];
Condition100%2/2
$$p->{"_errors"} ||= []
l!l
138
MC/DC100%1/1
$$p->{"_errors"} ||= []
$$p->{"_errors"}
Truth table100%2/2
A
A= $$p->{"_errors"}
Aresult
11
00
7489  
7490 # Normalize condition (strip surrounding parens)
7491 39 $condition =~ s/^\(|\)$//g;
7492 39 $condition =~ s/\s+/ /g;
7493  
7494 # Try to extract a meaningful invalid constraint
7495 39 my $constraint;
7496  
7497 # Examples:
7498 # $age <= 0
7499 # $x eq ''
7500 # length($s) < 3
7501 39 ▶ if ($condition =~ /\$$param\s*([!<>=]=?|eq|ne|lt|gt|le|ge)\s*(.+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($condition =~ /\$$param\s*([!<>=]=?|eq|ne|lt|gt|le|ge)\s*(.+)/) { } 7 32 39
elsif ($condition =~ /length\s*\(\s*\$$param\s*\)\s*([<>=!]+)\s*(\d+)/) { } 4 28 32
elsif ($condition =~ /\$$param\s*==\s*0/) { } 0 28 28
7502 7 $constraint = "$1 $2";
7503 }
7504 elsif ($condition =~ /length\s*\(\s*\$$param\s*\)\s*([<>=!]+)\s*(\d+)/) {
7505 4 $constraint = "length $1 $2";
7506 }
7507 elsif ($condition =~ /\$$param\s*==\s*0/) {
7508 0 $constraint = '== 0';
7509 }
7510  
7511 # Store results
7512 39 ▶ push @{ $$p->{_invalid} }, $constraint if $constraint;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint 11 28 39
7513 39 ▶ push @{ $$p->{_errors} }, $message if defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if defined $message 34 5 39
7514  
7515 39 ▶ $self->_log(
Branch: 1 branch
BranchTrueFalseTotal
defined $message ? : 34 5 39
7516 " ERROR: $param invalid when [$condition]" .
7517 (defined $message ? " => '$message'" : '')
7518 );
7519 }
7520  
7521 # Numeric comparison with literal
7522 550 ▶ if ($code =~ /\b\Q$param\E\s*(<=|<|>=|>)\s*(-?\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\b\Q$param\E\s*(<=|<|>=|>)\s*(-?\d+)/) 21 529 550
7523 21 my ($op, $num) = ($1, $2);
7524  
7525 # Mark required
7526 21 $$p->{optional} = 0;
7527  
7528 21 ▶ if ($op eq '<=') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '<=') { } 1 20 21
elsif ($op eq '<') { } 5 15 20
elsif ($op eq '>=') { } 2 13 15
elsif ($op eq '>') { } 13 0 13
7529 1 $$p->{min} = $num + 1;
7530 } elsif ($op eq '<') {
7531 5 $$p->{min} = $num;
7532 } elsif ($op eq '>=') {
7533 2 $$p->{max} = $num - 1;
7534 } elsif ($op eq '>') {
7535 13 $$p->{max} = $num;
7536 }
7537  
7538 21 $self->_log(" ERROR: $param normalized constraint from '$op $num'");
7539 }
7540 }
7541  
7542 # --------------------------------------------------
7543 # _extract_parameters_from_signature
7544 #
7545 # Purpose: Extract parameter names and positions
7546 # from a method's signature, trying
7547 # modern Perl subroutine signatures
7548 # first and falling back to traditional
7549 # @_ extraction styles.
7550 #
7551 # Entry: $params - hashref to populate with
7552 # parameter specs (modified
7553 # in place).
7554 # $code - method body source string.
7555 #
7556 # Exit: Returns nothing. Populates $params.
7557 #
7558 # Side effects: Logs detections to stdout when
7559 # verbose is set.
7560 #
7561 # Notes: Three traditional styles are
7562 # supported: (1) my ($self, ...) = @_,
7563 # (2) my $self = shift; my $x = shift,
7564 # (3) my $x = $_[N]. $self and $class
7565 # are always excluded from the returned
7566 # parameters.
7567 # --------------------------------------------------
7568 1632 ▶sub _extract_parameters_from_signature {
Subroutine _extract_parameters_from_signature: called CC=24 SCAR=35.7
7569 1632 my ($self, $params, $code) = @_;
7570  
7571 # Modern Style: Subroutine signatures with attributes
7572 # Handle multi-line signatures
7573 # sub foo :attr1 :attr2(val) (
7574 # $self,
7575 # $x :Type,
7576 # $y = default
7577 # ) { }
7578  
7579 # Try to match signature after attributes
7580 # Look for the parameter list - it's the last (...) before the opening brace
7581 # that contains sigils ($, %, @)
7582 1632 ▶ if ($code =~ /sub\s+\w+\s*(?::\w+(?:\([^)]*\))?\s*)*\(((?:[^()]|\([^)]*\))*)\)\s*\{/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /sub\s+\w+\s*(?::\w+(?:\([^)]*\))?\s*)*\(((?:[^()]|\([^)]*\))*)\)\s*\{/s) 6 1626 1632
7583 6 my $potential_sig = $1;
7584  
7585 # Check if this looks like parameters (has sigils)
7586 6 ▶ if ($potential_sig =~ /[\$\%\@]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($potential_sig =~ /[\$\%\@]/) 6 0 6
7587 6 $self->_log(" SIG: Found modern signature: ($potential_sig)");
7588 6 $self->_parse_modern_signature($params, $potential_sig);
7589 6 return;
7590 }
7591 }
7592  
7593 # Direct-index style: my $self = $_[0]; my $arg = $_[1]; ...
7594 # Must be checked before Style 1 to avoid matching @_ inside closures
7595 # defined in the body of a method that uses this style.
7596 1626 ▶ if($code =~ /my\s+\$(?:self|class)\s*=\s*\$_\[0\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(?:self|class)\s*=\s*\$_\[0\]/) 138 1488 1626
7597 138 my $pos = 0;
7598 138 ▶ while($code =~ /my\s+\$(\w+)\s*=\s*\$_\[(\d+)\]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*\$_\[(\d+)\]/g) 140 138 278
7599 140 my $name = $1;
7600 140 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 138 2 140
7601 2 ▶ $params->{$name} //= { _source => SOURCE_CODE, optional => 1, position => $pos++ };
Condition50%1/2
$params->{$name} //= {"_source", "code", "optional", 1, "position", $pos++}
l!l
02
MC/DC0%0/1
$params->{$name} //= {"_source", "code", "optional", 1, "position", $pos++}
$params->{$name}
Truth table50%1/2
A
A= $params->{$name}
Aresult
11
00
7602 2 $self->_log(" CODE: Found direct-index parameter '\$$name' at \$_[$2]");
7603 }
7604 138 return;
7605 }
7606  
7607 # Traditional Style 1: my ($self, $arg1, $arg2) = @_;
7608 1488 ▶ if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) { } 882 606 1488
elsif ($code =~ /my\s+\$self\s*=\s*shift/) { } 60 546 606
7609 882 my $sig = $1;
7610 882 my $pos = 0;
7611  
7612 882 ▶ while ($sig =~ /\$(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig =~ /\$(\w+)/g) 1678 882 2560
7613 1678 my $name = $1;
7614  
7615 1678 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 760 918 1678
7616  
7617 918 ▶ $params->{$name} //= {
Condition50%1/2
$params->{$name} //= {"_source", "code", "optional", 1}
l!l
0918
MC/DC0%0/1
$params->{$name} //= {"_source", "code", "optional", 1}
$params->{$name}
Truth table50%1/2
A
A= $params->{$name}
Aresult
11
00
7618 _source => SOURCE_CODE,
7619 optional => 1,
7620 };
7621  
7622 918 ▶ $params->{$name}{position} = $pos unless exists $params->{$name}{position};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$name}{"position"} 918 0 918
7623  
7624 918 $pos++;
7625 }
7626 882 return;
7627 } elsif ($code =~ /my\s+\$self\s*=\s*shift/) {
7628 # Traditional Style 2: my $self = shift; my $arg1 = shift;
7629 60 my @shifts;
7630 60 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*shift/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*shift/g) 70 60 130
7631 70 push @shifts, $1;
7632 }
7633 60 ▶ shift @shifts if @shifts && $shifts[0] =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if @shifts and $shifts[0] =~ /^(self|class|pkg|proto|klass)$/i 60 0 60
Condition33%1/3
@shifts and $shifts[0] =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
0060
MC/DC0%0/2
@shifts and $shifts[0] =~ /^(self|class|pkg|proto|klass)$/i
@shifts $shifts[0] =~ /^(self|class|pkg|proto|klass)$/i
Truth table33%1/3
A and B
A= @shiftsB= $shifts[0] =~ /^(self|class|pkg|proto|klass)$/i
ABresult
0X0
100
111
7634 60 my $pos = 0;
7635 60 foreach my $param (@shifts) {
7636 10 ▶ $params->{$param} ||= { _source => SOURCE_CODE, optional => 1, position => $pos++ };
Condition50%1/2
$params->{$param} ||= {"_source", "code", "optional", 1, "position", $pos++}
l!l
010
MC/DC0%0/1
$params->{$param} ||= {"_source", "code", "optional", 1, "position", $pos++}
$params->{$param}
Truth table50%1/2
A
A= $params->{$param}
Aresult
11
00
7637 }
7638 60 return;
7639 }
7640  
7641 # Traditional Style 3: Function parameters (no $self)
7642 546 ▶ if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) 0 546 546
7643 0 my $sig = $1;
7644 0 my @param_names = $sig =~ /\$(\w+)/g;
7645 0 my $pos = 0;
7646 0 foreach my $param (@param_names) {
7647 0 ▶ next if $param =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $param =~ /^(self|class|pkg|proto|klass)$/i 0 0 0
7648 0 ▶ $params->{$param} ||= { _source => SOURCE_CODE, optional => 1, position => $pos++ };
Condition0%0/2
$params->{$param} ||= {"_source", "code", "optional", 1, "position", $pos++}
l!l
00
MC/DC0%0/1
$params->{$param} ||= {"_source", "code", "optional", 1, "position", $pos++}
$params->{$param}
Truth table0%0/2
A
A= $params->{$param}
Aresult
11
00
7649 }
7650 }
7651  
7652 # De-duplicate
7653 546 my %seen;
7654 546 foreach my $param (keys %$params) {
7655 0 ▶ if ($seen{$param}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$param}++) 0 0 0
7656 0 $self->_log(" WARNING: Duplicate parameter '$param' found");
7657 }
7658 }
7659 }
7660  
7661 # --------------------------------------------------
7662 # _parse_modern_signature
7663 #
7664 # Purpose: Parse a Perl 5.20+ subroutine
7665 # signature string into individual
7666 # parameter specs, respecting nested
7667 # structures when splitting on commas.
7668 #
7669 # Entry: $params - hashref to populate
7670 # (modified in place).
7671 # $sig - signature string with outer
7672 # parentheses already removed.
7673 #
7674 # Exit: Returns nothing. Populates $params
7675 # via _parse_signature_parameter.
7676 #
7677 # Side effects: Logs parsing details to stdout when
7678 # verbose is set.
7679 # --------------------------------------------------
7680 8 ▶sub _parse_modern_signature {
Subroutine _parse_modern_signature: called CC=12 SCAR=24.9
7681 8 my ($self, $params, $sig) = @_;
7682  
7683 8 $self->_log(" DEBUG: Parsing signature: [$sig]");
7684  
7685 # Split signature by commas, but respect nested structures (e.g. a
7686 # default value containing a hashref/arrayref literal)
7687 8 require Text::Balanced;
7688 8 my @parts;
7689 8 my $current = '';
7690 8 my $rest = $sig;
7691  
7692 8 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 173 8 181
7693 173 ▶ if (substr($rest, 0, 1) =~ /[(\[{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) =~ /[(\[{]/) 1 172 173
7694 # extract_bracketed advances $rest past the extracted block
7695 # in place, so $rest must not be re-truncated afterwards
7696 1 my $extracted = Text::Balanced::extract_bracketed($rest, '(){}[]');
7697 1 ▶ last unless defined $extracted; # Unbalanced brackets
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 1 1
7698 1 $current .= $extracted;
7699 1 next;
7700 }
7701 172 ▶ if (substr($rest, 0, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) eq ",") 15 157 172
7702 15 push @parts, $current;
7703 15 $current = '';
7704 15 $rest = substr($rest, 1);
7705 15 next;
7706 }
7707 157 $current .= substr($rest, 0, 1);
7708 157 $rest = substr($rest, 1);
7709 }
7710 8 ▶ push @parts, $current if $current =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $current =~ /\S/ 8 0 8
7711  
7712 8 my $position = 0;
7713  
7714 8 foreach my $part (@parts) {
7715 23 $part =~ s/^\s+|\s+$//g;
7716  
7717 # Skip empty parts
7718 23 ▶ next unless $part;
Branch: 1 branch
BranchTrueFalseTotal
unless $part 0 23 23
7719  
7720 # Parse different parameter types
7721 23 my $param_info = $self->_parse_signature_parameter($part, $position);
7722  
7723 23 ▶ if ($param_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_info) 23 0 23
7724 23 my $name = $param_info->{name};
7725  
7726 # Skip self/class
7727 23 ▶ if ($name =~ /^(self|class|pkg|proto|klass)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(self|class|pkg|proto|klass)$/i) 8 15 23
7728 8 next;
7729 }
7730  
7731 15 $params->{$name} = $param_info;
7732 $self->_log(" SIG: $name has position $position" .
7733 ($param_info->{optional} ? ' (optional)' : '') .
7734 15 ▶ ($param_info->{_default} ? ", default: $param_info->{_default}" : ''));
Branch: 2 branches
BranchTrueFalseTotal
$param_info->{'optional'} ? : 1 14 15
$param_info->{'_default'} ? : 1 14 15
7735 15 $position++;
7736 }
7737 }
7738 }
7739  
7740 # --------------------------------------------------
7741 # _parse_signature_parameter
7742 #
7743 # Purpose: Parse a single parameter declaration
7744 # from a modern Perl signature, handling
7745 # type constraints, default values,
7746 # plain scalars, and slurpy array/hash
7747 # parameters.
7748 #
7749 # Entry: $part - a single parameter string
7750 # (one comma-separated
7751 # element from the signature).
7752 # $position - zero-based position index
7753 # of this parameter.
7754 #
7755 # Exit: Returns a parameter info hashref on
7756 # success, or undef if the string does
7757 # not match any known pattern.
7758 #
7759 # Side effects: None.
7760 #
7761 # Notes: Six patterns are tried in order:
7762 # (1) :Type with default,
7763 # (2) :Type without default,
7764 # (3) default without type,
7765 # (4) plain $name,
7766 # (5) slurpy @name,
7767 # (6) slurpy %name.
7768 # --------------------------------------------------
7769 30 ▶sub _parse_signature_parameter {
Subroutine _parse_signature_parameter: called CC=20 SCAR=38.2
7770 30 my ($self, $part, $position) = @_;
7771  
7772 30 my %info = (
7773 _source => SOURCE_SIGNATURE,
7774 position => $position,
7775 optional => 0,
7776 );
7777  
7778 # Pattern 1: Type constraint WITH default: $name :Type = default
7779 30 ▶ if ($part =~ /^\$(\w+)\s*:\s*(\w+)\s*=\s*(.+)$/s) {
Branch: 6 branches
BranchTrueFalseTotal
if ($part =~ /^\$(\w+)\s*:\s*(\w+)\s*=\s*(.+)$/s) { } 1 29 30
elsif ($part =~ /^\$(\w+)\s*:\s*(\w+)\s*$/s) { } 1 28 29
elsif ($part =~ /^\$(\w+)\s*=\s*(.+)$/s) { } 2 26 28
elsif ($part =~ /^\$(\w+)$/s) { } 23 3 26
elsif ($part =~ /^\@(\w+)$/s) { } 1 2 3
elsif ($part =~ /^\%(\w+)$/s) { } 1 1 2
7780 1 my ($name, $constraint, $default) = ($1, $2, $3);
7781 1 $default =~ s/^\s+|\s+$//g;
7782  
7783 1 $info{name} = $name;
7784 1 $info{optional} = 1;
7785 1 $info{_default} = $self->_clean_default_value($default, 1);
7786  
7787 # Apply type constraint
7788 1 ▶ if ($constraint =~ /^(Int|Integer)$/i) {
Branch: 6 branches
BranchTrueFalseTotal
if ($constraint =~ /^(Int|Integer)$/i) { } 0 1 1
elsif ($constraint =~ /^(Num|Number)$/i) { } 1 0 1
elsif ($constraint =~ /^(Str|String)$/i) { } 0 0 0
elsif ($constraint =~ /^(Bool|Boolean)$/i) { } 0 0 0
elsif ($constraint =~ /^(Array|ArrayRef)$/i) { } 0 0 0
elsif ($constraint =~ /^(Hash|HashRef)$/i) { } 0 0 0
7789 0 $info{type} = 'integer';
7790 } elsif ($constraint =~ /^(Num|Number)$/i) {
7791 1 $info{type} = 'number';
7792 } elsif ($constraint =~ /^(Str|String)$/i) {
7793 0 $info{type} = 'string';
7794 } elsif ($constraint =~ /^(Bool|Boolean)$/i) {
7795 0 $info{type} = 'boolean';
7796 } elsif ($constraint =~ /^(Array|ArrayRef)$/i) {
7797 0 $info{type} = 'arrayref';
7798 } elsif ($constraint =~ /^(Hash|HashRef)$/i) {
7799 0 $info{type} = 'hashref';
7800 } else {
7801 0 $info{type} = 'object';
7802 0 $info{isa} = $constraint;
7803 }
7804  
7805 1 return \%info;
7806 } elsif ($part =~ /^\$(\w+)\s*:\s*(\w+)\s*$/s) {
7807 # Pattern 2: Type constraint WITHOUT default: $name :Type
7808 1 my ($name, $constraint) = ($1, $2);
7809 1 $info{name} = $name;
7810 1 $info{optional} = 0;
7811  
7812 # Apply type constraint (same as above)
7813 1 ▶ if ($constraint =~ /^(Int|Integer)$/i) {
Branch: 6 branches
BranchTrueFalseTotal
if ($constraint =~ /^(Int|Integer)$/i) { } 1 0 1
elsif ($constraint =~ /^(Num|Number)$/i) { } 0 0 0
elsif ($constraint =~ /^(Str|String)$/i) { } 0 0 0
elsif ($constraint =~ /^(Bool|Boolean)$/i) { } 0 0 0
elsif ($constraint =~ /^(Array|ArrayRef)$/i) { } 0 0 0
elsif ($constraint =~ /^(Hash|HashRef)$/i) { } 0 0 0
7814 1 $info{type} = 'integer';
7815 } elsif ($constraint =~ /^(Num|Number)$/i) {
7816 0 $info{type} = 'number';
7817 } elsif ($constraint =~ /^(Str|String)$/i) {
7818 0 $info{type} = 'string';
7819 } elsif ($constraint =~ /^(Bool|Boolean)$/i) {
7820 0 $info{type} = 'boolean';
7821 } elsif ($constraint =~ /^(Array|ArrayRef)$/i) {
7822 0 $info{type} = 'arrayref';
7823 } elsif ($constraint =~ /^(Hash|HashRef)$/i) {
7824 0 $info{type} = 'hashref';
7825 } else {
7826 0 $info{type} = 'object';
7827 0 $info{isa} = $constraint;
7828 }
7829  
7830 1 return \%info;
7831 } elsif ($part =~ /^\$(\w+)\s*=\s*(.+)$/s) {
7832 # Pattern 3: Default WITHOUT type: $name = default
7833 2 my ($name, $default) = ($1, $2);
7834 2 $default =~ s/^\s+|\s+$//g;
7835  
7836 2 $info{name} = $name;
7837 2 $info{optional} = 1;
7838 2 $info{_default} = $self->_clean_default_value($default, 1);
7839 2 ▶ $info{type} = $self->_infer_type_from_default($info{_default}) if $self->can('_infer_type_from_default');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_infer_type_from_default") 2 0 2
7840  
7841 2 return \%info;
7842 }
7843  
7844 # Pattern 4: Plain parameter: $name
7845 elsif ($part =~ /^\$(\w+)$/s) {
7846 23 $info{name} = $1;
7847 23 $info{optional} = 0;
7848 23 return \%info;
7849 }
7850  
7851 # Pattern 5: Array parameter: @name
7852 elsif ($part =~ /^\@(\w+)$/s) {
7853 1 $info{name} = $1;
7854 1 $info{type} = 'array';
7855 1 $info{slurpy} = 1;
7856 1 $info{optional} = 1;
7857 1 return \%info;
7858 }
7859  
7860 # Pattern 6: Hash parameter: %name
7861 elsif ($part =~ /^\%(\w+)$/s) {
7862 1 $info{name} = $1;
7863 1 $info{type} = 'hash';
7864 1 $info{slurpy} = 1;
7865 1 $info{optional} = 1;
7866 1 return \%info;
7867 }
7868  
7869 1 return undef;
7870 }
7871  
7872 # --------------------------------------------------
7873 # _infer_type_from_default
7874 #
7875 # Purpose: Infer a parameter type from its
7876 # default value when no explicit type
7877 # annotation is available.
7878 #
7879 # Entry: $default - the cleaned default value
7880 # scalar, hashref, or
7881 # arrayref. May be undef.
7882 #
7883 # Exit: Returns a type string ('hashref',
7884 # 'arrayref', 'integer', 'number',
7885 # 'boolean', 'string'), or undef if
7886 # $default is undef.
7887 #
7888 # Side effects: None.
7889 # --------------------------------------------------
7890 11 ▶sub _infer_type_from_default {
Subroutine _infer_type_from_default: called CC=8 SCAR=21.2
7891 11 my ($self, $default) = @_;
7892  
7893 11 ▶ return undef unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 1 10 11
7894  
7895 10 ▶ if (ref($default) eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $default eq 'HASH') { } 1 9 10
elsif (ref $default eq 'ARRAY') { } 1 8 9
elsif ($default =~ /^-?\d+$/) { } 5 3 8
elsif ($default =~ /^-?\d+\.\d+$/) { } 1 2 3
elsif ($default eq '1' or $default eq '0') { } 0 2 2
7896 1 return 'hashref';
7897 } elsif (ref($default) eq 'ARRAY') {
7898 1 return 'arrayref';
7899 } elsif ($default =~ /^-?\d+$/) {
7900 5 return 'integer';
7901 } elsif ($default =~ /^-?\d+\.\d+$/) {
7902 1 return 'number';
7903 ▶ } elsif ($default eq '1' || $default eq '0') {
Condition33%1/3
$default eq 1 or $default eq 0
l!l&&r!l&&!r
002
MC/DC0%0/2
$default eq 1 or $default eq 0
$default eq 1 $default eq 0
Truth table33%1/3
A or B
A= $default eq 1B= $default eq 0
ABresult
1X1
011
000
7904 0 return 'boolean';
7905 } else {
7906 2 return 'string';
7907 }
7908 }
7909  
7910 # --------------------------------------------------
7911 # _extract_subroutine_attributes
7912 #
7913 # Purpose: Extract Perl subroutine attributes
7914 # (e.g. :lvalue, :method, :Returns(Int))
7915 # from a method's source string.
7916 #
7917 # Entry: $code - method body source string.
7918 #
7919 # Exit: Returns a hashref of attribute name
7920 # to value (1 for flag-only attributes,
7921 # the attribute argument string for
7922 # attributes with values).
7923 # Returns an empty hashref if no
7924 # attributes are found.
7925 #
7926 # Side effects: Logs detections to stdout when
7927 # verbose is set.
7928 # --------------------------------------------------
7929 821 ▶sub _extract_subroutine_attributes {
Subroutine _extract_subroutine_attributes: called CC=10 SCAR=23.0
7930 821 my ($self, $code) = @_;
7931  
7932 821 my %attributes;
7933  
7934 # Extract all attributes from the sub declaration
7935 # Attributes are :name or :name(value) between sub name and either ( or {
7936 # Pattern: sub name ATTRIBUTES ( params ) { }
7937 # or: sub name ATTRIBUTES { }
7938  
7939 # First, find the attributes section (everything between sub name and ( or { )
7940 821 my $attr_section = '';
7941  
7942 821 ▶ if($code =~ /sub\s+\w+\s+((?::\w+(?:\([^)]*\))?\s*)+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /sub\s+\w+\s+((?::\w+(?:\([^)]*\))?\s*)+)/s) 3 818 821
7943 3 $attr_section = $1;
7944 }
7945  
7946 # Parse individual attributes from the section
7947 821 ▶ if($attr_section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_section) 3 818 821
7948 3 ▶ while($attr_section =~ /:(\w+)(?:\(([^)]*)\))?/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_section =~ /:(\w+)(?:\(([^)]*)\))?/g) 4 3 7
7949 4 my ($name, $value) = ($1, $2);
7950  
7951 4 ▶ if (defined $value && $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne '') { } 1 3 4
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
301
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
7952 1 $attributes{$name} = $value;
7953 1 $self->_log(" ATTR: Found attribute :$name($value)");
7954 } else {
7955 3 $attributes{$name} = 1;
7956 3 $self->_log(" ATTR: Found attribute :$name");
7957 }
7958 }
7959 }
7960  
7961 # Process common attributes
7962 821 ▶ if ($attributes{Returns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{"Returns"}) 1 820 821
7963 1 my $return_type = $attributes{Returns};
7964 1 ▶ if ($return_type ne '1') { # Only log if it's an actual type, not just the flag
Branch: 1 branch
BranchTrueFalseTotal
if ($return_type ne 1) 1 0 1
7965 1 $self->_log(" ATTR: Method declares return type: $return_type");
7966 }
7967 }
7968  
7969 821 ▶ if ($attributes{lvalue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{"lvalue"}) 2 819 821
7970 2 $self->_log(" ATTR: Method is lvalue (can be assigned to)");
7971 }
7972  
7973 821 ▶ if ($attributes{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{"method"}) 1 820 821
7974 1 $self->_log(' ATTR: Method explicitly marked as :method');
7975 }
7976  
7977 821 return \%attributes;
7978 }
7979  
7980 # --------------------------------------------------
7981 # _analyze_postfix_dereferencing
7982 #
7983 # Purpose: Detect usage of Perl 5.20+ postfix
7984 # dereferencing syntax in a method body
7985 # and record which dereference forms
7986 # are used.
7987 #
7988 # Entry: $code - method body source string.
7989 #
7990 # Exit: Returns a hashref whose keys are
7991 # dereference form names (array_deref,
7992 # hash_deref, scalar_deref, code_deref,
7993 # array_slice, hash_slice) with value 1
7994 # when detected.
7995 # Returns an empty hashref if no
7996 # postfix dereferencing is found.
7997 #
7998 # Side effects: Logs detections to stdout when
7999 # verbose is set.
8000 # --------------------------------------------------
8001 823 ▶sub _analyze_postfix_dereferencing {
Subroutine _analyze_postfix_dereferencing: called CC=7 SCAR=19.5
8002 823 my ($self, $code) = @_;
8003  
8004 823 my %derefs;
8005  
8006 # Array dereference: $ref->@*
8007 823 ▶ if ($code =~ /\$\w+\s*->\s*\@\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\@\*/) 1 822 823
8008 1 $derefs{array_deref} = 1;
8009 1 $self->_log(" MODERN: Uses postfix array dereferencing (->@*)");
8010 }
8011  
8012 # Hash dereference: $ref->%*
8013 823 ▶ if ($code =~ /\$\w+\s*->\s*\%\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\%\*/) 1 822 823
8014 1 $derefs{hash_deref} = 1;
8015 1 $self->_log(" MODERN: Uses postfix hash dereferencing (->%*)");
8016 }
8017  
8018 # Scalar dereference: $ref->$*
8019 823 ▶ if ($code =~ /\$\w+\s*->\s*\$\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\$\*/) 1 822 823
8020 1 $derefs{scalar_deref} = 1;
8021 1 $self->_log(' MODERN: Uses postfix scalar dereferencing (->$*)');
8022 }
8023  
8024 # Code dereference: $ref->&*
8025 823 ▶ if ($code =~ /\$\w+\s*->\s*\&\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\&\*/) 0 823 823
8026 0 $derefs{code_deref} = 1;
8027 0 $self->_log(" MODERN: Uses postfix code dereferencing (->&*)");
8028 }
8029  
8030 # Array element: $ref->@[0,2,4]
8031 823 ▶ if ($code =~ /\$\w+\s*->\s*\@\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\@\[/) 1 822 823
8032 1 $derefs{array_slice} = 1;
8033 1 $self->_log(" MODERN: Uses postfix array slice (->@[...])");
8034 }
8035  
8036 # Hash element: $ref->%{key1,key2}
8037 823 ▶ if ($code =~ /\$\w+\s*->\s*\%\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\%\{/) 1 822 823
8038 1 $derefs{hash_slice} = 1;
8039 1 $self->_log(" MODERN: Uses postfix hash slice (->%{...})");
8040 }
8041  
8042 823 return \%derefs;
8043 }
8044  
8045 # --------------------------------------------------
8046 # _extract_field_declarations
8047 #
8048 # Purpose: Extract Perl 5.38 field declarations
8049 # from a class body or method source
8050 # string, capturing field names,
8051 # :param attributes, default values,
8052 # and :isa type constraints.
8053 #
8054 # Entry: $code - source string potentially
8055 # containing 'field $name ...'
8056 # declarations.
8057 #
8058 # Exit: Returns a hashref of field name to
8059 # field_info hashref. Returns an empty
8060 # hashref if no field declarations
8061 # are found.
8062 #
8063 # Side effects: Logs detections to stdout when
8064 # verbose is set.
8065 # --------------------------------------------------
8066 822 ▶sub _extract_field_declarations {
Subroutine _extract_field_declarations: called CC=7 SCAR=19.5
8067 822 my ($self, $code) = @_;
8068  
8069 822 my %fields;
8070  
8071 # Pattern: field $name :param;
8072 # Pattern: field $name :param(name);
8073 # Pattern: field $name = default;
8074 # More lenient pattern to catch various formats
8075 822 ▶ while ($code =~ /^\s*field\s+\$(\w+)\s*([^;]*);/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\s*field\s+\$(\w+)\s*([^;]*);/gm) 4 822 826
8076 4 my ($name, $modifiers) = ($1, $2);
8077  
8078 4 $self->_log(" FIELD: Found field \$$name with modifiers: [$modifiers]");
8079  
8080 4 my %field_info = (
8081 name => $name,
8082 _source => SOURCE_FIELD
8083 );
8084  
8085 # Check for :param attribute
8086 4 ▶ if ($modifiers =~ /:param(?:\(([^)]+)\))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers =~ /:param(?:\(([^)]+)\))?/) 4 0 4
8087 4 $field_info{is_param} = 1;
8088  
8089 4 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 1 3 4
8090 # Explicit parameter name
8091 1 $field_info{param_name} = $1;
8092 } else {
8093 # Implicit - field name is param name
8094 3 $field_info{param_name} = $name;
8095 }
8096  
8097 4 $self->_log(" FIELD: $name maps to parameter: $field_info{param_name}");
8098 }
8099  
8100 # Check for default value - must come before type constraint check
8101 4 ▶ if ($modifiers =~ /=\s*([^:;]+)(?::|;|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers =~ /=\s*([^:;]+)(?::|;|$)/) 1 3 4
8102 1 my $default = $1;
8103 1 $default =~ s/\s+$//;
8104 1 $field_info{_default} = $self->_clean_default_value($default, 1);
8105 1 $field_info{optional} = 1;
8106 1 ▶ $self->_log(" FIELD: $name has default: " . (defined $field_info{_default} ? $field_info{_default} : 'undef'));
Branch: 1 branch
BranchTrueFalseTotal
defined $field_info{'_default'} ? : 1 0 1
8107 }
8108  
8109 # Check for type constraints
8110 4 ▶ if ($modifiers =~ /:isa\(([^)]+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers =~ /:isa\(([^)]+)\)/) 1 3 4
8111 1 $field_info{isa} = $1;
8112 1 $field_info{type} = 'object';
8113 1 $self->_log(" FIELD: $name has type constraint: $1");
8114 }
8115  
8116 4 $fields{$name} = \%field_info;
8117 }
8118  
8119 822 return \%fields;
8120 }
8121  
8122 # --------------------------------------------------
8123 # _merge_field_declarations
8124 #
8125 # Purpose: Integrate Perl 5.38 field declarations
8126 # that carry the :param attribute into
8127 # the code parameter hashref, so they
8128 # appear as constructor parameters in
8129 # the generated schema.
8130 #
8131 # Entry: $params - hashref of parameters
8132 # extracted from code analysis
8133 # (modified in place).
8134 # $fields - hashref of field declarations
8135 # as returned by
8136 # _extract_field_declarations.
8137 #
8138 # Exit: Returns nothing. Modifies $params
8139 # in place.
8140 #
8141 # Side effects: Logs merges to stdout when verbose
8142 # is set.
8143 #
8144 # Notes: Only fields with is_param => 1 are
8145 # merged. The param_name key in the
8146 # field (which may differ from the
8147 # field name if :param(name) was used)
8148 # determines the parameter key.
8149 # --------------------------------------------------
8150 0 ▶sub _merge_field_declarations {
Subroutine _merge_field_declarations: not called CC=8 SCAR=42.8
8151 0 my ($self, $params, $fields) = @_;
8152  
8153 0 foreach my $field_name (keys %$fields) {
8154 0 my $field = $fields->{$field_name};
8155  
8156 # Only process fields that are parameters
8157 0 ▶ next unless $field->{is_param};
Branch: 1 branch
BranchTrueFalseTotal
unless $field->{"is_param"} 0 0 0
8158  
8159 0 my $param_name = $field->{param_name};
8160  
8161 # Create or update parameter info
8162 0 ▶ $params->{$param_name} ||= {};
Condition0%0/2
$params->{$param_name} ||= {}
l!l
00
MC/DC0%0/1
$params->{$param_name} ||= {}
$params->{$param_name}
Truth table0%0/2
A
A= $params->{$param_name}
Aresult
11
00
8163 0 my $p = $params->{$param_name};
8164  
8165 # Merge field information into parameter
8166 0 ▶ $p->{_source} = SOURCE_FIELD unless $p->{_source};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"_source"} 0 0 0
8167 0 ▶ $p->{field_name} = $field_name if $field_name ne $param_name;
Branch: 1 branch
BranchTrueFalseTotal
if $field_name ne $param_name 0 0 0
8168  
8169 0 ▶ if ($field->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"_default"}) 0 0 0
8170 0 $p->{_default} = $field->{_default};
8171 0 $p->{optional} = 1;
8172 }
8173  
8174 0 ▶ if ($field->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"isa"}) 0 0 0
8175 0 $p->{isa} = $field->{isa};
8176 0 $p->{type} = 'object';
8177 }
8178  
8179 0 $self->_log(" MERGED: Field $field_name -> parameter $param_name");
8180 }
8181 }
8182  
8183 # --------------------------------------------------
8184 # _extract_defaults_from_code
8185 #
8186 # Purpose: Scan a method body for default value
8187 # assignment patterns and populate the
8188 # optional and _default fields of
8189 # known parameters.
8190 #
8191 # Entry: $params - hashref of parameters
8192 # (modified in place).
8193 # $code - method body source string.
8194 # $method - method hashref, used for
8195 # constructor-specific
8196 # exclusions of $class and
8197 # $self.
8198 #
8199 # Exit: Returns nothing. Modifies $params
8200 # in place.
8201 #
8202 # Side effects: Logs detections to stdout when
8203 # verbose is set.
8204 #
8205 # Notes: Eight default patterns are tried.
8206 # Only parameters already present in
8207 # $params are updated — this method
8208 # does not add new parameters.
8209 # Falls back to extracting all @_
8210 # assignments if $params is empty
8211 # after the main pass.
8212 # --------------------------------------------------
8213 819 ▶sub _extract_defaults_from_code {
Subroutine _extract_defaults_from_code: called CC=35 SCAR=37.5
8214 819 my ($self, $params, $code, $method) = @_;
8215  
8216 # Pattern 1: my $param = value;
8217 819 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*([^;]+);/g) 1016 819 1835
8218 1016 my ($param, $value) = ($1, $2);
8219 1016 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 1009 7 1016
8220 7 ▶ next if $value =~ /->/; # deref/method call, not a default value
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /->/ 1 6 7
8221  
8222 6 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
8223 6 $params->{$param}{optional} = 1;
8224 6 $self->_log(" CODE: $param has default: " . $self->_format_default($params->{$param}{_default}));
8225 }
8226  
8227 # Pattern 2: $param = value unless defined $param;
8228 819 ▶ while ($code =~ /\$(\w+)\s*=\s*([^;]+?)\s+unless\s+(?:defined\s+)?\$\1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*=\s*([^;]+?)\s+unless\s+(?:defined\s+)?\$\1/g) 3 819 822
8229 3 my ($param, $value) = ($1, $2);
8230 3 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 3 3
8231  
8232 3 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
8233 3 $params->{$param}{optional} = 1;
8234 3 $self->_log(" CODE: $param has default (unless): " . $self->_format_default($params->{$param}{_default}));
8235 }
8236  
8237 # Pattern 3: $param = value unless $param;
8238 819 ▶ while ($code =~ /\$(\w+)\s*=\s*([^;]+?)\s+unless\s+\$\1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*=\s*([^;]+?)\s+unless\s+\$\1/g) 1 819 820
8239 1 my ($param, $value) = ($1, $2);
8240 1 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 1 1
8241  
8242 1 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
8243 1 $params->{$param}{optional} = 1;
8244 1 $self->_log(" CODE: $param has default (unless): " . $self->_format_default($params->{$param}{_default}));
8245 }
8246  
8247 # Pattern 4: $param = $param || 'default';
8248 819 ▶ while ($code =~ /\$(\w+)\s*=\s*\$\1\s*\|\|\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*=\s*\$\1\s*\|\|\s*([^;]+);/g) 7 819 826
8249 7 my ($param, $value) = ($1, $2);
8250 7 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 7 7
8251  
8252 7 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
8253 7 $params->{$param}{optional} = 1;
8254 7 $self->_log(" CODE: $param has default (||): " . $self->_format_default($params->{$param}{_default}));
8255 }
8256  
8257 # Pattern 5: $param ||= 'default';
8258 819 ▶ while ($code =~ /\$(\w+)\s*\|\|=\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*\|\|=\s*([^;]+);/g) 19 819 838
8259 19 my ($param, $value) = ($1, $2);
8260 19 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 6 13 19
8261  
8262 13 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
8263 13 $params->{$param}{optional} = 1;
8264 13 $self->_log(" CODE: $param has default (||=): " . $self->_format_default($params->{$param}{_default}));
8265 }
8266  
8267 # Pattern 6: $param //= 'default';
8268 819 ▶ while ($code =~ /\$(\w+)\s*\/\/=\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[\$(\w+)\s*//=\s*([^;]+);]g) 8 819 827
8269 8 my ($param, $value) = ($1, $2);
8270 8 ▶ next unless exists $params->{$param}; # Using -> because $params is a reference
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 8 8
8271  
8272 8 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
8273  
8274 8 $params->{$param}{optional} = 1;
8275 8 $self->_log(" CODE: $param has default (//=): " . $self->_format_default($params->{$param}{_default}));
8276 }
8277  
8278 # Pattern 7: $param = defined $param ? $param : 'default';
8279 819 ▶ while ($code =~ /\$(\w+)\s*=\s*defined\s+\$\1\s*\?\s*\$\1\s*:\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*=\s*defined\s+\$\1\s*\?\s*\$\1\s*:\s*([^;]+);/g) 4 819 823
8280 4 my ($param, $value) = ($1, $2);
8281  
8282 # Create param entry if it doesn't exist
8283 4 ▶ $params->{$param} ||= {};
Condition50%1/2
$params->{$param} ||= {}
l!l
40
MC/DC0%0/1
$params->{$param} ||= {}
$params->{$param}
Truth table50%1/2
A
A= $params->{$param}
Aresult
11
00
8284  
8285 4 my $cleaned = $self->_clean_default_value($value, 1);
8286  
8287 4 $params->{$param}{_default} = $cleaned;
8288 4 $params->{$param}{optional} = 1;
8289 4 $self->_log(" CODE: $param has default (ternary): " . $self->_format_default($params->{$param}{_default}));
8290 }
8291  
8292 # Pattern 8: $param = $args{param} || 'default';
8293 819 ▶ while ($code =~ /\$(\w+)\s*=\s*\$args\{['"]?\w+['"]?\}\s*\|\|\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*=\s*\$args\{['"]?\w+['"]?\}\s*\|\|\s*([^;]+);/g) 0 819 819
8294 0 my ($param, $value) = ($1, $2);
8295 0 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 0 0
8296  
8297 0 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
8298 0 $params->{$param}{optional} = 1;
8299 0 $self->_log(" CODE: $param has default (from args): " . $self->_format_default($params->{$param}{_default}));
8300 }
8301  
8302 # Pattern for non-empty hashref
8303 819 ▶ while ($code =~ /\$(\w+)\s*\|\|=\s*\{[^}]+\}/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*\|\|=\s*\{[^}]+\}/gs) 1 819 820
8304 1 my $param = $1;
8305 1 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 1 1
8306  
8307 # Return empty hashref as placeholder (can't evaluate complex hashrefs)
8308 1 $params->{$param}{_default} = {};
8309 1 $params->{$param}{optional} = 1;
8310 1 $self->_log(" CODE: $param has hashref default (||=)");
8311 }
8312  
8313 # Fallback: extract parameters from classic Perl body styles
8314 # Only run if signature extraction found nothing AND the code does not use
8315 # the direct-index ($_[0]) style — that style is used for no-param methods
8316 # whose empty %params would otherwise trigger this fallback and pick up
8317 # my (...) = @_ from inner closures as if they were method params.
8318 # TODO: On constructors, use $class to help to determine the output type
8319 819 ▶ if (!keys %{$params} && $code !~ /my\s+\$(?:self|class|pkg|proto|klass)\s*=\s*\$_\[0\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %{$params;} || $code =~ /my\s+\$(?:self|class|pkg|proto|klass)\s*=\s*\$_\[0\]/) 399 420 819
Condition100%3/3
keys %{$params;} || $code =~ /my\s+\$(?:self|class|pkg|proto|klass)\s*=\s*\$_\[0\]/
l!l&&r!l&&!r
35268399
MC/DC100%2/2
keys %{$params;} || $code =~ /my\s+\$(?:self|class|pkg|proto|klass)\s*=\s*\$_\[0\]/
keys %{$params;} $code =~ /my\s+\$(?:self|class|pkg|proto|klass)\s*=\s*\$_\[0\]/
Truth table100%3/3
A || B
A= keys %{$params;}B= $code =~ /my\s+\$(?:self|class|pkg|proto|klass)\s*=\s*\$_\[0\]/
ABresult
1X1
011
000
8320 399 my $position = 0;
8321  
8322 # Style 1: my ($a, $b) = @_;
8323 399 ▶ while ($code =~ /my\s*\(\s*([^)]+)\s*\)\s*=\s*\@_/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s*\(\s*([^)]+)\s*\)\s*=\s*\@_/g) 106 399 505
8324 106 my @vars = $1 =~ /\$(\w+)/g;
8325 106 foreach my $var (@vars) {
8326 104 ▶ if(($position == 0) && ($var =~ /^(self|class|pkg|proto|klass)$/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i) { } 104 0 104
Condition33%1/3
$position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
00104
MC/DC0%0/2
$position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i
$position == 0 $var =~ /^(self|class|pkg|proto|klass)$/i
Truth table33%1/3
A and B
A= $position == 0B= $var =~ /^(self|class|pkg|proto|klass)$/i
ABresult
0X0
100
111
8327 # Invocant — skip it
8328 104 delete $params->{$var};
8329 } else {
8330 0 ▶ $params->{$var} ||= { position => $position++ };
Condition0%0/2
$params->{$var} ||= {"position", $position++}
l!l
00
MC/DC0%0/1
$params->{$var} ||= {"position", $position++}
$params->{$var}
Truth table0%0/2
A
A= $params->{$var}
Aresult
11
00
8331 0 $self->_log(" CODE: $var extracted from \@_ list assignment");
8332 }
8333 }
8334 }
8335  
8336 # Style 2: my $x = shift;
8337 399 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*shift\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*shift\b/g) 72 399 471
8338 72 my $var = $1;
8339 72 ▶ if(($position == 0) && ($var =~ /^(self|class|pkg|proto|klass)$/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i) { } 28 44 72
Condition66%2/3
$position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
04428
MC/DC50%1/2
$position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i
$position == 0 $var =~ /^(self|class|pkg|proto|klass)$/i
Truth table66%2/3
A and B
A= $position == 0B= $var =~ /^(self|class|pkg|proto|klass)$/i
ABresult
0X0
100
111
8340 # Invocant — skip it
8341 28 delete $params->{$var};
8342 } else {
8343 44 ▶ $params->{$var} ||= { position => $position++ };
Condition50%1/2
$params->{$var} ||= {"position", $position++}
l!l
044
MC/DC0%0/1
$params->{$var} ||= {"position", $position++}
$params->{$var}
Truth table50%1/2
A
A= $params->{$var}
Aresult
11
00
8344 44 $self->_log(" CODE: $var is extracted from shift");
8345 }
8346 }
8347  
8348 # Style 3: my $x = $_[0];
8349 399 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*\$_\[(\d+)\]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*\$_\[(\d+)\]/g) 24 399 423
8350 24 my ($var, $index) = ($1, $2);
8351 24 ▶ if(($index > 0) || ($var !~ /^(self|class|pkg|proto|klass)$/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > 0 or not $var =~ /^(self|class|pkg|proto|klass)$/i) 24 0 24
Condition33%1/3
$index > 0 or not $var =~ /^(self|class|pkg|proto|klass)$/i
l!l&&r!l&&!r
0240
MC/DC0%0/2
$index > 0 or not $var =~ /^(self|class|pkg|proto|klass)$/i
$index > 0 not $var =~ /^(self|class|pkg|proto|klass)$/i
Truth table33%1/3
A or not(B)
A= $index > 0B= not $var =~ /^(self|class|pkg|proto|klass)$/i
ABresult
1X1
011
000
8352 24 ▶ $params->{$var} ||= { position => $index };
Condition50%1/2
$params->{$var} ||= {"position", $index}
l!l
024
MC/DC0%0/1
$params->{$var} ||= {"position", $index}
$params->{$var}
Truth table50%1/2
A
A= $params->{$var}
Aresult
11
00
8353 24 $self->_log(" CODE: $var is extracted from \$_\[$index\]");
8354 }
8355 }
8356 }
8357 }
8358  
8359 # --------------------------------------------------
8360 # _format_default
8361 #
8362 # Purpose: Format a default value for display
8363 # in verbose log output.
8364 #
8365 # Entry: $default - the default value to
8366 # format. May be undef,
8367 # a scalar, a hashref, or
8368 # an arrayref.
8369 #
8370 # Exit: Returns a display string: 'undef'
8371 # for undef, 'HASH ref' / 'ARRAY ref'
8372 # for references, or the value itself
8373 # for scalars.
8374 #
8375 # Side effects: None.
8376 # --------------------------------------------------
8377 47 ▶sub _format_default {
Subroutine _format_default: called CC=3 SCAR=11.0
8378 47 my ($self, $default) = @_;
8379 47 ▶ return 'undef' unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 6 41 47
8380 41 ▶ return ref($default) . ' ref' if ref($default);
Branch: 1 branch
BranchTrueFalseTotal
if ref $default 10 31 41
8381 31 return $default;
8382 }
8383  
8384 # Resolve the right-hand side of a length comparison: a literal or a
8385 # Readonly/use-constant name.
8386 35 ▶sub _length_constraint_value {
Subroutine _length_constraint_value: called CC=3 SCAR=11.0
8387 35 my ($self, $rhs) = @_;
8388  
8389 35 ▶ return $rhs + 0 if $rhs =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $rhs =~ /^\d+$/ 17 18 35
8390 18 ▶ return undef unless $rhs =~ /^\$(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rhs =~ /^\$(\w+)$/ 0 18 18
8391 18 return $self->_module_constants()->{$1};
8392 }
8393  
8394 # Record the accepted length range. When the comparison is an error
8395 # condition (an `if` guard), the accepted range is its negation.
8396 35 ▶sub _record_length_constraint {
Subroutine _record_length_constraint: called CC=14 SCAR=37.0
8397 35 my ($self, $p, $op, $val, $invert) = @_;
8398  
8399 35 ▶ if($invert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invert) 7 28 35
8400 7 my %negated = ('<' => '>=', '<=' => '>', '>' => '<=', '>=' => '<');
8401 7 $op = $negated{$op};
8402 }
8403  
8404 35 ▶ if($op eq '<') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '<') { } 0 35 35
elsif ($op eq '<=') { } 13 22 35
elsif ($op eq '>') { } 0 22 22
elsif ($op eq '>=') { } 22 0 22
8405 0 my $max = $val - 1;
8406 0 ▶ $p->{max} = $max if !defined($p->{max}) || $max < $p->{max};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $p->{"max"} or $max < $p->{"max"} 0 0 0
Condition0%0/3
not defined $p->{"max"} or $max < $p->{"max"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $p->{"max"} or $max < $p->{"max"}
not defined $p->{"max"} $max < $p->{"max"}
Truth table0%0/3
not(A) or B
A= not defined $p->{"max"}B= $max < $p->{"max"}
ABresult
1X1
011
000
8407 } elsif($op eq '<=') {
8408 13 ▶ $p->{max} = $val if !defined($p->{max}) || $val < $p->{max};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $p->{"max"} or $val < $p->{"max"} 13 0 13
Condition33%1/3
not defined $p->{"max"} or $val < $p->{"max"}
l!l&&r!l&&!r
1300
MC/DC0%0/2
not defined $p->{"max"} or $val < $p->{"max"}
not defined $p->{"max"} $val < $p->{"max"}
Truth table33%1/3
not(A) or B
A= not defined $p->{"max"}B= $val < $p->{"max"}
ABresult
1X1
011
000
8409 } elsif($op eq '>') {
8410 0 my $min = $val + 1;
8411 0 ▶ $p->{min} = $min if !defined($p->{min}) || $min > $p->{min};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $p->{"min"} or $min > $p->{"min"} 0 0 0
Condition0%0/3
not defined $p->{"min"} or $min > $p->{"min"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $p->{"min"} or $min > $p->{"min"}
not defined $p->{"min"} $min > $p->{"min"}
Truth table0%0/3
not(A) or B
A= not defined $p->{"min"}B= $min > $p->{"min"}
ABresult
1X1
011
000
8412 } elsif($op eq '>=') {
8413 22 ▶ $p->{min} = $val if !defined($p->{min}) || $val > $p->{min};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $p->{"min"} or $val > $p->{"min"} 21 1 22
Condition66%2/3
not defined $p->{"min"} or $val > $p->{"min"}
l!l&&r!l&&!r
2101
MC/DC50%1/2
not defined $p->{"min"} or $val > $p->{"min"}
not defined $p->{"min"} $val > $p->{"min"}
Truth table66%2/3
not(A) or B
A= not defined $p->{"min"}B= $val > $p->{"min"}
ABresult
1X1
011
000
8414 }
8415 }
8416  
8417 # Return ($op, $value) when a guard condition is exactly one length
8418 # comparison, or an empty list when the comparison is only one operand of a
8419 # compound condition. A compound condition enforces no bound of its own:
8420 # `croak ... if length($t) < 5 || $other` still croaks for long text when
8421 # $other is true, and `... if length($t) < 5 && $other` accepts short text
8422 # when $other is false. Only a condition that is nothing but the comparison
8423 # makes the negation of that comparison a real constraint.
8424 332 ▶sub _whole_condition_length_guard {
Subroutine _whole_condition_length_guard: called CC=10 SCAR=23.5
8425 332 my ($self, $param, $condition) = @_;
8426  
8427 332 $condition =~ s/^\s+//;
8428 332 $condition =~ s/\s+$//;
8429  
8430 # Drop balanced outer parentheses, as in `if (length($t) < 5)`.
8431 332 ▶ while($condition =~ /^\((.*)\)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition =~ /^\((.*)\)$/s) 1 332 333
8432 1 my $inner = $1;
8433 1 my $depth = 0;
8434 1 my $balanced = 1;
8435 1 foreach my $char (split(//, $inner)) {
8436 15 ▶ if($char eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '(') { } 1 14 15
elsif ($char eq ')') { } 1 13 14
8437 1 $depth++;
8438 } elsif($char eq ')') {
8439 1 $depth--;
8440 1 ▶ if($depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0) 0 1 1
8441 0 $balanced = 0;
8442 0 last;
8443 }
8444 }
8445 }
8446 1 ▶ last unless $balanced && ($depth == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $balanced and $depth == 0 0 1 1
Condition33%1/3
$balanced and $depth == 0
!ll&&!rl&&r
001
MC/DC0%0/2
$balanced and $depth == 0
$balanced $depth == 0
Truth table33%1/3
A and B
A= $balancedB= $depth == 0
ABresult
0X0
100
111
8447 1 $condition = $inner;
8448 1 $condition =~ s/^\s+//;
8449 1 $condition =~ s/\s+$//;
8450 }
8451  
8452 332 ▶ return unless $condition =~ /^length\s*\(\s*\$\Q$param\E\s*\)\s*([<>]=?)\s*(\d+|\$\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $condition =~ /^length\s*\(\s*\$\Q$param\E\s*\)\s*([<>]=?)\s*(\d+|\$\w+)$/ 297 35 332
8453  
8454 35 my ($op, $rhs) = ($1, $2);
8455 35 my $val = $self->_length_constraint_value($rhs);
8456 35 ▶ return unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 35 35
8457  
8458 35 return ($op, $val);
8459 }
8460  
8461 # --------------------------------------------------
8462 # _module_constants
8463 #
8464 # Purpose: Build and cache a hash of numeric
8465 # constant values declared in the target
8466 # module source, covering both Readonly
8467 # lexicals and 'use constant' barewords.
8468 # Used by _analyze_parameter_constraints
8469 # to resolve right-hand sides like
8470 # $MIN_NAME_LEN that are not literals.
8471 #
8472 # Entry: None (uses $self->{_document}).
8473 #
8474 # Exit: Returns a hashref { NAME => value }.
8475 # Returns {} if the PPI document is not
8476 # yet loaded.
8477 #
8478 # Side effects: Caches result in $self->{_constants}.
8479 # --------------------------------------------------
8480 18 ▶sub _module_constants {
Subroutine _module_constants: called CC=5 SCAR=16.7
8481 18 my ($self) = @_;
8482 18 ▶ return $self->{_constants} if exists $self->{_constants};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_constants"} 13 5 18
8483  
8484 5 my %c;
8485 5 my $doc = $self->{_document};
8486 5 ▶ unless ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 0 5 5
8487 0 $self->{_constants} = \%c;
8488 0 return \%c;
8489 }
8490  
8491 5 my $src = $doc->serialize();
8492  
8493 # Readonly my $CONST => numeric_value;
8494 5 ▶ while ($src =~ /Readonly\s+(?:my|our)\s+\$(\w+)\s*=>\s*([+-]?\d+(?:\.\d+)?)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /Readonly\s+(?:my|our)\s+\$(\w+)\s*=>\s*([+-]?\d+(?:\.\d+)?)/g) 34 5 39
8495 34 $c{$1} = $2;
8496 }
8497  
8498 # use constant CONST => numeric_value;
8499 5 ▶ while ($src =~ /use\s+constant\s+(\w+)\s*=>\s*([+-]?\d+(?:\.\d+)?)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /use\s+constant\s+(\w+)\s*=>\s*([+-]?\d+(?:\.\d+)?)/g) 0 5 5
8500 0 $c{$1} = $2;
8501 }
8502  
8503 5 $self->{_constants} = \%c;
8504 5 return \%c;
8505 }
8506  
8507 # --------------------------------------------------
8508 # _analyze_parameter_constraints
8509 #
8510 # Purpose: Infer min, max, and regex match
8511 # constraints for a single parameter
8512 # from length checks, numeric
8513 # comparisons, and regex match
8514 # patterns in the method body.
8515 #
8516 # Entry: $p_ref - reference to the parameter
8517 # hashref (modified in place).
8518 # $param - parameter name string.
8519 # $code - method body source string.
8520 #
8521 # Exit: Returns nothing. Modifies the
8522 # referenced parameter hashref.
8523 #
8524 # Side effects: Logs detections to stdout when
8525 # verbose is set.
8526 #
8527 # Notes: Numeric comparisons that appear
8528 # inside die/croak guard conditions
8529 # are excluded to avoid inferring
8530 # invalid-input ranges as valid
8531 # constraints.
8532 # --------------------------------------------------
8533 550 ▶sub _analyze_parameter_constraints {
Subroutine _analyze_parameter_constraints: called CC=27 SCAR=40.5
8534 550 my ($self, $p_ref, $param, $code) = @_;
8535 550 my $p = $$p_ref;
8536  
8537 # Do not treat comparisons inside die/croak/confess as valid constraints
8538 550 my $guarded = 0;
8539 550 ▶ if ($code =~ /(die|croak|confess)\b[^{;]*\bif\b[^{;]*\$$param\b/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(die|croak|confess)\b[^{;]*\bif\b[^{;]*\$$param\b/s) 58 492 550
8540 58 $guarded = 1;
8541 }
8542  
8543 # Length checks for strings. A comparison only describes the accepted
8544 # range when it guards an error: `croak ... unless length >= 5` accepts
8545 # >= 5, while `croak ... if length < 5` also accepts >= 5. Reading the
8546 # second condition literally would invent a max of 4 and can contradict
8547 # a min inferred from the first form.
8548 550 ▶ $p->{type} ||= 'string' if $code =~ /length\s*\(\s*\$$param\s*\)/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /length\s*\(\s*\$$param\s*\)/ 32 518 550
Condition50%1/2
$p->{"type"} ||= "string"
l!l
032
MC/DC0%0/1
$p->{"type"} ||= "string"
$p->{"type"}
Truth table50%1/2
A
A= $p->{"type"}
Aresult
11
00
8549  
8550 550 my $postfix_guards = $code;
8551 550 ▶ while ($postfix_guards =~ /\b(?:croak|die|confess)\b[^;{}]*?\b(if|unless)\b\s*([^;{}]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postfix_guards =~ /\b(?:croak|die|confess)\b[^;{}]*?\b(if|unless)\b\s*([^;{}]+)/g) 332 550 882
8552 332 my ($guard, $condition) = ($1, $2);
8553 332 my ($op, $val) = $self->_whole_condition_length_guard($param, $condition);
8554 332 ▶ next unless defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 297 35 332
8555 35 $self->_record_length_constraint($p, $op, $val, ($guard eq 'if'));
8556 35 $self->_log(" CODE: $param length constraint from $guard guard: $op $val");
8557 }
8558  
8559 550 my $block_guards = $code;
8560 550 ▶ while ($block_guards =~ /\b(if|unless)\s*\(\s*length\s*\(\s*\$$param\s*\)\s*([<>]=?)\s*(\d+|\$\w+)\s*\)\s*\{([^{}]*)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_guards =~ /\b(if|unless)\s*\(\s*length\s*\(\s*\$$param\s*\)\s*([<>]=?)\s*(\d+|\$\w+)\s*\)\s*\{([^{}]*)\}/g) 0 550 550
8561 0 my ($guard, $op, $rhs, $block) = ($1, $2, $3, $4);
8562 0 ▶ next unless $block =~ /\b(?:croak|die|confess)\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $block =~ /\b(?:croak|die|confess)\b/ 0 0 0
8563 0 my $val = $self->_length_constraint_value($rhs);
8564 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
8565 0 $self->_record_length_constraint($p, $op, $val, ($guard eq 'if'));
8566 0 $self->_log(" CODE: $param length constraint from $guard block: $op $rhs");
8567 }
8568  
8569 # Numeric range checks (only if NOT part of error guard)
8570 550 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $guarded and $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/) 16 534 550
Condition100%3/3
not $guarded and $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/
!ll&&!rl&&r
5847616
MC/DC100%2/2
not $guarded and $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/
not $guarded $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/
Truth table100%3/3
not(A) and B
A= not $guardedB= $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/
ABresult
0X0
100
111
8571 !$guarded
8572 && $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/
8573 ) {
8574 16 my ($op, $val) = ($1, $2);
8575 16 ▶ $p->{type} ||= looks_like_number($val) ? 'number' : 'integer';
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($val) ? : 0 0 0
Condition33%1/3
$p->{"type"} ||= &looks_like_number($val) ? "number" : "integer"
l!l&&r!l&&!r
1600
MC/DC0%0/2
$p->{"type"} ||= &looks_like_number($val) ? "number" : "integer"
$p->{"type"} &looks_like_number($val) ? "number" : "integer"
Truth table33%1/3
A ||= B
A= $p->{"type"}B= &looks_like_number($val) ? "number" : "integer"
ABresult
1X1
011
000
8576  
8577 16 ▶ if ($op eq '<' || $op eq '<=') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '<' or $op eq '<=') { } 3 13 16
elsif ($op eq '>' or $op eq '>=') { } 13 0 13
Condition66%2/3
$op eq "<" or $op eq "<="
l!l&&r!l&&!r
3013
MC/DC50%1/2
$op eq "<" or $op eq "<="
$op eq "<" $op eq "<="
Truth table66%2/3
A or B
A= $op eq "<"B= $op eq "<="
ABresult
1X1
011
000
8578 # Only set max if it tightens the range
8579 3 ▶ my $max = ($op eq '<') ? $val - 1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 3 0 3
8580 3 ▶ $p->{max} = $max if !defined($p->{max}) || $max < $p->{max};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $p->{"max"} or $max < $p->{"max"} 3 0 3
Condition33%1/3
not defined $p->{"max"} or $max < $p->{"max"}
l!l&&r!l&&!r
300
MC/DC0%0/2
not defined $p->{"max"} or $max < $p->{"max"}
not defined $p->{"max"} $max < $p->{"max"}
Truth table33%1/3
not(A) or B
A= not defined $p->{"max"}B= $max < $p->{"max"}
ABresult
1X1
011
000
8581 ▶ } elsif ($op eq '>' || $op eq '>=') {
Condition66%2/3
$op eq ">" or $op eq ">="
l!l&&r!l&&!r
1120
MC/DC0%0/2
$op eq ">" or $op eq ">="
$op eq ">" $op eq ">="
Truth table66%2/3
A or B
A= $op eq ">"B= $op eq ">="
ABresult
1X1
011
000
8582 13 ▶ my $min = ($op eq '>') ? $val + 1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '>' ? : 11 2 13
8583 13 ▶ $p->{min} = $min if !defined($p->{min}) || $min > $p->{min};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $p->{"min"} or $min > $p->{"min"} 13 0 13
Condition33%1/3
not defined $p->{"min"} or $min > $p->{"min"}
l!l&&r!l&&!r
1300
MC/DC0%0/2
not defined $p->{"min"} or $min > $p->{"min"}
not defined $p->{"min"} $min > $p->{"min"}
Truth table33%1/3
not(A) or B
A= not defined $p->{"min"}B= $min > $p->{"min"}
ABresult
1X1
011
000
8584 }
8585 }
8586  
8587 # Regex pattern matching with better capture
8588 550 ▶ if($code =~ /\$$param\s*=~\s*(?:qr?\/([^\/]+)\/|(\$[\w:]+|\$\{\w+\}))/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[\$$param\s*=~\s*(?:qr?/([^/]+)/|(\$[\w:]+|\$\{\w+\}))]) 1 549 550
8589 1 my ($body, $variable) = ($1, $2);
8590 1 ▶ $p->{type} ||= 'string';
Condition50%1/2
$p->{"type"} ||= "string"
l!l
01
MC/DC0%0/1
$p->{"type"} ||= "string"
$p->{"type"}
Truth table50%1/2
A
A= $p->{"type"}
Aresult
11
00
8591  
8592 1 ▶ if(defined($variable)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $variable) { } 1 0 1
elsif (not _pattern_is_self_contained($body)) { } 0 0 0
8593 # The pattern lives in a variable whose value is not in the
8594 # schema, and the harness would emit that name as a regex.
8595 1 $self->_log(" CODE: $param matches $variable, a pattern held in a variable; not recorded");
8596 } elsif(!_pattern_is_self_contained($body)) {
8597 0 $self->_log(" CODE: $param matches '$body', which depends on run-time variables; not recorded");
8598 } else {
8599 0 $p->{matches} = "/$body/";
8600 0 $self->_log(" CODE: $param matches pattern: $p->{matches}");
8601 }
8602 }
8603 }
8604  
8605 # --------------------------------------------------
8606 # _pattern_is_self_contained
8607 #
8608 # Purpose: Decide whether a regex taken from the
8609 # source text still means the same thing
8610 # once the harness compiles it from a
8611 # string.
8612 #
8613 # Entry: $pattern - regex body as written in the
8614 # source.
8615 #
8616 # Exit: Returns true when the pattern stands
8617 # alone. \Q and \E are handled by the
8618 # regex lexer, so they quote nothing when
8619 # the pattern arrives as an interpolated
8620 # string; a pattern interpolating a
8621 # variable depends on a value no schema
8622 # carries (q_wrap's delimiter loop uses
8623 # /\Q$l\E|\Q$r\E/).
8624 # --------------------------------------------------
8625 37 ▶sub _pattern_is_self_contained {
Subroutine _pattern_is_self_contained: called CC=5 SCAR=17.2
8626 37 my ($pattern) = @_;
8627  
8628 37 ▶ return 0 unless defined($pattern) && length($pattern);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern and length $pattern 0 37 37
Condition33%1/3
defined $pattern and length $pattern
!ll&&!rl&&r
0037
MC/DC0%0/2
defined $pattern and length $pattern
defined $pattern length $pattern
Truth table33%1/3
A and B
A= defined $patternB= length $pattern
ABresult
0X0
100
111
8629 37 ▶ return 0 if $pattern =~ /\\[QE]/;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /\\[QE]/ 4 33 37
8630 33 ▶ return 0 if $pattern =~ /(?<!\\)[\$\@]\w/;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /(?<!\\)[\$\@]\w/ 0 33 33
8631  
8632 33 return 1;
8633 }
8634  
8635 # --------------------------------------------------
8636 # _analyze_parameter_validation
8637 #
8638 # Purpose: Determine optionality and extract
8639 # default values for a single parameter
8640 # by analysing explicit required checks
8641 # (die/croak unless defined) and default
8642 # assignment patterns in the method body.
8643 #
8644 # Entry: $p_ref - reference to the parameter
8645 # hashref (modified in place).
8646 # $param - parameter name string.
8647 # $code - method body source string.
8648 #
8649 # Exit: Returns nothing. Modifies the
8650 # referenced parameter hashref.
8651 #
8652 # Side effects: Logs detections to stdout when
8653 # verbose is set.
8654 #
8655 # Notes: Explicit required checks take highest
8656 # priority and override any default
8657 # value detected earlier.
8658 # --------------------------------------------------
8659 550 ▶sub _analyze_parameter_validation {
Subroutine _analyze_parameter_validation: called CC=24 SCAR=33.8
8660 550 my ($self, $p_ref, $param, $code) = @_;
8661 550 my $p = $$p_ref;
8662  
8663 # Required/optional checks
8664 550 my $is_required = 0;
8665  
8666 # Die/croak if not defined
8667 550 ▶ if ($code =~ /(?:die|croak|confess)\s+[^;]*unless\s+(?:defined\s+)?\$$param/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)\s+[^;]*unless\s+(?:defined\s+)?\$$param/s) 69 481 550
8668 69 $is_required = 1;
8669 }
8670  
8671 # A routine that refuses an empty value ("croak ... unless defined $x &&
8672 # length $x") does not accept one, so the harness's empty-string case is
8673 # an error rather than a survival case.
8674 550 ▶ if($code =~ /(?:croak|die|confess)\b[^;]*\bunless\b[^;]*?(?:length\s*\(?\s*\$$param\b|defined\s+\$$param\b[^;]*?\blength\b)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:croak|die|confess)\b[^;]*\bunless\b[^;]*?(?:length\s*\(?\s*\$$param\b|defined\s+\$$param\b[^;]*?\blength\b)/s) 22 528 550
8675 22 $is_required = 1;
8676 22 $p->{_not_empty} = 1;
8677 22 $self->_log(" CODE: $param must not be empty (the code rejects it)");
8678 }
8679  
8680 # Extract default values with the new method
8681 550 my $default_value = $self->_extract_default_value($param, $code);
8682 550 ▶ if (defined $default_value && !exists $p->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default_value and not exists $p->{"_default"}) 1 549 550
Condition100%3/3
defined $default_value and not exists $p->{"_default"}
!ll&&!rl&&r
519301
MC/DC100%2/2
defined $default_value and not exists $p->{"_default"}
defined $default_value not exists $p->{"_default"}
Truth table100%3/3
A and not(B)
A= defined $default_valueB= not exists $p->{"_default"}
ABresult
0X0
100
111
8683 1 $p->{optional} = 1;
8684 1 $p->{_default} = $default_value;
8685  
8686 # Try to infer type from default value if not already set
8687 1 ▶ unless ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 1 0 1
8688 1 ▶ if (looks_like_number($default_value)) {
Branch: 5 branches
BranchTrueFalseTotal
if (&looks_like_number($default_value)) { } 1 0 1
elsif (ref $default_value eq 'ARRAY') { } 0 0 0
elsif (ref $default_value eq 'HASH') { } 0 0 0
elsif ($default_value eq 'undef') { } 0 0 0
elsif (defined $default_value and not ref $default_value) { } 0 0 0
8689 1 ▶ $p->{type} = $default_value =~ /\./ ? 'number' : 'integer';
Branch: 1 branch
BranchTrueFalseTotal
$default_value =~ /\./ ? : 0 1 1
8690 } elsif (ref($default_value) eq 'ARRAY') {
8691 0 $p->{type} = 'arrayref';
8692 } elsif (ref($default_value) eq 'HASH') {
8693 0 $p->{type} = 'hashref';
8694 } elsif ($default_value eq 'undef') {
8695 0 $p->{type} = 'scalar'; # undef can be any scalar
8696 ▶ } elsif (defined $default_value && !ref($default_value)) {
Condition0%0/3
defined $default_value and not ref $default_value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $default_value and not ref $default_value
defined $default_value not ref $default_value
Truth table0%0/3
A and not(B)
A= defined $default_valueB= not ref $default_value
ABresult
0X0
100
111
8697 0 $p->{type} = 'string';
8698 }
8699 }
8700  
8701 1 ▶ $self->_log(" CODE: $param has default value: " . (ref($default_value) ? ref($default_value) . ' ref' : $default_value));
Branch: 1 branch
BranchTrueFalseTotal
ref $default_value ? : 0 1 1
8702 }
8703  
8704 # Also check for simple default assignment without condition
8705 # Pattern: $param = 'value';
8706 550 ▶ if (!$default_value && !exists $p->{_default} && $code =~ /\$$param\s*=\s*([^;{}]+?)(?:\s*[;}])/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $default_value || exists $p->{"_default"} and $code =~ /\$$param\s*=\s*([^;{}]+?)(?:\s*[;}])/s) 102 448 550
Condition100%6/6
not $default_value || exists $p->{"_default"} and $code =~ /\$$param\s*=\s*([^;{}]+?)(?:\s*[;}])/s
!ll&&!rl&&r
42406102
$default_value || exists $p->{'_default'}
l!l&&r!l&&!r
2814508
MC/DC100%3/3
not $default_value || exists $p->{"_default"} and $code =~ /\$$param\s*=\s*([^;{}]+?)(?:\s*[;}])/s
$default_value exists $p->{'_default'} $code =~ /\$$param\s*=\s*([^;{}]+?)(?:\s*[;}])/s
Truth table100%4/4
not(A || B) and C
A= $default_valueB= exists $p->{'_default'}C= $code =~ /\$$param\s*=\s*([^;{}]+?)(?:\s*[;}])/s
ABCresult
1XX0
01X0
0000
0011
8707 102 my $assignment = $1;
8708 # Make sure it's not part of a larger expression
8709 102 ▶ if ($assignment !~ /\$$param/ && $assignment !~ /^shift/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($assignment =~ /\$$param/ or $assignment =~ /^shift/) 57 45 102
Condition100%3/3
$assignment =~ /\$$param/ or $assignment =~ /^shift/
l!l&&r!l&&!r
14457
MC/DC100%2/2
$assignment =~ /\$$param/ or $assignment =~ /^shift/
$assignment =~ /\$$param/ $assignment =~ /^shift/
Truth table100%3/3
A or B
A= $assignment =~ /\$$param/B= $assignment =~ /^shift/
ABresult
1X1
011
000
8710 57 my $possible_default = $assignment;
8711 57 $possible_default =~ s/\s*;\s*$//;
8712 57 $possible_default = $self->_clean_default_value($possible_default);
8713 57 ▶ if (defined $possible_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $possible_default) 24 33 57
8714 24 $p->{_default} = $possible_default;
8715 24 $p->{optional} = 1;
8716 24 $self->_log(" CODE: $param has unconditional default: $possible_default");
8717 }
8718 }
8719 }
8720  
8721 # A bare truthiness test ("if($dir) {...}") is a presence test: the
8722 # routine does something different when nothing was passed, so the
8723 # argument can be omitted. An explicit required check wins over this.
8724 550 ▶ if (!$is_required && ($code =~ /\b(?:if|unless)\s*\(\s*\$$param\s*\)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_required and $code =~ /\b(?:if|unless)\s*\(\s*\$$param\s*\)/) 1 549 550
Condition100%3/3
not $is_required and $code =~ /\b(?:if|unless)\s*\(\s*\$$param\s*\)/
!ll&&!rl&&r
794701
MC/DC100%2/2
not $is_required and $code =~ /\b(?:if|unless)\s*\(\s*\$$param\s*\)/
not $is_required $code =~ /\b(?:if|unless)\s*\(\s*\$$param\s*\)/
Truth table100%3/3
not(A) and B
A= not $is_requiredB= $code =~ /\b(?:if|unless)\s*\(\s*\$$param\s*\)/
ABresult
0X0
100
111
8725 1 $p->{optional} = 1;
8726 1 $self->_log(" CODE: $param is optional (only used when truthy)");
8727 }
8728  
8729 # Explicit required check overrides default detection
8730 550 ▶ if ($is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required) 79 471 550
8731 79 $p->{optional} = 0;
8732 79 ▶ delete $p->{_default} if exists $p->{_default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{"_default"} 15 64 79
8733 79 $self->_log(" CODE: $param is required (validation check)");
8734 }
8735 }
8736  
8737 # --------------------------------------------------
8738 # _positional_pod_input
8739 #
8740 # Purpose: Recognise a lone documented input parameter that is really the
8741 # first positional argument under another name.
8742 #
8743 # Entry: $pod - parameters from _analyze_pod; mutated.
8744 # $code_params - parameters from code analysis; mutated.
8745 # $code - the subroutine body as source text.
8746 #
8747 # Exit: Returns the documented name when it is treated as position 0,
8748 # otherwise undef.
8749 #
8750 # Notes: Two or more documented names describe a hash of arguments, and
8751 # a body that reads an argument hash (my %args = @_, $args->{...},
8752 # $_[0]{...}) keeps the named style even with one name. A body
8753 # that shows no positional use at all -- a wrapper forwarding @_,
8754 # say -- proves nothing either way, so it stays named rather than
8755 # guessing.
8756 #
8757 # Side effects: On success sets position 0, drops stray documented names and
8758 # replaces the code analysis with the one argument, renamed.
8759 # --------------------------------------------------
8760 797 ▶sub _positional_pod_input {
Subroutine _positional_pod_input: called CC=7 SCAR=19.5
8761 797 my ($self, $pod, $code_params, $code) = @_;
8762  
8763 797 ▶ my @documented = grep { ($pod->{$_}{_source} // '') eq SOURCE_INPUT_SPEC } keys %$pod;
Condition100%2/2
$pod->{$_}{"_source"} // ""
l!l
49346
MC/DC100%1/1
$pod->{$_}{"_source"} // ""
$pod->{$_}{"_source"}
Truth table100%2/2
A
A= $pod->{$_}{"_source"}
Aresult
11
00
8764 797 ▶ return undef unless @documented == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @documented == 1 606 191 797
8765 191 ▶ return undef unless $self->_argument_style($code) eq 'positional';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_argument_style($code) eq "positional" 57 134 191
8766  
8767 134 my ($name) = @documented;
8768 134 $pod->{$name}{position} = 0;
8769  
8770 # The body names the same argument however it likes, so carry its facts
8771 # (a directory tested with -d, say) to the documented name.
8772 134 my @code_names = keys %$code_params;
8773 134 ▶ my $source = $code_params->{$name} ? $name
Branch: 2 branches
BranchTrueFalseTotal
$code_params->{$name} ? : 130 4 134
@code_names == 1 ? : 0 4 4
8774 : (@code_names == 1 ? $code_names[0] : undef);
8775  
8776 # Empty list, not {}: a hashref here would become a one-element list and
8777 # land in the hash as a "HASH(0x...)" key.
8778 134 ▶ %$code_params = $source ? ($name => $code_params->{$source}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$source ? : 130 4 134
8779 134 %$pod = ($name => $pod->{$name});
8780 134 $self->_log(" POD: single Input spec '$name' describes a positional argument");
8781  
8782 134 return $name;
8783 }
8784  
8785 # --------------------------------------------------
8786 # _argument_style
8787 #
8788 # Purpose: Classify how a subroutine reads its arguments.
8789 #
8790 # Entry: $code - the subroutine body as source text.
8791 #
8792 # Exit: 'named' when the body reads an argument hash, 'positional'
8793 # when it reads an argument by position, '' when neither shows.
8794 #
8795 # Notes: $self->{...} is object state, not an argument, so it is removed
8796 # before looking for hash dereferences. '' is not 'positional':
8797 # a wrapper that only forwards @_ proves nothing, so it keeps the
8798 # named style these heuristics started from. An invocant shift
8799 # is not an argument.
8800 #
8801 # Side effects: None.
8802 # --------------------------------------------------
8803 191 ▶sub _argument_style {
Subroutine _argument_style: called CC=9 SCAR=22.5
8804 191 my ($self, $code) = @_;
8805  
8806 191 ▶ return '' unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 191 191
8807  
8808 191 my $body = $code;
8809 191 $body =~ s/\$self\s*->\s*\{[^}]*\}//g;
8810  
8811 191 ▶ return 'named' if($body =~ /\bmy\s+%\w+\s*=[^;]*\@_/s);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\bmy\s+%\w+\s*=[^;]*\@_/s 0 191 191
8812 191 ▶ return 'named' if($body =~ /\bmy\s*\(\s*[^)]*%\w+\s*\)\s*=\s*\@_/s);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\bmy\s*\(\s*[^)]*%\w+\s*\)\s*=\s*\@_/s 9 182 191
8813 182 ▶ return 'named' if($body =~ /\$_\s*\[\s*\d+\s*\]\s*(?:->\s*)?\{/);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\$_\s*\[\s*\d+\s*\]\s*(?:->\s*)?\{/ 0 182 182
8814 182 ▶ return 'named' if($body =~ /\$\w+\s*->\s*\{/);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\$\w+\s*->\s*\{/ 48 134 182
8815  
8816 134 ▶ return 'positional' if($body =~ /\$_\s*\[\s*\d+\s*\]/);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\$_\s*\[\s*\d+\s*\]/ 22 112 134
8817 112 ▶ return 'positional' if($body =~ /\bmy\s+\$(?!self\b|class\b)\w+\s*=\s*shift\b/);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\bmy\s+\$(?!self\b|class\b)\w+\s*=\s*shift\b/ 33 79 112
8818 79 ▶ return 'positional' if($body =~ /\bmy\s*\((?!\s*\$(?:self|class)\s*\))[^)]*\)\s*=\s*\@_/s);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\bmy\s*\((?!\s*\$(?:self|class)\s*\))[^)]*\)\s*=\s*\@_/s 79 0 79
8819  
8820 0 return '';
8821 }
8822  
8823 # --------------------------------------------------
8824 # _merge_parameter_analyses
8825 #
8826 # Purpose: Merge parameter information from POD,
8827 # code, and signature analysis into a
8828 # single authoritative parameter hashref
8829 # for each parameter.
8830 #
8831 # Entry: $pod - hashref of parameters from POD
8832 # analysis.
8833 # $code - hashref of parameters from
8834 # code analysis.
8835 # $sig - hashref of parameters from
8836 # signature analysis (optional,
8837 # defaults to empty hashref).
8838 #
8839 # Exit: Returns a merged hashref of parameter
8840 # name to spec hashref. Each spec has
8841 # all available information combined,
8842 # with POD taking highest priority,
8843 # code second, and signature filling
8844 # remaining gaps.
8845 #
8846 # Side effects: Logs merged parameter details to
8847 # stdout when verbose is set.
8848 #
8849 # Notes: Position is determined by majority
8850 # vote across all sources, with the
8851 # lowest position winning ties. Optional
8852 # status is determined by
8853 # _determine_optional_status. The single
8854 # _source provenance marker is retained on
8855 # each merged parameter.
8856 # --------------------------------------------------
8857 # --------------------------------------------------
8858 # _sanitize_parameter_names
8859 #
8860 # Purpose: Drop keys that cannot be parameter names, and report them.
8861 #
8862 # Entry: $params - hashref keyed by parameter name; modified in place.
8863 # $where - the analysis stage, for the warning text.
8864 #
8865 # Exit: Number of keys dropped.
8866 #
8867 # Notes: A hashref used where a list of key/value pairs was meant
8868 # stringifies to "HASH(0x...)" and silently becomes a parameter.
8869 # Such a key is never a variable the routine can read, so the
8870 # schema would test an argument that does not exist. Perl hash
8871 # keys are always strings, so the string form is what we see.
8872 #
8873 # Side effects: Emits a carp warning for each dropped key.
8874 # --------------------------------------------------
8875 4626 ▶sub _sanitize_parameter_names {
Subroutine _sanitize_parameter_names: called CC=5 SCAR=16.2
8876 4626 my ($self, $params, $where) = @_;
8877  
8878 4626 my $dropped = 0;
8879 4626 for my $name (keys %$params) {
8880 3302 ▶ my $invalid = ref($name)
Condition66%4/6
ref $name || $name =~ /\A(?:HASH|ARRAY|SCALAR|CODE|GLOB|REF|REGEXP|Regexp|LVALUE|VSTRING|FORMAT|IO)\(0x[0-9a-fA-F]+\)\z/ || $name =~ /\s/
l!l&&r!l&&!r
203300
ref $name or $name =~ /\A(?:HASH|ARRAY|SCALAR|CODE|GLOB|REF|REGEXP|Regexp|LVALUE|VSTRING|FORMAT|IO)\(0x[0-9a-fA-F]+\)\z/
l!l&&r!l&&!r
023300
MC/DC33%1/3
ref $name || $name =~ /\A(?:HASH|ARRAY|SCALAR|CODE|GLOB|REF|REGEXP|Regexp|LVALUE|VSTRING|FORMAT|IO)\(0x[0-9a-fA-F]+\)\z/ || $name =~ /\s/
ref $name $name =~ /\A(?:HASH|ARRAY|SCALAR|CODE|GLOB|REF|REGEXP|Regexp|LVALUE|VSTRING|FORMAT|IO)\(0x[0-9a-fA-F]+\)\z/ $name =~ /\s/
Truth table50%2/4
A or B || C
A= ref $nameB= $name =~ /\A(?:HASH|ARRAY|SCALAR|CODE|GLOB|REF|REGEXP|Regexp|LVALUE|VSTRING|FORMAT|IO)\(0x[0-9a-fA-F]+\)\z/C= $name =~ /\s/
ABCresult
1XX1
01X1
0011
0000
8881 || $name =~ /\A(?:HASH|ARRAY|SCALAR|CODE|GLOB|REF|REGEXP|Regexp|LVALUE|VSTRING|FORMAT|IO)\(0x[0-9a-fA-F]+\)\z/
8882 || $name =~ /\s/;
8883 3302 ▶ next unless $invalid;
Branch: 1 branch
BranchTrueFalseTotal
unless $invalid 3300 2 3302
8884  
8885 2 carp(__PACKAGE__ . ": ignoring parameter key '$name' from $where: not a parameter name");
8886 2 delete $params->{$name};
8887 2 $dropped++;
8888 }
8889  
8890 4626 return $dropped;
8891 }
8892  
8893 797 ▶sub _merge_parameter_analyses {
Subroutine _merge_parameter_analyses: called CC=55 SCAR=46.1
8894 797 my ($self, $pod, $code, $sig) = @_;
8895  
8896 # Belt and braces: every analysis should already be clean, but a merge is
8897 # the last point before these keys become schema parameters.
8898 $self->_sanitize_parameter_names($_, 'parameter merge')
8899 797 for grep { ref($_) eq 'HASH' } ($pod, $code, $sig);
8900  
8901 # A routine's POD may name a positional argument differently from the
8902 # variable the body assigns it to (`$value` documented, `$val` in code).
8903 # Calls are positional and the POD is the specification, so the code
8904 # analysis of that slot belongs to the documented parameter instead of
8905 # inventing a second argument.
8906 797 ▶ if(ref($pod) eq 'HASH' && ref($code) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pod eq "HASH" and ref $code eq "HASH") 797 0 797
Condition33%1/3
ref $pod eq "HASH" and ref $code eq "HASH"
!ll&&!rl&&r
00797
MC/DC0%0/2
ref $pod eq "HASH" and ref $code eq "HASH"
ref $pod eq "HASH" ref $code eq "HASH"
Truth table33%1/3
A and B
A= ref $pod eq "HASH"B= ref $code eq "HASH"
ABresult
0X0
100
111
8907 797 for my $code_name (keys %$code) {
8908 525 ▶ next if exists $pod->{$code_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pod->{$code_name} 351 174 525
8909 174 my $pos = $code->{$code_name}{position};
8910 174 ▶ next unless defined($pos);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 174 174
8911 my ($pod_name) = grep {
8912 174 !exists($code->{$_})
8913 && defined($pod->{$_}{position})
8914 17 ▶ && ($pod->{$_}{position} == $pos)
Condition83%5/6
!exists $code->{$_} && defined $pod->{$_}{'position'} && $pod->{$_}{'position'} == $pos
!ll&&!rl&&r
1601
not exists $code->{$_} and defined $pod->{$_}{"position"}
!ll&&!rl&&r
1421
MC/DC66%2/3
!exists $code->{$_} && defined $pod->{$_}{'position'} && $pod->{$_}{'position'} == $pos
not exists $code->{$_} defined $pod->{$_}{"position"} $pod->{$_}{'position'} == $pos
Truth table75%3/4
not(A) and B && C
A= not exists $code->{$_}B= defined $pod->{$_}{"position"}C= $pod->{$_}{'position'} == $pos
ABCresult
0XX0
10X0
1100
1111
8915 } sort keys %$pod;
8916 174 ▶ next unless defined($pod_name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod_name 173 1 174
8917 1 $code->{$pod_name} = delete $code->{$code_name};
8918 1 $self->_log(" MERGE: code parameter \$$code_name is the documented \$$pod_name");
8919 }
8920 }
8921  
8922 797 my %merged;
8923  
8924 797 my @constraint_keys = qw(min max memberof matches nomatch notmemberof);
8925 797 my %is_constraint = map { $_ => 1 } @constraint_keys;
8926  
8927 # Start with all parameters from all sources
8928 797 my %all_params = map { $_ => 1 } (keys %$pod, keys %$code, keys %$sig);
8929  
8930 797 foreach my $param (keys %all_params) {
8931 712 my $p = $merged{$param} = {};
8932  
8933 # Collect position from all sources
8934 712 my @positions;
8935 712 ▶ push @positions, $pod->{$param}{position} if $pod->{$param} && defined $pod->{$param}{position};
Branch: 1 branch
BranchTrueFalseTotal
if $pod->{$param} and defined $pod->{$param}{"position"} 263 449 712
Condition100%3/3
$pod->{$param} and defined $pod->{$param}{"position"}
!ll&&!rl&&r
173276263
MC/DC100%2/2
$pod->{$param} and defined $pod->{$param}{"position"}
$pod->{$param} defined $pod->{$param}{"position"}
Truth table100%3/3
A and B
A= $pod->{$param}B= defined $pod->{$param}{"position"}
ABresult
0X0
100
111
8936 712 ▶ push @positions, $sig->{$param}{position} if $sig->{$param} && defined $sig->{$param}{position};
Branch: 1 branch
BranchTrueFalseTotal
if $sig->{$param} and defined $sig->{$param}{"position"} 0 712 712
Condition33%1/3
$sig->{$param} and defined $sig->{$param}{"position"}
!ll&&!rl&&r
71200
MC/DC0%0/2
$sig->{$param} and defined $sig->{$param}{"position"}
$sig->{$param} defined $sig->{$param}{"position"}
Truth table33%1/3
A and B
A= $sig->{$param}B= defined $sig->{$param}{"position"}
ABresult
0X0
100
111
8937 712 ▶ push @positions, $code->{$param}{position} if $code->{$param} && defined $code->{$param}{position};
Branch: 1 branch
BranchTrueFalseTotal
if $code->{$param} and defined $code->{$param}{"position"} 525 187 712
Condition66%2/3
$code->{$param} and defined $code->{$param}{"position"}
!ll&&!rl&&r
1870525
MC/DC50%1/2
$code->{$param} and defined $code->{$param}{"position"}
$code->{$param} defined $code->{$param}{"position"}
Truth table66%2/3
A and B
A= $code->{$param}B= defined $code->{$param}{"position"}
ABresult
0X0
100
111
8938  
8939 # Use the most common position, or lowest if tie
8940 712 ▶ if (@positions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@positions) 544 168 712
8941 544 my %pos_count;
8942 544 $pos_count{$_}++ for @positions;
8943 0 ▶ my ($best_pos) = sort { $pos_count{$b} <=> $pos_count{$a} || $a <=> $b } keys %pos_count;
Condition0%0/3
$pos_count{$b} <=> $pos_count{$a} || $a <=> $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos_count{$b} <=> $pos_count{$a} || $a <=> $b
$pos_count{$b} <=> $pos_count{$a} $a <=> $b
Truth table0%0/3
A || B
A= $pos_count{$b} <=> $pos_count{$a}B= $a <=> $b
ABresult
1X1
011
000
8944 544 ▶ $p->{position} = $best_pos unless(exists($p->{position}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p->{"position"} 544 0 544
8945 }
8946  
8947 # POD has highest priority for type info and explicit declarations
8948 712 ▶ if ($pod->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod->{$param}) 539 173 712
8949 539 %$p = (%$p, %{$pod->{$param}});
8950 539 ▶ $p->{_source} //= SOURCE_POD;
Condition100%2/2
$p->{"_source"} //= "pod"
l!l
49346
MC/DC100%1/1
$p->{"_source"} //= "pod"
$p->{"_source"}
Truth table100%2/2
A
A= $p->{"_source"}
Aresult
11
00
8951  
8952 # Record which source each constraint came from, so a documented
8953 # minimum does not make a maximum enforced by the code tolerant.
8954 539 ▶ my $source = $pod->{$param}{_source} // SOURCE_POD;
Condition100%2/2
$pod->{$param}{"_source"} // "pod"
l!l
49346
MC/DC100%1/1
$pod->{$param}{"_source"} // "pod"
$pod->{$param}{"_source"}
Truth table100%2/2
A
A= $pod->{$param}{"_source"}
Aresult
11
00
8955 539 for my $key (@constraint_keys) {
8956 3234 ▶ next unless exists($pod->{$param}{$key}) && defined($pod->{$param}{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pod->{$param}{$key} and defined $pod->{$param}{$key} 3122 112 3234
Condition66%2/3
exists $pod->{$param}{$key} and defined $pod->{$param}{$key}
!ll&&!rl&&r
31220112
MC/DC50%1/2
exists $pod->{$param}{$key} and defined $pod->{$param}{$key}
exists $pod->{$param}{$key} defined $pod->{$param}{$key}
Truth table66%2/3
A and B
A= exists $pod->{$param}{$key}B= defined $pod->{$param}{$key}
ABresult
0X0
100
111
8957 112 ▶ $p->{_constraint_source}{$key} //= $source;
Condition33%1/3
$p->{"_constraint_source"}{$key} //= $source
l!l&&r!l&&!r
01120
MC/DC0%0/2
$p->{"_constraint_source"}{$key} //= $source
$p->{"_constraint_source"}{$key} $source
Truth table33%1/3
A //= B
A= $p->{"_constraint_source"}{$key}B= $source
ABresult
1X1
011
000
8958 }
8959 }
8960  
8961 # Code analysis adds concrete evidence (but doesn't override POD explicit types)
8962 712 ▶ if ($code->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->{$param}) 525 187 712
8963 525 foreach my $key (keys %{$code->{$param}}) {
8964 2097 ▶ next if $key eq '_source';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_source" 457 1640 2097
8965 1640 ▶ next if $key eq 'position';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "position" 525 1115 1640
8966 # A formal input-spec row that names no type says the argument
8967 # exists and in which position, not that it has none: the
8968 # code's evidence still supplies the type.
8969  
8970 # Only override if POD didn't provide this info or it's a stronger signal
8971 1115 my $from_pod = exists $pod->{$param};
8972 # A documented type is the specification; only a type the code
8973 # shows outright replaces it, not one a pattern in the body
8974 # was guessed to imply (qr/$pattern/ is not division).
8975 1115 ▶ my $heuristic_type = (($code->{$param}{_type_confidence} // '') eq 'heuristic');
Condition100%2/2
$code->{$param}{"_type_confidence"} // ""
l!l
364751
MC/DC100%1/1
$code->{$param}{"_type_confidence"} // ""
$code->{$param}{"_type_confidence"}
Truth table100%2/2
A
A= $code->{$param}{"_type_confidence"}
Aresult
11
00
8976 1115 ▶ if (!exists $p->{$key} ||
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $p->{$key} or $key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string" and not $heuristic_type) 684 431 1115
Condition93%14/15
not exists $p->{$key} or $key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string" and not $heuristic_type
l!l&&r!l&&!r
6813431
$key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string" and not $heuristic_type
!ll&&!rl&&r
42473
$key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string"
!ll&&!rl&&r
3943010
$key eq "type" and $from_pod and $p->{"type"} eq "string"
!ll&&!rl&&r
26113340
$key eq "type" and $from_pod
!ll&&!rl&&r
2610173
MC/DC83%5/6
not exists $p->{$key} or $key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string" and not $heuristic_type
not exists $p->{$key} $key eq "type" $from_pod $p->{"type"} eq "string" $code->{$param}{$key} ne "string" not $heuristic_type
Truth table85%6/7
not(A) or B and C and D and E and not(F)
A= not exists $p->{$key}B= $key eq "type"C= $from_podD= $p->{"type"} eq "string"E= $code->{$param}{$key} ne "string"F= not $heuristic_type
ABCDEFresult
1XXXXX1
0111111
00XXXX0
010XXX0
0110XX0
01110X0
0111100
8977 ($key eq 'type' && $from_pod && $p->{type} eq 'string' &&
8978 $code->{$param}{$key} ne 'string' && !$heuristic_type)) {
8979 684 $p->{$key} = $code->{$param}{$key};
8980 684 ▶ $p->{_constraint_source}{$key} = SOURCE_CODE if $is_constraint{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $is_constraint{$key} 43 641 684
8981 }
8982 }
8983 525 ▶ $p->{_source} //= $code->{$param}{_source};
Condition100%3/3
$p->{"_source"} //= $code->{$param}{"_source"}
l!l&&r!l&&!r
35215815
MC/DC100%2/2
$p->{"_source"} //= $code->{$param}{"_source"}
$p->{"_source"} $code->{$param}{"_source"}
Truth table100%3/3
A //= B
A= $p->{"_source"}B= $code->{$param}{"_source"}
ABresult
1X1
011
000
8984 }
8985  
8986 # Signature fills in remaining gaps
8987 712 ▶ if ($sig->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->{$param}) 0 712 712
8988 0 foreach my $key (keys %{$sig->{$param}}) {
8989 0 ▶ next if $key eq '_source';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_source" 0 0 0
8990 0 ▶ next if $key eq 'position';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "position" 0 0 0
8991 0 ▶ next if defined($p->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{$key} 0 0 0
8992 0 $p->{$key} = $sig->{$param}{$key};
8993 0 ▶ $p->{_constraint_source}{$key} = SOURCE_SIGNATURE if $is_constraint{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $is_constraint{$key} 0 0 0
8994 }
8995 0 ▶ $p->{_source} //= $sig->{$param}{_source};
Condition0%0/3
$p->{"_source"} //= $sig->{$param}{"_source"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p->{"_source"} //= $sig->{$param}{"_source"}
$p->{"_source"} $sig->{$param}{"_source"}
Truth table0%0/3
A //= B
A= $p->{"_source"}B= $sig->{$param}{"_source"}
ABresult
1X1
011
000
8996 }
8997  
8998 # The source is the single provenance decision: validator-backed
8999 # parameters are type-checked, POD/input-spec parameters are
9000 # documentation, and code/signature/field parameters are inferred.
9001  
9002 # Handle optional field with better logic
9003 712 $self->_determine_optional_status($p, $pod->{$param}, $code->{$param});
9004 }
9005  
9006 # Debug logging
9007 797 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 2 795 797
9008 2 ▶ foreach my $param (sort { ($merged{$a}{position} || 999) <=> ($merged{$b}{position} || 999) } keys %merged) {
Condition50%2/4
$merged{$a}{'position'} || 999
l!l
10
$merged{$b}{'position'} || 999
l!l
01
MC/DC0%0/1
$merged{$a}{'position'} || 999
$merged{$a}{'position'}
MC/DC0%0/1
$merged{$b}{'position'} || 999
$merged{$b}{'position'}
Truth table50%1/2
A
A= $merged{$a}{'position'}
Aresult
11
00
Truth table50%1/2
A
A= $merged{$b}{'position'}
Aresult
11
00
9009 2 my $p = $merged{$param};
9010 $self->_log(" MERGED $param: " .
9011 'pos=' . (defined($p->{position}) ? $p->{position} : 'none') .
9012 ", type=" . ($p->{type} || 'none') .
9013 2 ▶ ", optional=" . (defined($p->{optional}) ? $p->{optional} : 'undef'));
Branch: 2 branches
BranchTrueFalseTotal
defined $p->{'position'} ? : 2 0 2
defined $p->{'optional'} ? : 2 0 2
Condition50%1/2
$p->{'type'} || 'none'
l!l
20
MC/DC0%0/1
$p->{'type'} || 'none'
$p->{'type'}
Truth table50%1/2
A
A= $p->{'type'}
Aresult
11
00
9014 }
9015 }
9016  
9017 797 return \%merged;
9018 }
9019  
9020 # --------------------------------------------------
9021 # _determine_optional_status
9022 #
9023 # Purpose: Set the optional field on a merged
9024 # parameter spec based on evidence from
9025 # POD and code analysis, with POD taking
9026 # highest priority.
9027 #
9028 # Entry: $merged_param - the merged parameter
9029 # hashref (modified in
9030 # place).
9031 # $pod_param - parameter spec from
9032 # POD analysis, or undef.
9033 # $code_param - parameter spec from
9034 # code analysis, or undef.
9035 #
9036 # Exit: Returns nothing. Sets or leaves
9037 # $merged_param->{optional}.
9038 #
9039 # Side effects: None.
9040 # --------------------------------------------------
9041 716 ▶sub _determine_optional_status {
Subroutine _determine_optional_status: called CC=6 SCAR=17.9
9042 716 my ($self, $merged_param, $pod_param, $code_param) = @_;
9043  
9044 716 ▶ my $pod_optional = $pod_param ? $pod_param->{optional} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pod_param ? : 543 173 716
9045 716 ▶ my $code_optional = $code_param ? $code_param->{optional} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$code_param ? : 529 187 716
9046  
9047 # Explicit POD declaration wins
9048 716 ▶ if (defined $pod_optional) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $pod_optional) { } 418 298 716
elsif (defined $code_optional) { } 265 33 298
elsif (keys %$merged_param > 0) { } 33 0 33
9049 418 $merged_param->{optional} = $pod_optional;
9050 }
9051 # Code validation evidence
9052 elsif (defined $code_optional) {
9053 265 $merged_param->{optional} = $code_optional;
9054 }
9055 # Default: if we have any info about the param, assume required
9056 elsif (keys %$merged_param > 0) {
9057 33 $merged_param->{optional} = 0;
9058 }
9059 # Otherwise leave undef (unknown)
9060 }
9061  
9062  
9063 # --------------------------------------------------
9064 # _calculate_input_confidence
9065 #
9066 # Purpose: Calculate a confidence score and level
9067 # for the input parameter analysis,
9068 # based on how much type, constraint,
9069 # and semantic information was inferred
9070 # for each parameter.
9071 #
9072 # Entry: $params - hashref of merged parameter
9073 # specs as produced by
9074 # _merge_parameter_analyses.
9075 #
9076 # Exit: Returns a hashref with keys:
9077 # level - one of: none,
9078 # very_low, low,
9079 # medium, high
9080 # score - numeric average
9081 # across all params
9082 # factors - arrayref of
9083 # human-readable
9084 # factor strings
9085 # per_parameter - hashref of per-
9086 # parameter score
9087 # and factor detail
9088 # Returns { level => 'none', ... } if
9089 # no parameters were found.
9090 #
9091 # Side effects: None.
9092 # --------------------------------------------------
9093 492 ▶sub _calculate_input_confidence {
Subroutine _calculate_input_confidence: called CC=24 SCAR=31.8
9094 492 my ($self, $params) = @_;
9095  
9096 492 my @factors; # Track all confidence factors
9097  
9098 492 ▶ return { level => 'none', factors => ['No parameters found'] } unless keys %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$params 180 312 492
9099  
9100 312 my $total_score = 0;
9101 312 my $count = 0;
9102 312 my %param_details; # Store per-parameter analysis
9103  
9104 312 foreach my $param (keys %$params) {
9105 464 my $p = $params->{$param};
9106 464 my $score = 0;
9107 464 my @param_factors;
9108  
9109 # Type information
9110 464 ▶ if ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'type'}) { } 464 0 464
9111 464 ▶ if ($p->{type} eq 'string' && ($p->{min} || $p->{max} || $p->{matches})) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->{'type'} eq 'string' and $p->{'min'} || $p->{'max'} || $p->{'matches'}) { } 55 409 464
elsif ($p->{'type'} eq 'string') { } 162 247 409
Condition100%9/9
$p->{"type"} eq "string" and $p->{"min"} || $p->{"max"} || $p->{"matches"}
!ll&&!rl&&r
24716255
$p->{"min"} or $p->{"max"} or $p->{"matches"}
l!l&&r!l&&!r
4312162
$p->{"min"} or $p->{"max"}
l!l&&r!l&&!r
358174
MC/DC100%4/4
$p->{"type"} eq "string" and $p->{"min"} || $p->{"max"} || $p->{"matches"}
$p->{"type"} eq "string" $p->{"min"} $p->{"max"} $p->{"matches"}
Truth table100%5/5
A and B or C or D
A= $p->{"type"} eq "string"B= $p->{"min"}C= $p->{"max"}D= $p->{"matches"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
9112 55 $score += 25;
9113 55 push @param_factors, "Type: constrained string (+25)";
9114 } elsif ($p->{type} eq 'string') {
9115 162 $score += 10;
9116 162 push @param_factors, "Type: plain string (+10)";
9117 } else {
9118 247 $score += 30;
9119 247 push @param_factors, "Type: $p->{type} (+30)";
9120 }
9121 } else {
9122 0 push @param_factors, "No type information (-0)";
9123 }
9124  
9125 # Constraints
9126 464 ▶ if (defined $p->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"min"}) 62 402 464
9127 62 $score += 15;
9128 62 push @param_factors, 'Has min constraint (+15)';
9129 }
9130 464 ▶ if (defined $p->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"max"}) 39 425 464
9131 39 $score += 15;
9132 39 push @param_factors, "Has max constraint (+15)";
9133 }
9134 464 ▶ if (defined $p->{optional}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"optional"}) 459 5 464
9135 459 $score += 20;
9136 459 push @param_factors, "Optional/required explicitly defined (+20)";
9137 }
9138 464 ▶ if ($p->{matches}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"matches"}) 25 439 464
9139 25 $score += 20;
9140 25 push @param_factors, 'Has regex pattern constraint (+20)';
9141 }
9142 464 ▶ if ($p->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"isa"}) 41 423 464
9143 41 $score += 25;
9144 41 push @param_factors, "Specific class constraint: $p->{isa} (+25)";
9145 }
9146  
9147 # Position information
9148 464 ▶ if (defined $p->{position}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"position"}) 321 143 464
9149 321 $score += 10;
9150 321 push @param_factors, "Position defined: $p->{position} (+10)";
9151 }
9152  
9153 # Default value
9154 464 ▶ if (exists $p->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p->{"_default"}) 35 429 464
9155 35 $score += 10;
9156 35 push @param_factors, "Has default value (+10)";
9157 }
9158  
9159 # Semantic information
9160 464 ▶ if ($p->{semantic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"semantic"}) 33 431 464
9161 33 $score += 15;
9162 33 push @param_factors, "Semantic type: $p->{semantic} (+15)";
9163 }
9164  
9165 464 $param_details{$param} = {
9166 score => $score,
9167 factors => \@param_factors
9168 };
9169  
9170 464 $total_score += $score;
9171 464 $count++;
9172 }
9173  
9174 312 ▶ my $avg = $count ? ($total_score / $count) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$count ? : 312 0 312
9175  
9176 # Build summary factors
9177 312 ▶ push @factors, sprintf("Analyzed %d parameter%s", $count, $count == 1 ? '' : 's');
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 239 73 312
9178 312 push @factors, sprintf("Average confidence score: %.1f", $avg);
9179  
9180 # Add top contributing factors
9181 312 my @sorted_params = sort { $param_details{$b}{score} <=> $param_details{$a}{score} } keys %param_details;
9182  
9183 312 ▶ if (@sorted_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_params) 312 0 312
9184 312 my $highest = $sorted_params[0];
9185 312 my $highest_score = $param_details{$highest}{score};
9186 312 push @factors, sprintf("Highest scoring parameter: \$$highest (score: %d)", $highest_score);
9187  
9188 312 ▶ if (@sorted_params > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_params > 1) 73 239 312
9189 73 my $lowest = $sorted_params[-1];
9190 73 my $lowest_score = $param_details{$lowest}{score};
9191 73 push @factors, sprintf("Lowest scoring parameter: \$$lowest (score: %d)", $lowest_score);
9192 }
9193 }
9194  
9195 # Determine confidence level
9196 312 my $level;
9197 312 ▶ if ($avg >= $CONFIDENCE_HIGH_THRESHOLD) {
Branch: 3 branches
BranchTrueFalseTotal
if ($avg >= $CONFIDENCE_HIGH_THRESHOLD) { } 226 86 312
elsif ($avg >= $CONFIDENCE_MEDIUM_THRESHOLD) { } 66 20 86
elsif ($avg >= $CONFIDENCE_LOW_THRESHOLD) { } 18 2 20
9198 226 $level = $LEVEL_HIGH;
9199 226 push @factors, "High confidence: comprehensive type and constraint information";
9200 } elsif ($avg >= $CONFIDENCE_MEDIUM_THRESHOLD) {
9201 66 $level = $LEVEL_MEDIUM;
9202 66 push @factors, "Medium confidence: some type or constraint information present";
9203 } elsif ($avg >= $CONFIDENCE_LOW_THRESHOLD) {
9204 18 $level = $LEVEL_LOW;
9205 18 push @factors, "Low confidence: minimal type information";
9206 } else {
9207 2 $level = $LEVEL_VERY_LOW;
9208 2 push @factors, "Very low confidence: little to no type information";
9209 }
9210  
9211 return {
9212 312 level => $level,
9213 score => $avg,
9214 factors => \@factors,
9215 per_parameter => \%param_details
9216 };
9217 }
9218  
9219 # --------------------------------------------------
9220 # _calculate_output_confidence
9221 #
9222 # Purpose: Calculate a confidence score and level
9223 # for the output analysis based on how
9224 # much return type, value, class,
9225 # context, and error convention
9226 # information was determined.
9227 #
9228 # Entry: $output - the output hashref as built
9229 # by _analyze_output.
9230 #
9231 # Exit: Returns a hashref with keys:
9232 # level - one of: none, very_low,
9233 # low, medium, high
9234 # score - numeric confidence score
9235 # factors - arrayref of factor strings
9236 # Returns { level => 'none', ... } if
9237 # output is empty.
9238 #
9239 # Side effects: None.
9240 # --------------------------------------------------
9241 827 ▶sub _calculate_output_confidence {
Subroutine _calculate_output_confidence: called CC=17 SCAR=28.3
9242 827 my ($self, $output) = @_;
9243  
9244 827 my @factors;
9245  
9246 827 ▶ return { level => 'none', factors => ['No return information found'] } unless keys %$output;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$output 23 804 827
9247  
9248 804 my $score = 0;
9249  
9250 # Type information
9251 804 ▶ if ($output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{'type'}) { } 797 7 804
9252 797 $score += 30;
9253 797 push @factors, "Return type defined: $output->{type} (+30)";
9254 } else {
9255 7 push @factors, 'No return type information (-0)';
9256 }
9257  
9258 # Specific value known
9259 804 ▶ if (defined $output->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output->{"value"}) 36 768 804
9260 36 $score += 30;
9261 36 push @factors, "Specific return value: $output->{value} (+30)";
9262 }
9263  
9264 # Class information for objects
9265 804 ▶ if ($output->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"isa"}) 94 710 804
9266 94 $score += 30;
9267 94 push @factors, "Returns specific class: $output->{isa} (+30)";
9268 }
9269  
9270 # Context-aware returns
9271 804 ▶ if ($output->{_context_aware}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_context_aware"}) 7 797 804
9272 7 $score += 20;
9273 7 push @factors, "Context-aware return (wantarray) (+20)";
9274  
9275 7 ▶ if ($output->{_list_context}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_list_context"}) 7 0 7
9276 7 push @factors, " List context: $output->{_list_context}{type}";
9277 }
9278 7 ▶ if ($output->{_scalar_context}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_scalar_context"}) 6 1 7
9279 6 push @factors, " Scalar context: $output->{_scalar_context}{type}";
9280 }
9281 }
9282  
9283 # Error handling information
9284 804 ▶ if ($output->{_error_return}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_return"}) 37 767 804
9285 37 $score += 15;
9286 37 push @factors, "Error return convention documented: $output->{_error_return} (+15)";
9287 }
9288  
9289 # Success/failure pattern
9290 804 ▶ if ($output->{_success_failure_pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_success_failure_pattern"}) 8 796 804
9291 8 $score += 10;
9292 8 push @factors, 'Success/failure pattern detected (+10)';
9293 }
9294  
9295 # Chainable methods
9296 804 ▶ if ($output->{_returns_self}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_returns_self"}) 13 791 804
9297 13 $score += 15;
9298 13 push @factors, "Chainable method (fluent interface) (+15)";
9299 }
9300  
9301 # Void context
9302 804 ▶ if ($output->{_void_context}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_void_context"}) 12 792 804
9303 12 $score += 20;
9304 12 push @factors, "Void context method (no meaningful return) (+20)";
9305 }
9306  
9307 # Exception handling
9308 804 ▶ if ($output->{_error_handling} && $output->{_error_handling}{exception_handling}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_handling"} and $output->{"_error_handling"}{"exception_handling"}) 20 784 804
Condition100%3/3
$output->{"_error_handling"} and $output->{"_error_handling"}{"exception_handling"}
!ll&&!rl&&r
7523220
MC/DC100%2/2
$output->{"_error_handling"} and $output->{"_error_handling"}{"exception_handling"}
$output->{"_error_handling"} $output->{"_error_handling"}{"exception_handling"}
Truth table100%3/3
A and B
A= $output->{"_error_handling"}B= $output->{"_error_handling"}{"exception_handling"}
ABresult
0X0
100
111
9309 20 $score += 10;
9310 20 push @factors, 'Exception handling present (+10)';
9311 }
9312  
9313 804 push @factors, sprintf("Total output confidence score: %d", $score);
9314  
9315 # Determine confidence level
9316 804 my $level;
9317 804 ▶ if ($score >= $CONFIDENCE_HIGH_THRESHOLD) {
Branch: 3 branches
BranchTrueFalseTotal
if ($score >= $CONFIDENCE_HIGH_THRESHOLD) { } 132 672 804
elsif ($score >= $CONFIDENCE_MEDIUM_THRESHOLD) { } 64 608 672
elsif ($score >= $CONFIDENCE_LOW_THRESHOLD) { } 605 3 608
9318 132 $level = $LEVEL_HIGH;
9319 132 push @factors, "High confidence: detailed return type and behavior";
9320 } elsif ($score >= $CONFIDENCE_MEDIUM_THRESHOLD) {
9321 64 $level = $LEVEL_MEDIUM;
9322 64 push @factors, "Medium confidence: return type defined";
9323 } elsif ($score >= $CONFIDENCE_LOW_THRESHOLD) {
9324 605 $level = $LEVEL_LOW;
9325 605 push @factors, "Low confidence: minimal return information";
9326 } else {
9327 3 $level = $LEVEL_VERY_LOW;
9328 3 push @factors, 'Very low confidence: little return information';
9329 }
9330  
9331 return {
9332 804 level => $level,
9333 score => $score,
9334 factors => \@factors
9335 };
9336 }
9337  
9338 # --------------------------------------------------
9339 # _generate_confidence_report
9340 #
9341 # Purpose: Generate a human-readable text report
9342 # of all confidence factors for a
9343 # schema, for debugging and review
9344 # purposes.
9345 #
9346 # Entry: $schema - schema hashref containing
9347 # a populated _analysis key.
9348 #
9349 # Exit: Returns a multi-line string report,
9350 # or nothing if $schema->{_analysis}
9351 # is absent.
9352 #
9353 # Side effects: None.
9354 # --------------------------------------------------
9355 sub _generate_confidence_report
9356 0 ▶{
Subroutine _generate_confidence_report: not called CC=10 SCAR=47.0
9357 0 my ($self, $schema) = @_;
9358  
9359 0 ▶ return unless $schema->{_analysis};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"_analysis"} 0 0 0
9360  
9361 0 my $analysis = $schema->{_analysis};
9362 0 my @report;
9363  
9364 0 ▶ push @report, "Confidence Analysis for " . ($schema->{method_name} || 'method');
Condition0%0/2
$schema->{'method_name'} || 'method'
l!l
00
MC/DC0%0/1
$schema->{'method_name'} || 'method'
$schema->{'method_name'}
Truth table0%0/2
A
A= $schema->{'method_name'}
Aresult
11
00
9365 0 push @report, '=' x 60;
9366 0 push @report, '';
9367  
9368 0 push @report, "Overall Confidence: " . uc($analysis->{overall_confidence});
9369 0 push @report, '';
9370  
9371 0 ▶ if ($analysis->{confidence_factors}{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($analysis->{"confidence_factors"}{"input"}) 0 0 0
9372 push @report, (
9373 "Input Parameters:",
9374 " Confidence Level: " . uc($analysis->{input_confidence})
9375 0 );
9376 0 foreach my $factor (@{$analysis->{confidence_factors}{input}}) {
9377 0 push @report, " - $factor";
9378 }
9379 0 push @report, '';
9380 }
9381  
9382 0 ▶ if ($analysis->{confidence_factors}{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($analysis->{"confidence_factors"}{"output"}) 0 0 0
9383 push @report, 'Return Value:',
9384 0 " Confidence Level: " . uc($analysis->{output_confidence});
9385 0 foreach my $factor (@{$analysis->{confidence_factors}{output}}) {
9386 0 push @report, " - $factor";
9387 }
9388 0 push @report, '';
9389 }
9390  
9391 0 ▶ if ($analysis->{per_parameter_scores}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($analysis->{"per_parameter_scores"}) 0 0 0
9392 0 push @report, 'Per-Parameter Analysis:';
9393 0 foreach my $param (sort keys %{$analysis->{per_parameter_scores}}) {
9394 0 my $details = $analysis->{per_parameter_scores}{$param};
9395 0 push @report, " \$$param (score: $details->{score}):";
9396 0 foreach my $factor (@{$details->{factors}}) {
9397 0 push @report, " - $factor";
9398 }
9399 }
9400 0 push @report, '';
9401 }
9402  
9403 0 return join("\n", @report);
9404 }
9405  
9406 # --------------------------------------------------
9407 # _generate_notes
9408 #
9409 # Purpose: Generate human-readable advisory notes
9410 # about parameters whose type or
9411 # optionality could not be determined,
9412 # to guide manual schema review.
9413 #
9414 # Entry: $params - hashref of merged parameter
9415 # specs.
9416 #
9417 # Exit: Returns an arrayref of note strings.
9418 # Returns an empty arrayref if all
9419 # parameters have known types and
9420 # optionality.
9421 #
9422 # Side effects: None.
9423 # --------------------------------------------------
9424 816 ▶sub _generate_notes {
Subroutine _generate_notes: called CC=4 SCAR=13.9
9425 816 my ($self, $params) = @_;
9426  
9427 816 my @notes;
9428  
9429 816 foreach my $param (sort keys %$params) {
9430 743 my $p = $params->{$param};
9431  
9432 743 ▶ unless ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 113 630 743
9433 113 push @notes, "$param: type unknown - please review - will set to 'string' as a default";
9434 }
9435  
9436 743 ▶ unless (defined $p->{optional}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p->{"optional"}) 11 732 743
9437 11 push @notes, "$param: optional status unknown";
9438 # Don't automatically set - let it be undef if we don't know
9439 }
9440 }
9441  
9442 816 return \@notes;
9443 }
9444  
9445 # --------------------------------------------------
9446 # _set_defaults
9447 #
9448 # Purpose: Apply default type values to any
9449 # parameters in a schema mode (input
9450 # or output) whose type was not set
9451 # during analysis, setting them to
9452 # 'string' as a conservative fallback.
9453 #
9454 # Entry: $schema - the schema hashref being
9455 # built by _analyze_method.
9456 # $mode - either 'input' or 'output'.
9457 #
9458 # Exit: Returns nothing. Modifies $schema in
9459 # place by setting type => 'string' on
9460 # any parameter that lacks a type, and
9461 # downgrading input confidence to 'low'.
9462 #
9463 # Side effects: Logs type defaulting to stdout when
9464 # verbose is set.
9465 #
9466 # Notes: Called after all analysis is complete
9467 # so that genuine type unknowns can be
9468 # distinguished from analysis gaps.
9469 # --------------------------------------------------
9470 1626 ▶sub _set_defaults {
Subroutine _set_defaults: called CC=4 SCAR=13.9
9471 1626 my ($self, $schema, $mode) = @_;
9472  
9473 1626 my $params = $schema->{$mode};
9474  
9475 1626 foreach my $param (keys %$params) {
9476 2003 my $p = $params->{$param};
9477  
9478 2003 ▶ next unless(ref($p) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p eq "HASH" 1197 806 2003
9479 806 ▶ unless ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 164 642 806
9480 164 $self->_log(" DEBUG {$mode}{$param}: Setting to 'string' as a default");
9481 164 $p->{'type'} = 'string';
9482 164 $schema->{_confidence}{$mode}->{level} = 'low'; # Setting a default means it's a guess
9483 }
9484 }
9485 }
9486  
9487 # --------------------------------------------------
9488 # _analyze_relationships
9489 #
9490 # Purpose: Detect inter-parameter relationships
9491 # in a method's source code, including
9492 # mutually exclusive parameters, required
9493 # groups, conditional requirements,
9494 # dependencies, and value-based
9495 # constraints.
9496 #
9497 # Entry: $method - method hashref containing
9498 # at minimum a 'body' key
9499 # with the source string.
9500 #
9501 # Exit: Returns an arrayref of relationship
9502 # hashrefs. Returns an empty arrayref
9503 # if no parameters or no relationships
9504 # are found.
9505 #
9506 # Side effects: Logs detections to stdout when
9507 # verbose is set.
9508 #
9509 # Notes: Parameter names are extracted via
9510 # _extract_parameters_from_signature, so
9511 # every style it supports -- my (...) =
9512 # @_, shift-style (my $x = shift), direct-
9513 # index ($_[N]), and modern signatures --
9514 # is analysed for relationships, not just
9515 # the my (...) = @_ list-assignment form.
9516 # --------------------------------------------------
9517 813 ▶sub _analyze_relationships {
Subroutine _analyze_relationships: called CC=2 SCAR=6.9
9518 813 my ($self, $method) = @_;
9519  
9520 813 my $code = $method->{body};
9521 813 my @relationships;
9522  
9523 # Extract all parameter names from the method, using the same
9524 # multi-style detection used for schema population so shift-style
9525 # and modern-signature methods get relationship analysis too
9526 my %params;
9527 813 $self->_extract_parameters_from_signature(\%params, $code);
9528 813 my @param_names = sort { $params{$a}{position} <=> $params{$b}{position} } keys %params;
9529  
9530 813 ▶ return [] unless @param_names;
Branch: 1 branch
BranchTrueFalseTotal
unless @param_names 470 343 813
9531  
9532 # Detect mutually exclusive parameters
9533 343 push @relationships, @{$self->_detect_mutually_exclusive($code, \@param_names)};
9534  
9535 # Detect required groups (OR logic)
9536 343 push @relationships, @{$self->_detect_required_groups($code, \@param_names)};
9537  
9538 # Detect conditional requirements (IF-THEN)
9539 343 push @relationships, @{$self->_detect_conditional_requirements($code, \@param_names)};
9540  
9541 # Detect dependencies
9542 343 push @relationships, @{$self->_detect_dependencies($code, \@param_names)};
9543  
9544 # Detect value-based constraints
9545 343 push @relationships, @{$self->_detect_value_constraints($code, \@param_names)};
9546  
9547 # Deduplicate relationships
9548 343 my @unique = $self->_deduplicate_relationships(\@relationships);
9549  
9550 343 return \@unique;
9551 }
9552  
9553 # --------------------------------------------------
9554 # _deduplicate_relationships
9555 #
9556 # Purpose: Remove duplicate relationship entries
9557 # from the relationships list by
9558 # computing a canonical signature for
9559 # each relationship type.
9560 #
9561 # Entry: $relationships - arrayref of
9562 # relationship hashrefs.
9563 #
9564 # Exit: Returns a deduplicated list of
9565 # relationship hashrefs.
9566 #
9567 # Side effects: None.
9568 # --------------------------------------------------
9569 347 ▶sub _deduplicate_relationships {
Subroutine _deduplicate_relationships: called CC=9 SCAR=22.0
9570 347 my ($self, $relationships) = @_;
9571  
9572 347 my @unique;
9573 my %seen;
9574  
9575 347 foreach my $rel (@$relationships) {
9576 # Create a signature for this relationship
9577 26 my $sig;
9578 26 ▶ if ($rel->{type} eq 'mutually_exclusive') {
Branch: 6 branches
BranchTrueFalseTotal
if ($rel->{'type'} eq 'mutually_exclusive') { } 9 17 26
elsif ($rel->{'type'} eq 'required_group') { } 4 13 17
elsif ($rel->{'type'} eq 'conditional_requirement') { } 7 6 13
elsif ($rel->{'type'} eq 'dependency') { } 3 3 6
elsif ($rel->{'type'} eq 'value_constraint') { } 2 1 3
elsif ($rel->{'type'} eq 'value_conditional') { } 1 0 1
9579 9 $sig = join(':', 'mutex', sort @{$rel->{params}});
9580 } elsif ($rel->{type} eq 'required_group') {
9581 4 $sig = join(':', 'reqgroup', sort @{$rel->{params}});
9582 } elsif ($rel->{type} eq 'conditional_requirement') {
9583 7 $sig = join(':', 'condreq', $rel->{if}, $rel->{then_required});
9584 } elsif ($rel->{type} eq 'dependency') {
9585 3 $sig = join(':', 'dep', $rel->{param}, $rel->{requires});
9586 } elsif ($rel->{type} eq 'value_constraint') {
9587 2 $sig = join(':', 'valcon', $rel->{if}, $rel->{then}, $rel->{operator}, $rel->{value});
9588 } elsif ($rel->{type} eq 'value_conditional') {
9589 1 $sig = join(':', 'valcond', $rel->{if}, $rel->{equals}, $rel->{then_required});
9590 } else {
9591 0 $sig = join(':', $rel->{type}, %$rel);
9592 }
9593  
9594 26 ▶ unless ($seen{$sig}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$sig}++) 20 6 26
9595 20 push @unique, $rel;
9596 }
9597 }
9598  
9599 347 return @unique;
9600 }
9601  
9602 # --------------------------------------------------
9603 # _detect_mutually_exclusive
9604 #
9605 # Purpose: Detect pairs of parameters that cannot
9606 # be specified together, by searching
9607 # for die/croak/confess patterns
9608 # that fire when both are truthy.
9609 #
9610 # Entry: $code - method body source string.
9611 # $param_names - arrayref of parameter
9612 # name strings.
9613 #
9614 # Exit: Returns an arrayref of relationship
9615 # hashrefs of type 'mutually_exclusive'.
9616 # Returns an empty arrayref if none found.
9617 #
9618 # Side effects: Logs detections to stdout when
9619 # verbose is set.
9620 # --------------------------------------------------
9621 346 ▶sub _detect_mutually_exclusive {
Subroutine _detect_mutually_exclusive: called CC=18 SCAR=30.2
9622 346 my ($self, $code, $param_names) = @_;
9623  
9624 346 my @relationships;
9625  
9626 # Pattern 1: die/croak if $x && $y
9627 # Look for: die/croak ... if $param1 && $param2
9628 346 foreach my $param1 (@$param_names) {
9629 473 foreach my $param2 (@$param_names) {
9630 915 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 473 442 915
9631  
9632 # Check various patterns
9633 442 ▶ if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2/ or $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param2\s+&&\s+\$$param1/) 12 430 442
Condition100%3/3
$code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2/ or $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param2\s+&&\s+\$$param1/
l!l&&r!l&&!r
66430
MC/DC100%2/2
$code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2/ or $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param2\s+&&\s+\$$param1/
$code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2/ $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param2\s+&&\s+\$$param1/
Truth table100%3/3
A or B
A= $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2/B= $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param2\s+&&\s+\$$param1/
ABresult
1X1
011
000
9634 $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param2\s+&&\s+\$$param1/) {
9635  
9636 # Avoid duplicates (param1,param2 vs param2,param1)
9637 12 my $found_reverse = 0;
9638 12 foreach my $rel (@relationships) {
9639 8 ▶ if ($rel->{type} eq 'mutually_exclusive' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "mutually_exclusive" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)) 6 2 8
Condition66%4/6
$rel->{"type"} eq "mutually_exclusive" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
!ll&&!rl&&r
026
$rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1
!ll&&!rl&&r
206
MC/DC33%1/3
$rel->{"type"} eq "mutually_exclusive" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
$rel->{"type"} eq "mutually_exclusive" $rel->{"params"}[0] eq $param2 $rel->{"params"}[1] eq $param1
Truth table50%2/4
A and B and C
A= $rel->{"type"} eq "mutually_exclusive"B= $rel->{"params"}[0] eq $param2C= $rel->{"params"}[1] eq $param1
ABCresult
0XX0
10X0
1100
1111
9640 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
9641 6 $found_reverse = 1;
9642 6 last;
9643 }
9644 }
9645  
9646 12 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 6 6 12
9647  
9648 6 push @relationships, {
9649 type => 'mutually_exclusive',
9650 params => [$param1, $param2],
9651 description => "Cannot specify both $param1 and $param2"
9652 };
9653  
9654 6 $self->_log(" RELATIONSHIP: $param1 and $param2 are mutually exclusive");
9655 }
9656  
9657 # Pattern 2: die "Cannot specify both X and Y"
9658 436 ▶ if ($code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*$param1[^'"]*$param2/i ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param1}[^'"]*$param2/i or $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param2}[^'"]*$param1/i) 1 435 436
Condition66%2/3
$code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param1}[^'"]*$param2/i or $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param2}[^'"]*$param1/i
l!l&&r!l&&!r
10435
MC/DC50%1/2
$code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param1}[^'"]*$param2/i or $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param2}[^'"]*$param1/i
$code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param1}[^'"]*$param2/i $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param2}[^'"]*$param1/i
Truth table66%2/3
A or B
A= $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param1}[^'"]*$param2/iB= $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*${param2}[^'"]*$param1/i
ABresult
1X1
011
000
9659 $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*$param2[^'"]*$param1/i) {
9660  
9661 1 my $found_reverse = 0;
9662 1 foreach my $rel (@relationships) {
9663 1 ▶ if ($rel->{type} eq 'mutually_exclusive' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "mutually_exclusive" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)) 0 1 1
Condition33%2/6
$rel->{"type"} eq "mutually_exclusive" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
!ll&&!rl&&r
010
$rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1
!ll&&!rl&&r
100
MC/DC0%0/3
$rel->{"type"} eq "mutually_exclusive" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
$rel->{"type"} eq "mutually_exclusive" $rel->{"params"}[0] eq $param2 $rel->{"params"}[1] eq $param1
Truth table25%1/4
A and B and C
A= $rel->{"type"} eq "mutually_exclusive"B= $rel->{"params"}[0] eq $param2C= $rel->{"params"}[1] eq $param1
ABCresult
0XX0
10X0
1100
1111
9664 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
9665 0 $found_reverse = 1;
9666 0 last;
9667 }
9668 }
9669  
9670 1 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 0 1 1
9671  
9672 1 push @relationships, {
9673 type => 'mutually_exclusive',
9674 params => [$param1, $param2],
9675 description => "Cannot specify both $param1 and $param2"
9676 };
9677  
9678 1 $self->_log(" RELATIONSHIP: $param1 and $param2 are mutually exclusive (from error message)");
9679 }
9680 }
9681 }
9682  
9683 346 return \@relationships;
9684 }
9685  
9686 # --------------------------------------------------
9687 # _detect_required_groups
9688 #
9689 # Purpose: Detect parameter groups where at least
9690 # one parameter must be specified (OR
9691 # logic), by searching for die/croak
9692 # patterns that fire unless any of the
9693 # group is truthy.
9694 #
9695 # Entry: $code - method body source string.
9696 # $param_names - arrayref of parameter
9697 # name strings.
9698 #
9699 # Exit: Returns an arrayref of relationship
9700 # hashrefs of type 'required_group'.
9701 # Returns an empty arrayref if none found.
9702 #
9703 # Side effects: Logs detections to stdout when
9704 # verbose is set.
9705 # --------------------------------------------------
9706 345 ▶sub _detect_required_groups {
Subroutine _detect_required_groups: called CC=18 SCAR=31.4
9707 345 my ($self, $code, $param_names) = @_;
9708  
9709 345 my @relationships;
9710  
9711 # Pattern 1: die/croak unless $x || $y
9712 345 foreach my $param1 (@$param_names) {
9713 471 foreach my $param2 (@$param_names) {
9714 911 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 471 440 911
9715  
9716 440 ▶ if ($code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param1\s+\|\|\s+\$$param2/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param1\s+\|\|\s+\$$param2/ or $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param2\s+\|\|\s+\$$param1/) 6 434 440
Condition100%3/3
$code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param1\s+\|\|\s+\$$param2/ or $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param2\s+\|\|\s+\$$param1/
l!l&&r!l&&!r
33434
MC/DC100%2/2
$code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param1\s+\|\|\s+\$$param2/ or $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param2\s+\|\|\s+\$$param1/
$code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param1\s+\|\|\s+\$$param2/ $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param2\s+\|\|\s+\$$param1/
Truth table100%3/3
A or B
A= $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param1\s+\|\|\s+\$$param2/B= $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param2\s+\|\|\s+\$$param1/
ABresult
1X1
011
000
9717 $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param2\s+\|\|\s+\$$param1/) {
9718  
9719 # Avoid duplicates
9720 6 my $found_reverse = 0;
9721 6 foreach my $rel (@relationships) {
9722 3 ▶ if ($rel->{type} eq 'required_group' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "required_group" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)) 3 0 3
Condition33%2/6
$rel->{"type"} eq "required_group" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
!ll&&!rl&&r
003
$rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1
!ll&&!rl&&r
003
MC/DC0%0/3
$rel->{"type"} eq "required_group" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
$rel->{"type"} eq "required_group" $rel->{"params"}[0] eq $param2 $rel->{"params"}[1] eq $param1
Truth table25%1/4
A and B and C
A= $rel->{"type"} eq "required_group"B= $rel->{"params"}[0] eq $param2C= $rel->{"params"}[1] eq $param1
ABCresult
0XX0
10X0
1100
1111
9723 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
9724 3 $found_reverse = 1;
9725 3 last;
9726 }
9727 }
9728  
9729 6 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 3 3 6
9730  
9731 3 push @relationships, {
9732 type => 'required_group',
9733 params => [$param1, $param2],
9734 logic => 'or',
9735 description => "Must specify either $param1 or $param2"
9736 };
9737  
9738 3 $self->_log(" RELATIONSHIP: Must specify either $param1 or $param2");
9739 }
9740  
9741 # Pattern 2: die "Must specify either X or Y"
9742 437 ▶ if ($code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*$param1[^'"]*or[^'"]*$param2/i ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param1}[^'"]*or[^'"]*$param2/i or $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param2}[^'"]*or[^'"]*$param1/i) 1 436 437
Condition66%2/3
$code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param1}[^'"]*or[^'"]*$param2/i or $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param2}[^'"]*or[^'"]*$param1/i
l!l&&r!l&&!r
10436
MC/DC50%1/2
$code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param1}[^'"]*or[^'"]*$param2/i or $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param2}[^'"]*or[^'"]*$param1/i
$code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param1}[^'"]*or[^'"]*$param2/i $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param2}[^'"]*or[^'"]*$param1/i
Truth table66%2/3
A or B
A= $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param1}[^'"]*or[^'"]*$param2/iB= $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*${param2}[^'"]*or[^'"]*$param1/i
ABresult
1X1
011
000
9743 $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*$param2[^'"]*or[^'"]*$param1/i) {
9744  
9745 1 my $found_reverse = 0;
9746 1 foreach my $rel (@relationships) {
9747 1 ▶ if ($rel->{type} eq 'required_group' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "required_group" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)) 0 1 1
Condition33%2/6
$rel->{"type"} eq "required_group" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
!ll&&!rl&&r
010
$rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1
!ll&&!rl&&r
100
MC/DC0%0/3
$rel->{"type"} eq "required_group" and ($rel->{"params"}[0] eq $param2 and $rel->{"params"}[1] eq $param1)
$rel->{"type"} eq "required_group" $rel->{"params"}[0] eq $param2 $rel->{"params"}[1] eq $param1
Truth table25%1/4
A and B and C
A= $rel->{"type"} eq "required_group"B= $rel->{"params"}[0] eq $param2C= $rel->{"params"}[1] eq $param1
ABCresult
0XX0
10X0
1100
1111
9748 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
9749 0 $found_reverse = 1;
9750 0 last;
9751 }
9752 }
9753  
9754 1 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 0 1 1
9755  
9756 1 push @relationships, {
9757 type => 'required_group',
9758 params => [$param1, $param2],
9759 logic => 'or',
9760 description => "Must specify either $param1 or $param2"
9761 };
9762  
9763 1 $self->_log(" RELATIONSHIP: Must specify either $param1 or $param2 (from error message)");
9764 }
9765 }
9766 }
9767  
9768 345 return \@relationships;
9769 }
9770  
9771 # --------------------------------------------------
9772 # _detect_conditional_requirements
9773 #
9774 # Purpose: Detect IF-THEN parameter relationships
9775 # where one parameter being present
9776 # makes another required, by searching
9777 # for die/croak patterns of the form
9778 # 'die if $x && !$y'.
9779 #
9780 # Entry: $code - method body source string.
9781 # $param_names - arrayref of parameter
9782 # name strings.
9783 #
9784 # Exit: Returns an arrayref of relationship
9785 # hashrefs of type
9786 # 'conditional_requirement'.
9787 # Returns an empty arrayref if none found.
9788 #
9789 # Side effects: Logs detections to stdout when
9790 # verbose is set.
9791 # --------------------------------------------------
9792 345 ▶sub _detect_conditional_requirements {
Subroutine _detect_conditional_requirements: called CC=7 SCAR=19.6
9793 345 my ($self, $code, $param_names) = @_;
9794  
9795 345 my @relationships;
9796  
9797 345 foreach my $param1 (@$param_names) {
9798 471 foreach my $param2 (@$param_names) {
9799 911 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 471 440 911
9800  
9801 # Pattern 1: die if $x && !$y (if x then y required)
9802 440 ▶ if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+!\$$param2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+!\$$param2/) 4 436 440
9803 4 push @relationships, {
9804 type => 'conditional_requirement',
9805 if => $param1,
9806 then_required => $param2,
9807 description => "When $param1 is specified, $param2 is required"
9808 };
9809  
9810 4 $self->_log(" RELATIONSHIP: $param1 requires $param2");
9811 }
9812  
9813 # Pattern 2: die if $x && !defined($y)
9814 440 ▶ if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+!defined\s*\(\s*\$$param2\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+!defined\s*\(\s*\$$param2\s*\)/) 0 440 440
9815 0 push @relationships, {
9816 type => 'conditional_requirement',
9817 if => $param1,
9818 then_required => $param2,
9819 description => "When $param1 is specified, $param2 is required"
9820 };
9821  
9822 0 $self->_log(" RELATIONSHIP: $param1 requires $param2 (defined check)");
9823 }
9824  
9825 # Pattern 3: Error message "X requires Y"
9826 440 ▶ if ($code =~ /(?:die|croak|confess)\s+['"]\w*$param1[^'"]*requires[^'"]*$param2/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)\s+['"]\w*${param1}[^'"]*requires[^'"]*$param2/i) 4 436 440
9827 4 push @relationships, {
9828 type => 'conditional_requirement',
9829 if => $param1,
9830 then_required => $param2,
9831 description => "When $param1 is specified, $param2 is required"
9832 };
9833  
9834 4 $self->_log(" RELATIONSHIP: $param1 requires $param2 (from error message)");
9835 }
9836 }
9837 }
9838  
9839 345 return \@relationships;
9840 }
9841  
9842 # --------------------------------------------------
9843 # _detect_dependencies
9844 #
9845 # Purpose: Detect simple parameter dependencies
9846 # where one parameter requires another
9847 # to also be present, by combining
9848 # error message pattern matching with
9849 # code condition matching.
9850 #
9851 # Entry: $code - method body source string.
9852 # $param_names - arrayref of parameter
9853 # name strings.
9854 #
9855 # Exit: Returns an arrayref of relationship
9856 # hashrefs of type 'dependency'.
9857 # Returns an empty arrayref if none found.
9858 #
9859 # Side effects: Logs detections to stdout when
9860 # verbose is set.
9861 # --------------------------------------------------
9862 347 ▶sub _detect_dependencies {
Subroutine _detect_dependencies: called CC=6 SCAR=17.9
9863 347 my ($self, $code, $param_names) = @_;
9864  
9865 347 my @relationships;
9866  
9867 347 foreach my $param1 (@$param_names) {
9868 475 foreach my $param2 (@$param_names) {
9869 919 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 475 444 919
9870  
9871 # Pattern 1: Error message mentions "X requires Y" AND code checks $x && !$y
9872 # Split into two checks to be more flexible
9873 444 ▶ if (($code =~ /(?:die|croak|confess)\s+['"]\w*$param1[^'"]*requires[^'"]*$param2/i) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)\s+['"]\w*${param1}[^'"]*requires[^'"]*$param2/i and $code =~ /if\s+\$$param1\s+&&\s+!\$$param2/) 6 438 444
Condition100%3/3
$code =~ /(?:die|croak|confess)\s+['"]\w*${param1}[^'"]*requires[^'"]*$param2/i and $code =~ /if\s+\$$param1\s+&&\s+!\$$param2/
!ll&&!rl&&r
43626
MC/DC100%2/2
$code =~ /(?:die|croak|confess)\s+['"]\w*${param1}[^'"]*requires[^'"]*$param2/i and $code =~ /if\s+\$$param1\s+&&\s+!\$$param2/
$code =~ /(?:die|croak|confess)\s+['"]\w*${param1}[^'"]*requires[^'"]*$param2/i $code =~ /if\s+\$$param1\s+&&\s+!\$$param2/
Truth table100%3/3
A and B
A= $code =~ /(?:die|croak|confess)\s+['"]\w*${param1}[^'"]*requires[^'"]*$param2/iB= $code =~ /if\s+\$$param1\s+&&\s+!\$$param2/
ABresult
0X0
100
111
9874 ($code =~ /if\s+\$$param1\s+&&\s+!\$$param2/)) {
9875  
9876 6 push @relationships, {
9877 type => 'dependency',
9878 param => $param1,
9879 requires => $param2,
9880 description => "$param1 requires $param2 to be specified"
9881 };
9882  
9883 6 $self->_log(" RELATIONSHIP: $param1 depends on $param2");
9884 }
9885 }
9886 }
9887  
9888 347 return \@relationships;
9889 }
9890  
9891 # --------------------------------------------------
9892 # _detect_value_constraints
9893 #
9894 # Purpose: Detect value-based constraints between
9895 # parameters, such as 'if $ssl then
9896 # $port must equal 443' or 'if $mode
9897 # eq secure then $key is required'.
9898 #
9899 # Entry: $code - method body source string.
9900 # $param_names - arrayref of parameter
9901 # name strings.
9902 #
9903 # Exit: Returns an arrayref of relationship
9904 # hashrefs of type 'value_constraint'
9905 # or 'value_conditional'.
9906 # Returns an empty arrayref if none found.
9907 #
9908 # Side effects: Logs detections to stdout when
9909 # verbose is set.
9910 # --------------------------------------------------
9911 343 ▶sub _detect_value_constraints {
Subroutine _detect_value_constraints: called CC=7 SCAR=19.7
9912 343 my ($self, $code, $param_names) = @_;
9913  
9914 343 my @relationships;
9915  
9916 343 foreach my $param1 (@$param_names) {
9917 467 foreach my $param2 (@$param_names) {
9918 903 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 467 436 903
9919  
9920 # Pattern 1: die if $x && $y != value
9921 436 ▶ if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2\s*!=\s*(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2\s*!=\s*(\d+)/) 2 434 436
9922 2 my $value = $1;
9923 2 push @relationships, {
9924 type => 'value_constraint',
9925 if => $param1,
9926 then => $param2,
9927 operator => '==',
9928 value => $value,
9929 description => "When $param1 is specified, $param2 must equal $value"
9930 };
9931  
9932 2 $self->_log(" RELATIONSHIP: $param1 requires $param2 == $value");
9933 }
9934  
9935 # Pattern 2: die if $x && $y < value
9936 436 ▶ if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2\s*<\s*(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+&&\s+\$$param2\s*<\s*(\d+)/) 0 436 436
9937 0 my $value = $1;
9938 0 push @relationships, {
9939 type => 'value_constraint',
9940 if => $param1,
9941 then => $param2,
9942 operator => '>=',
9943 value => $value,
9944 description => "When $param1 is specified, $param2 must be >= $value"
9945 };
9946  
9947 0 $self->_log(" RELATIONSHIP: $param1 requires $param2 >= $value");
9948 }
9949  
9950 # Pattern 3: die if $x eq 'value' && !$y
9951 436 ▶ if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+eq\s+['"]([^'"]+)['"]\s+&&\s+!\$$param2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:die|croak|confess)[^;]*if\s+\$$param1\s+eq\s+['"]([^'"]+)['"]\s+&&\s+!\$$param2/) 1 435 436
9952 1 my $value = $1;
9953 1 push @relationships, {
9954 type => 'value_conditional',
9955 if => $param1,
9956 equals => $value,
9957 then_required => $param2,
9958 description => "When $param1 equals '$value', $param2 is required"
9959 };
9960  
9961 1 $self->_log(" RELATIONSHIP: $param1='$value' requires $param2");
9962 }
9963 }
9964 }
9965  
9966 343 return \@relationships;
9967 }
9968  
9969 # Write a single method schema to a YAML file in output_dir.
9970 #
9971 # Entry: $method_name is a non-empty string; $schema is a hashref.
9972 # Exit: YAML file written to output_dir/$method_name.yml.
9973 # Side effects: Creates output_dir if it does not exist.
9974 # Notes: Croaks if output_dir was not set in new().
9975  
9976 537 ▶sub _write_schema {
Subroutine _write_schema: called CC=36 SCAR=36.0
9977 537 my ($self, $method_name, $schema) = @_;
9978  
9979 # output_dir is required here — croak early with a clear message
9980 # rather than letting make_path fail with a cryptic error
9981 537 ▶ croak(__PACKAGE__, ': output_dir must be provided to new() when writing schema files') unless defined $self->{output_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"output_dir"} 1 536 537
9982  
9983 536 ▶ make_path($self->{output_dir}) unless -d $self->{output_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"output_dir"} 7 529 536
9984  
9985 536 my $filename = "$self->{output_dir}/${method_name}.yml";
9986  
9987 536 local $YAML::XS::QuoteNumericStrings = 0;
9988  
9989 # Extract package name for module field
9990 536 my $package_name = '';
9991 536 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 534 2 536
9992 534 my $package_stmt = $self->{_document}->find_first('PPI::Statement::Package');
9993 534 ▶ $package_name = $package_stmt ? $package_stmt->namespace : '';
Branch: 1 branch
BranchTrueFalseTotal
$package_stmt ? : 529 5 534
9994 534 ▶ $self->{_package_name} //= $package_name;
Condition33%1/3
$self->{"_package_name"} //= $package_name
l!l&&r!l&&!r
53400
MC/DC0%0/2
$self->{"_package_name"} //= $package_name
$self->{"_package_name"} $package_name
Truth table33%1/3
A //= B
A= $self->{"_package_name"}B= $package_name
ABresult
1X1
011
000
9995 }
9996  
9997 # Clean up schema for output - use the format expected by App::Test::Generator::Template
9998 536 my $output = {
9999 function => $method_name,
10000 module => $package_name,
10001 config => {
10002 close_stdin => 1,
10003 dedup => 1,
10004 test_nuls => 0,
10005 test_undef => 0,
10006 test_empty => 1,
10007 test_non_ascii => 0,
10008 test_security => 0
10009 }
10010 };
10011  
10012 # A file the POD documents the routine creating or writing is the
10013 # requested result, not a leftover, so turn that destructive check off.
10014 536 ▶ if($schema->{_analysis}{side_effects}{writes_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_analysis"}{"side_effects"}{"writes_file"}) 10 526 536
10015 10 $output->{config}{check_files} = 0;
10016 }
10017 # A routine that returns a database handle keeps a descriptor open by
10018 # design; the harness is told not to call that a leak.
10019 536 ▶ if($schema->{_analysis}{side_effects}{keeps_fd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_analysis"}{"side_effects"}{"keeps_fd"}) 2 534 536
10020 2 $output->{config}{check_fds} = 0;
10021 }
10022  
10023 # The routine has a precondition no generated value can meet, so there is
10024 # nothing meaningful to fuzz; the harness and all-fuzz report it skipped.
10025 536 ▶ $output->{skip} = $schema->{skip} if($schema->{skip});
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"skip"} 19 517 536
10026  
10027 # A class method is called as Module->routine(...), not Module::routine(...).
10028 536 ▶ $output->{invocant} = $schema->{invocant} if($schema->{invocant});
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"invocant"} 3 533 536
10029  
10030 # Process input parameters with advanced type handling
10031 536 ▶ if($schema->{'input'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"input"}) 519 17 536
10032 519 ▶ if(scalar(keys %{$schema->{'input'}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$schema{'input'};}) { } 356 163 519
10033 356 $output->{'input'} = {};
10034  
10035 356 foreach my $param_name (sort keys %{$schema->{'input'}}) {
10036 578 my $param = $schema->{'input'}{$param_name};
10037 578 ▶ if($param->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"name"}) 2 576 578
10038 2 my $name = delete $param->{name};
10039 2 ▶ if($name ne $param_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne $param_name) 0 2 2
10040 # Sanity check
10041 0 croak("BUG: Parameter name - expected $param_name, got $name");
10042 }
10043 }
10044 578 my $cleaned_param = $self->_serialize_parameter_for_yaml($param);
10045 578 $output->{'input'}{$param_name} = $cleaned_param;
10046 }
10047  
10048 # If some params have positions and others neither a position nor
10049 # named: 1, treat the whole input as a named (hash) API and strip
10050 # all positions. Such mixed state arises when a named-API method
10051 # also happens to have a Params::Get positional-key call alongside
10052 # =head4 Input named-block params that carry no position. A param
10053 # marked named: 1 is deliberate: it is an option after the
10054 # positional arguments, and the two kinds are fuzzed together.
10055 356 my @with_pos = grep { defined $output->{input}{$_}{position} } sort keys %{$output->{input}};
10056 356 my @without_pos = grep { !defined $output->{input}{$_}{position} } sort keys %{$output->{input}};
10057 356 ▶ if (@with_pos && grep { !$output->{input}{$_}{named} } @without_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (@with_pos and grep {not $output->{"input"}{$_}{"named"};} @without_pos) 4 352 356
Condition100%3/3
@with_pos and grep {not $output->{"input"}{$_}{"named"};} @without_pos
!ll&&!rl&&r
523004
MC/DC100%2/2
@with_pos and grep {not $output->{"input"}{$_}{"named"};} @without_pos
@with_pos grep {not $output->{"input"}{$_}{"named"};} @without_pos
Truth table100%3/3
A and B
A= @with_posB= grep {not $output->{"input"}{$_}{"named"};} @without_pos
ABresult
0X0
100
111
10058 4 delete $output->{input}{$_}{position} for @with_pos;
10059 }
10060 } else {
10061 163 delete $output->{input};
10062 }
10063 }
10064  
10065 # Process output
10066 536 ▶ if($schema->{'output'} && (scalar(keys %{$schema->{'output'}}))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"} and scalar keys %{$$schema{"output"};}) 536 0 536
Condition50%1/2
$schema->{"output"} and scalar keys %{$$schema{"output"};}
!ll
0536
MC/DC0%0/1
$schema->{"output"} and scalar keys %{$$schema{"output"};}
$schema->{"output"}
Truth table50%1/2
A
A= $schema->{"output"}
Aresult
00
11
10067 536 ▶ if((ref($schema->{output}{_error_handling}) eq 'HASH') && (scalar(keys %{$schema->{output}{_error_handling}}) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"output"}{"_error_handling"} eq "HASH" and scalar keys %{$$schema{"output"}{"_error_handling"};} == 0) 481 55 536
Condition100%3/3
ref $schema->{"output"}{"_error_handling"} eq "HASH" and scalar keys %{$$schema{"output"}{"_error_handling"};} == 0
!ll&&!rl&&r
1144481
MC/DC100%2/2
ref $schema->{"output"}{"_error_handling"} eq "HASH" and scalar keys %{$$schema{"output"}{"_error_handling"};} == 0
ref $schema->{"output"}{"_error_handling"} eq "HASH" scalar keys %{$$schema{"output"}{"_error_handling"};} == 0
Truth table100%3/3
A and B
A= ref $schema->{"output"}{"_error_handling"} eq "HASH"B= scalar keys %{$$schema{"output"}{"_error_handling"};} == 0
ABresult
0X0
100
111
10068 481 delete $schema->{output}{_error_handling};
10069 }
10070 # value/alt_value are inference metadata; Return::Set does not recognise them as validation rules
10071 536 my %out_spec = %{$schema->{'output'}};
10072 536 delete @out_spec{qw(value alt_value)};
10073 536 $output->{'output'} = \%out_spec;
10074 }
10075  
10076 536 ▶ if($schema->{'output'}{'type'} && ($schema->{'output'}{'type'} eq 'scalar')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"type"} and $schema->{"output"}{"type"} eq "scalar") 0 536 536
Condition66%2/3
$schema->{"output"}{"type"} and $schema->{"output"}{"type"} eq "scalar"
!ll&&!rl&&r
195170
MC/DC0%0/2
$schema->{"output"}{"type"} and $schema->{"output"}{"type"} eq "scalar"
$schema->{"output"}{"type"} $schema->{"output"}{"type"} eq "scalar"
Truth table66%2/3
A and B
A= $schema->{"output"}{"type"}B= $schema->{"output"}{"type"} eq "scalar"
ABresult
0X0
100
111
10077 0 $schema->{'output'}{'type'} = 'string';
10078 0 $schema->{_confidence}{output}->{level} = 'low'; # A guess
10079 }
10080  
10081 # Add 'new' field if object instantiation is needed
10082 536 ▶ if ($schema->{new}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"new"}) 294 242 536
10083 # TODO: consider allowing parent class packages up the ISA chain
10084 294 ▶ if(ref($schema->{new}) || ($schema->{new} eq $package_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{'new'} or $schema->{'new'} eq $package_name) { } 262 32 294
Condition100%3/3
ref $schema->{"new"} or $schema->{"new"} eq $package_name
l!l&&r!l&&!r
11015232
MC/DC100%2/2
ref $schema->{"new"} or $schema->{"new"} eq $package_name
ref $schema->{"new"} $schema->{"new"} eq $package_name
Truth table100%3/3
A or B
A= ref $schema->{"new"}B= $schema->{"new"} eq $package_name
ABresult
1X1
011
000
10085 262 ▶ $output->{new} = $schema->{new} eq $package_name ? undef : $schema->{'new'};
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'new'} eq $package_name ? : 152 110 262
10086 } else {
10087 32 $self->_log(" NEW: Don't use $schema->{new} for object insantiation");
10088 32 delete $schema->{new};
10089 32 delete $output->{new};
10090 }
10091 }
10092  
10093 536 ▶ if(!defined($schema->{_confidence}{input}->{level})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema->{"_confidence"}{"input"}{"level"}) 484 52 536
10094 484 $schema->{_confidence}{input} = $self->_calculate_input_confidence($schema->{input});
10095 }
10096 536 ▶ if(!defined($schema->{_confidence}{output}->{level})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema->{"_confidence"}{"output"}{"level"}) 1 535 536
10097 1 $schema->{_confidence}{output} = $self->_calculate_output_confidence($schema->{output});
10098 }
10099  
10100 # Add relationships if detected
10101 536 ▶ if ($schema->{relationships} && @{$schema->{relationships}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"relationships"} and @{$schema->{"relationships"};}) 7 529 536
Condition66%2/3
$schema->{"relationships"} and @{$schema->{"relationships"};}
!ll&&!rl&&r
52907
MC/DC50%1/2
$schema->{"relationships"} and @{$schema->{"relationships"};}
$schema->{"relationships"} @{$schema->{"relationships"};}
Truth table66%2/3
A and B
A= $schema->{"relationships"}B= @{$schema->{"relationships"};}
ABresult
0X0
100
111
10102 7 $output->{relationships} = $schema->{relationships};
10103 }
10104  
10105 536 ▶ if($schema->{accessor} && scalar(keys %{$schema->{accessor}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}) 20 516 536
Condition100%2/2
$schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}
!ll
51620
MC/DC100%1/1
$schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}
$schema->{"accessor"}
Truth table100%2/2
A
A= $schema->{"accessor"}
Aresult
00
11
10106 20 $output->{accessor} = $schema->{accessor};
10107 }
10108  
10109 536 open my $fh, '>', $filename;
10110 536 print $fh YAML::XS::Dump($output);
10111 536 print $fh $self->_generate_schema_comments($schema, $method_name);
10112 536 close $fh;
10113  
10114 my $rel_info = $schema->{relationships} ?
10115 536 ▶ ' [' . scalar(@{$schema->{relationships}}) . ' relationships]' : '';
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'relationships'} ? : 7 529 536
10116 $self->_log(" Wrote: $filename (input confidence: $schema->{_confidence}{input}->{level})" .
10117 536 ▶ ($schema->{new} ? " [requires: $schema->{new}]" : '') . $rel_info);
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'new'} ? : 262 274 536
10118 }
10119  
10120 # --------------------------------------------------
10121 # _generate_schema_comments
10122 #
10123 # Purpose: Generate the YAML comment block
10124 # appended to the end of each written
10125 # schema file, containing provenance,
10126 # confidence levels, parameter type
10127 # notes, relationship summaries, and
10128 # warnings about types requiring
10129 # special test setup.
10130 #
10131 # Entry: $schema - the schema hashref as
10132 # built by _analyze_method.
10133 # $method_name - the method name string,
10134 # used in the fuzz
10135 # command hint.
10136 #
10137 # Exit: Returns a string of YAML comment lines
10138 # beginning with a blank line and ending
10139 # with a trailing newline.
10140 #
10141 # Side effects: None.
10142 # --------------------------------------------------
10143 536 ▶sub _generate_schema_comments {
Subroutine _generate_schema_comments: called CC=26 SCAR=32.6
10144 536 my ($self, $schema, $method_name) = @_;
10145  
10146 536 my @comments;
10147  
10148 536 push @comments, '';
10149 536 push @comments, '# Generated by ' . ref($self);
10150 536 push @comments, "# Run: fuzz-harness-generator -r $self->{output_dir}/${method_name}.yml";
10151 536 push @comments, '#';
10152 536 push @comments, "# Input confidence: $schema->{_confidence}{input}->{level}";
10153 536 push @comments, "# Output confidence: $schema->{_confidence}{output}->{level}";
10154  
10155 # Add notes about parameters
10156 536 ▶ if ($schema->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"input"}) 519 17 536
10157 519 my @param_notes;
10158 519 foreach my $param_name (sort keys %{$schema->{input}}) {
10159 578 my $p = $schema->{input}{$param_name};
10160  
10161 578 ▶ if ($p->{semantic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"semantic"}) 34 544 578
10162 34 push @param_notes, "$param_name: $p->{semantic}";
10163 }
10164  
10165 578 ▶ if ($p->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"enum"}) 8 570 578
10166 8 push @param_notes, "$param_name: enum with " . scalar(@{$p->{enum}}) . " values";
10167 }
10168  
10169 578 ▶ if ($p->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"isa"}) 41 537 578
10170 41 push @param_notes, "$param_name: requires $p->{isa} object";
10171 }
10172 }
10173  
10174 519 ▶ if (@param_notes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param_notes) 71 448 519
10175 71 push @comments, '#';
10176 71 push @comments, '# Parameter types detected:';
10177 71 foreach my $note (sort @param_notes) {
10178 83 push @comments, "# - $note";
10179 }
10180 }
10181 }
10182  
10183 # Add relationship notes, sorted so their order does not depend on
10184 # detection order.
10185 536 ▶ if ($schema->{relationships} && @{$schema->{relationships}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"relationships"} and @{$schema->{"relationships"};}) 7 529 536
Condition66%2/3
$schema->{"relationships"} and @{$schema->{"relationships"};}
!ll&&!rl&&r
52907
MC/DC50%1/2
$schema->{"relationships"} and @{$schema->{"relationships"};}
$schema->{"relationships"} @{$schema->{"relationships"};}
Truth table66%2/3
A and B
A= $schema->{"relationships"}B= @{$schema->{"relationships"};}
ABresult
0X0
100
111
10186 7 push @comments, (
10187 '#',
10188 '# Parameter relationships detected:'
10189 );
10190 my @relationship_notes = map {
10191 16 ▶ $_->{description} || _format_relationship($_)
Condition33%1/3
$_->{'description'} || _format_relationship($_)
l!l&&r!l&&!r
1600
MC/DC0%0/2
$_->{'description'} || _format_relationship($_)
$_->{'description'} _format_relationship($_)
Truth table33%1/3
A || B
A= $_->{'description'}B= _format_relationship($_)
ABresult
1X1
011
000
10192 7 } @{$schema->{relationships}};
10193 7 foreach my $desc (sort @relationship_notes) {
10194 16 push @comments, "# - $desc";
10195 }
10196 }
10197  
10198 # Add general notes
10199 536 ▶ if ($schema->{_notes} && scalar(@{$schema->{_notes}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_notes"} and scalar @{$schema->{"_notes"};}) 42 494 536
Condition100%2/2
$schema->{"_notes"} and scalar @{$schema->{"_notes"};}
!ll
2534
MC/DC100%1/1
$schema->{"_notes"} and scalar @{$schema->{"_notes"};}
$schema->{"_notes"}
Truth table100%2/2
A
A= $schema->{"_notes"}
Aresult
00
11
10200 42 push @comments, '#';
10201 42 push @comments, '# Notes:';
10202 42 foreach my $note (sort @{$schema->{_notes}}) {
10203 76 push @comments, "# - $note";
10204 }
10205 }
10206  
10207 536 ▶ if($schema->{_analysis}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_analysis"}) 536 0 536
10208 536 push @comments, (
10209 '#',
10210 '# Analysis:',
10211 '# TODO:',
10212 );
10213 # confidence_factors:
10214 # input:
10215 # - No parameters found
10216 # output:
10217 # - 'Return type defined: object (+30)'
10218 # - 'Total output confidence score: 30'
10219 # - 'Medium confidence: return type defined'
10220 # input_confidence: none
10221 # output_confidence: medium
10222 # overall_confidence: none
10223 }
10224  
10225 # Add warnings for complex types
10226 536 my @warnings;
10227 536 ▶ if ($schema->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"input"}) 519 17 536
10228 519 foreach my $param_name (sort keys %{$schema->{input}}) {
10229 578 my $p = $schema->{input}{$param_name};
10230  
10231 578 ▶ if ($p->{type} && $p->{type} eq 'coderef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"type"} and $p->{"type"} eq "coderef") 12 566 578
Condition66%2/3
$p->{"type"} and $p->{"type"} eq "coderef"
!ll&&!rl&&r
056612
MC/DC50%1/2
$p->{"type"} and $p->{"type"} eq "coderef"
$p->{"type"} $p->{"type"} eq "coderef"
Truth table66%2/3
A and B
A= $p->{"type"}B= $p->{"type"} eq "coderef"
ABresult
0X0
100
111
10232 12 push @warnings, "Parameter '$param_name' is a coderef - you'll need to provide a sub {} in tests";
10233 }
10234  
10235 578 ▶ if ($p->{semantic} && $p->{semantic} eq 'filehandle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"semantic"} and $p->{"semantic"} eq "filehandle") 2 576 578
Condition100%3/3
$p->{"semantic"} and $p->{"semantic"} eq "filehandle"
!ll&&!rl&&r
544322
MC/DC100%2/2
$p->{"semantic"} and $p->{"semantic"} eq "filehandle"
$p->{"semantic"} $p->{"semantic"} eq "filehandle"
Truth table100%3/3
A and B
A= $p->{"semantic"}B= $p->{"semantic"} eq "filehandle"
ABresult
0X0
100
111
10236 2 push @warnings, "Parameter '$param_name' is a filehandle - consider using IO::String or mock";
10237 }
10238  
10239 578 ▶ if ($p->{isa} && $p->{isa} =~ /DateTime/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"isa"} and $p->{"isa"} =~ /DateTime/) 2 576 578
Condition100%3/3
$p->{"isa"} and $p->{"isa"} =~ /DateTime/
!ll&&!rl&&r
537392
MC/DC100%2/2
$p->{"isa"} and $p->{"isa"} =~ /DateTime/
$p->{"isa"} $p->{"isa"} =~ /DateTime/
Truth table100%3/3
A and B
A= $p->{"isa"}B= $p->{"isa"} =~ /DateTime/
ABresult
0X0
100
111
10240 2 push @warnings, "Parameter '$param_name' requires DateTime - ensure DateTime is loaded";
10241 }
10242 }
10243 }
10244  
10245 536 ▶ if (@warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (@warnings) 16 520 536
10246 16 push @comments, '#';
10247 16 push @comments, '# WARNINGS - Manual test setup may be required:';
10248 16 foreach my $warning (sort @warnings) {
10249 16 push @comments, "# ! $warning";
10250 }
10251 }
10252  
10253 536 push @comments, '';
10254  
10255 536 return join("\n", @comments);
10256 }
10257  
10258 # --------------------------------------------------
10259 # _serialize_parameter_for_yaml
10260 #
10261 # Purpose: Convert a parameter spec hashref into
10262 # a cleaned, YAML-serialisable form
10263 # suitable for App::Test::Generator
10264 # consumption, handling semantic type
10265 # mappings, enum values, and object
10266 # class annotations.
10267 #
10268 # Entry: $param - parameter spec hashref as
10269 # produced by the merge and
10270 # analysis pipeline.
10271 #
10272 # Exit: Returns a new hashref containing only
10273 # the fields App::Test::Generator
10274 # understands, with internal _ keys
10275 # and semantic keys removed or converted.
10276 #
10277 # Side effects: None.
10278 #
10279 # Notes: Semantic types are mapped to
10280 # appropriate base types with additional
10281 # constraint and note fields.
10282 # The original $param hashref is not
10283 # modified.
10284 # --------------------------------------------------
10285 587 ▶sub _serialize_parameter_for_yaml {
Subroutine _serialize_parameter_for_yaml: called CC=62 SCAR=47.8
10286 587 my ($self, $param) = @_;
10287  
10288 587 my %cleaned;
10289  
10290 # Copy basic fields that App::Test::Generator expects
10291 587 foreach my $field (qw(type position named optional min max sample_max matches default _not_empty)) {
10292 5870 ▶ $cleaned{$field} = $param->{$field} if defined $param->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{$field} 1713 4157 5870
10293 }
10294  
10295 # Provenance drives the harness's tolerance and type-check decisions.
10296 587 ▶ $cleaned{_source} = $param->{_source} if defined($param->{_source}) && length($param->{_source});
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{"_source"} and length $param->{"_source"} 562 25 587
Condition66%2/3
defined $param->{"_source"} and length $param->{"_source"}
!ll&&!rl&&r
250562
MC/DC50%1/2
defined $param->{"_source"} and length $param->{"_source"}
defined $param->{"_source"} length $param->{"_source"}
Truth table66%2/3
A and B
A= defined $param->{"_source"}B= length $param->{"_source"}
ABresult
0X0
100
111
10297  
10298 # Per-constraint provenance: a documented minimum and a code-enforced
10299 # maximum on the same field are separate decisions.
10300 587 ▶ if(ref($param->{_constraint_source}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param->{"_constraint_source"} eq "HASH") 87 500 587
10301 87 my %provenance;
10302 87 foreach my $key (qw(min max memberof matches nomatch notmemberof)) {
10303 522 my $source = $param->{_constraint_source}{$key};
10304 522 ▶ $provenance{$key} = $source if defined($source) && length($source);
Branch: 1 branch
BranchTrueFalseTotal
if defined $source and length $source 114 408 522
Condition66%2/3
defined $source and length $source
!ll&&!rl&&r
4080114
MC/DC50%1/2
defined $source and length $source
defined $source length $source
Truth table66%2/3
A and B
A= defined $sourceB= length $source
ABresult
0X0
100
111
10305 }
10306 87 ▶ $cleaned{_constraint_source} = \%provenance if keys %provenance;
Branch: 1 branch
BranchTrueFalseTotal
if keys %provenance 87 0 87
10307 }
10308  
10309 # Handle advanced type mappings
10310 587 ▶ if(my $semantic = $param->{semantic}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $semantic = $param->{"semantic"}) 38 549 587
10311 38 ▶ if ($semantic eq 'datetime_object') {
Branch: 13 branches
BranchTrueFalseTotal
if ($semantic eq 'datetime_object') { } 2 36 38
elsif ($semantic eq 'timepiece_object') { } 0 36 36
elsif ($semantic eq 'date_string') { } 1 35 36
elsif ($semantic eq 'bytes') { } 0 35 35
elsif ($semantic eq 'iso8601_string') { } 1 34 35
elsif ($semantic eq 'unix_timestamp') { } 2 32 34
elsif ($semantic eq 'datetime_parseable') { } 0 32 32
elsif ($semantic eq 'filehandle') { } 2 30 32
elsif ($semantic eq 'filepath') { } 1 29 30
elsif ($semantic eq 'existing_file') { } 10 19 29
elsif ($semantic eq 'directory') { } 1 18 19
elsif ($semantic eq 'callback') { } 7 11 18
elsif ($semantic eq 'enum') { } 8 3 11
10312 # DateTime objects: test generator needs to know how to create them
10313 2 $cleaned{type} = 'object';
10314 2 ▶ $cleaned{isa} = $param->{isa} || 'DateTime';
Condition50%1/2
$param->{'isa'} || 'DateTime'
l!l
20
MC/DC0%0/1
$param->{'isa'} || 'DateTime'
$param->{'isa'}
Truth table50%1/2
A
A= $param->{'isa'}
Aresult
11
00
10315 2 $cleaned{_note} = 'Requires DateTime object';
10316 } elsif ($semantic eq 'timepiece_object') {
10317 0 $cleaned{type} = 'object';
10318 0 ▶ $cleaned{isa} = $param->{isa} || 'Time::Piece';
Condition0%0/2
$param->{'isa'} || 'Time::Piece'
l!l
00
MC/DC0%0/1
$param->{'isa'} || 'Time::Piece'
$param->{'isa'}
Truth table0%0/2
A
A= $param->{'isa'}
Aresult
11
00
10319 0 $cleaned{_note} = 'Requires Time::Piece object';
10320 } elsif ($semantic eq 'date_string') {
10321 # Date strings: provide regex pattern
10322 1 $cleaned{type} = 'string';
10323 1 ▶ $cleaned{matches} ||= '/^\d{4}-\d{2}-\d{2}$/';
Condition50%1/2
$cleaned{"matches"} ||= "/^\\d{4}-\\d{2}-\\d{2}\$/"
l!l
10
MC/DC0%0/1
$cleaned{"matches"} ||= "/^\\d{4}-\\d{2}-\\d{2}\$/"
$cleaned{"matches"}
Truth table50%1/2
A
A= $cleaned{"matches"}
Aresult
11
00
10324 1 $cleaned{_example} = '2024-12-12';
10325 } elsif ($semantic eq 'bytes') {
10326 # A byte string is not text: the harness passes bytes and makes no
10327 # wide-character (BOM) probe, which a byte-oriented write rejects.
10328 0 $cleaned{semantic} = 'bytes';
10329 0 ▶ $cleaned{type} //= 'string';
Condition0%0/2
$cleaned{"type"} //= "string"
l!l
00
MC/DC0%0/1
$cleaned{"type"} //= "string"
$cleaned{"type"}
Truth table0%0/2
A
A= $cleaned{"type"}
Aresult
11
00
10330 } elsif ($semantic eq 'iso8601_string') {
10331 1 $cleaned{type} = 'string';
10332 1 ▶ $cleaned{matches} ||= '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z?$/';
Condition50%1/2
$cleaned{"matches"} ||= "/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z?\$/"
l!l
10
MC/DC0%0/1
$cleaned{"matches"} ||= "/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z?\$/"
$cleaned{"matches"}
Truth table50%1/2
A
A= $cleaned{"matches"}
Aresult
11
00
10333 1 $cleaned{_example} = '2024-12-12T10:30:00Z';
10334 } elsif ($semantic eq 'unix_timestamp') {
10335 2 $cleaned{type} = 'integer';
10336 2 ▶ $cleaned{min} ||= 0;
Condition50%1/2
$cleaned{"min"} ||= 0
l!l
02
MC/DC0%0/1
$cleaned{"min"} ||= 0
$cleaned{"min"}
Truth table50%1/2
A
A= $cleaned{"min"}
Aresult
11
00
10337 2 ▶ $cleaned{max} ||= $INT32_MAX; # 32-bit max
Condition66%2/3
$cleaned{"max"} ||= $INT32_MAX
l!l&&r!l&&!r
110
MC/DC0%0/2
$cleaned{"max"} ||= $INT32_MAX
$cleaned{"max"} $INT32_MAX
Truth table66%2/3
A ||= B
A= $cleaned{"max"}B= $INT32_MAX
ABresult
1X1
011
000
10338 2 $cleaned{_note} = 'UNIX timestamp';
10339 } elsif ($semantic eq 'datetime_parseable') {
10340 0 $cleaned{type} = 'string';
10341 0 $cleaned{_note} = 'Must be parseable as datetime';
10342 } elsif ($semantic eq 'filehandle') {
10343 # File handles: special handling needed
10344 2 $cleaned{type} = 'object';
10345 2 ▶ $cleaned{isa} = $param->{isa} || 'IO::Handle';
Condition50%1/2
$param->{'isa'} || 'IO::Handle'
l!l
20
MC/DC0%0/1
$param->{'isa'} || 'IO::Handle'
$param->{'isa'}
Truth table50%1/2
A
A= $param->{'isa'}
Aresult
11
00
10346 2 $cleaned{_note} = 'File handle - may need mock in tests';
10347 } elsif ($semantic eq 'filepath') {
10348 # File paths: string with path pattern
10349 1 $cleaned{type} = 'string';
10350 1 ▶ $cleaned{matches} ||= '/^[\\w\\/.\\-_]+$/';
Condition50%1/2
$cleaned{"matches"} ||= "/^[\\w\\/.\\-_]+\$/"
l!l
01
MC/DC0%0/1
$cleaned{"matches"} ||= "/^[\\w\\/.\\-_]+\$/"
$cleaned{"matches"}
Truth table50%1/2
A
A= $cleaned{"matches"}
Aresult
11
00
10351 1 $cleaned{_note} = 'File path';
10352 } elsif ($semantic eq 'existing_file') {
10353 # The routine only tests that the file exists. Any existing
10354 # file satisfies that, so keep the semantic for the harness to
10355 # supply a temporary file instead of guessing at a path.
10356 10 $cleaned{type} = 'string';
10357 10 $cleaned{semantic} = 'existing_file';
10358 10 $cleaned{_note} = 'Existing file';
10359 } elsif ($semantic eq 'directory') {
10360 # The value must name an existing directory. No pattern can
10361 # express that and any generated path would fail the -d test,
10362 # so keep the semantic for the harness to act on instead of
10363 # guessing at the spelling.
10364 1 $cleaned{type} = 'string';
10365 1 $cleaned{semantic} = 'directory';
10366 1 $cleaned{_note} = 'Existing directory';
10367 } elsif ($semantic eq 'callback') {
10368 # Coderefs: mark as special type
10369 7 $cleaned{type} = 'coderef';
10370 7 $cleaned{_note} = 'CODE reference - provide sub { } in tests';
10371 } elsif ($semantic eq 'enum') {
10372 # Enum: keep as string but add valid values.
10373 # If the formal input spec explicitly declared a different type, honour
10374 # it — the enum values from code analysis are regex-internal alternates
10375 # (e.g. numeric format variants) that are meaningless as string choices.
10376 8 ▶ if (($param->{_source} // '') eq SOURCE_INPUT_SPEC && defined $param->{type} && $param->{type} ne 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if (($param->{'_source'} // '') eq 'input_spec' and defined $param->{'type'} and $param->{'type'} ne 'string') { } 0 8 8
Condition37%3/8
($param->{"_source"} // "") eq "input_spec" and defined $param->{"type"} and $param->{"type"} ne "string"
!ll&&!rl&&r
800
($param->{"_source"} // "") eq "input_spec" and defined $param->{"type"}
!ll&&!rl&&r
800
$param->{"_source"} // ""
l!l
80
MC/DC0%0/3
($param->{"_source"} // "") eq "input_spec" and defined $param->{"type"} and $param->{"type"} ne "string"
$param->{"_source"} defined $param->{"type"} $param->{"type"} ne "string"
Truth table0%0/4
A and B and C
A= $param->{"_source"}B= defined $param->{"type"}C= $param->{"type"} ne "string"
ABCresult
0XX0
10X0
1100
1111
10377 # Formal spec wins; suppress spurious regex-derived enum.
10378 0 delete $cleaned{enum};
10379 } else {
10380 8 $cleaned{type} = 'string';
10381 8 ▶ if ($param->{enum} && ref($param->{enum}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"enum"} and ref $param->{"enum"} eq "ARRAY") 8 0 8
Condition33%1/3
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/2
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY"
$param->{"enum"} ref $param->{"enum"} eq "ARRAY"
Truth table33%1/3
A and B
A= $param->{"enum"}B= ref $param->{"enum"} eq "ARRAY"
ABresult
0X0
100
111
10382 8 $cleaned{enum} = $param->{enum};
10383 8 $cleaned{_note} = 'Must be one of: ' . join(', ', @{$param->{enum}});
10384 }
10385 }
10386 }
10387 }
10388  
10389 # The harness injected this value itself (a stub's advertised column), so
10390 # it is a fixture rather than a documented domain: no outside value is
10391 # expected to be an error.
10392 587 ▶ $cleaned{_fixture} = 1 if($param->{_fixture});
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"_fixture"} 0 587 587
10393  
10394 # The element shape of an arrayref of hashrefs, read from the POD's nested
10395 # key block: a YAML scalar cannot describe it.
10396 587 ▶ if(ref($param->{schema}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param->{"schema"} eq "HASH") 0 587 587
10397 0 $cleaned{schema} = $param->{schema};
10398 }
10399  
10400 # Handle memberof even if not marked with semantic.
10401 # enum and memberof are mutually exclusive — only set memberof when enum
10402 # is not already being output (avoids the "has both" validation error).
10403 # Also suppress when the formal input spec declared a non-string type —
10404 # the enum values are regex-internal alternates, not valid string members.
10405 587 ▶ my $formal_non_string = (($param->{_source} // '') eq SOURCE_INPUT_SPEC) && defined $param->{type} && $param->{type} ne 'string';
Condition87%7/8
($param->{'_source'} // '') eq 'input_spec' && defined $param->{'type'} && $param->{'type'} ne 'string'
!ll&&!rl&&r
264162161
($param->{"_source"} // "") eq "input_spec" and defined $param->{"type"}
!ll&&!rl&&r
2640323
$param->{"_source"} // ""
l!l
56225
MC/DC66%2/3
($param->{'_source'} // '') eq 'input_spec' && defined $param->{'type'} && $param->{'type'} ne 'string'
$param->{"_source"} defined $param->{"type"} $param->{'type'} ne 'string'
Truth table75%3/4
A and B && C
A= $param->{"_source"}B= defined $param->{"type"}C= $param->{'type'} ne 'string'
ABCresult
0XX0
10X0
1100
1111
10406 587 ▶ if($param->{enum} && ref($param->{enum}) eq 'ARRAY' && !$cleaned{enum} && !$formal_non_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"enum"} and ref $param->{"enum"} eq "ARRAY" and not $cleaned{"enum"} and not $formal_non_string) 1 586 587
Condition77%7/9
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY" and not $cleaned{"enum"} and not $formal_non_string
!ll&&!rl&&r
58601
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY" and not $cleaned{"enum"}
!ll&&!rl&&r
57881
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY"
!ll&&!rl&&r
57809
MC/DC50%2/4
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY" and not $cleaned{"enum"} and not $formal_non_string
$param->{"enum"} ref $param->{"enum"} eq "ARRAY" not $cleaned{"enum"} not $formal_non_string
Truth table60%3/5
A and B and not(C) and not(D)
A= $param->{"enum"}B= ref $param->{"enum"} eq "ARRAY"C= not $cleaned{"enum"}D= not $formal_non_string
ABCDresult
0XXX0
10XX0
110X0
11100
11111
10407 1 $cleaned{memberof} = $param->{enum};
10408 }
10409 587 ▶ if($param->{memberof} && ref($param->{memberof}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"memberof"} and ref $param->{"memberof"} eq "ARRAY") 13 574 587
Condition66%2/3
$param->{"memberof"} and ref $param->{"memberof"} eq "ARRAY"
!ll&&!rl&&r
574013
MC/DC50%1/2
$param->{"memberof"} and ref $param->{"memberof"} eq "ARRAY"
$param->{"memberof"} ref $param->{"memberof"} eq "ARRAY"
Truth table66%2/3
A and B
A= $param->{"memberof"}B= ref $param->{"memberof"} eq "ARRAY"
ABresult
0X0
100
111
10410 13 $cleaned{memberof} = $param->{memberof};
10411 }
10412  
10413 # Handle object class
10414 587 ▶ if ($param->{isa} && !$cleaned{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"isa"} and not $cleaned{"isa"}) 38 549 587
Condition100%3/3
$param->{"isa"} and not $cleaned{"isa"}
!ll&&!rl&&r
545438
MC/DC100%2/2
$param->{"isa"} and not $cleaned{"isa"}
$param->{"isa"} not $cleaned{"isa"}
Truth table100%3/3
A and not(B)
A= $param->{"isa"}B= not $cleaned{"isa"}
ABresult
0X0
100
111
10415 38 $cleaned{isa} = $param->{isa};
10416 }
10417  
10418 # Add format hints where available
10419 587 ▶ if ($param->{format}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"format"}) 1 586 587
10420 1 $cleaned{_format} = $param->{format};
10421 }
10422  
10423 # Patterns harvested from source or POD arrive as slash-delimited
10424 # literals ('/^abc$/'). Consumers compile what they are given with
10425 # qr//, which would take the delimiters to be part of the pattern, so
10426 # store the pattern body. It stays a plain string: a compiled Regexp
10427 # is written to YAML as a !!perl/regexp object that a YAML consumer
10428 # cannot read back, and the schema is a data file.
10429 587 ▶ if(defined(my $pattern = $cleaned{matches})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pattern = $cleaned{"matches"})) 28 559 587
10430 28 ▶ if((reftype($pattern) // '') eq 'REGEXP') {
Branch: 2 branches
BranchTrueFalseTotal
if ((builtin::reftype($pattern) // '') eq 'REGEXP') { } 0 28 28
elsif (not ref $pattern) { } 28 0 28
Condition50%1/2
builtin::reftype($pattern) // ""
l!l
028
MC/DC0%0/1
builtin::reftype($pattern) // ""
builtin::reftype($pattern)
Truth table50%1/2
A
A= builtin::reftype($pattern)
Aresult
11
00
10431 # Some extraction paths hand over a compiled pattern, possibly a
10432 # blessed subclass (e.g. a regexp from a Safe compartment).
10433 0 my ($body) = re::regexp_pattern($pattern);
10434 0 ▶ $cleaned{matches} = $body if defined($body);
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 0 0 0
10435 } elsif(!ref($pattern)) {
10436 28 (my $body = $pattern) =~ s{^/(.+)/$}{$1}s;
10437 28 my $compiled = eval { qr/$body/ };
10438 28 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 28 28
10439 0 $self->_log(" invalid matches pattern '$pattern': $@");
10440 } else {
10441 28 $cleaned{matches} = $body;
10442 }
10443 }
10444 }
10445  
10446 # A stand-in for an arrayref of live objects: the harness has to build
10447 # one, and a YAML scalar cannot describe it.
10448 587 ▶ if(ref($param->{_stub_elements}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param->{"_stub_elements"} eq "HASH") 0 587 587
10449 0 $cleaned{_stub_elements} = $param->{_stub_elements};
10450 }
10451  
10452 # Remove internal fields. The directory and existing-file semantics are
10453 # kept: the harness needs them to supply a path that exists.
10454 delete $cleaned{semantic}
10455 587 ▶ unless(($cleaned{semantic} // '') =~ /^(?:directory|existing_file|bytes)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless ($cleaned{"semantic"} // "") =~ /^(?:directory|existing_file|bytes)$/ 576 11 587
Condition100%2/2
$cleaned{"semantic"} // ""
l!l
11576
MC/DC100%1/1
$cleaned{"semantic"} // ""
$cleaned{"semantic"}
Truth table100%2/2
A
A= $cleaned{"semantic"}
Aresult
11
00
10456  
10457 587 return \%cleaned;
10458 }
10459  
10460 # --------------------------------------------------
10461 # _format_relationship
10462 #
10463 # Purpose: Format a relationship hashref as a
10464 # short human-readable description
10465 # string for use in YAML comments.
10466 #
10467 # Entry: $rel - relationship hashref as
10468 # produced by the relationship
10469 # detection methods.
10470 #
10471 # Exit: Returns a description string.
10472 # Returns 'Unknown relationship' for
10473 # unrecognised types.
10474 #
10475 # Side effects: None.
10476 # --------------------------------------------------
10477 10 ▶sub _format_relationship {
Subroutine _format_relationship: called CC=7 SCAR=19.5
10478 10 my $rel = $_[0];
10479  
10480 10 ▶ if ($rel->{type} eq 'mutually_exclusive') {
Branch: 6 branches
BranchTrueFalseTotal
if ($rel->{'type'} eq 'mutually_exclusive') { } 2 8 10
elsif ($rel->{'type'} eq 'required_group') { } 1 7 8
elsif ($rel->{'type'} eq 'conditional_requirement') { } 2 5 7
elsif ($rel->{'type'} eq 'dependency') { } 2 3 5
elsif ($rel->{'type'} eq 'value_constraint') { } 1 2 3
elsif ($rel->{'type'} eq 'value_conditional') { } 1 1 2
10481 2 return 'Mutually exclusive: ' . join(', ', @{$rel->{params}});
10482 } elsif ($rel->{type} eq 'required_group') {
10483 1 return "Required group (OR): " . join(', ', @{$rel->{params}});
10484 } elsif ($rel->{type} eq 'conditional_requirement') {
10485 2 return "If $rel->{if} then $rel->{then_required} required";
10486 } elsif ($rel->{type} eq 'dependency') {
10487 2 return "$rel->{param} depends on $rel->{requires}";
10488 } elsif ($rel->{type} eq 'value_constraint') {
10489 1 return "If $rel->{if} then $rel->{then} $rel->{operator} $rel->{value}";
10490 } elsif ($rel->{type} eq 'value_conditional') {
10491 1 return "If $rel->{if}='$rel->{equals}' then $rel->{then_required} required";
10492 }
10493 1 return 'Unknown relationship';
10494 }
10495  
10496 # --------------------------------------------------
10497 # _needs_object_instantiation
10498 #
10499 # Purpose: Determine whether a method requires
10500 # --------------------------------------------------
10501 # _ctor_representative_args
10502 #
10503 # Purpose: Given a new() method schema, return a
10504 # hashref of representative values for its
10505 # required parameters, suitable for use as
10506 # the new: key in an instance-method schema.
10507 # Returns {} for a no-arg constructor.
10508 # Returns undef when any required param is
10509 # of a type that cannot be represented as a
10510 # plain YAML scalar (e.g. coderef, object).
10511 #
10512 # Entry: $ctor_schema - schema hashref for new().
10513 #
10514 # Exit: Hashref of {param => value} or undef.
10515 # --------------------------------------------------
10516  
10517 # A parameter whose value the routine reads from disk is a precondition, not an
10518 # argument the harness can make up. Two cases are distinguishable:
10519 #
10520 # * the routine only tests that the file exists (`-e`/`-f`) and keeps the
10521 # path, as a constructor validating its argument does. Any existing file
10522 # will do, so the parameter is marked semantic => 'existing_file' and the
10523 # harness supplies a real temporary file.
10524 # * the routine parses or slurps the file's contents. No synthesized or
10525 # empty fixture can satisfy that, so the routine is reported as skipped
10526 # rather than failed on a call it could never make correctly.
10527 sub _requires_input_file
10528 813 ▶{
Subroutine _requires_input_file: called CC=15 SCAR=27.2
10529 813 my ($body, $input) = @_;
10530  
10531 813 ▶ return undef unless(ref($input) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 36 777 813
10532 777 ▶ return undef unless(defined($body) && length($body));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and length $body 0 777 777
Condition33%1/3
defined $body and length $body
!ll&&!rl&&r
00777
MC/DC0%0/2
defined $body and length $body
defined $body length $body
Truth table33%1/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
10533  
10534 777 foreach my $param (sort keys %{$input}) {
10535 726 my $p = quotemeta($param);
10536  
10537 # The value is often copied out of %args under another name first.
10538 726 my @names = ($p);
10539 726 ▶ while($body =~ /\bmy\s+\$(\w+)\s*=\s*\$args\{\s*['"]?$p['"]?\s*\}\s*;/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\bmy\s+\$(\w+)\s*=\s*\$args\{\s*['"]?${p}['"]?\s*\}\s*;/g) 7 726 733
10540 7 ▶ push @names, quotemeta($1) if($1 ne $param);
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne $param 5 2 7
10541 }
10542 726 my $v = '(?:' . join('|', @names) . ')';
10543  
10544 # A file whose contents are read, not merely tested for existence.
10545 726 ▶ return 'needs an existing input file whose contents it parses'
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\b(?:PPI::Document->new|_load_schema|_slurp|read_file|slurp_file)\s*\(\s*\$$v\b/ or $body =~ /\bopen\b\s*\(?[^;]*['"]<[^;]*\$$v\b/ 18 708 726
Condition100%3/3
$body =~ /\b(?:PPI::Document->new|_load_schema|_slurp|read_file|slurp_file)\s*\(\s*\$$v\b/ or $body =~ /\bopen\b\s*\(?[^;]*['"]<[^;]*\$$v\b/
l!l&&r!l&&!r
99708
MC/DC100%2/2
$body =~ /\b(?:PPI::Document->new|_load_schema|_slurp|read_file|slurp_file)\s*\(\s*\$$v\b/ or $body =~ /\bopen\b\s*\(?[^;]*['"]<[^;]*\$$v\b/
$body =~ /\b(?:PPI::Document->new|_load_schema|_slurp|read_file|slurp_file)\s*\(\s*\$$v\b/ $body =~ /\bopen\b\s*\(?[^;]*['"]<[^;]*\$$v\b/
Truth table100%3/3
A or B
A= $body =~ /\b(?:PPI::Document->new|_load_schema|_slurp|read_file|slurp_file)\s*\(\s*\$$v\b/B= $body =~ /\bopen\b\s*\(?[^;]*['"]<[^;]*\$$v\b/
ABresult
1X1
011
000
10546 if($body =~ /\b(?:PPI::Document->new|_load_schema|_slurp|read_file|slurp_file)\s*\(\s*\$$v\b/
10547 || $body =~ /\bopen\b\s*\(?[^;]*['"]<[^;]*\$$v\b/);
10548  
10549 708 ▶ my $tests_existence = ($body =~ /-[ef]\s+\$args\{\s*['"]?$p['"]?\s*\}/)
Condition100%6/6
$body =~ /-[ef]\s+\$args\{\s*['"]?${p}['"]?\s*\}/ || $body =~ /-[ef]\s+\$$v\b/ || $body =~ /-[ef]\s+\$self->\{\s*['"]?${p}['"]?\s*\}/
l!l&&r!l&&!r
83697
$body =~ /-[ef]\s+\$args\{\s*['"]?${p}['"]?\s*\}/ or $body =~ /-[ef]\s+\$$v\b/
l!l&&r!l&&!r
71700
MC/DC100%3/3
$body =~ /-[ef]\s+\$args\{\s*['"]?${p}['"]?\s*\}/ || $body =~ /-[ef]\s+\$$v\b/ || $body =~ /-[ef]\s+\$self->\{\s*['"]?${p}['"]?\s*\}/
$body =~ /-[ef]\s+\$args\{\s*['"]?${p}['"]?\s*\}/ $body =~ /-[ef]\s+\$$v\b/ $body =~ /-[ef]\s+\$self->\{\s*['"]?${p}['"]?\s*\}/
Truth table100%4/4
A or B || C
A= $body =~ /-[ef]\s+\$args\{\s*['"]?${p}['"]?\s*\}/B= $body =~ /-[ef]\s+\$$v\b/C= $body =~ /-[ef]\s+\$self->\{\s*['"]?${p}['"]?\s*\}/
ABCresult
1XX1
01X1
0011
0000
10550 || ($body =~ /-[ef]\s+\$$v\b/)
10551 || ($body =~ /-[ef]\s+\$self->\{\s*['"]?$p['"]?\s*\}/);
10552 # Object::Configure reads the file its caller's config_file names and
10553 # croaks when it is missing; that test is not in this body to be seen.
10554 708 ▶ $tests_existence ||= ($param eq 'config_file')
Condition83%5/6
$tests_existence ||= $param eq "config_file" && $body =~ /\bObject::Configure::configure\s*\(/
l!l&&r!l&&!r
111696
$param eq 'config_file' && $body =~ /\bObject::Configure::configure\s*\(/
!ll&&!rl&&r
69601
MC/DC0%0/3
$tests_existence ||= $param eq "config_file" && $body =~ /\bObject::Configure::configure\s*\(/
$tests_existence $param eq 'config_file' $body =~ /\bObject::Configure::configure\s*\(/
Truth table0%0/4
A ||= B && C
A= $tests_existenceB= $param eq 'config_file'C= $body =~ /\bObject::Configure::configure\s*\(/
ABCresult
1XX1
0111
00X0
0100
10555 && ($body =~ /\bObject::Configure::configure\s*\(/);
10556 708 ▶ next unless($tests_existence);
Branch: 1 branch
BranchTrueFalseTotal
unless $tests_existence 696 12 708
10557  
10558 12 my $spec = $input->{$param};
10559 12 ▶ $spec->{semantic} = 'existing_file' if(ref($spec) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec eq "HASH" 12 0 12
10560 }
10561  
10562 759 return undef;
10563 }
10564  
10565 # The part of a formal-spec entry that describes the entry itself, without
10566 # any nested { ... } such as a properties map. The named and positional
10567 # parsers capture up to the first closing brace, so an unbalanced trailing
10568 # nested opening is dropped as well.
10569 sub _own_spec_text
10570 398 ▶{
Subroutine _own_spec_text: called CC=3 SCAR=11.0
10571 398 ▶ my $spec = $_[0] // '';
Condition50%1/2
$_[0] // ""
l!l
3980
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
10572  
10573 398 ▶ 1 while($spec =~ s/\{[^{}]*\}//g);
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/\{[^{}]*\}//g and do { '???' } 2 398 400
10574 398 $spec =~ s/\{.*\z//s;
10575  
10576 398 return $spec;
10577 }
10578  
10579 # The optional flag a formal-spec entry gives its own parameter: 0 or 1, or
10580 # undef when it says nothing. An explicit optional wins over required.
10581 sub _spec_optionality
10582 392 ▶{
Subroutine _spec_optionality: called CC=3 SCAR=11.0
10583 392 my $own = _own_spec_text($_[0]);
10584  
10585 392 ▶ return $1 + 0 if($own =~ /\boptional\s*=>\s*(0|1)/i);
Branch: 1 branch
BranchTrueFalseTotal
if $own =~ /\boptional\s*=>\s*(0|1)/i 206 186 392
10586 186 ▶ return 1 - $1 if($own =~ /\brequired\s*=>\s*(0|1)/i);
Branch: 1 branch
BranchTrueFalseTotal
if $own =~ /\brequired\s*=>\s*(0|1)/i 2 184 186
10587 184 return undef;
10588 }
10589  
10590 # The name => { ... } entries at brace depth 0 of a bare-list spec, as
10591 # [ name, body ] pairs with the body's nested braces intact.
10592 sub _top_level_hash_entries
10593 29 ▶{
Subroutine _top_level_hash_entries: called CC=10 SCAR=23.0
10594 29 my $text = $_[0];
10595  
10596 29 my (@entries, $pending, $name, $start);
10597 29 my $depth = 0;
10598 29 ▶ while($text =~ /\b(\w+)\s*=>\s*(?=\{)|([{}])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\b(\w+)\s*=>\s*(?=\{)|([{}])/g) 34 29 63
10599 34 ▶ if(defined($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 10 24 34
10600 10 ▶ $pending = $1 if($depth == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 8 2 10
10601 10 next;
10602 }
10603 24 ▶ if($2 eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "{") 12 12 24
10604 12 ▶ if(($depth == 0) && defined($pending)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 and defined $pending) 8 4 12
Condition100%3/3
$depth == 0 and defined $pending
!ll&&!rl&&r
228
MC/DC100%2/2
$depth == 0 and defined $pending
$depth == 0 defined $pending
Truth table100%3/3
A and B
A= $depth == 0B= defined $pending
ABresult
0X0
100
111
10605 8 ($name, $start) = ($pending, pos($text));
10606 }
10607 12 undef $pending;
10608 12 $depth++;
10609 12 next;
10610 }
10611 12 ▶ next unless($depth > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $depth > 0 0 12 12
10612 12 $depth--;
10613 12 ▶ if(($depth == 0) && defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 and defined $name) 8 4 12
Condition100%3/3
$depth == 0 and defined $name
!ll&&!rl&&r
228
MC/DC100%2/2
$depth == 0 and defined $name
$depth == 0 defined $name
Truth table100%3/3
A and B
A= $depth == 0B= defined $name
ABresult
0X0
100
111
10614 8 push @entries, [ $name, substr($text, $start, pos($text) - 1 - $start) ];
10615 8 undef $name;
10616 }
10617 }
10618  
10619 29 return @entries;
10620 }
10621  
10622 # True when a required arrayref constructor parameter is not a list of
10623 # values but a list of live objects: the constructor rejects an empty list,
10624 # calls methods on the elements, or checks that they are blessed. A YAML
10625 # fixture cannot contain such objects, so both new() and the instance methods
10626 # that need its result must be reported as skipped.
10627 #
10628 # Database::Join->new is the motivating case: its databases parameter must
10629 # hold one or more Database::Abstraction instances, and the constructor calls
10630 # columns() on each one to build its routing table.
10631 # True when a constructor takes an object it will call methods on and the
10632 # schema gives no can/isa for standing one in, or an existing directory whose
10633 # files it reads. A YAML fixture can hold neither, so new() is reported as
10634 # skipped and the instance methods that need its result are not fuzzed.
10635 sub _constructor_needs_live_dependencies
10636 98 ▶{
Subroutine _constructor_needs_live_dependencies: called CC=10 SCAR=24.8
10637 98 my ($ctor_schema) = @_;
10638  
10639 98 my $input = $ctor_schema->{input};
10640 98 ▶ return undef unless(ref($input) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 0 98 98
10641  
10642 98 for my $param (sort keys %$input) {
10643 129 my $spec = $input->{$param};
10644 129 ▶ next unless(ref($spec) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 0 129 129
10645  
10646 129 ▶ my $type = $spec->{type} // '';
Condition50%1/2
$spec->{"type"} // ""
l!l
1290
MC/DC0%0/1
$spec->{"type"} // ""
$spec->{"type"}
Truth table50%1/2
A
A= $spec->{"type"}
Aresult
11
00
10647 129 ▶ if(($type =~ /\bobject\b/) && !defined($spec->{can}) && !defined($spec->{isa})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /\bobject\b/ and not defined $spec->{"can"} and not defined $spec->{"isa"}) 8 121 129
Condition66%4/6
$type =~ /\bobject\b/ and not defined $spec->{"can"} and not defined $spec->{"isa"}
!ll&&!rl&&r
12108
$type =~ /\bobject\b/ and not defined $spec->{"can"}
!ll&&!rl&&r
12108
MC/DC33%1/3
$type =~ /\bobject\b/ and not defined $spec->{"can"} and not defined $spec->{"isa"}
$type =~ /\bobject\b/ not defined $spec->{"can"} not defined $spec->{"isa"}
Truth table50%2/4
A and not(B) and not(C)
A= $type =~ /\bobject\b/B= not defined $spec->{"can"}C= not defined $spec->{"isa"}
ABCresult
0XX0
10X0
1100
1111
10648 8 return "constructor needs a live '$param' object, which a fixture cannot provide";
10649 }
10650 121 ▶ if(($spec->{semantic} // '') eq 'directory') {
Branch: 1 branch
BranchTrueFalseTotal
if (($spec->{"semantic"} // "") eq "directory") 0 121 121
Condition50%1/2
$spec->{"semantic"} // ""
l!l
0121
MC/DC0%0/1
$spec->{"semantic"} // ""
$spec->{"semantic"}
Truth table50%1/2
A
A= $spec->{"semantic"}
Aresult
11
00
10651 0 return "constructor needs an existing '$param' directory, which a fixture cannot provide";
10652 }
10653 }
10654  
10655 90 return undef;
10656 }
10657  
10658 # The required arrayref constructor parameters whose elements are live
10659 # objects, with the reason each one is: the constructor rejects an empty
10660 # list, calls a method on an element, or checks that an element is blessed.
10661 # The names matter as well as the reasons, because a stand-in can be supplied
10662 # for every such parameter when the POD says what the elements are.
10663 sub _constructor_live_object_param_reasons
10664 198 ▶{
Subroutine _constructor_live_object_param_reasons: called CC=15 SCAR=28.2
10665 198 my ($body, $ctor_schema) = @_;
10666  
10667 0 ▶ my $content = ref($body) ? eval { $body->content } : $body;
Branch: 1 branch
BranchTrueFalseTotal
ref $body ? : 0 198 198
10668 198 ▶ return () unless(defined($content) && length($content));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 198 198
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
00198
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table33%1/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
10669  
10670 198 my $input = $ctor_schema->{input};
10671 198 ▶ return () unless(ref($input) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 0 198 198
10672  
10673 198 my %reasons;
10674 198 for my $param (sort keys %$input) {
10675 294 my $spec = $input->{$param};
10676 294 ▶ next unless(ref($spec) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 0 294 294
10677 294 ▶ next if($spec->{optional});
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"optional"} 154 140 294
10678  
10679 140 ▶ my $type = $spec->{type} // '';
Condition50%1/2
$spec->{"type"} // ""
l!l
1400
MC/DC0%0/1
$spec->{"type"} // ""
$spec->{"type"}
Truth table50%1/2
A
A= $spec->{"type"}
Aresult
11
00
10680 140 ▶ next unless(($type eq 'arrayref') || ($type eq 'array'));
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "arrayref" or $type eq "array" 132 8 140
Condition66%2/3
$type eq "arrayref" or $type eq "array"
l!l&&r!l&&!r
80132
MC/DC50%1/2
$type eq "arrayref" or $type eq "array"
$type eq "arrayref" $type eq "array"
Truth table66%2/3
A or B
A= $type eq "arrayref"B= $type eq "array"
ABresult
1X1
011
000
10681  
10682 8 my $p = quotemeta($param);
10683 8 my $field = qr/\$\w+(?:->)?\{\s*['"]?$p['"]?\s*\}/;
10684  
10685 # `croak ... unless @{ $p->{databases} }`.
10686 8 my $empty_guard = ($content =~ /\bcroak\b[^;]*?\bunless\b[^;]*?\@\{\s*$field\s*\}/s);
10687  
10688 # An element is blessed or has a method called on it.
10689 8 ▶ my $elements_are_objects =
Condition66%2/3
$content =~ /$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*->\s*\w+/ || $content =~ /\bblessed\s*\(\s*$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*\)/
l!l&&r!l&&!r
503
MC/DC50%1/2
$content =~ /$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*->\s*\w+/ || $content =~ /\bblessed\s*\(\s*$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*\)/
$content =~ /$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*->\s*\w+/ $content =~ /\bblessed\s*\(\s*$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*\)/
Truth table66%2/3
A || B
A= $content =~ /$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*->\s*\w+/B= $content =~ /\bblessed\s*\(\s*$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*\)/
ABresult
1X1
011
000
10690 ($content =~ /$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*->\s*\w+/)
10691 || ($content =~ /\bblessed\s*\(\s*$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*\)/);
10692  
10693 8 ▶ next unless($empty_guard || $elements_are_objects);
Branch: 1 branch
BranchTrueFalseTotal
unless $empty_guard or $elements_are_objects 3 5 8
Condition100%3/3
$empty_guard or $elements_are_objects
l!l&&r!l&&!r
413
MC/DC100%2/2
$empty_guard or $elements_are_objects
$empty_guard $elements_are_objects
Truth table100%3/3
A or B
A= $empty_guardB= $elements_are_objects
ABresult
1X1
011
000
10694  
10695 5 ▶ $reasons{$param} = $empty_guard
Branch: 1 branch
BranchTrueFalseTotal
$empty_guard ? : 4 1 5
10696 ? "needs a non-empty arrayref of live objects in '$param'"
10697 : "needs live objects in '$param'";
10698 }
10699  
10700 198 return %reasons;
10701 }
10702  
10703 sub _constructor_requires_live_objects
10704 100 ▶{
Subroutine _constructor_requires_live_objects: called CC=2 SCAR=6.9
10705 100 my ($body, $ctor_schema) = @_;
10706  
10707 100 my %reasons = _constructor_live_object_param_reasons($body, $ctor_schema);
10708 100 ▶ return undef unless(%reasons);
Branch: 1 branch
BranchTrueFalseTotal
unless %reasons 97 3 100
10709  
10710 3 my ($param) = sort keys %reasons;
10711 3 return $reasons{$param};
10712 }
10713  
10714 # The class the POD says a constructor's object elements must be, e.g.
10715 # "Each element of C<databases> must be an already-instantiated subclass of
10716 # C<Database::Abstraction>". Reading it from the documentation keeps the
10717 # stand-in the harness builds tied to what the constructor promises rather
10718 # than to a class name guessed from the code.
10719 sub _pod_constructor_element_class
10720 2 ▶{
Subroutine _pod_constructor_element_class: called CC=6 SCAR=19.3
10721 2 my ($pod) = @_;
10722  
10723 2 ▶ return undef unless(defined($pod) && length($pod));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod and length $pod 0 2 2
Condition33%1/3
defined $pod and length $pod
!ll&&!rl&&r
002
MC/DC0%0/2
defined $pod and length $pod
defined $pod length $pod
Truth table33%1/3
A and B
A= defined $podB= length $pod
ABresult
0X0
100
111
10724  
10725 2 ▶ return $1 if($pod =~ /\bsubclass(?:es)?\s+of\s+(?:C<)?([A-Z]\w*(?:::\w+)+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $pod =~ /\bsubclass(?:es)?\s+of\s+(?:C<)?([A-Z]\w*(?:::\w+)+)/ 1 1 2
10726 1 ▶ return $1 if($pod =~ /\bisa\s*\(\s*['\"]?([A-Z]\w*(?:::\w+)+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $pod =~ /\bisa\s*\(\s*['\"]?([A-Z]\w*(?:::\w+)+)/ 0 1 1
10727 1 ▶ return $1 if($pod =~ /\binstances?\s+of\s+(?:C<)?([A-Z]\w*(?:::\w+)+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $pod =~ /\binstances?\s+of\s+(?:C<)?([A-Z]\w*(?:::\w+)+)/ 0 1 1
10728  
10729 1 return undef;
10730 }
10731  
10732 # Teach the harness to stand an object in for a constructor argument that
10733 # YAML cannot hold. Database::Join->new requires a non-empty list of live
10734 # Database::Abstraction objects: nothing a generated file can name gives it
10735 # one, so every instance method used to be skipped. The POD names the base
10736 # class, and the documented join_column default names the column the
10737 # constructor checks for, so a stub that answers the DA methods the
10738 # constructor calls can be built: it is blessed into a package inheriting
10739 # from the documented class and answers columns(), schema() and
10740 # selectall_arrayref().
10741 #
10742 # Only the Database::Abstraction interface is stubbed: those are the methods
10743 # the canned answers describe. Anything else keeps the skip.
10744 sub _constructor_object_stubs
10745 98 ▶{
Subroutine _constructor_object_stubs: called CC=16 SCAR=29.4
10746 98 my ($ctor_schema, $pod, $body) = @_;
10747  
10748 98 my %reasons = _constructor_live_object_param_reasons($body, $ctor_schema);
10749 98 ▶ return {} unless(%reasons);
Branch: 1 branch
BranchTrueFalseTotal
unless %reasons 96 2 98
10750  
10751 2 my $base = _pod_constructor_element_class($pod);
10752 2 ▶ return {} unless(defined($base) && ($base eq 'Database::Abstraction'));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base and $base eq "Database::Abstraction" 1 1 2
Condition66%2/3
defined $base and $base eq "Database::Abstraction"
!ll&&!rl&&r
101
MC/DC50%1/2
defined $base and $base eq "Database::Abstraction"
defined $base $base eq "Database::Abstraction"
Truth table66%2/3
A and B
A= defined $baseB= $base eq "Database::Abstraction"
ABresult
0X0
100
111
10753  
10754 1 ▶ my $input = $ctor_schema->{input} // {};
Condition50%1/2
$ctor_schema->{"input"} // {}
l!l
10
MC/DC0%0/1
$ctor_schema->{"input"} // {}
$ctor_schema->{"input"}
Truth table50%1/2
A
A= $ctor_schema->{"input"}
Aresult
11
00
10755 my $join_column = (ref($input->{join_column}) eq 'HASH')
10756 1 ▶ ? $input->{join_column}{default} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $input->{'join_column'} eq 'HASH' ? : 1 0 1
10757 1 ▶ return {} unless(defined($join_column) && !ref($join_column) && length($join_column));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $join_column and not ref $join_column and length $join_column 0 1 1
Condition33%2/6
defined $join_column and not ref $join_column and length $join_column
!ll&&!rl&&r
001
defined $join_column and not ref $join_column
!ll&&!rl&&r
001
MC/DC0%0/3
defined $join_column and not ref $join_column and length $join_column
defined $join_column not ref $join_column length $join_column
Truth table25%1/4
A and not(B) and C
A= defined $join_columnB= not ref $join_columnC= length $join_column
ABCresult
0XX0
10X0
1100
1111
10758  
10759 1 my %stubs;
10760 1 for my $param (sort keys %reasons) {
10761 # The constructor's own `croak ... unless @{ $p->{$param} }` documents
10762 # that the list must not be empty.
10763 1 $input->{$param}{min} = 1;
10764 1 my $stub = {
10765 isa => $base,
10766 methods => {
10767 columns => [ $join_column ],
10768 schema => {},
10769 selectall_arrayref => [],
10770 },
10771 };
10772 1 $input->{$param}{_stub_elements} = $stub;
10773 1 $stubs{$param} = $stub;
10774 }
10775  
10776 # The stand-in advertises only the join column, so that is the only value
10777 # its columns() reports and the only one the constructor's check accepts.
10778 # The list is a harness fixture, not a documented domain: every other
10779 # string is absent from every stub database and rightly croaks, so only
10780 # the fixture value is exercised.
10781 1 ▶ if(ref($input->{join_column}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input->{"join_column"} eq "HASH") 1 0 1
10782 1 $input->{join_column}{memberof} = [ $join_column ];
10783 1 $input->{join_column}{_constraint_source}{memberof} = 'fixture';
10784 1 $input->{join_column}{_fixture} = 1;
10785 }
10786  
10787 # A documented predicate's column must exist in the merged view too, and
10788 # the only column the stand-in publishes is the join column
10789 # (Database::Join::new's mandatory_filters).
10790 1 for my $param (keys %$input) {
10791 4 my $element = $input->{$param}{schema};
10792 4 ▶ next unless(ref($element) eq 'HASH' && ref($element->{col}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $element eq "HASH" and ref $element->{"col"} eq "HASH" 3 1 4
Condition100%3/3
ref $element eq "HASH" and ref $element->{"col"} eq "HASH"
!ll&&!rl&&r
211
MC/DC100%2/2
ref $element eq "HASH" and ref $element->{"col"} eq "HASH"
ref $element eq "HASH" ref $element->{"col"} eq "HASH"
Truth table100%3/3
A and B
A= ref $element eq "HASH"B= ref $element->{"col"} eq "HASH"
ABresult
0X0
100
111
10793 0 ▶ next if(ref($element->{col}{memberof}) eq 'ARRAY' && @{$element->{col}{memberof}});
Branch: 1 branch
BranchTrueFalseTotal
if ref $element->{"col"}{"memberof"} eq "ARRAY" and @{$element->{"col"}{"memberof"};} 0 1 1
Condition33%1/3
ref $element->{"col"}{"memberof"} eq "ARRAY" and @{$element->{"col"}{"memberof"};}
!ll&&!rl&&r
100
MC/DC0%0/2
ref $element->{"col"}{"memberof"} eq "ARRAY" and @{$element->{"col"}{"memberof"};}
ref $element->{"col"}{"memberof"} eq "ARRAY" @{$element->{"col"}{"memberof"};}
Truth table33%1/3
A and B
A= ref $element->{"col"}{"memberof"} eq "ARRAY"B= @{$element->{"col"}{"memberof"};}
ABresult
0X0
100
111
10794 1 $element->{col}{memberof} = [ $join_column ];
10795 1 $element->{col}{_constraint_source}{memberof} = 'fixture';
10796 1 $element->{col}{_fixture} = 1;
10797 }
10798  
10799 1 return \%stubs;
10800 }
10801  
10802 # --------------------------------------------------
10803 # _keeps_database_handle
10804 #
10805 # Purpose: Decide whether a call keeps a database handle open on purpose,
10806 # so the harness's "no file descriptors leaked" check can be off.
10807 #
10808 # Entry: $body - the subroutine source text.
10809 # $pod - the routine's POD text ('' when it has none).
10810 #
10811 # Exit: Returns 1 when the call holds a descriptor rather than leaking
10812 # one, 0 otherwise.
10813 #
10814 # Notes: Three documented shapes, each narrow. A handle returned to the
10815 # caller needs the POD to document C<dbh> (Database::Join's
10816 # dbi_source). A routine that queries the documented SQLite
10817 # cache goes through the cache path, whose first call builds the
10818 # cache and keeps the handle on the object (count and aggregate
10819 # document "the cached join tables" / "the existing SQLite
10820 # cache"). Storing the handle on the object is retention in
10821 # itself, so it needs no inference (the cache builder).
10822 # Invalidation is not retention: Database::Join::add_database
10823 # deletes and disconnects the cache and documents no cache, so
10824 # its calls stay checked.
10825 #
10826 # Side effects: None.
10827 # --------------------------------------------------
10828 sub _keeps_database_handle
10829 813 ▶{
Subroutine _keeps_database_handle: called CC=8 SCAR=21.6
10830 813 my ($body, $pod) = @_;
10831  
10832 813 ▶ return 0 unless defined($body);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 813 813
10833  
10834 # A handle handed to the caller, as the POD's output documents it.
10835 813 ▶ return 1 if(($body =~ /\breturn\s*\{[^}]*\bdbh\s*=>/s) && ($pod =~ /\bdbh\b/));
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\breturn\s*\{[^}]*\bdbh\s*=>/s and $pod =~ /\bdbh\b/ 1 812 813
Condition66%2/3
$body =~ /\breturn\s*\{[^}]*\bdbh\s*=>/s and $pod =~ /\bdbh\b/
!ll&&!rl&&r
81201
MC/DC50%1/2
$body =~ /\breturn\s*\{[^}]*\bdbh\s*=>/s and $pod =~ /\bdbh\b/
$body =~ /\breturn\s*\{[^}]*\bdbh\s*=>/s $pod =~ /\bdbh\b/
Truth table66%2/3
A and B
A= $body =~ /\breturn\s*\{[^}]*\bdbh\s*=>/sB= $pod =~ /\bdbh\b/
ABresult
0X0
100
111
10836  
10837 # The cache builder stores the handle on the object for the next call.
10838 812 ▶ return 1 if(($body =~ /\$self\s*->\s*\{_sqlite_cache\}\s*=\s*\{/s)
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\$self\s*->\s*\{_sqlite_cache\}\s*=\s*\{/s and $body =~ /\bdbh\s*=>/ 0 812 812
Condition33%1/3
$body =~ /\$self\s*->\s*\{_sqlite_cache\}\s*=\s*\{/s and $body =~ /\bdbh\s*=>/
!ll&&!rl&&r
81200
MC/DC0%0/2
$body =~ /\$self\s*->\s*\{_sqlite_cache\}\s*=\s*\{/s and $body =~ /\bdbh\s*=>/
$body =~ /\$self\s*->\s*\{_sqlite_cache\}\s*=\s*\{/s $body =~ /\bdbh\s*=>/
Truth table33%1/3
A and B
A= $body =~ /\$self\s*->\s*\{_sqlite_cache\}\s*=\s*\{/sB= $body =~ /\bdbh\s*=>/
ABresult
0X0
100
111
10839 && ($body =~ /\bdbh\s*=>/));
10840  
10841 # A routine that queries the documented SQLite cache reaches it through
10842 # the cache path, whose first call builds it and keeps the handle.
10843 812 ▶ return 1 if(($pod =~ /\b(?:SQLite\s+)?cache/i)
Branch: 1 branch
BranchTrueFalseTotal
if $pod =~ /\b(?:SQLite\s+)?cache/i and $body =~ /\$self\s*->\s*(?:_build_sqlite_cache|_sqlite_join|_sqlite_aggregate)\s*\(/ 1 811 812
Condition100%3/3
$pod =~ /\b(?:SQLite\s+)?cache/i and $body =~ /\$self\s*->\s*(?:_build_sqlite_cache|_sqlite_join|_sqlite_aggregate)\s*\(/
!ll&&!rl&&r
81011
MC/DC100%2/2
$pod =~ /\b(?:SQLite\s+)?cache/i and $body =~ /\$self\s*->\s*(?:_build_sqlite_cache|_sqlite_join|_sqlite_aggregate)\s*\(/
$pod =~ /\b(?:SQLite\s+)?cache/i $body =~ /\$self\s*->\s*(?:_build_sqlite_cache|_sqlite_join|_sqlite_aggregate)\s*\(/
Truth table100%3/3
A and B
A= $pod =~ /\b(?:SQLite\s+)?cache/iB= $body =~ /\$self\s*->\s*(?:_build_sqlite_cache|_sqlite_join|_sqlite_aggregate)\s*\(/
ABresult
0X0
100
111
10844 && ($body =~ /\$self\s*->\s*(?:_build_sqlite_cache|_sqlite_join|_sqlite_aggregate)\s*\(/));
10845  
10846 811 return 0;
10847 }
10848  
10849 # True when the routine's whole body is a croak, with nothing but simple
10850 # assignments before it: such a method exists only to report that the
10851 # operation is unsupported, and every call is expected to die.
10852 sub _always_croaks
10853 813 ▶{
Subroutine _always_croaks: called CC=3 SCAR=11.0
10854 813 my ($body) = @_;
10855  
10856 813 ▶ my $code = $body // '';
Condition50%1/2
$body // ""
l!l
8130
MC/DC0%0/1
$body // ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
10857 813 $code =~ s/#[^\n]*//g;
10858 813 $code =~ s/\A\s*sub\s+\w+\s*\{//;
10859 813 $code =~ s/\}\s*\z//;
10860  
10861 813 ▶ return ($code =~ /\A\s*(?:my\s+[^;]+;\s*)*croak\b[^;]*;\s*\z/s) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /\A\s*(?:my\s+[^;]+;\s*)*croak\b[^;]*;\s*\z/s ? : 8 805 813
10862 }
10863  
10864 # True when the constructor refuses to build the package itself. Two idioms
10865 # are recognised: an explicit abstract-class check (Class::Abstract and
10866 # friends), and a croak over a constructor argument that the schema does not
10867 # describe, which the harness therefore cannot pass.
10868 sub _constructor_refuses_direct_instantiation
10869 98 ▶{
Subroutine _constructor_refuses_direct_instantiation: called CC=10 SCAR=24.6
10870 98 my ($body, $ctor_schema) = @_;
10871  
10872 # _find_methods() keeps the body as source text, but accept a PPI node too.
10873 0 ▶ my $content = ref($body) ? eval { $body->content } : $body;
Branch: 1 branch
BranchTrueFalseTotal
ref $body ? : 0 98 98
10874 98 ▶ return 0 unless(defined($content) && length($content));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 98 98
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
0098
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table33%1/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
10875  
10876 98 ▶ return 1 if($content =~ /\bcheck_abstract\s*\(/);
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\bcheck_abstract\s*\(/ 0 98 98
10877 98 ▶ return 1 if(($content =~ /\bcroak\b/) && ($content =~ /\beq\s+__PACKAGE__\b/));
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\bcroak\b/ and $content =~ /\beq\s+__PACKAGE__\b/ 1 97 98
Condition100%3/3
$content =~ /\bcroak\b/ and $content =~ /\beq\s+__PACKAGE__\b/
!ll&&!rl&&r
57401
MC/DC100%2/2
$content =~ /\bcroak\b/ and $content =~ /\beq\s+__PACKAGE__\b/
$content =~ /\bcroak\b/ $content =~ /\beq\s+__PACKAGE__\b/
Truth table100%3/3
A and B
A= $content =~ /\bcroak\b/B= $content =~ /\beq\s+__PACKAGE__\b/
ABresult
0X0
100
111
10878  
10879 97 ▶ my %declared = (ref($ctor_schema->{input}) eq 'HASH') ? %{$ctor_schema->{input}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $ctor_schema->{'input'} eq 'HASH' ? : 97 0 97
10880 97 ▶ while($content =~ /\$args\{\s*(['"]?)([\w:]+)\1\s*\}\s*or\s+croak\b/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\$args\{\s*(['"]?)([\w:]+)\1\s*\}\s*or\s+croak\b/gs) 1 96 97
10881 1 ▶ return 1 unless(exists($declared{$2}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $declared{$2} 1 0 1
10882 }
10883  
10884 96 return 0;
10885 }
10886  
10887 98 ▶sub _ctor_representative_args {
Subroutine _ctor_representative_args: called CC=10 SCAR=23.1
10888 98 my ($self, $ctor_schema) = @_;
10889  
10890 98 my %REP = (
10891 string => 'test',
10892 integer => 42,
10893 float => 3.14,
10894 boolean => 1,
10895 hashref => {},
10896 arrayref => [],
10897 any => 'test',
10898 );
10899  
10900 98 ▶ my $input = $ctor_schema->{input} // {};
Condition50%1/2
$ctor_schema->{"input"} // {}
l!l
980
MC/DC0%0/1
$ctor_schema->{"input"} // {}
$ctor_schema->{"input"}
Truth table50%1/2
A
A= $ctor_schema->{"input"}
Aresult
11
00
10901 98 my %args;
10902 98 for my $param_name (keys %$input) {
10903 110 ▶ my $spec = $input->{$param_name} // {};
Condition50%1/2
$input->{$param_name} // {}
l!l
1100
MC/DC0%0/1
$input->{$param_name} // {}
$input->{$param_name}
Truth table50%1/2
A
A= $input->{$param_name}
Aresult
11
00
10904 110 ▶ next if $spec->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"optional"} 47 63 110
10905 # A constructor that reads a *file* argument is often not buildable
10906 # from an arbitrary one: the file has to be a module, live under the
10907 # class's lib_dir, or be parseable (Mutator, SchemaExtractor). Leave
10908 # it unbuilt, as before, rather than failing every method of the
10909 # class. A directory is a fixture the harness can make without
10910 # knowing what is in it.
10911 63 ▶ if ($param_name =~ /(?:file|filename)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_name =~ /(?:file|filename)/i) 10 53 63
10912 10 return undef;
10913 }
10914 53 ▶ if ($param_name =~ /(?:dir|path)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_name =~ /(?:dir|path)/i) 2 51 53
10915 2 $args{$param_name} = { '_path' => 'directory' };
10916 2 next;
10917 }
10918 # A list of live objects the POD describes: the harness builds a
10919 # stand-in for each element instead of an empty arrayref.
10920 51 ▶ if (ref($spec->{_stub_elements}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec->{"_stub_elements"} eq "HASH") 1 50 51
10921 1 $args{$param_name} = [ { '_stub' => $spec->{_stub_elements} } ];
10922 1 next;
10923 }
10924 50 ▶ my $type = $spec->{type} // 'string';
Condition50%1/2
$spec->{"type"} // "string"
l!l
500
MC/DC0%0/1
$spec->{"type"} // "string"
$spec->{"type"}
Truth table50%1/2
A
A= $spec->{"type"}
Aresult
11
00
10925 50 ▶ unless (exists $REP{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $REP{$type}) 7 43 50
10926 7 return undef;
10927 }
10928 43 $args{$param_name} = $REP{$type};
10929 }
10930 81 return \%args;
10931 }
10932  
10933 # an object to be instantiated before
10934 # it can be called, and if so return
10935 # the package name to instantiate.
10936 #
10937 # Entry: $method_name - name of the method.
10938 # $method_body - method source string.
10939 # $method_info - method hashref from
10940 # _find_methods (optional,
10941 # for backward compat).
10942 #
10943 # Exit: Returns the package name string if
10944 # object instantiation is required.
10945 # Returns undef if the method is a
10946 # constructor, factory, singleton, or
10947 # pure class method.
10948 #
10949 # Side effects: Logs analysis decisions to stdout
10950 # when verbose is set.
10951 #
10952 # Notes: Orchestrates five detection sub-steps:
10953 # factory detection, singleton detection,
10954 # instance method detection, inheritance
10955 # check, and constructor requirements.
10956 # Instance method detection overrides
10957 # factory detection when both fire.
10958 # --------------------------------------------------
10959 818 ▶sub _needs_object_instantiation {
Subroutine _needs_object_instantiation: called CC=24 SCAR=32.0
10960 818 my ($self, $method_name, $method_body, $method_info) = @_;
10961  
10962 # Allow method_info to be optional for backward compatibility
10963 818 ▶ $method_info ||= {};
Condition50%1/2
$method_info ||= {}
l!l
8180
MC/DC0%0/1
$method_info ||= {}
$method_info
Truth table50%1/2
A
A= $method_info
Aresult
11
00
10964  
10965 818 my $doc = $self->{_document};
10966 818 ▶ return undef unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 818 818
10967  
10968 # Get the current package name
10969 818 my $package_stmt = $doc->find_first('PPI::Statement::Package');
10970 818 ▶ my $current_package = $package_stmt ? $package_stmt->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$package_stmt ? : 812 6 818
10971 818 ▶ $self->{_package_name} //= $current_package;
Condition66%2/3
$self->{"_package_name"} //= $current_package
l!l&&r!l&&!r
81620
MC/DC0%0/2
$self->{"_package_name"} //= $current_package
$self->{"_package_name"} $current_package
Truth table66%2/3
A //= B
A= $self->{"_package_name"}B= $current_package
ABresult
1X1
011
000
10972  
10973 # Initialize result structure
10974 818 my $result = {
10975 package => $current_package,
10976 needs_object => 0,
10977 type => 'unknown',
10978 details => {},
10979 constructor_params => undef,
10980 };
10981  
10982 # Track whether we should explicitly skip object instantiation
10983 818 my $skip_object = 0;
10984  
10985 # Skip constructors and destructors
10986 818 ▶ if ($method_name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name eq "new") 98 720 818
10987 98 $self->_log(" OBJECT: Constructor '$method_name' detected; skipping instantiation analysis");
10988 98 return undef;
10989 }
10990 720 ▶ if($method_name =~ /^(create|build|construct|init|DESTROY)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^(create|build|construct|init|DESTROY)$/i) 3 717 720
10991 3 $skip_object = 1;
10992 }
10993  
10994 # 1. Check for factory methods that return instances
10995 720 my $is_factory = $self->_detect_factory_method(
10996 $method_name, $method_body, $current_package, $method_info
10997 );
10998  
10999 # 2. Check for singleton patterns
11000 720 my $is_singleton = $self->_detect_singleton_pattern($method_name, $method_body);
11001 720 ▶ if ($is_singleton) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_singleton) 1 719 720
11002 1 $result->{needs_object} = 0; # Singleton methods return the singleton instance
11003 1 $result->{type} = 'singleton_accessor';
11004 1 $result->{details} = $is_singleton;
11005 1 $self->_log(" OBJECT: Detected singleton accessor '$method_name'");
11006 # Singleton accessors typically don't need object creation in tests
11007 # as they're called on the class, not instance
11008 1 $skip_object = 1;
11009 }
11010  
11011 # 3. Check if this is an instance method that needs an object
11012 720 my $is_instance_method = $self->_detect_instance_method($method_name, $method_body);
11013 720 ▶ if ($is_instance_method &&
Branch: 1 branch
BranchTrueFalseTotal
if ($is_instance_method and $is_instance_method->{"explicit_self"} || $is_instance_method->{"shift_self"} || $is_instance_method->{"accesses_object_data"} || $is_instance_method->{"calls_instance_methods"} && scalar @{$is_instance_method->{"calls_instance_methods"};}) 393 327 720
Condition78%11/14
$is_instance_method and $is_instance_method->{"explicit_self"} || $is_instance_method->{"shift_self"} || $is_instance_method->{"accesses_object_data"} || $is_instance_method->{"calls_instance_methods"} && scalar @{$is_instance_method->{"calls_instance_methods"};}
!ll&&!rl&&r
31611393
$is_instance_method->{"explicit_self"} or $is_instance_method->{"shift_self"} or $is_instance_method->{"accesses_object_data"} or $is_instance_method->{"calls_instance_methods"} and scalar @{$is_instance_method->{"calls_instance_methods"};}
l!l&&r!l&&!r
393011
$is_instance_method->{"explicit_self"} or $is_instance_method->{"shift_self"} or $is_instance_method->{"accesses_object_data"}
l!l&&r!l&&!r
393011
$is_instance_method->{"explicit_self"} or $is_instance_method->{"shift_self"}
l!l&&r!l&&!r
3613211
$is_instance_method->{"calls_instance_methods"} and scalar @{$is_instance_method->{"calls_instance_methods"};}
!ll
110
MC/DC60%3/5
$is_instance_method and $is_instance_method->{"explicit_self"} || $is_instance_method->{"shift_self"} || $is_instance_method->{"accesses_object_data"} || $is_instance_method->{"calls_instance_methods"} && scalar @{$is_instance_method->{"calls_instance_methods"};}
$is_instance_method $is_instance_method->{"explicit_self"} $is_instance_method->{"shift_self"} $is_instance_method->{"accesses_object_data"} $is_instance_method->{"calls_instance_methods"}
Truth table66%4/6
A and B or C or D or E
A= $is_instance_methodB= $is_instance_method->{"explicit_self"}C= $is_instance_method->{"shift_self"}D= $is_instance_method->{"accesses_object_data"}E= $is_instance_method->{"calls_instance_methods"}
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
11014 ($is_instance_method->{explicit_self} ||
11015 $is_instance_method->{shift_self} ||
11016 $is_instance_method->{accesses_object_data} ||
11017 ($is_instance_method->{calls_instance_methods} &&
11018 0 scalar @{$is_instance_method->{calls_instance_methods}}))) {
11019  
11020 # Instance-only methods override factory detection
11021 393 ▶ if ($is_factory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_factory) 26 367 393
11022 26 $self->_log(
11023 " OBJECT: Instance-only method '$method_name' overrides factory detection"
11024 );
11025 }
11026  
11027 393 $result->{needs_object} = 1;
11028 393 $result->{type} = 'instance_method';
11029 393 $result->{details} = $is_instance_method;
11030  
11031 # 4. Check for inheritance - if parent class constructor should be used
11032 393 my $inheritance_info = $self->_check_inheritance_for_constructor(
11033 $current_package, $method_body
11034 );
11035 393 ▶ if ($inheritance_info && $inheritance_info->{use_parent_constructor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inheritance_info and $inheritance_info->{"use_parent_constructor"}) 24 369 393
Condition100%3/3
$inheritance_info and $inheritance_info->{"use_parent_constructor"}
!ll&&!rl&&r
16520424
MC/DC100%2/2
$inheritance_info and $inheritance_info->{"use_parent_constructor"}
$inheritance_info $inheritance_info->{"use_parent_constructor"}
Truth table100%3/3
A and B
A= $inheritance_infoB= $inheritance_info->{"use_parent_constructor"}
ABresult
0X0
100
111
11036 24 $result->{package} = $inheritance_info->{parent_class};
11037 24 $result->{details}{inheritance} = $inheritance_info;
11038 24 $self->_log(
11039 " OBJECT: Method '$method_name' uses parent class constructor: $inheritance_info->{parent_class}"
11040 );
11041 }
11042  
11043 # 5. Check if constructor needs specific parameters
11044 my $constructor_needs = $self->_detect_constructor_requirements(
11045 $current_package, $result->{package}
11046 393 );
11047 393 ▶ if ($constructor_needs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constructor_needs) 113 280 393
11048 113 $result->{constructor_params} = $constructor_needs;
11049 113 $result->{details}{constructor_requirements} = $constructor_needs;
11050 113 $self->_log(
11051 " OBJECT: Constructor for $result->{package} requires parameters"
11052 );
11053 }
11054  
11055 # Return the package name (or parent package) that needs instantiation
11056 393 return $result->{package};
11057 }
11058  
11059 # 6. Check for class methods that might need objects from other classes
11060 327 my $needs_other_object = $self->_detect_external_object_dependency($method_body);
11061 327 ▶ if ($needs_other_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_other_object) 6 321 327
11062 6 $result->{needs_object} = 1;
11063 6 $result->{type} = 'external_dependency';
11064 $result->{package} = $needs_other_object->{package}
11065 6 ▶ if $needs_other_object->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $needs_other_object->{"package"} 6 0 6
11066 6 $result->{details} = $needs_other_object;
11067  
11068 6 $self->_log(
11069 " OBJECT: Method '$method_name' depends on external object: $needs_other_object->{package}"
11070 );
11071 6 ▶ return $result->{package} if $result->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"package"} 6 0 6
11072 }
11073  
11074 # Factory method only if NOT instance-based
11075 321 ▶ if ($is_factory && !$skip_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_factory and not $skip_object) 2 319 321
Condition100%3/3
$is_factory and not $skip_object
!ll&&!rl&&r
31722
MC/DC100%2/2
$is_factory and not $skip_object
$is_factory not $skip_object
Truth table100%3/3
A and not(B)
A= $is_factoryB= not $skip_object
ABresult
0X0
100
111
11076 2 $result->{needs_object} = 0;
11077 2 $result->{type} = 'factory';
11078 2 $result->{details} = $is_factory;
11079 $self->_log(
11080 " OBJECT: Detected factory method '$method_name' returns $is_factory->{returns_class} objects"
11081 2 ▶ ) if $is_factory->{returns_class};
Branch: 1 branch
BranchTrueFalseTotal
if $is_factory->{"returns_class"} 2 0 2
11082 }
11083  
11084 321 return undef;
11085 }
11086  
11087 # --------------------------------------------------
11088 # _detect_factory_method
11089 #
11090 # Purpose: Detect whether a method is a factory
11091 # that creates and returns object
11092 # instances rather than operating on
11093 # an existing instance.
11094 #
11095 # Entry: $method_name - method name string.
11096 # $method_body - method source string.
11097 # $current_package - current package name.
11098 # $method_info - method hashref
11099 # (optional).
11100 #
11101 # Exit: Returns a factory_info hashref on
11102 # detection, or undef if the method
11103 # is not a factory.
11104 # The hashref includes: returns_class,
11105 # confidence, and one of:
11106 # returns_blessed, returns_new,
11107 # returns_factory_result, pod_hint.
11108 #
11109 # Side effects: None.
11110 # --------------------------------------------------
11111 723 ▶sub _detect_factory_method {
Subroutine _detect_factory_method: called CC=19 SCAR=32.9
11112 723 my ($self, $method_name, $method_body, $current_package, $method_info) = @_;
11113  
11114 723 my %factory_info;
11115  
11116 # Check method name patterns
11117 723 ▶ if ($method_name =~ /^(create_|make_|build_|get_)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^(create_|make_|build_|get_)/i) 21 702 723
11118 21 $factory_info{name_pattern} = 1;
11119 }
11120  
11121 # Look for object creation patterns in the method body
11122 723 ▶ if ($method_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body) 723 0 723
11123 # Pattern 1: Returns a blessed reference
11124 723 ▶ if ($method_body =~ /return\s+bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?/s ||
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /return\s+bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?/s or $method_body =~ /bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?.*return/s) 6 717 723
Condition66%2/3
$method_body =~ /return\s+bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?/s or $method_body =~ /bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?.*return/s
l!l&&r!l&&!r
60717
MC/DC50%1/2
$method_body =~ /return\s+bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?/s or $method_body =~ /bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?.*return/s
$method_body =~ /return\s+bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?/s $method_body =~ /bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?.*return/s
Truth table66%2/3
A or B
A= $method_body =~ /return\s+bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?/sB= $method_body =~ /bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?.*return/s
ABresult
1X1
011
000
11125 $method_body =~ /bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?.*return/s) {
11126 6 my $class_name = $1;
11127  
11128 # Handle variable class names
11129 6 ▶ if ($class_name =~ /^\$(class|self|package)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class_name =~ /^\$(class|self|package)$/) { } 3 3 6
elsif ($class_name =~ /^\$/) { } 1 2 3
11130 3 $factory_info{returns_class} = $current_package;
11131 } elsif ($class_name =~ /^\$/) {
11132 1 $factory_info{returns_class} = 'VARIABLE'; # Unknown variable
11133 } else {
11134 2 $factory_info{returns_class} = $class_name;
11135 }
11136  
11137 6 $factory_info{returns_blessed} = 1;
11138 6 $factory_info{confidence} = 'high';
11139 6 return \%factory_info;
11140 }
11141  
11142 # Pattern 2: Returns ->new() call on class or $self
11143 717 ▶ if ($method_body =~ /return\s+([\$\w:]+)->new\(/s ||
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /return\s+([\$\w:]+)->new\(/s or $method_body =~ /([\$\w:]+)->new\(.*return/s) 33 684 717
Condition100%3/3
$method_body =~ /return\s+([\$\w:]+)->new\(/s or $method_body =~ /([\$\w:]+)->new\(.*return/s
l!l&&r!l&&!r
231684
MC/DC100%2/2
$method_body =~ /return\s+([\$\w:]+)->new\(/s or $method_body =~ /([\$\w:]+)->new\(.*return/s
$method_body =~ /return\s+([\$\w:]+)->new\(/s $method_body =~ /([\$\w:]+)->new\(.*return/s
Truth table100%3/3
A or B
A= $method_body =~ /return\s+([\$\w:]+)->new\(/sB= $method_body =~ /([\$\w:]+)->new\(.*return/s
ABresult
1X1
011
000
11144 $method_body =~ /([\$\w:]+)->new\(.*return/s) {
11145 33 my $target = $1;
11146  
11147 # Determine what class is being instantiated
11148 33 ▶ if ($target eq '$self' || $target eq 'shift' || $target =~ /^\$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target eq '$self' or $target eq 'shift' or $target =~ /^\$/) { } 0 33 33
elsif ($target =~ /::/) { } 28 5 33
Condition33%2/6
$target eq "\$self" or $target eq "shift" or $target =~ /^\$/
l!l&&r!l&&!r
0033
$target eq "\$self" or $target eq "shift"
l!l&&r!l&&!r
0033
MC/DC0%0/3
$target eq "\$self" or $target eq "shift" or $target =~ /^\$/
$target eq "\$self" $target eq "shift" $target =~ /^\$/
Truth table25%1/4
A or B or C
A= $target eq "\$self"B= $target eq "shift"C= $target =~ /^\$/
ABCresult
1XX1
01X1
0011
0000
11149 0 $factory_info{returns_class} = $current_package;
11150 0 $factory_info{self_new} = 1;
11151 } elsif ($target =~ /::/) {
11152 28 $factory_info{returns_class} = $target;
11153 28 $factory_info{external_class} = 1;
11154 } else {
11155 5 $factory_info{returns_class} = $target;
11156 }
11157  
11158 33 $factory_info{returns_new} = 1;
11159 33 $factory_info{confidence} = 'medium';
11160 33 return \%factory_info;
11161 }
11162  
11163 # Pattern 3: Returns an object from another factory method
11164 684 ▶ if ($method_body =~ /return\s+([\$\w:]+)->(create_|make_|build_|get_)/i ||
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /return\s+([\$\w:]+)->(create_|make_|build_|get_)/i or $method_body =~ /([\$\w:]+)->(create_|make_|build_|get_).*return/is) 0 684 684
Condition33%1/3
$method_body =~ /return\s+([\$\w:]+)->(create_|make_|build_|get_)/i or $method_body =~ /([\$\w:]+)->(create_|make_|build_|get_).*return/is
l!l&&r!l&&!r
00684
MC/DC0%0/2
$method_body =~ /return\s+([\$\w:]+)->(create_|make_|build_|get_)/i or $method_body =~ /([\$\w:]+)->(create_|make_|build_|get_).*return/is
$method_body =~ /return\s+([\$\w:]+)->(create_|make_|build_|get_)/i $method_body =~ /([\$\w:]+)->(create_|make_|build_|get_).*return/is
Truth table33%1/3
A or B
A= $method_body =~ /return\s+([\$\w:]+)->(create_|make_|build_|get_)/iB= $method_body =~ /([\$\w:]+)->(create_|make_|build_|get_).*return/is
ABresult
1X1
011
000
11165 $method_body =~ /([\$\w:]+)->(create_|make_|build_|get_).*return/si) {
11166 0 $factory_info{returns_factory_result} = 1;
11167 0 $factory_info{confidence} = 'low';
11168 0 return \%factory_info;
11169 }
11170 }
11171  
11172 # Check for return type hints in POD if available
11173 684 ▶ if ($method_info && ref($method_info) eq 'HASH' && $method_info->{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_info and ref $method_info eq "HASH" and $method_info->{"pod"}) 455 229 684
Condition50%3/6
$method_info and ref $method_info eq "HASH" and $method_info->{"pod"}
!ll&&!rl&&r
0229455
$method_info and ref $method_info eq "HASH"
!ll&&!rl&&r
00684
MC/DC33%1/3
$method_info and ref $method_info eq "HASH" and $method_info->{"pod"}
$method_info ref $method_info eq "HASH" $method_info->{"pod"}
Truth table50%2/4
A and B and C
A= $method_infoB= ref $method_info eq "HASH"C= $method_info->{"pod"}
ABCresult
0XX0
10X0
1100
1111
11174 455 my $pod = $method_info->{pod};
11175 455 ▶ if ($pod =~ /returns?\s+(?:an?\s+)?(object|instance|new\s+\w+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /returns?\s+(?:an?\s+)?(object|instance|new\s+\w+)/i) 0 455 455
11176 0 $factory_info{pod_hint} = 1;
11177 0 $factory_info{confidence} = 'low';
11178 0 return \%factory_info;
11179 }
11180 }
11181  
11182 684 return undef;
11183 }
11184  
11185 # --------------------------------------------------
11186 # _detect_singleton_pattern
11187 #
11188 # Purpose: Detect singleton accessor methods
11189 # that return a shared instance rather
11190 # than creating a new object, by
11191 # checking the method name and body
11192 # for singleton patterns.
11193 #
11194 # Entry: $method_name - method name string.
11195 # $method_body - method source string.
11196 #
11197 # Exit: Returns a singleton_info hashref on
11198 # detection (always contains at least
11199 # name_pattern => 1), or undef if the
11200 # method name does not match the
11201 # singleton accessor pattern.
11202 #
11203 # Side effects: None.
11204 #
11205 # Notes: Only fires for methods named
11206 # instance, get_instance, singleton,
11207 # or shared_instance. Methods not
11208 # matching these names always return
11209 # undef regardless of body content.
11210 # --------------------------------------------------
11211 726 ▶sub _detect_singleton_pattern {
Subroutine _detect_singleton_pattern: called CC=12 SCAR=25.4
11212 726 my ($self, $method_name, $method_body) = @_;
11213  
11214 # Check method name patterns
11215 726 ▶ return undef unless $method_name =~ /^(instance|get_instance|singleton|shared_instance)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $method_name =~ /^(instance|get_instance|singleton|shared_instance)$/i 721 5 726
11216  
11217 5 my %singleton_info = (
11218 name_pattern => 1,
11219 );
11220  
11221 # Look for singleton patterns in code
11222 5 ▶ if ($method_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body) 5 0 5
11223 # Pattern 1: Static/state variable holding instance
11224 5 ▶ if ($method_body =~ /(?:my\s+)?(?:our\s+)?\$(?:instance|_instance|singleton)\b/s ||
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /(?:my\s+)?(?:our\s+)?\$(?:instance|_instance|singleton)\b/s or $method_body =~ /state\s+\$(?:instance|_instance|singleton)\b/s) 4 1 5
Condition66%2/3
$method_body =~ /(?:my\s+)?(?:our\s+)?\$(?:instance|_instance|singleton)\b/s or $method_body =~ /state\s+\$(?:instance|_instance|singleton)\b/s
l!l&&r!l&&!r
401
MC/DC50%1/2
$method_body =~ /(?:my\s+)?(?:our\s+)?\$(?:instance|_instance|singleton)\b/s or $method_body =~ /state\s+\$(?:instance|_instance|singleton)\b/s
$method_body =~ /(?:my\s+)?(?:our\s+)?\$(?:instance|_instance|singleton)\b/s $method_body =~ /state\s+\$(?:instance|_instance|singleton)\b/s
Truth table66%2/3
A or B
A= $method_body =~ /(?:my\s+)?(?:our\s+)?\$(?:instance|_instance|singleton)\b/sB= $method_body =~ /state\s+\$(?:instance|_instance|singleton)\b/s
ABresult
1X1
011
000
11225 $method_body =~ /state\s+\$(?:instance|_instance|singleton)\b/s) {
11226 4 $singleton_info{static_variable} = 1;
11227 4 $singleton_info{confidence} = 'high';
11228 }
11229  
11230 # Pattern 2: Returns $instance if defined (with better regex)
11231 5 ▶ if ($method_body =~ /return\s+\$instance\s+if\s+(?:defined\s+)?\$instance/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /return\s+\$instance\s+if\s+(?:defined\s+)?\$instance/ or $method_body =~ /unless\s+\$instance.*?=\s*.*?new/) 1 4 5
Condition66%2/3
$method_body =~ /return\s+\$instance\s+if\s+(?:defined\s+)?\$instance/ or $method_body =~ /unless\s+\$instance.*?=\s*.*?new/
l!l&&r!l&&!r
104
MC/DC50%1/2
$method_body =~ /return\s+\$instance\s+if\s+(?:defined\s+)?\$instance/ or $method_body =~ /unless\s+\$instance.*?=\s*.*?new/
$method_body =~ /return\s+\$instance\s+if\s+(?:defined\s+)?\$instance/ $method_body =~ /unless\s+\$instance.*?=\s*.*?new/
Truth table66%2/3
A or B
A= $method_body =~ /return\s+\$instance\s+if\s+(?:defined\s+)?\$instance/B= $method_body =~ /unless\s+\$instance.*?=\s*.*?new/
ABresult
1X1
011
000
11232 $method_body =~ /unless\s+\$instance.*?=\s*.*?new/) {
11233 1 $singleton_info{returns_instance} = 1;
11234 1 $singleton_info{confidence} = 'high';
11235 }
11236  
11237 # Pattern 3: ||= new() pattern (with better regex)
11238 5 ▶ if ($method_body =~ /\$instance\s*\|\|=\s*.*?new/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$instance\s*\|\|=\s*.*?new/ or $method_body =~ /\$instance\s*=\s*.*?new\s+unless\s+(?:defined\s+)?\$instance/) 2 3 5
Condition66%2/3
$method_body =~ /\$instance\s*\|\|=\s*.*?new/ or $method_body =~ /\$instance\s*=\s*.*?new\s+unless\s+(?:defined\s+)?\$instance/
l!l&&r!l&&!r
203
MC/DC50%1/2
$method_body =~ /\$instance\s*\|\|=\s*.*?new/ or $method_body =~ /\$instance\s*=\s*.*?new\s+unless\s+(?:defined\s+)?\$instance/
$method_body =~ /\$instance\s*\|\|=\s*.*?new/ $method_body =~ /\$instance\s*=\s*.*?new\s+unless\s+(?:defined\s+)?\$instance/
Truth table66%2/3
A or B
A= $method_body =~ /\$instance\s*\|\|=\s*.*?new/B= $method_body =~ /\$instance\s*=\s*.*?new\s+unless\s+(?:defined\s+)?\$instance/
ABresult
1X1
011
000
11239 $method_body =~ /\$instance\s*=\s*.*?new\s+unless\s+(?:defined\s+)?\$instance/) {
11240 2 $singleton_info{lazy_initialization} = 1;
11241 2 $singleton_info{confidence} = 'medium';
11242 }
11243  
11244 # Pattern 4: Direct return of $instance variable
11245 5 ▶ if ($method_body =~ /return\s+\$instance;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /return\s+\$instance;/) 3 2 5
11246 3 $singleton_info{returns_instance} = 1;
11247 3 ▶ $singleton_info{confidence} = 'high' unless $singleton_info{confidence};
Branch: 1 branch
BranchTrueFalseTotal
unless $singleton_info{"confidence"} 0 3 3
11248 }
11249 }
11250  
11251 5 ▶ return \%singleton_info if keys %singleton_info > 0; # Need at least name pattern
Branch: 1 branch
BranchTrueFalseTotal
if keys %singleton_info > 0 5 0 5
11252  
11253 0 return undef;
11254 }
11255  
11256 # --------------------------------------------------
11257 # _detect_instance_method
11258 #
11259 # Purpose: Detect whether a method is an
11260 # instance method that requires a
11261 # blessed object ($self) to be called,
11262 # through multiple detection patterns
11263 # of varying confidence.
11264 #
11265 # Entry: $method_name - method name string.
11266 # $method_body - method source string.
11267 #
11268 # Exit: Returns an instance_info hashref if
11269 # any instance method signal is found.
11270 # Returns undef if no signals are
11271 # detected.
11272 # The hashref may contain: explicit_self,
11273 # shift_self, uses_self,
11274 # accesses_object_data,
11275 # calls_instance_methods,
11276 # private_method, and confidence.
11277 #
11278 # Side effects: None.
11279 # --------------------------------------------------
11280 725 ▶sub _detect_instance_method {
Subroutine _detect_instance_method: called CC=14 SCAR=26.4
11281 725 my ($self, $method_name, $method_body) = @_;
11282  
11283 725 my %instance_info;
11284  
11285 # Pattern 1: my ($self, ...) = @_;
11286 725 ▶ if ($method_body =~ /my\s*\(\s*\$self\s*[,)]/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($method_body =~ /my\s*\(\s*\$self\s*[,)]/) { } 306 419 725
elsif ($method_body =~ /my\s+\$self\s*=\s*\$_\[0\]/) { } 55 364 419
elsif ($method_body =~ /my\s+\$self\s*=\s*shift/) { } 34 330 364
elsif ($method_body =~ /\$self\s*->\s*(\w+|[\{\[])/) { } 2 328 330
11287 306 $instance_info{explicit_self} = 1;
11288 306 $instance_info{confidence} = 'high';
11289 }
11290  
11291 # Pattern 1b: my $self = $_[0]; (direct-index style)
11292 elsif ($method_body =~ /my\s+\$self\s*=\s*\$_\[0\]/) {
11293 55 $instance_info{explicit_self} = 1;
11294 55 $instance_info{confidence} = 'high';
11295 }
11296  
11297 # Pattern 2: my $self = shift;
11298 elsif ($method_body =~ /my\s+\$self\s*=\s*shift/) {
11299 34 $instance_info{shift_self} = 1;
11300 34 $instance_info{confidence} = 'high';
11301 }
11302  
11303 # Pattern 3: Uses $self->something (including hash/array access)
11304 # This catches $self->{value} and $self->[0] as well as $self->method()
11305 elsif ($method_body =~ /\$self\s*->\s*(\w+|[\{\[])/) {
11306 2 $instance_info{uses_self} = 1;
11307 2 $instance_info{confidence} = 'medium';
11308 }
11309  
11310 # Pattern 4: Accesses object data: $self->{...}, $self->[...]
11311 725 ▶ if ($method_body =~ /\$self\s*->\s*[\{\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$self\s*->\s*[\{\[]/) 169 556 725
11312 169 $instance_info{accesses_object_data} = 1;
11313 169 ▶ $instance_info{confidence} = 'high' unless $instance_info{confidence} eq 'high';
Branch: 1 branch
BranchTrueFalseTotal
unless $instance_info{"confidence"} eq "high" 2 167 169
11314 }
11315  
11316 # Pattern 5: Calls other instance methods on $self
11317 725 ▶ if ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/s) 46 679 725
11318 46 $instance_info{calls_instance_methods} = [];
11319 46 ▶ while ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/g) 136 46 182
11320 136 push @{$instance_info{calls_instance_methods}}, $1;
11321 }
11322 46 ▶ $instance_info{confidence} = 'high' if @{$instance_info{calls_instance_methods}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$instance_info{"calls_instance_methods"};} 46 0 46
11323 }
11324  
11325 # Pattern 6: Method name suggests instance method (not perfect but helpful)
11326 725 ▶ if ($method_name =~ /^_/ && $method_name !~ /^_new/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^_/ and not $method_name =~ /^_new/) 17 708 725
Condition100%3/3
$method_name =~ /^_/ and not $method_name =~ /^_new/
!ll&&!rl&&r
705317
MC/DC100%2/2
$method_name =~ /^_/ and not $method_name =~ /^_new/
$method_name =~ /^_/ not $method_name =~ /^_new/
Truth table100%3/3
A and not(B)
A= $method_name =~ /^_/B= not $method_name =~ /^_new/
ABresult
0X0
100
111
11327 # Private methods are usually instance methods
11328 17 $instance_info{private_method} = 1;
11329 17 ▶ $instance_info{confidence} = 'low' unless exists $instance_info{confidence};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $instance_info{"confidence"} 11 6 17
11330 }
11331  
11332 725 ▶ return \%instance_info if keys %instance_info;
Branch: 1 branch
BranchTrueFalseTotal
if keys %instance_info 408 317 725
11333 317 return undef;
11334 }
11335  
11336 # --------------------------------------------------
11337 # _check_inheritance_for_constructor
11338 #
11339 # Purpose: Determine whether the current package
11340 # uses an inherited constructor from a
11341 # parent class, by examining use parent,
11342 # use base, and @ISA declarations.
11343 #
11344 # Entry: $current_package - current package
11345 # name string.
11346 # $method_body - method source string
11347 # (checked for SUPER::
11348 # calls).
11349 #
11350 # Exit: Returns an inheritance_info hashref
11351 # if any inheritance information is
11352 # found, or undef otherwise.
11353 # The hashref may contain:
11354 # parent_statements, isa_array,
11355 # uses_super, calls_super_new,
11356 # has_own_constructor,
11357 # use_parent_constructor, parent_class.
11358 #
11359 # Side effects: None.
11360 # --------------------------------------------------
11361 396 ▶sub _check_inheritance_for_constructor {
Subroutine _check_inheritance_for_constructor: called CC=18 SCAR=29.8
11362 396 my ($self, $current_package, $method_body) = @_;
11363  
11364 396 my $doc = $self->{_document};
11365 396 ▶ return undef unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 1 395 396
11366  
11367 395 my %inheritance_info;
11368  
11369 # 1. Look for parent/base statements
11370 my @parent_classes;
11371  
11372 # Find all 'use parent' or 'use base' statements
11373 395 ▶ my $includes = $doc->find('PPI::Statement::Include') || [];
Condition100%2/2
$doc->find('PPI::Statement::Include') || []
l!l
33065
MC/DC100%1/1
$doc->find('PPI::Statement::Include') || []
$doc->find('PPI::Statement::Include')
Truth table100%2/2
A
A= $doc->find('PPI::Statement::Include')
Aresult
11
00
11374 395 foreach my $inc (@$includes) {
11375 1539 my $content = $inc->content;
11376 1539 ▶ if ($content =~ /use\s+(parent|base)\s+['"]?([\w:]+)['"]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /use\s+(parent|base)\s+['"]?([\w:]+)['"]?/) 28 1511 1539
11377 28 push @parent_classes, $2;
11378 28 $inheritance_info{parent_statements} = \@parent_classes;
11379 }
11380 # Also check for multiple parents: use parent qw(Class1 Class2)
11381 1539 ▶ if ($content =~ /use\s+(parent|base)\s+qw?[\(\[]?(.+?)[\)\]]?;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /use\s+(parent|base)\s+qw?[\(\[]?(.+?)[\)\]]?;/) 0 1539 1539
11382 0 my $parents = $2;
11383 0 my @multi_parents = split /\s+/, $parents;
11384 0 push @parent_classes, @multi_parents;
11385 0 $inheritance_info{parent_statements} = \@parent_classes;
11386 }
11387 }
11388  
11389 # 2. Look for @ISA assignments (with or without 'our')
11390 395 ▶ my $isas = $doc->find('PPI::Statement::Variable') || [];
Condition100%2/2
$doc->find('PPI::Statement::Variable') || []
l!l
3932
MC/DC100%1/1
$doc->find('PPI::Statement::Variable') || []
$doc->find('PPI::Statement::Variable')
Truth table100%2/2
A
A= $doc->find('PPI::Statement::Variable')
Aresult
11
00
11391 395 foreach my $isa (@$isas) {
11392 13850 my $content = $isa->content();
11393 # Match both "our @ISA = qw(...)" and "@ISA = qw(...)"
11394 13850 ▶ if ($content =~ /(?:our\s+)?\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /(?:our\s+)?\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) 0 13850 13850
11395 0 my $parents = $1;
11396 0 my @isa_parents = split(/\s+/, $parents);
11397 0 push @parent_classes, @isa_parents;
11398 0 $inheritance_info{isa_array} = \@isa_parents;
11399 }
11400 }
11401  
11402 # Also look for @ISA in regular statements
11403 395 ▶ my $statements = $doc->find('PPI::Statement') || [];
Condition50%1/2
$doc->find('PPI::Statement') || []
l!l
3950
MC/DC0%0/1
$doc->find('PPI::Statement') || []
$doc->find('PPI::Statement')
Truth table50%1/2
A
A= $doc->find('PPI::Statement')
Aresult
11
00
11404 395 foreach my $stmt (@$statements) {
11405 119383 my $content = $stmt->content;
11406 119383 ▶ if ($content =~ /\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) 19 119364 119383
11407 19 my $parents = $1;
11408 19 my @isa_parents = split(/\s+/, $parents);
11409 19 push @parent_classes, @isa_parents;
11410 19 $inheritance_info{isa_array} = \@isa_parents;
11411 }
11412 }
11413  
11414 # 3. Check if method uses SUPER:: calls
11415 395 ▶ if ($method_body && $method_body =~ /SUPER::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body and $method_body =~ /SUPER::/) 2 393 395
Condition100%3/3
$method_body and $method_body =~ /SUPER::/
!ll&&!rl&&r
23912
MC/DC100%2/2
$method_body and $method_body =~ /SUPER::/
$method_body $method_body =~ /SUPER::/
Truth table100%3/3
A and B
A= $method_bodyB= $method_body =~ /SUPER::/
ABresult
0X0
100
111
11416 2 $inheritance_info{uses_super} = 1;
11417 2 ▶ if ($method_body =~ /SUPER::new/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /SUPER::new/) 0 2 2
11418 0 $inheritance_info{calls_super_new} = 1;
11419 }
11420 }
11421  
11422 # 4. Check if current package has its own new method
11423 1172725 ▶ my $has_own_new = $doc->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
11424 1172725 ▶ $_[1]->isa('PPI::Statement::Sub') &&
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Sub") and $_[1]->name eq "new" 3924 1168801 1172725
11425 $_[1]->name eq 'new'
11426 395 });
11427  
11428 395 ▶ if ($has_own_new) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_own_new) { } 205 190 395
elsif (@parent_classes) { } 25 165 190
11429 205 $inheritance_info{has_own_constructor} = 1;
11430 } elsif (@parent_classes) {
11431 # No own constructor, but has parents - might need parent constructor
11432 25 $inheritance_info{use_parent_constructor} = 1;
11433 25 $inheritance_info{parent_class} = $parent_classes[0]; # Use first parent
11434 }
11435  
11436 395 ▶ return \%inheritance_info if keys %inheritance_info;
Branch: 1 branch
BranchTrueFalseTotal
if keys %inheritance_info 230 165 395
11437 165 return undef;
11438 }
11439  
11440 # --------------------------------------------------
11441 # _detect_constructor_requirements
11442 #
11443 # Purpose: Analyse the new() method of the
11444 # current or target package to determine
11445 # what parameters the constructor
11446 # requires, including required and
11447 # optional parameters and their defaults.
11448 #
11449 # Entry: $current_package - the package being
11450 # analysed.
11451 # $target_package - the package whose
11452 # constructor will
11453 # be called (may
11454 # differ from current
11455 # for inherited
11456 # constructors).
11457 #
11458 # Exit: Returns a requirements hashref on
11459 # success, or undef if no new() method
11460 # is found. For external classes,
11461 # returns a minimal hashref with
11462 # external_class => 1.
11463 #
11464 # Side effects: None.
11465 # --------------------------------------------------
11466 397 ▶sub _detect_constructor_requirements {
Subroutine _detect_constructor_requirements: called CC=16 SCAR=27.8
11467 397 my ($self, $current_package, $target_package) = @_;
11468  
11469 397 my $doc = $self->{_document};
11470 397 ▶ return undef unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 397 397
11471  
11472 # If target is different from current, we can't analyze it
11473 # (external class, parent class in different file)
11474 397 ▶ if ($target_package ne $current_package) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_package ne $current_package) 25 372 397
11475 return {
11476 25 external_class => 1,
11477 package => $target_package,
11478 note => "Constructor for external class $target_package - parameters unknown"
11479 };
11480 }
11481  
11482 # Find the new method in current package
11483 94091 ▶ my $new_method = $doc->find_first(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
11484 94091 ▶ $_[1]->isa('PPI::Statement::Sub') &&
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Sub") and $_[1]->name eq "new" 1068 93023 94091
11485 $_[1]->name eq 'new'
11486 372 });
11487  
11488 372 ▶ return undef unless $new_method;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_method 166 206 372
11489  
11490 206 my %requirements;
11491  
11492 # Get method body
11493 206 my $body = $new_method->content;
11494  
11495 # Look for parameter extraction patterns - handle both $self and $class
11496 206 ▶ if ($body =~ /my\s*\(\s*\$(self|class)\s*,\s*(.+?)\)\s*=\s*\@_/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /my\s*\(\s*\$(self|class)\s*,\s*(.+?)\)\s*=\s*\@_/s) 132 74 206
11497 132 my $params = $2;
11498 132 my @param_names = $params =~ /\$(\w+)/g;
11499  
11500 132 ▶ if (@param_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param_names) 3 129 132
11501 3 $requirements{parameters} = \@param_names;
11502 3 $requirements{parameter_count} = scalar @param_names;
11503 }
11504 }
11505  
11506 # Look for shift patterns
11507 206 my @shift_params;
11508 206 ▶ while ($body =~ /my\s+\$(\w+)\s*=\s*shift/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /my\s+\$(\w+)\s*=\s*shift/g) 10 206 216
11509 10 push @shift_params, $1;
11510 }
11511 # Remove $self or $class if present
11512 206 @shift_params = grep { $_ !~ /^(self|class|pkg|proto|klass)$/i } @shift_params;
11513  
11514 206 ▶ if (@shift_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@shift_params) 0 206 206
11515 0 $requirements{parameters} = \@shift_params;
11516 0 $requirements{parameter_count} = scalar @shift_params;
11517 0 $requirements{shift_pattern} = 1;
11518 }
11519  
11520 # Look for validation of parameters (more flexible pattern)
11521 206 my @required_params;
11522 206 ▶ if ($body =~ /croak.*unless.*(?:defined\s+)?\$(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /croak.*unless.*(?:defined\s+)?\$(\w+)/g) 90 116 206
11523 90 push @required_params, $1;
11524 }
11525 206 ▶ if ($body =~ /die.*unless.*(?:defined\s+)?\$(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /die.*unless.*(?:defined\s+)?\$(\w+)/g) 1 205 206
11526 1 push @required_params, $1;
11527 }
11528  
11529 206 ▶ if (@required_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@required_params) 91 115 206
11530 91 $requirements{required_parameters} = \@required_params;
11531 }
11532  
11533 # Look for default values (optional parameters)
11534 206 my @optional_params;
11535 my %default_values;
11536  
11537 # Use the new _extract_default_value method
11538 # Check for each parameter in the constructor body
11539 206 ▶ if ($requirements{parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requirements{"parameters"}) 3 203 206
11540 3 foreach my $param (@{$requirements{parameters}}) {
11541 5 my $default = $self->_extract_default_value($param, $body);
11542 5 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 2 3 5
11543 2 push @optional_params, $param;
11544 2 $default_values{$param} = $default;
11545 }
11546 }
11547 }
11548  
11549 206 ▶ if (@optional_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@optional_params) 2 204 206
11550 2 $requirements{optional_parameters} = \@optional_params;
11551 2 $requirements{default_values} = \%default_values;
11552 }
11553  
11554 206 ▶ return \%requirements if keys %requirements;
Branch: 1 branch
BranchTrueFalseTotal
if keys %requirements 91 115 206
11555 115 return undef;
11556 }
11557  
11558  
11559 # --------------------------------------------------
11560 # _detect_external_object_dependency
11561 #
11562 # Purpose: Detect whether a method creates or
11563 # depends on objects from classes other
11564 # than the current package, by scanning
11565 # for ->new() calls on named classes
11566 # and method calls on typed variables.
11567 #
11568 # Entry: $method_body - method source string.
11569 # May be undef.
11570 #
11571 # Exit: Returns a dependency_info hashref if
11572 # external object usage is found, or
11573 # undef otherwise.
11574 # The hashref may contain:
11575 # creates_objects (arrayref of class
11576 # names), uses_objects (arrayref of
11577 # class names), and package (the primary
11578 # dependency class).
11579 #
11580 # Side effects: None.
11581 # --------------------------------------------------
11582 333 ▶sub _detect_external_object_dependency {
Subroutine _detect_external_object_dependency: called CC=15 SCAR=27.1
11583 333 my ($self, $method_body) = @_;
11584  
11585 333 ▶ return undef unless $method_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $method_body 1 332 333
11586  
11587 332 my %dependency_info;
11588  
11589 # Pattern 1: Creates objects of other classes with ->new() or ->create()
11590 # Reset pos for global match
11591 332 pos($method_body) = 0;
11592 332 ▶ while ($method_body =~ /(\w+(?:::\w+)*)->(?:new|create)\(/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /(\w+(?:::\w+)*)->(?:new|create)\(/g) 14 332 346
11593 14 my $class = $1;
11594 14 ▶ next if $class eq 'main' || $class eq '__PACKAGE__' || $class =~ /^\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "main" or $class eq "__PACKAGE__" or $class =~ /^\$/ 1 13 14
Condition66%4/6
$class eq "main" or $class eq "__PACKAGE__" or $class =~ /^\$/
l!l&&r!l&&!r
1013
$class eq "main" or $class eq "__PACKAGE__"
l!l&&r!l&&!r
0113
MC/DC33%1/3
$class eq "main" or $class eq "__PACKAGE__" or $class =~ /^\$/
$class eq "main" $class eq "__PACKAGE__" $class =~ /^\$/
Truth table50%2/4
A or B or C
A= $class eq "main"B= $class eq "__PACKAGE__"C= $class =~ /^\$/
ABCresult
1XX1
01X1
0011
0000
11595 13 push @{$dependency_info{creates_objects}}, $class;
11596 }
11597  
11598 332 ▶ if ($dependency_info{creates_objects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dependency_info{"creates_objects"}) 9 323 332
11599 # Remove duplicates
11600 9 my %seen;
11601 9 $dependency_info{creates_objects} = [grep { !$seen{$_}++ } @{$dependency_info{creates_objects}}];
11602 9 $dependency_info{package} = $dependency_info{creates_objects}[0];
11603 }
11604  
11605 # Pattern 2: Calls methods on objects from other classes
11606 332 ▶ if ($method_body =~ /\$(\w+)->\w+\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$(\w+)->\w+\(/) 13 319 332
11607 13 my %object_vars;
11608 # Reset pos for global match — the if check above used a
11609 # non-/g match so it cannot have advanced pos, but the while
11610 # loop's own /g matches still need to start from the beginning.
11611 13 pos($method_body) = 0;
11612 13 ▶ while ($method_body =~ /\$(\w+)->\w+\(/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$(\w+)->\w+\(/g) 26 13 39
11613 26 $object_vars{$1}++;
11614 }
11615  
11616 # Try to determine type of object variables
11617 13 my @object_classes;
11618 13 foreach my $var (keys %object_vars) {
11619 # Look for type declarations or assignments
11620 23 ▶ if ($method_body =~ /my\s+\$$var\s*=\s*(\w+(?:::\w+)+)->(?:new|create)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method_body =~ /my\s+\$$var\s*=\s*(\w+(?:::\w+)+)->(?:new|create)/) { } 9 14 23
elsif ($method_body =~ /my\s+\$$var\s*=\s*(\w+(?:::\w+)+)->/) { } 0 14 14
11621 9 push @object_classes, $1;
11622 } elsif ($method_body =~ /my\s+\$$var\s*=\s*(\w+(?:::\w+)+)->/) {
11623 0 push @object_classes, $1;
11624 }
11625 }
11626  
11627 13 ▶ if (@object_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@object_classes) 9 4 13
11628 9 $dependency_info{uses_objects} = \@object_classes;
11629 9 ▶ $dependency_info{package} = $object_classes[0] unless $dependency_info{package};
Branch: 1 branch
BranchTrueFalseTotal
unless $dependency_info{"package"} 1 8 9
11630 }
11631 }
11632  
11633 # Pattern 3: Receives objects as parameters (type hints in comments/POD)
11634 # This would need integration with parameter analysis
11635  
11636 332 ▶ return \%dependency_info if keys %dependency_info;
Branch: 1 branch
BranchTrueFalseTotal
if keys %dependency_info 10 322 332
11637 322 return undef;
11638 }
11639  
11640 # --------------------------------------------------
11641 # _get_parent_class
11642 #
11643 # Purpose: Find the first parent class of the
11644 # current package by searching the
11645 # PPI document for use parent, use base,
11646 # or our @ISA declarations.
11647 #
11648 # Entry: None (operates on $self->{_document}).
11649 #
11650 # Exit: Returns the parent class name string,
11651 # or undef if no parent is found.
11652 #
11653 # Side effects: None.
11654 # --------------------------------------------------
11655 0 ▶sub _get_parent_class {
Subroutine _get_parent_class: not called CC=5 SCAR=34.0
11656 0 my $self = $_[0];
11657  
11658 0 my $doc = $self->{_document};
11659 0 ▶ return unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 0 0
11660  
11661 # Look for use parent statements
11662 0 ▶ my $parent_stmt = $doc->find_first(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
11663 0 ▶ $_[1]->isa('PPI::Statement::Include') &&
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Include") and $_[1]->type eq "use" and $_[1]->module =~ /^(parent|base)$/ and $_[1]->arguments =~ /['"](\w+(?:::\w+)*)['"]/ 0 0 0
Condition0%0/6
$_[1]->isa("PPI::Statement::Include") and $_[1]->type eq "use" and $_[1]->module =~ /^(parent|base)$/
!ll&&!rl&&r
000
$_[1]->isa("PPI::Statement::Include") and $_[1]->type eq "use"
!ll&&!rl&&r
000
MC/DC0%0/3
$_[1]->isa("PPI::Statement::Include") and $_[1]->type eq "use" and $_[1]->module =~ /^(parent|base)$/
$_[1]->isa("PPI::Statement::Include") $_[1]->type eq "use" $_[1]->module =~ /^(parent|base)$/
Truth table0%0/4
A and B and C
A= $_[1]->isa("PPI::Statement::Include")B= $_[1]->type eq "use"C= $_[1]->module =~ /^(parent|base)$/
ABCresult
0XX0
10X0
1100
1111
11664 $_[1]->type eq 'use' &&
11665 $_[1]->module =~ /^(parent|base)$/ &&
11666 $_[1]->arguments =~ /['"](\w+(?:::\w+)*)['"]/
11667 0 });
11668 0 ▶ if ($parent_stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_stmt) 0 0 0
11669 0 my $parent = $1;
11670 0 return $parent;
11671 }
11672  
11673 # Look for @ISA assignment
11674 0 ▶ my $isa_stmt = $doc->find_first(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
11675 0 ▶ $_[1]->isa('PPI::Statement') &&
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement") and $_[1]->content =~ /our\s+\@ISA\s*=\s*\(\s*['"](\w+(?:::\w+)*)['"]\s*\)/ 0 0 0
11676 $_[1]->content =~ /our\s+\@ISA\s*=\s*\(\s*['"](\w+(?:::\w+)*)['"]\s*\)/
11677 0 });
11678 0 ▶ if ($isa_stmt && $isa_stmt->content =~ /['"](\w+(?:::\w+)*)['"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isa_stmt and $isa_stmt->content =~ /['"](\w+(?:::\w+)*)['"]/) 0 0 0
Condition0%0/3
$isa_stmt and $isa_stmt->content =~ /['"](\w+(?:::\w+)*)['"]/
!ll&&!rl&&r
000
MC/DC0%0/2
$isa_stmt and $isa_stmt->content =~ /['"](\w+(?:::\w+)*)['"]/
$isa_stmt $isa_stmt->content =~ /['"](\w+(?:::\w+)*)['"]/
Truth table0%0/3
A and B
A= $isa_stmtB= $isa_stmt->content =~ /['"](\w+(?:::\w+)*)['"]/
ABresult
0X0
100
111
11679 0 return $1;
11680 }
11681  
11682 0 return;
11683 }
11684  
11685 # --------------------------------------------------
11686 # _get_class_for_instance_method
11687 #
11688 # Purpose: Determine which class should be used
11689 # for object instantiation when testing
11690 # an instance method, preferring the
11691 # current package if it has a new()
11692 # method, falling back to the parent
11693 # class otherwise.
11694 #
11695 # Entry: None (operates on $self->{_document}).
11696 #
11697 # Exit: Returns the package name string to
11698 # use for instantiation. Returns
11699 # 'UNKNOWN_PACKAGE' if no package
11700 # statement is found.
11701 #
11702 # Side effects: Stores the package name in
11703 # $self->{_package_name} if not
11704 # already set.
11705 # --------------------------------------------------
11706 0 ▶sub _get_class_for_instance_method {
Subroutine _get_class_for_instance_method: not called CC=5 SCAR=34.0
11707 0 my $self = $_[0];
11708  
11709 # Get the current package
11710 0 my $doc = $self->{_document};
11711 0 my $package_stmt = $doc->find_first('PPI::Statement::Package');
11712 0 ▶ return 'UNKNOWN_PACKAGE' unless $package_stmt;
Branch: 1 branch
BranchTrueFalseTotal
unless $package_stmt 0 0 0
11713 0 my $package_name = $package_stmt->namespace;
11714 0 ▶ $self->{_package_name} //= $package_name;
Condition0%0/3
$self->{"_package_name"} //= $package_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_package_name"} //= $package_name
$self->{"_package_name"} $package_name
Truth table0%0/3
A //= B
A= $self->{"_package_name"}B= $package_name
ABresult
1X1
011
000
11715  
11716 # Check if the current package has a 'new' method
11717 0 ▶ my $has_new = $doc->find(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
11718 0 ▶ $_[1]->isa('PPI::Statement::Sub') && $_[1]->name eq 'new'
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Sub") and $_[1]->name eq "new" 0 0 0
11719 0 });
11720  
11721 0 ▶ if ($has_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_new) 0 0 0
11722 0 return $package_name;
11723 }
11724  
11725 # Otherwise, try to get the parent class
11726 0 my $parent = $self->_get_parent_class();
11727 0 ▶ return $parent if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 0 0 0
11728  
11729 # Fallback to current package
11730 0 return $package_name;
11731 }
11732  
11733 # --------------------------------------------------
11734 # _extract_default_value
11735 #
11736 # Purpose: Extract a default value for a named
11737 # parameter from a method body by
11738 # matching multiple common Perl default
11739 # assignment idioms.
11740 #
11741 # Entry: $param - parameter name string.
11742 # $code - method body source string.
11743 #
11744 # Exit: Returns the cleaned default value
11745 # scalar on success, or undef if no
11746 # default assignment pattern is found.
11747 #
11748 # Side effects: None.
11749 #
11750 # Notes: Eight patterns are tried in order:
11751 # ||, //=, defined ternary, unless
11752 # defined, ||=, //, multi-line if
11753 # !defined, unless defined block.
11754 # Comment lines are stripped from the
11755 # code before matching to avoid false
11756 # positives. Delegates to
11757 # _clean_default_value for value
11758 # normalisation.
11759 # --------------------------------------------------
11760 573 ▶sub _extract_default_value {
Subroutine _extract_default_value: called CC=19 SCAR=29.5
11761 573 my ($self, $param, $code) = @_;
11762  
11763 573 ▶ return undef unless $param && $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $param and $code 3 570 573
Condition100%3/3
$param and $code
!ll&&!rl&&r
12570
MC/DC100%2/2
$param and $code
$param $code
Truth table100%3/3
A and B
A= $paramB= $code
ABresult
0X0
100
111
11764  
11765 # Clean up the code for easier pattern matching
11766 # Remove comments to avoid false positives
11767 570 my $clean_code = $code;
11768 570 $clean_code =~ s/#.*$//gm;
11769 570 $clean_code =~ s/^\s+|\s+$//g;
11770  
11771 # Pattern 1: $param = $param || 'default_value'
11772 # Also handles: $param = $arg || 'default'
11773 570 ▶ if ($clean_code =~ /\$$param\s*=\s*(?:\$$param|\$[a-zA-Z_]\w*)\s*\|\|\s*([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ /\$$param\s*=\s*(?:\$$param|\$[a-zA-Z_]\w*)\s*\|\|\s*([^;]+)/) 12 558 570
11774 12 my $default = $1;
11775 12 $default =~ s/\s*;\s*$//;
11776 12 $default = $self->_clean_default_value($default);
11777 12 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 12 0 12
11778 }
11779  
11780 # Pattern 2: $param //= 'default_value'
11781 558 ▶ if ($clean_code =~ /\$$param\s*\/\/=\s*([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ m[\$$param\s*//=\s*([^;]+)]) 10 548 558
11782 10 my $default = $1;
11783 10 $default =~ s/\s*;\s*$//;
11784 10 $default = $self->_clean_default_value($default);
11785 10 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 6 4 10
11786 }
11787  
11788 # Pattern 3: $param = defined $param ? $param : 'default'
11789 # Also handles: $param = defined $arg ? $arg : 'default'
11790 552 ▶ if ($clean_code =~ /\$$param\s*=\s*defined\s+(?:\$$param|\$[a-zA-Z_]\w*)\s*\?\s*(?:\$$param|\$[a-zA-Z_]\w*)\s*:\s*([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ /\$$param\s*=\s*defined\s+(?:\$$param|\$[a-zA-Z_]\w*)\s*\?\s*(?:\$$param|\$[a-zA-Z_]\w*)\s*:\s*([^;]+)/) 6 546 552
11791 6 my $default = $1;
11792 6 $default =~ s/\s*;\s*$//;
11793 6 $default = $self->_clean_default_value($default);
11794 6 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 6 0 6
11795 }
11796  
11797 # Pattern 4: $param = 'default' unless defined $param;
11798 546 ▶ if ($clean_code =~ /\$$param\s*=\s*([^;]+?)\s+unless\s+defined\s+(?:\$$param|\$[a-zA-Z_]\w*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ /\$$param\s*=\s*([^;]+?)\s+unless\s+defined\s+(?:\$$param|\$[a-zA-Z_]\w*)/) 3 543 546
11799 3 my $default = $1;
11800 3 $default = $self->_clean_default_value($default);
11801 3 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 3 0 3
11802 }
11803  
11804 # Pattern 5: $param ||= 'default'
11805 543 ▶ if ($clean_code =~ /\$$param\s*\|\|=\s*([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ /\$$param\s*\|\|=\s*([^;]+)/) 16 527 543
11806 16 my $default = $1;
11807 16 $default =~ s/\s*;\s*$//;
11808 16 $default = $self->_clean_default_value($default);
11809 16 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 16 0 16
11810 }
11811  
11812 # Pattern 6: $param = $arg // 'default'
11813 527 ▶ if ($clean_code =~ /\$$param\s*=\s*(?:\$$param|\$[a-zA-Z_]\w*)\s*\/\/\s*([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ m[\$$param\s*=\s*(?:\$$param|\$[a-zA-Z_]\w*)\s*//\s*([^;]+)]) 2 525 527
11814 2 my $default = $1;
11815 2 $default =~ s/\s*;\s*$//;
11816 2 $default = $self->_clean_default_value($default);
11817 2 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 1 1 2
11818 }
11819  
11820 # Pattern 7: Multi-line: if (!defined $param) { $param = 'default'; }
11821 526 ▶ if ($clean_code =~ /if\s*\(\s*!defined\s+\$$param\s*\)\s*\{[^}]*\$$param\s*=\s*([^;]+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ /if\s*\(\s*!defined\s+\$$param\s*\)\s*\{[^}]*\$$param\s*=\s*([^;]+)/s) 1 525 526
11822 1 my $default = $1;
11823 1 $default =~ s/\s*;\s*$//;
11824 1 $default = $self->_clean_default_value($default);
11825 1 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 1 0 1
11826 }
11827  
11828 # Pattern 8: unless (defined $param) { $param = 'default'; }
11829 525 ▶ if ($clean_code =~ /unless\s*\(\s*defined\s+\$$param\s*\)\s*\{[^}]*\$$param\s*=\s*([^;]+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ /unless\s*\(\s*defined\s+\$$param\s*\)\s*\{[^}]*\$$param\s*=\s*([^;]+)/s) 1 524 525
11830 1 my $default = $1;
11831 1 $default =~ s/\s*;\s*$//;
11832 1 $default = $self->_clean_default_value($default);
11833 1 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 1 0 1
11834 }
11835  
11836 524 return undef;
11837 }
11838  
11839 # --------------------------------------------------
11840 # _extract_test_hints
11841 #
11842 # Purpose: Extract structured test hints from
11843 # a method's code and schema, including
11844 # boundary values, invalid inputs, and
11845 # valid input examples from POD.
11846 #
11847 # Entry: $method - method hashref.
11848 # $schema - schema hashref as built so
11849 # far by _analyze_method.
11850 #
11851 # Exit: Returns a hints hashref with keys:
11852 # boundary_values, invalid_inputs,
11853 # equivalence_classes, valid_inputs.
11854 # Keys with empty arrays are deleted
11855 # before returning.
11856 #
11857 # Side effects: None.
11858 # --------------------------------------------------
11859 813 ▶sub _extract_test_hints {
Subroutine _extract_test_hints: called CC=4 SCAR=13.9
11860 813 my ($self, $method, $schema) = @_;
11861  
11862 813 my %hints = (
11863 boundary_values => [],
11864 invalid_inputs => [],
11865 equivalence_classes => [],
11866 valid_inputs => [],
11867 );
11868  
11869 813 my $code = $method->{body};
11870 813 ▶ return {} unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 813 813
11871  
11872 813 $self->_extract_invalid_input_hints($code, \%hints);
11873 813 $self->_extract_boundary_value_hints($code, \%hints);
11874  
11875 # prune empties
11876 813 for my $k (keys %hints) {
11877 3252 ▶ delete $hints{$k} unless @{$hints{$k}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$hints{$k};} 3118 134 3252
11878 }
11879  
11880 813 return \%hints;
11881 }
11882  
11883 # --------------------------------------------------
11884 # _extract_invalid_input_hints
11885 #
11886 # Purpose: Detect likely invalid input values
11887 # from a method body by looking for
11888 # defined checks, empty string checks,
11889 # and negative number checks.
11890 #
11891 # Entry: $code - method body source string.
11892 # $hints - hints hashref (modified in
11893 # place via invalid_inputs key).
11894 #
11895 # Exit: Returns nothing. Appends to
11896 # $hints->{invalid_inputs}.
11897 #
11898 # Side effects: None.
11899 # --------------------------------------------------
11900 817 ▶sub _extract_invalid_input_hints {
Subroutine _extract_invalid_input_hints: called CC=5 SCAR=16.1
11901 817 my ($self, $code, $hints) = @_;
11902  
11903 # undef invalid
11904 817 ▶ if ($code =~ /defined\s*\(\s*\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /defined\s*\(\s*\$/) 64 753 817
11905 64 push @{ $hints->{invalid_inputs} }, 'undef';
11906 }
11907  
11908 # empty string invalid
11909 817 ▶ if ($code =~ /\beq\s*''/ || $code =~ /\blength\s*\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\beq\s*''/ or $code =~ /\blength\s*\(/) 42 775 817
Condition100%3/3
$code =~ /\beq\s*''/ or $code =~ /\blength\s*\(/
l!l&&r!l&&!r
438775
MC/DC100%2/2
$code =~ /\beq\s*''/ or $code =~ /\blength\s*\(/
$code =~ /\beq\s*''/ $code =~ /\blength\s*\(/
Truth table100%3/3
A or B
A= $code =~ /\beq\s*''/B= $code =~ /\blength\s*\(/
ABresult
1X1
011
000
11910 42 push @{ $hints->{invalid_inputs} }, '';
11911 }
11912  
11913 # negative number invalid
11914 817 ▶ if ($code =~ /\$\w+\s*<\s*0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*<\s*0/) 13 804 817
11915 13 push @{ $hints->{invalid_inputs} }, -1;
11916 }
11917 }
11918  
11919 # --------------------------------------------------
11920 # _extract_boundary_value_hints
11921 #
11922 # Purpose: Extract numeric boundary values from
11923 # comparison operators in a method body,
11924 # adding both the boundary value and
11925 # the value one step either side.
11926 #
11927 # Entry: $code - method body source string.
11928 # $hints - hints hashref (modified in
11929 # place via boundary_values key).
11930 #
11931 # Exit: Returns nothing. Appends to and
11932 # deduplicates $hints->{boundary_values}.
11933 #
11934 # Side effects: None.
11935 # --------------------------------------------------
11936 816 ▶sub _extract_boundary_value_hints {
Subroutine _extract_boundary_value_hints: called CC=6 SCAR=17.9
11937 816 my ($self, $code, $hints) = @_;
11938  
11939 816 ▶ while ($code =~ /\$\w+\s*(<=|<|>=|>)\s*(\d+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*(<=|<|>=|>)\s*(\d+)/g) 63 816 879
11940 63 my ($op, $n) = ($1, $2);
11941  
11942 63 ▶ if ($op eq '<') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '<') { } 19 44 63
elsif ($op eq '<=') { } 5 39 44
elsif ($op eq '>') { } 28 11 39
elsif ($op eq '>=') { } 11 0 11
11943 19 push @{ $hints->{boundary_values} }, $n, $n+1;
11944 } elsif ($op eq '<=') {
11945 5 push @{ $hints->{boundary_values} }, $n, $n+1;
11946 } elsif ($op eq '>') {
11947 28 push @{ $hints->{boundary_values} }, $n, $n-1;
11948 } elsif ($op eq '>=') {
11949 11 push @{ $hints->{boundary_values} }, $n, $n-1;
11950 }
11951 }
11952  
11953 # Remove duplicates
11954 816 my %seen;
11955 816 $hints->{boundary_values} = [ grep { !$seen{$_}++ } @{ $hints->{boundary_values} } ];
11956 }
11957  
11958 # --------------------------------------------------
11959 # _extract_pod_examples
11960 #
11961 # Purpose: Extract example method call patterns from a method's
11962 # SYNOPSIS section (=head1 or =head2) and from any
11963 # =for example begin/end blocks, and add them as
11964 # valid_inputs hints for fuzzing.
11965 #
11966 # Entry: $pod - POD string for the method. May be undef.
11967 # $hints - hints hashref (modified in place via the
11968 # valid_inputs key).
11969 #
11970 # Exit: Returns $hints. Appends to $hints->{valid_inputs}.
11971 #
11972 # Side effects: Logs the number of examples found to stdout when
11973 # verbose is set.
11974 #
11975 # Notes: For standalone runnable round-trip tests (not just
11976 # fuzzing hints) use App::Test::Generator::PodExampleExtractor
11977 # and bin/pod-example-tester instead.
11978 # --------------------------------------------------
11979 813 ▶sub _extract_pod_examples {
Subroutine _extract_pod_examples: called CC=17 SCAR=28.5
11980 813 my ($self, $pod, $hints) = @_;
11981  
11982 813 ▶ return $hints unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 239 574 813
11983  
11984 574 my @examples;
11985  
11986 # Accept both =head1 SYNOPSIS (module-level) and =head2 SYNOPSIS (method-level)
11987 574 my $synopsis = '';
11988 574 ▶ if($pod =~ /=head[12]\s+SYNOPSIS\s*(.+?)(?=\n=head|\z)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=head[12]\s+SYNOPSIS\s*(.+?)(?=\n=head|\z)/s) 3 571 574
11989 3 $synopsis = $1;
11990 }
11991  
11992 # Also collect =for example begin ... =for example end blocks
11993 574 my @for_blocks;
11994 574 ▶ while($pod =~ /=for\s+example\s+begin(.+?)=for\s+example\s+end/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=for\s+example\s+begin(.+?)=for\s+example\s+end/gs) 0 574 574
11995 0 push @for_blocks, $1;
11996 }
11997 574 $synopsis .= join('', @for_blocks);
11998  
11999 574 ▶ return $hints unless length($synopsis);
Branch: 1 branch
BranchTrueFalseTotal
unless length $synopsis 571 3 574
12000  
12001 # Constructor examples: ->wilma(foo => 'bar', count => 5)
12002 3 ▶ while ($synopsis =~ /->([a-z_0-9A-Z]+)\s*\(\s*(.*?)\s*\)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($synopsis =~ /->([a-z_0-9A-Z]+)\s*\(\s*(.*?)\s*\)/gs) 3 3 6
12003 3 my ($method, $args) = ($1, $2);
12004 3 my %kv;
12005  
12006 3 ▶ while ($args =~ /(\w+)\s*=>\s*(?:'([^']*)'|"([^"]*)"|(\d+))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /(\w+)\s*=>\s*(?:'([^']*)'|"([^"]*)"|(\d+))/g) 5 3 8
12007 5 my $key = $1;
12008 5 ▶ my $val = defined $2 ? $2 : defined $3 ? $3 : $4;
Branch: 2 branches
BranchTrueFalseTotal
defined $2 ? : 2 3 5
defined $3 ? : 0 3 3
12009 5 $kv{$key} = $val;
12010 }
12011  
12012 3 ▶ push @examples, {
Branch: 1 branch
BranchTrueFalseTotal
if %kv 2 1 3
12013 style => 'named',
12014 source => 'pod',
12015 args => \%kv,
12016 function => $method, # TODO: add a sanity check this is what we expect
12017 } if %kv;
12018 }
12019  
12020 3 ▶ unless(scalar(@examples)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @examples) 1 2 3
12021 # Positional calls: func($a, $b)
12022 1 ▶ while ($synopsis =~ /\b(\w+)\s*\(\s*(.*?)\s*\)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($synopsis =~ /\b(\w+)\s*\(\s*(.*?)\s*\)/gs) 1 1 2
12023 1 my ($func, $argstr) = ($1, $2);
12024  
12025 # next if $func eq 'new'; # already handled
12026  
12027 1 my @args = map { s/^\s+|\s+$//gr } split /\s*,\s*/, $argstr;
12028  
12029 1 ▶ next unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 1 1
12030  
12031 1 push @examples, {
12032 style => 'positional',
12033 source => 'pod',
12034 function => $func,
12035 args => \@args,
12036 };
12037 }
12038 }
12039  
12040 3 ▶ if (scalar(@examples)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @examples) 3 0 3
12041 3 ▶ $hints->{valid_inputs} ||= [];
Condition50%1/2
$hints->{"valid_inputs"} ||= []
l!l
03
MC/DC0%0/1
$hints->{"valid_inputs"} ||= []
$hints->{"valid_inputs"}
Truth table50%1/2
A
A= $hints->{"valid_inputs"}
Aresult
11
00
12042 3 push @{ $hints->{valid_inputs} }, @examples;
12043  
12044 3 $self->_log(" POD: extracted " . scalar(@examples) . " example call(s)");
12045 }
12046  
12047 3 for my $k (qw(boundary_values invalid_inputs valid_inputs equivalence_classes)) {
12048 12 ▶ $hints->{$k} //= [];
Condition100%2/2
$hints->{$k} //= []
l!l
39
MC/DC100%1/1
$hints->{$k} //= []
$hints->{$k}
Truth table100%2/2
A
A= $hints->{$k}
Aresult
11
00
12049 }
12050  
12051 3 return $hints;
12052 }
12053  
12054 # --------------------------------------------------
12055 # _clean_default_value
12056 #
12057 # Purpose: Normalise a raw default value string
12058 # extracted from code or POD into a
12059 # clean Perl scalar, handling quoted
12060 # strings, numeric literals, boolean
12061 # keywords, empty containers, and
12062 # undef.
12063 #
12064 # Entry: $value - raw value string.
12065 # May be undef.
12066 # $from_code - true if the value was
12067 # extracted from source
12068 # code (affects escape
12069 # sequence handling).
12070 #
12071 # Exit: Returns the cleaned value:
12072 # undef for undef or unparseable
12073 # {} for empty hashrefs
12074 # [] for empty arrayrefs
12075 # integer for whole numbers
12076 # float for decimal numbers
12077 # 1 or 0 for boolean keywords
12078 # string for everything else
12079 #
12080 # Side effects: None.
12081 # --------------------------------------------------
12082 255 ▶sub _clean_default_value {
Subroutine _clean_default_value: called CC=23 SCAR=31.5
12083 255 my ($self, $value, $from_code) = @_;
12084  
12085 255 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 254 255
12086  
12087 # Remove leading/trailing whitespace
12088 254 $value =~ s/^\s+|\s+$//g;
12089  
12090 # Remove parenthetical notes like "(no password)" only if there's content before them
12091 254 $value =~ s/(\S+)\s*\([^)]+\)\s*$/$1/;
12092 254 $value =~ s/^\s+|\s+$//g;
12093  
12094 # Handle chained || or // operators - extract the rightmost value
12095 254 ▶ if ($value =~ /\|\||\/{2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[\|\||/{2}]) 6 248 254
12096 6 my @parts = split(/\s*(?:\|\||\/{2})\s*/, $value);
12097 6 $value = $parts[-1];
12098 6 $value =~ s/^\s+|\s+$//g;
12099 }
12100  
12101 # Remove trailing semicolon if present
12102 254 $value =~ s/;\s*$//;
12103  
12104 # Handle q{}, qq{}, qw{} quotes
12105 254 ▶ if ($value =~ /^qq?\{(.*?)\}$/s) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ /^qq?\{(.*?)\}$/s) { } 2 252 254
elsif ($value =~ /^qw\{(.*?)\}$/s) { } 0 252 252
elsif ($value =~ /^q[qwx]?\s*([^a-zA-Z0-9\{\[])(.*?)\1$/s) { } 0 252 252
12106 2 $value = $1;
12107 } elsif ($value =~ /^qw\{(.*?)\}$/s) {
12108 0 $value = $1;
12109 } elsif ($value =~ /^q[qwx]?\s*([^a-zA-Z0-9\{\[])(.*?)\1$/s) {
12110 0 $value = $2;
12111 }
12112  
12113 # Handle quoted strings
12114 254 ▶ if ($value =~ /^(['"])(.*)\1$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(['"])(.*)\1$/s) 52 202 254
12115 52 $value = $2;
12116  
12117 52 ▶ if ($from_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_code) 16 36 52
12118 # In regex captures from source code, escape sequences are doubled
12119 # \\n in capture needs to become \n for the test
12120 16 $value =~ s/\\\\/\\/g;
12121 }
12122  
12123 # Only unescape the quote characters themselves
12124 52 $value =~ s/\\"/"/g;
12125 52 $value =~ s/\\'/'/g;
12126  
12127 # If NOT from code (i.e., from POD), interpret escape sequences
12128 52 ▶ unless ($from_code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from_code) 36 16 52
12129 36 $value =~ s/\\n/\n/g;
12130 36 $value =~ s/\\r/\r/g;
12131 36 $value =~ s/\\t/\t/g;
12132 36 $value =~ s/\\\\/\\/g;
12133 }
12134 }
12135  
12136 # Sometimes trailing ) is left on
12137 254 ▶ if($value !~ /^\(/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\(/) 252 2 254
12138 252 $value =~ s/\)$//;
12139 }
12140  
12141 # Handle Perl empty hash (must be before numeric/boolean checks)
12142 254 ▶ if ($value =~ /^\{\s*\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\{\s*\}$/) 13 241 254
12143 13 return {};
12144 }
12145  
12146 # Handle Perl empty list/array
12147 241 ▶ if ($value =~ /^\[\s*\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\[\s*\]$/) 7 234 241
12148 7 return [];
12149 }
12150  
12151 # Handle numeric values
12152 234 ▶ if ($value =~ /^-?\d+(?:\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-?\d+(?:\.\d+)?$/) 66 168 234
12153 66 ▶ if ($value =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\./) { } 13 53 66
12154 13 return $value + 0;
12155 } else {
12156 53 return int($value);
12157 }
12158 }
12159  
12160 # Handle boolean keywords
12161 168 ▶ if ($value =~ /^(true|false)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|false)$/i) 7 161 168
12162 7 ▶ return lc($1) eq 'true' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc $1 eq 'true' ? : 3 4 7
12163 }
12164  
12165 # Handle Perl boolean constants
12166 161 ▶ if ($value eq '1') {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq '1') { } 0 161 161
elsif ($value eq '0') { } 0 161 161
12167 0 return 1;
12168 } elsif ($value eq '0') {
12169 0 return 0;
12170 }
12171  
12172 # Handle undef
12173 161 ▶ if ($value eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "undef") 7 154 161
12174 7 return undef;
12175 }
12176  
12177 # Handle __PACKAGE__ and similar constants
12178 154 ▶ if ($value eq '__PACKAGE__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "__PACKAGE__") 1 153 154
12179 1 return '__PACKAGE__';
12180 }
12181  
12182 # Remove surrounding parentheses
12183 153 $value =~ s/^\((.+)\)$/$1/;
12184  
12185 # Handle expressions we can't evaluate
12186 153 ▶ if ($value =~ /^\$[a-zA-Z_]/ || $value =~ /\(.*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\$[a-zA-Z_]/ or $value =~ /\(.*\)/) 47 106 153
Condition100%3/3
$value =~ /^\$[a-zA-Z_]/ or $value =~ /\(.*\)/
l!l&&r!l&&!r
407106
MC/DC100%2/2
$value =~ /^\$[a-zA-Z_]/ or $value =~ /\(.*\)/
$value =~ /^\$[a-zA-Z_]/ $value =~ /\(.*\)/
Truth table100%3/3
A or B
A= $value =~ /^\$[a-zA-Z_]/B= $value =~ /\(.*\)/
ABresult
1X1
011
000
12187 47 ▶ return if($value =~ /^\$|\@|\%/); # The default is a value, so who knows its type?
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\$|\@|\%/ 40 7 47
12188 # return $value;
12189 }
12190  
12191 113 return $value;
12192 }
12193  
12194 # --------------------------------------------------
12195 # _validate_pod_code_agreement
12196 #
12197 # Purpose: Compare POD parameter documentation
12198 # against code-inferred parameters and
12199 # return a list of disagreements when
12200 # strict_pod mode is enabled.
12201 #
12202 # Entry: $pod_params - hashref of parameters
12203 # from POD analysis.
12204 # $code_params - hashref of parameters
12205 # from code analysis.
12206 # $method_name - method name string,
12207 # used for context in
12208 # error messages.
12209 #
12210 # Exit: Returns a list of disagreement
12211 # strings. Returns an empty list if
12212 # all parameters agree.
12213 #
12214 # Side effects: None.
12215 #
12216 # Notes: Type mismatches are classified as
12217 # either 'compatible' (e.g. integer vs
12218 # number) or 'incompatible' via
12219 # _types_are_compatible. $self and
12220 # $class are excluded from undocumented
12221 # parameter warnings in appropriate
12222 # context.
12223 # --------------------------------------------------
12224 407 ▶sub _validate_pod_code_agreement {
Subroutine _validate_pod_code_agreement: called CC=36 SCAR=36.3
12225 407 my ($self, $pod_params, $code_params, $method_name) = @_;
12226  
12227 407 my @errors;
12228  
12229 # Get all parameter names from both sources
12230 407 my %all_params = map { $_ => 1 } (keys %$pod_params, keys %$code_params);
12231  
12232 407 foreach my $param (sort keys %all_params) {
12233 426 ▶ my $pod = $pod_params->{$param} || {};
Condition100%2/2
$pod_params->{$param} || {}
l!l
40521
MC/DC100%1/1
$pod_params->{$param} || {}
$pod_params->{$param}
Truth table100%2/2
A
A= $pod_params->{$param}
Aresult
11
00
12234 426 ▶ my $code = $code_params->{$param} || {};
Condition100%2/2
$code_params->{$param} || {}
l!l
268158
MC/DC100%1/1
$code_params->{$param} || {}
$code_params->{$param}
Truth table100%2/2
A
A= $code_params->{$param}
Aresult
11
00
12235  
12236 # A =head3|4 Input formal spec is authoritative for the parameter's
12237 # existence, type, optionality and pattern: heuristic code analysis may
12238 # legitimately disagree. Its bounds are still checked below, because a
12239 # formal minimum and a code maximum that do not overlap leave the merged
12240 # schema with no valid value.
12241 426 ▶ unless(($pod->{_source} // '') eq SOURCE_INPUT_SPEC) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($pod->{"_source"} // "") eq "input_spec") 69 357 426
Condition100%2/2
$pod->{"_source"} // ""
l!l
40026
MC/DC100%1/1
$pod->{"_source"} // ""
$pod->{"_source"}
Truth table100%2/2
A
A= $pod->{"_source"}
Aresult
11
00
12242 # Check if parameter exists in both
12243 69 ▶ if (exists $pod_params->{$param} && !exists $code_params->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pod_params->{$param} and not exists $code_params->{$param}) 4 65 69
Condition100%3/3
exists $pod_params->{$param} and not exists $code_params->{$param}
!ll&&!rl&&r
21444
MC/DC100%2/2
exists $pod_params->{$param} and not exists $code_params->{$param}
exists $pod_params->{$param} not exists $code_params->{$param}
Truth table100%3/3
A and not(B)
A= exists $pod_params->{$param}B= not exists $code_params->{$param}
ABresult
0X0
100
111
12244 4 push @errors, "Parameter '\$$param' documented in POD but not found in code signature";
12245 4 next;
12246 }
12247  
12248 65 ▶ if(!exists $pod_params->{$param} && exists $code_params->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $pod_params->{$param} and exists $code_params->{$param}) 21 44 65
Condition66%2/3
not exists $pod_params->{$param} and exists $code_params->{$param}
!ll&&!rl&&r
44021
MC/DC50%1/2
not exists $pod_params->{$param} and exists $code_params->{$param}
not exists $pod_params->{$param} exists $code_params->{$param}
Truth table66%2/3
not(A) and B
A= not exists $pod_params->{$param}B= exists $code_params->{$param}
ABresult
0X0
100
111
12249 21 ▶ if($param =~ /^(class|pkg|proto|klass)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(class|pkg|proto|klass)$/i) 0 21 21
12250 # class invocant, not a user-facing parameter
12251 0 next;
12252 }
12253 21 ▶ if($param eq 'self') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "self") 0 21 21
12254 # instance invocant, not a user-facing parameter
12255 0 next;
12256 }
12257 21 push @errors, "Parameter '\$$param' found in code but not documented in POD";
12258 21 next;
12259 }
12260  
12261 # Compare types if both exist
12262 44 ▶ if ($pod->{type} && $code->{type} && $pod->{type} ne $code->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod->{"type"} and $code->{"type"} and $pod->{"type"} ne $code->{"type"}) 9 35 44
Condition100%6/6
$pod->{"type"} and $code->{"type"} and $pod->{"type"} ne $code->{"type"}
!ll&&!rl&&r
18179
$pod->{"type"} and $code->{"type"}
!ll&&!rl&&r
21626
MC/DC100%3/3
$pod->{"type"} and $code->{"type"} and $pod->{"type"} ne $code->{"type"}
$pod->{"type"} $code->{"type"} $pod->{"type"} ne $code->{"type"}
Truth table100%4/4
A and B and C
A= $pod->{"type"}B= $code->{"type"}C= $pod->{"type"} ne $code->{"type"}
ABCresult
0XX0
10X0
1100
1111
12263 9 ▶ if (!$self->_types_are_compatible($pod->{type}, $code->{type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_types_are_compatible($pod->{'type'}, $code->{'type'})) { } 5 4 9
12264 5 push @errors, "Type mismatch for '\$$param': POD says '$pod->{type}', code suggests '$code->{type}' (incompatible)";
12265 } else {
12266 4 push @errors, "Type difference for '\$$param': POD says '$pod->{type}', code suggests '$code->{type}' (compatible)";
12267 }
12268 }
12269  
12270 # Compare optional status if both exist
12271 44 ▶ if (exists $pod->{optional} && exists $code->{optional} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pod->{"optional"} and exists $code->{"optional"} and $pod->{"optional"} != $code->{"optional"}) 3 41 44
Condition100%6/6
exists $pod->{"optional"} and exists $code->{"optional"} and $pod->{"optional"} != $code->{"optional"}
!ll&&!rl&&r
3293
exists $pod->{"optional"} and exists $code->{"optional"}
!ll&&!rl&&r
31112
MC/DC100%3/3
exists $pod->{"optional"} and exists $code->{"optional"} and $pod->{"optional"} != $code->{"optional"}
exists $pod->{"optional"} exists $code->{"optional"} $pod->{"optional"} != $code->{"optional"}
Truth table100%4/4
A and B and C
A= exists $pod->{"optional"}B= exists $code->{"optional"}C= $pod->{"optional"} != $code->{"optional"}
ABCresult
0XX0
10X0
1100
1111
12272 $pod->{optional} != $code->{optional}) {
12273 3 ▶ my $pod_status = $pod->{optional} ? 'optional' : 'required';
Branch: 1 branch
BranchTrueFalseTotal
$pod->{'optional'} ? : 1 2 3
12274 3 ▶ my $code_status = $code->{optional} ? 'optional' : 'required';
Branch: 1 branch
BranchTrueFalseTotal
$code->{'optional'} ? : 2 1 3
12275 3 push @errors, "Optional status mismatch for '\$$param': POD says '$pod_status', code suggests '$code_status'";
12276 }
12277  
12278 # Check regex patterns
12279 44 ▶ if ($pod->{matches} && $code->{matches} && $pod->{matches} ne $code->{matches}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod->{"matches"} and $code->{"matches"} and $pod->{"matches"} ne $code->{"matches"}) 0 44 44
Condition50%3/6
$pod->{"matches"} and $code->{"matches"} and $pod->{"matches"} ne $code->{"matches"}
!ll&&!rl&&r
4400
$pod->{"matches"} and $code->{"matches"}
!ll&&!rl&&r
3950
MC/DC0%0/3
$pod->{"matches"} and $code->{"matches"} and $pod->{"matches"} ne $code->{"matches"}
$pod->{"matches"} $code->{"matches"} $pod->{"matches"} ne $code->{"matches"}
Truth table50%2/4
A and B and C
A= $pod->{"matches"}B= $code->{"matches"}C= $pod->{"matches"} ne $code->{"matches"}
ABCresult
0XX0
10X0
1100
1111
12280 0 push @errors, "Pattern mismatch for '\$$param': POD says '$pod->{matches}', code suggests '$code->{matches}'";
12281 }
12282 }
12283  
12284 # Check constraints (min/max), including a formal spec's bounds.
12285 401 ▶ if (defined $pod->{min} && defined $code->{min} && $pod->{min} != $code->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pod->{"min"} and defined $code->{"min"} and $pod->{"min"} != $code->{"min"}) 2 399 401
Condition100%6/6
defined $pod->{"min"} and defined $code->{"min"} and $pod->{"min"} != $code->{"min"}
!ll&&!rl&&r
39092
defined $pod->{"min"} and defined $code->{"min"}
!ll&&!rl&&r
3573311
MC/DC100%3/3
defined $pod->{"min"} and defined $code->{"min"} and $pod->{"min"} != $code->{"min"}
defined $pod->{"min"} defined $code->{"min"} $pod->{"min"} != $code->{"min"}
Truth table100%4/4
A and B and C
A= defined $pod->{"min"}B= defined $code->{"min"}C= $pod->{"min"} != $code->{"min"}
ABCresult
0XX0
10X0
1100
1111
12286 2 push @errors, "Min constraint mismatch for '\$$param': POD says '$pod->{min}', code suggests '$code->{min}'";
12287 }
12288  
12289 401 ▶ if (defined $pod->{max} && defined $code->{max} && $pod->{max} != $code->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pod->{"max"} and defined $code->{"max"} and $pod->{"max"} != $code->{"max"}) 1 400 401
Condition100%6/6
defined $pod->{"max"} and defined $code->{"max"} and $pod->{"max"} != $code->{"max"}
!ll&&!rl&&r
39191
defined $pod->{"max"} and defined $code->{"max"}
!ll&&!rl&&r
3731810
MC/DC100%3/3
defined $pod->{"max"} and defined $code->{"max"} and $pod->{"max"} != $code->{"max"}
defined $pod->{"max"} defined $code->{"max"} $pod->{"max"} != $code->{"max"}
Truth table100%4/4
A and B and C
A= defined $pod->{"max"}B= defined $code->{"max"}C= $pod->{"max"} != $code->{"max"}
ABCresult
0XX0
10X0
1100
1111
12290 1 push @errors, "Max constraint mismatch for '\$$param': POD says '$pod->{max}', code suggests '$code->{max}'";
12291 }
12292  
12293 401 ▶ if (defined $pod->{min} && defined $code->{max} && $pod->{min} > $code->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pod->{"min"} and defined $code->{"max"} and $pod->{"min"} > $code->{"max"}) 2 399 401
Condition100%6/6
defined $pod->{"min"} and defined $code->{"max"} and $pod->{"min"} > $code->{"max"}
!ll&&!rl&&r
39272
defined $pod->{"min"} and defined $code->{"max"}
!ll&&!rl&&r
357359
MC/DC100%3/3
defined $pod->{"min"} and defined $code->{"max"} and $pod->{"min"} > $code->{"max"}
defined $pod->{"min"} defined $code->{"max"} $pod->{"min"} > $code->{"max"}
Truth table100%4/4
A and B and C
A= defined $pod->{"min"}B= defined $code->{"max"}C= $pod->{"min"} > $code->{"max"}
ABCresult
0XX0
10X0
1100
1111
12294 2 push @errors, "Constraint contradiction for '\$$param': POD says min '$pod->{min}', code suggests max '$code->{max}' (no valid value)";
12295 }
12296  
12297 401 ▶ if (defined $code->{min} && defined $pod->{max} && $code->{min} > $pod->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code->{"min"} and defined $pod->{"max"} and $code->{"min"} > $pod->{"max"}) 0 401 401
Condition83%5/6
defined $code->{"min"} and defined $pod->{"max"} and $code->{"min"} > $pod->{"max"}
!ll&&!rl&&r
39380
defined $code->{"min"} and defined $pod->{"max"}
!ll&&!rl&&r
38858
MC/DC0%0/3
defined $code->{"min"} and defined $pod->{"max"} and $code->{"min"} > $pod->{"max"}
defined $code->{"min"} defined $pod->{"max"} $code->{"min"} > $pod->{"max"}
Truth table75%3/4
A and B and C
A= defined $code->{"min"}B= defined $pod->{"max"}C= $code->{"min"} > $pod->{"max"}
ABCresult
0XX0
10X0
1100
1111
12298 0 push @errors, "Constraint contradiction for '\$$param': code suggests min '$code->{min}', POD says max '$pod->{max}' (no valid value)";
12299 }
12300 }
12301  
12302 # Return errors (empty array if no errors)
12303 407 return @errors;
12304 }
12305  
12306 # --------------------------------------------------
12307 # _validate_strictness_level
12308 #
12309 # Purpose: Validate and normalise the strict_pod
12310 # option value accepted by new() into
12311 # an integer level: 0 (off), 1 (warn),
12312 # or 2 (fatal).
12313 #
12314 # Entry: $val - the raw value passed to
12315 # strict_pod in new(). May be
12316 # undef, a number, or a string.
12317 #
12318 # Exit: Returns 0, 1, or 2.
12319 # Croaks if the value is not recognised.
12320 #
12321 # Side effects: None.
12322 # --------------------------------------------------
12323 1169 ▶sub _validate_strictness_level {
Subroutine _validate_strictness_level: called CC=5 SCAR=16.1
12324 1169 my $val = $_[0];
12325  
12326 1169 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 963 206 1169
12327  
12328 # Numeric
12329 206 ▶ return 0 if $val =~ /^(0|off|none)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(0|off|none)$/i 10 196 206
12330 196 ▶ return 1 if $val =~ /^(1|warn|warning)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(1|warn|warning)$/i 146 50 196
12331 50 ▶ return 2 if $val =~ /^(2|fatal|die|error)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(2|fatal|die|error)$/i 48 2 50
12332  
12333 2 croak("Invalid value for --strict-pod: '$val' (use off|warn|fatal)");
12334 }
12335  
12336 # --------------------------------------------------
12337 # _types_are_compatible
12338 #
12339 # Purpose: Determine whether two type strings
12340 # are compatible for POD/code agreement
12341 # checking, allowing semantically
12342 # equivalent types (e.g. 'integer' and
12343 # 'number') to coexist without
12344 # triggering a strict POD warning.
12345 #
12346 # Entry: $pod_type - type string from POD.
12347 # $code_type - type string from code.
12348 #
12349 # Exit: Returns 1 if compatible, 0 otherwise.
12350 #
12351 # Side effects: None.
12352 # --------------------------------------------------
12353 21 ▶sub _types_are_compatible {
Subroutine _types_are_compatible: called CC=4 SCAR=13.9
12354 21 my ($self, $pod_type, $code_type) = @_;
12355  
12356 # Exact match is always compatible
12357 21 ▶ return 1 if $pod_type eq $code_type;
Branch: 1 branch
BranchTrueFalseTotal
if $pod_type eq $code_type 3 18 21
12358  
12359 # Define compatibility matrix
12360 18 my %compatible_types = (
12361 'integer' => ['number', 'scalar'],
12362 'number' => ['scalar'],
12363 'string' => ['scalar'],
12364 'scalar' => ['string', 'integer', 'number'],
12365 'arrayref' => ['array'],
12366 'hashref' => ['hash'],
12367 );
12368  
12369 # Check if code_type is compatible with pod_type
12370 18 ▶ if (my $allowed = $compatible_types{$pod_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed = $compatible_types{$pod_type}) 17 1 18
12371 17 return grep { $_ eq $code_type } @$allowed;
12372 }
12373  
12374 # Check if pod_type is compatible with code_type
12375 1 ▶ if (my $allowed = $compatible_types{$code_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed = $compatible_types{$code_type}) 1 0 1
12376 1 return grep { $_ eq $pod_type } @$allowed;
12377 }
12378  
12379 0 return 0; # Not compatible
12380 }
12381  
12382 =head2 generate_pod_validation_report
12383  
12384 Generate a human-readable report of all POD/code disagreements found
12385 across a set of extracted schemas.
12386  
12387 my $schemas = $extractor->extract_all(no_write => 1);
12388 my $report = $extractor->generate_pod_validation_report($schemas);
12389 print $report;
12390  
12391 =head3 Arguments
12392  
12393 =over 4
12394  
12395 =item * C<$schemas>
12396  
12397 A hashref of method name to schema hashref as returned by
12398 C<extract_all>. Required.
12399  
12400 =back
12401  
12402 =head3 Returns
12403  
12404 A string containing the full validation report, or a single line
12405 confirming all methods passed if no disagreements were found.
12406  
12407 =head3 Side effects
12408  
12409 None.
12410  
12411 =head3 Notes
12412  
12413 Only methods whose schemas contain a C<_pod_validation_errors> key
12414 (populated when C<strict_pod> is 1 or 2) appear in the report. If
12415 C<strict_pod> was 0 when C<extract_all> was called, this method will
12416 always return the all-passed message.
12417  
12418 =head3 API specification
12419  
12420 =head4 input
12421  
12422 {
12423 self => { type => OBJECT, isa => 'App::Test::Generator::SchemaExtractor' },
12424 schemas => { type => HASHREF },
12425 }
12426  
12427 =head4 output
12428  
12429 { type => SCALAR }
12430  
12431 =cut
12432  
12433 17 ▶sub generate_pod_validation_report {
Subroutine generate_pod_validation_report: called CC=5 SCAR=16.1
Pod documented
12434 17 my ($self, $schemas) = @_;
12435  
12436 17 my @reports;
12437 17 foreach my $method_name (sort keys %$schemas) {
12438 26 my $schema = $schemas->{$method_name};
12439  
12440 26 ▶ if (my $errors = $schema->{_pod_validation_errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $schema->{"_pod_validation_errors"}) 16 10 26
12441 16 push @reports, "Method: $method_name";
12442 16 ▶ push @reports, " Severity: " . ($schema->{_pod_disagreement} ? 'warning' : 'fatal');
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'_pod_disagreement'} ? : 13 3 16
12443 16 push @reports, " Errors:";
12444 16 push @reports, map { " - $_" } @$errors;
12445 16 push @reports, '';
12446 }
12447 }
12448  
12449 17 ▶ if (@reports) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reports) { } 11 6 17
12450 11 return join("\n", "POD/Code Validation Report:", '=' x 40, '', @reports);
12451 } else {
12452 6 return 'POD/Code Validation: All methods passed consistency checks.';
12453 }
12454 }
12455  
12456 =head2 _log
12457  
12458 Log a message if verbose mode is on.
12459  
12460 =cut
12461  
12462 14105 ▶sub _log {
Subroutine _log: called CC=2 SCAR=6.9
12463 14105 my($self, $msg) = @_;
12464  
12465 14105 ▶ print "$msg\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 38 14067 14105
12466 }
12467  
12468 =head1 NOTES
12469  
12470 C<SchemaExtractor> uses heuristic analysis of Perl source and POD to infer
12471 parameter types and constraints. Inference accuracy improves with
12472 well-documented modules; C<=head3 Input> / C<=head4 Input> formal specs are
12473 parsed at highest priority and override all heuristics.
12474  
12475 A lone name in a C<=head4 input> block is treated as the first positional
12476 argument when the body reads a positional argument (C<< $_[0] >>, a list
12477 assignment from C<@_>, or a C<shift>) and does not read an argument hash
12478 (C<my %args = @_>, a hashref dereference, or C<< $_[0]{...} >>); the
12479 documented name is then a label for that argument, as C<name> is for
12480 C<$_[0]> in L<Lingua::EN::NameCase/nc>. Two or more names, and a body that
12481 only forwards C<@_>, remain named arguments.
12482  
12483 The output is always a best-effort schema suitable as a starting template;
12484 review and augment the generated YAML before using it as a definitive
12485 specification. Pass it to L<App::Test::Generator> to generate fuzz harnesses.
12486  
12487 =head1 TODO
12488  
12489 Extend C<=head4 Input> parsing to cover the C<enum>/C<memberof> constraint
12490 synonym (union types, e.g. C<scalar | scalarref>, are already handled by
12491 C<_map_formal_input_type>).
12492  
12493 =head1 SEE ALSO
12494  
12495 =over 4
12496  
12497 =item * L<App::Test::Generator> - Generate fuzz and corpus-driven test harnesses
12498  
12499 Output from this module serves as input to that module.
12500 So with well-documented code, you can automatically create your tests.
12501  
12502 =item * L<App::Test::Generator::Template> - Template of the file of tests created by C<App::Test::Generator>
12503  
12504 =back
12505  
12506 =head1 AUTHOR
12507  
12508 Nigel Horne, C<< <njh at nigelhorne.com> >>
12509  
12510 =head1 LICENCE AND COPYRIGHT
12511  
12512 Copyright 2025-2026 Nigel Horne.
12513  
12514 Usage is subject to GPL2 licence terms.
12515 If you use it,
12516 please let me know.
12517  
12518 =cut
12519  
12520 157 1;
« lib/App/Test/Generator.pm ↑ Summary bin/fuzz-harness-generator »