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

filter: Statementstmt 85.1% 3022 / 3548 Branchbran 75.8% 1839 / 2426 Conditioncond 64.4% 789 / 1225 MC/DCmcdc 43.8% 309 / 704 Subroutinesub 92.0% 139 / 151 Podpod 100% 3 / 3 totaltotal 75.7% 6101 / 8057 CC 1768 SCAR 43.6
CC
1768
Cov
78%
_parse_schema_hash
756.0
_analyze_pod
390.2
_analyze_output_from_pod
221.9
CRAP 78.3
« bin/fuzz-harness-generator ↑ Summary lib/App/Test/Generator/TestStrategy.pm »
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.50
88  
89 =cut
90  
91 157 our $VERSION = '0.50';
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: position 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 1145 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
1378 1145 my $class = shift;
1379  
1380 # Handle hash or hashref arguments
1381 1145 ▶ my $params = Params::Get::get_params('input_file', @_) || {};
Condition50%1/2
Params::Get::get_params('input_file', @_) || {}
l!l
11430
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 1143 ▶ croak(__PACKAGE__, ': input_file required') unless exists $params->{input_file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{"input_file"} 0 1143 1143
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 1143 ▶ 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
341802
$params->{"include_private"} // 0
l!l
1321011
$params->{"confidence_threshold"} // $DEFAULT_CONFIDENCE_THRESH
l!l&&r!l&&!r
131860152
$params->{"max_parameters"} // $DEFAULT_MAX_PARAMETERS
l!l&&r!l&&!r
128863152
$params->{"allow_signature_exec"} // 0
l!l
1291013
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 1142 ▶ unless (-f $self->{input_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"input_file"}) 3 1139 1142
1400 3 croak(__PACKAGE__, ": Input file '$self->{input_file}' does not exist");
1401 }
1402  
1403 1139 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 301 ▶sub extract_all {
Subroutine extract_all: called CC=76 SCAR=44.2
Pod documented
1482 301 my $self = shift;
1483 301 ▶ my $params = Params::Get::get_params(undef, @_) || {};
Condition100%2/2
Params::Get::get_params(undef, @_) || {}
l!l
95206
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 301 $self->_log("Parsing $self->{input_file}...");
1486 301 $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 301 ▶ 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"}]) 301 0 301
1491  
1492 # Store document for later use
1493 301 $self->{_document} = $document;
1494  
1495 301 my $package_name = $self->_extract_package_name($document);
1496 301 ▶ $self->{_package_name} //= $package_name;
Condition66%2/3
$self->{"_package_name"} //= $package_name
l!l&&r!l&&!r
29380
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 301 $self->_log("Package: $package_name");
1498  
1499 301 my $methods = $self->_find_methods($document);
1500 301 $self->_log('Found ' . scalar(@$methods) . ' methods (pre-dedup)');
1501  
1502 301 my %schemas;
1503 my %delegates;
1504 301 my $ctor_body;
1505 301 foreach my $method (@{$methods}) {
1506 773 $self->_log("\nAnalyzing method: $method->{name}");
1507  
1508 773 my $schema = $self->_analyze_method($method);
1509 769 $schemas{$method->{name}} = $schema;
1510 769 $schema->{'module'} = $package_name;
1511 769 ▶ $ctor_body = $method->{body} if($method->{name} eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $method->{"name"} eq "new" 95 674 769
1512  
1513 769 my $target = $self->_delegated_method($method->{body});
1514 769 ▶ $delegates{$method->{name}} = $target if defined($target);
Branch: 1 branch
BranchTrueFalseTotal
if defined $target 2 767 769
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 297 ▶ if (exists $schemas{new}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schemas{"new"}) 95 202 297
1524 95 my $ctor_args = $self->_ctor_representative_args($schemas{new});
1525 95 ▶ if (defined $ctor_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctor_args) 77 18 95
1526 77 for my $method_name (keys %schemas) {
1527 247 ▶ next if $method_name eq 'new';
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "new" 77 170 247
1528 170 my $schema = $schemas{$method_name};
1529 170 ▶ next unless exists $schema->{new} && defined $schema->{new};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"new"} and defined $schema->{"new"} 12 158 170
Condition66%2/3
exists $schema->{"new"} and defined $schema->{"new"}
!ll&&!rl&&r
120158
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
1530 158 $schema->{new} = $ctor_args;
1531 158 $self->_log(" ENRICH: $method_name new: enriched with ctor args");
1532 }
1533 }
1534 }
1535  
1536 # A constructor that insists on a non-empty arrayref of live objects
1537 # cannot be built from YAML: no generated array is made of blessed
1538 # instances whose methods it can call. Database::Join->new is the case
1539 # that exposed this. Recording the precondition on new() itself lets
1540 # callers skip that constructor and, through the block below, gives the
1541 # instance methods new: ~ as well.
1542 297 ▶ 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"})) 1 296 297
Condition83%5/6
$ctor_body and $schemas{"new"} and my $reason = _constructor_requires_live_objects($ctor_body, $schemas{"new"})
!ll&&!rl&&r
202941
$ctor_body and $schemas{"new"}
!ll&&!rl&&r
202095
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
1543 && (my $reason = _constructor_requires_live_objects($ctor_body, $schemas{new}))) {
1544 1 $schemas{new}{skip} = $reason;
1545 1 $self->_log(" UNBUILDABLE: new: $reason");
1546 }
1547  
1548 # A constructor can refuse to build the package itself: an abstract base
1549 # class croaks on direct instantiation, and a constructor that croaks over
1550 # an argument the extractor cannot supply cannot be built either. Either
1551 # way no harness can get a $self, so the instance methods are not fuzzable
1552 # this way: leaving new: as the package name makes _write_schema emit
1553 # new: ~, which callers report as skipped rather than failed.
1554 297 ▶ 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 295 297
Condition83%5/6
$ctor_body and $schemas{"new"} and _constructor_refuses_direct_instantiation($ctor_body, $schemas{"new"})
!ll&&!rl&&r
202932
$ctor_body and $schemas{"new"}
!ll&&!rl&&r
202095
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
1555 # The class itself cannot be constructed, so its new() is expected to
1556 # die rather than return an object.
1557 2 $schemas{new}{output} = { _STATUS => 'DIES' };
1558 2 for my $method_name (keys %schemas) {
1559 4 ▶ next if($method_name eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "new" 2 2 4
1560 2 my $schema = $schemas{$method_name};
1561 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
1562 2 $schema->{new} = $package_name;
1563 2 $self->_log(" UNBUILDABLE: $method_name new: ~ ($package_name refuses to be built)");
1564 }
1565 }
1566  
1567 # Third pass: contracts the type inference cannot see in the signature.
1568 # A routine can document behaviour in its body that changes what the
1569 # harness may assert: an abstract method always dies, a routine returning
1570 # a list must be called in list context, one that returns nothing has no
1571 # output to validate, and a parameter checked against a pattern rejects
1572 # values outside it. A routine that reads a file it is given cannot be
1573 # fuzzed with a synthesized value at all.
1574 297 foreach my $method (@{$methods}) {
1575 769 my $schema = $schemas{$method->{name}};
1576 769 ▶ next unless($schema && ref($schema->{output}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless $schema and ref $schema->{"output"} eq "HASH" 0 769 769
Condition33%1/3
$schema and ref $schema->{"output"} eq "HASH"
!ll&&!rl&&r
00769
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
1577 769 ▶ my $body = $method->{body} // '';
Condition50%1/2
$method->{"body"} // ""
l!l
7690
MC/DC0%0/1
$method->{"body"} // ""
$method->{"body"}
Truth table50%1/2
A
A= $method->{"body"}
Aresult
11
00
1578  
1579 769 ▶ 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 762 769
elsif ($body =~ /\breturn\s+\@\{/ and ($method->{'pod'} // '') =~ /\b(?:a\s+)?list\s+of\b|\breturns?\s+a\s+list\b/i) { } 7 755 762
elsif ($body =~ /\breturn\s*;/ and not $body =~ /\breturn\s+[^\s;]/) { } 9 746 755
elsif ($body =~ /\breturn\s+\\?[A-Z]\w*(?:::\w+)+(?:->)?\w*\s*\(/) { } 3 743 746
1580 7 $schema->{output}{_STATUS} = 'DIES';
1581 ▶ } 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
744117
$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
1582 # Called in scalar context a list return yields its count, so the
1583 # harness has to capture the list. Only when the POD says the
1584 # routine returns a list, not an arrayref.
1585 7 $schema->{output}{type} = 'array';
1586 ▶ } elsif(($body =~ /\breturn\s*;/) && ($body !~ /\breturn\s+[^\s;]/)) {
Condition100%3/3
$body =~ /\breturn\s*;/ and not $body =~ /\breturn\s+[^\s;]/
!ll&&!rl&&r
74249
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
1587 # The routine returns nothing: say so, rather than omitting the
1588 # type. 'void' is the documented way to describe that (see
1589 # _detect_void_context), and the harness validates no return
1590 # value for it.
1591 9 $schema->{output}{type} = 'void';
1592 } elsif($body =~ /\breturn\s+\\?[A-Z]\w*(?:::\w+)+(?:->)?\w*\s*\(/) {
1593 # Delegated to another package: its result type is not visible here.
1594 3 delete $schema->{output}{type};
1595 }
1596  
1597 769 ▶ if(my $skip = _requires_input_file($body, $schema->{input})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $skip = _requires_input_file($body, $schema->{"input"})) 18 751 769
1598 18 $schema->{skip} = $skip;
1599 }
1600  
1601 # A method that reads required keys out of a constructor hashref cannot
1602 # be given a working object: the harness has no schema to put there.
1603 769 ▶ if(ref($schema->{new}) eq 'HASH' && %{$schema->{new}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"new"} eq "HASH" and %{$schema->{"new"};}) 55 714 769
Condition100%3/3
ref $schema->{"new"} eq "HASH" and %{$schema->{"new"};}
!ll&&!rl&&r
61310155
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
1604 55 foreach my $param (keys %{$schema->{new}}) {
1605 97 my $p = quotemeta($param);
1606 # Read directly, or through a local copy of the hashref.
1607 97 ▶ 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
0889
$body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?${p}['"]?\s*\}/ and $body =~ /\$$1->\{/
!ll&&!rl&&r
8908
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
1608 || ($body =~ /my\s+\$(\w+)\s*=\s*\$self->\{\s*['"]?$p['"]?\s*\}/ && $body =~ /\$$1->\{/);
1609 97 ▶ next unless($reads_keys && ($body =~ /\bcroak\b/));
Branch: 1 branch
BranchTrueFalseTotal
unless $reads_keys and $body =~ /\bcroak\b/ 92 5 97
Condition100%3/3
$reads_keys and $body =~ /\bcroak\b/
!ll&&!rl&&r
8935
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
1610 5 ▶ $schema->{new} = $self->{_package_name} if($self->{_package_name});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_package_name"} 5 0 5
1611 5 last;
1612 }
1613 }
1614  
1615 769 ▶ _next_parameter: foreach my $param_name (keys %{$schema->{input} // {}}) {
Condition100%2/2
$schema->{"input"} // {}
l!l
73435
MC/DC100%1/1
$schema->{"input"} // {}
$schema->{"input"}
Truth table100%2/2
A
A= $schema->{"input"}
Aresult
11
00
1616 681 my $spec = $schema->{input}{$param_name};
1617 681 ▶ next unless(ref($spec) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 0 681 681
1618 681 my $p = quotemeta($param_name);
1619  
1620 681 ▶ 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 677 681
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
31677
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
1621 || $body =~ /\$args\{\s*['"]?$p['"]?\s*\}\s*->\s*\(/) {
1622 4 $spec->{type} = 'coderef';
1623 4 next _next_parameter;
1624 }
1625  
1626 # The value is checked against a file-level constant set: the module
1627 # member list is the documented domain, e.g. add_evidence's category.
1628 677 ▶ 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 664 677
1629 13 my $var = $1;
1630 13 ▶ if($body =~ /[\$%](\w+)\s*\{\s*\$$var\s*\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /[\$%](\w+)\s*\{\s*\$$var\s*\}/) 7 6 13
1631 7 my $const = $1;
1632 7 ▶ my $source = $self->{_document} ? $self->{_document}->serialize : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_document'} ? : 7 0 7
1633 7 ▶ if($source =~ /%$const\s*(?:=|=>)\s*.*?qw\s*[\(\{\[]([^\)\}\]]+)[\)\}\]]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /%$const\s*(?:=|=>)\s*.*?qw\s*[\(\{\[]([^\)\}\]]+)[\)\}\]]/s) 7 0 7
1634 7 my @values = split(' ', $1);
1635 7 ▶ $spec->{memberof} = \@values if(@values);
Branch: 1 branch
BranchTrueFalseTotal
if @values 7 0 7
1636 }
1637 }
1638 }
1639  
1640 # A routine that checks the value against a pattern and croaks is a
1641 # validator for that parameter: values outside the pattern are
1642 # documented to die, so the pattern belongs in the schema.
1643 677 my $pattern;
1644 677 ▶ if($body =~ /\$args\{\s*['"]?$p['"]?\s*\}\s*=~\s*\/([^\/\n]+)\//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body =~ m[\$args\{\s*['"]?${p}['"]?\s*\}\s*=~\s*/([^/\n]+)/]) { } 4 673 677
elsif ($body =~ m[\$$p\b\s*=~\s*/([^/\n]+)/]) { } 23 650 673
1645 4 $pattern = $1;
1646 } elsif($body =~ /\$$p\b\s*=~\s*\/([^\/\n]+)\//) {
1647 23 $pattern = $1;
1648 }
1649 677 ▶ if(defined($pattern) && ($body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pattern and $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i) 9 668 677
Condition100%3/3
defined $pattern and $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
!ll&&!rl&&r
650189
MC/DC100%2/2
defined $pattern and $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
defined $pattern $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
Truth table100%3/3
A and B
A= defined $patternB= $body =~ /\bcroak\b[^;\n]*(?:not a valid|invalid)/i
ABresult
0X0
100
111
1650 9 $spec->{matches} = $pattern;
1651 9 $spec->{_source} = SOURCE_VALIDATOR;
1652 }
1653 }
1654 }
1655  
1656 # A constructor that parses a file it is given cannot be built by the
1657 # harness either, so its instance methods get new: ~ like any other
1658 # unbuildable class.
1659 297 ▶ if($schemas{new} && $schemas{new}{skip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schemas{"new"} and $schemas{"new"}{"skip"}) 5 292 297
Condition100%3/3
$schemas{"new"} and $schemas{"new"}{"skip"}
!ll&&!rl&&r
202905
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
1660 5 for my $method_name (keys %schemas) {
1661 16 ▶ next if($method_name eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq "new" 5 11 16
1662 11 my $schema = $schemas{$method_name};
1663 11 ▶ next unless(exists($schema->{new}) && defined($schema->{new}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"new"} and defined $schema->{"new"} 0 11 11
Condition33%1/3
exists $schema->{"new"} and defined $schema->{"new"}
!ll&&!rl&&r
0011
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
1664 11 $schema->{new} = $package_name;
1665 11 $self->_log(" UNBUILDABLE: $method_name new: ~ ($package_name constructor is not auto-buildable)");
1666 }
1667 }
1668  
1669 # A routine whose whole body is `return $self->other(\@_)` has
1670 # exactly other()'s input contract. CGI::Info::get_cookie is such a
1671 # forwarder, and without this it looked like it took no parameters at all,
1672 # so the harness called it with none and died on the required cookie_name.
1673 297 for my $method_name (sort keys %delegates) {
1674 2 my $target = $delegates{$method_name};
1675 2 ▶ next if $method_name eq $target;
Branch: 1 branch
BranchTrueFalseTotal
if $method_name eq $target 0 2 2
1676 2 my $target_schema = $schemas{$target};
1677 next unless $target_schema && ref($target_schema->{input}) eq 'HASH'
1678 2 ▶ && scalar(keys %{$target_schema->{input}});
Branch: 1 branch
BranchTrueFalseTotal
unless $target_schema and ref $target_schema->{"input"} eq "HASH" and scalar keys %{$$target_schema{"input"};} 1 1 2
Condition80%4/5
$target_schema and ref $target_schema->{"input"} eq "HASH" and scalar keys %{$$target_schema{"input"};}
!ll
11
$target_schema and ref $target_schema->{"input"} eq "HASH"
!ll&&!rl&&r
101
MC/DC50%1/2
$target_schema and ref $target_schema->{"input"} eq "HASH" and scalar keys %{$$target_schema{"input"};}
$target_schema ref $target_schema->{"input"} eq "HASH"
Truth table66%2/3
A and B
A= $target_schemaB= ref $target_schema->{"input"} eq "HASH"
ABresult
0X0
100
111
1679  
1680 1 my $schema = $schemas{$method_name};
1681 1 ▶ next unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 1 1
1682 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
1683  
1684 $schema->{input} = {
1685 1 map { $_ => { %{$target_schema->{input}{$_}} } } sort keys %{$target_schema->{input}}
1686 };
1687 $schema->{_confidence}{input} = {
1688 1 'level' => 'medium',
1689 'factors' => [ "Forwarded to $target()" ]
1690 };
1691 1 push @{$schema->{_notes}}, "input inherited from $target(), which this routine forwards \@_ to";
1692 1 $self->_log(" DELEGATE: $method_name input inherited from $target");
1693 }
1694  
1695 # A multi-package file is analysed as its primary package, but it is not
1696 # fuzzed: the helper package's routines are deliberately excluded, and
1697 # the primary package's routines may not be fuzzable without it. Mark
1698 # them skipped rather than letting the harness fail on every routine.
1699 297 ▶ if($self->{_multiple_packages}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_multiple_packages"}) 1 296 297
1700 1 for my $method_name (keys %schemas) {
1701 $schemas{$method_name}{skip} //=
1702 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
1703 }
1704 }
1705  
1706 297 ▶ unless ($params->{no_write}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"no_write"}) 206 91 297
1707 206 for my $method_name (keys %schemas) {
1708 528 $self->_write_schema($method_name, $schemas{$method_name});
1709 }
1710 }
1711  
1712 297 return \%schemas;
1713 }
1714  
1715 # --------------------------------------------------
1716 # _delegated_method
1717 #
1718 # Purpose: Detect a routine whose whole body
1719 # forwards its arguments to another
1720 # method of the same object.
1721 #
1722 # Entry: $code - the subroutine's source text.
1723 #
1724 # Exit: Returns the name of the method it
1725 # forwards to, or undef when the routine
1726 # does anything else.
1727 #
1728 # Side effects: None.
1729 #
1730 # Notes: Only the bare `return $self->other(\@_)`
1731 # shape counts. Such a forwarder has
1732 # exactly the target's input contract,
1733 # which extract_all() copies so that the
1734 # harness passes the right arguments
1735 # (CGI::Info::get_cookie -> cookie).
1736 # --------------------------------------------------
1737 769 ▶sub _delegated_method {
Subroutine _delegated_method: called CC=3 SCAR=11.0
1738 769 my ($self, $code) = @_;
1739  
1740 769 ▶ return unless defined($code);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 769 769
1741  
1742 769 (my $body = $code) =~ s/\A\s*sub\s+\w+\s*//;
1743 769 $body =~ s/\s+/ /g;
1744  
1745 769 ▶ return $body =~ /\A\{ (?:my \$self = shift; )?return \$self->(\w+)\(\s*\\\@_\s*\); \}\z/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$body =~ /\A\{ (?:my \$self = shift; )?return \$self->(\w+)\(\s*\\\@_\s*\); \}\z/ ? : 2 767 769
1746 }
1747  
1748 # --------------------------------------------------
1749 # _extract_package_name
1750 #
1751 # Purpose: Extract the Perl package name from a
1752 # PPI document, or from the cached value
1753 # stored at construction time.
1754 #
1755 # Entry: $document - a PPI::Document, or undef
1756 # to use $self->{_document}.
1757 #
1758 # Exit: Returns the package namespace string,
1759 # or an empty string if no package
1760 # statement is found.
1761 #
1762 # Side effects: Stores the package name in
1763 # $self->{_package_name} if not already
1764 # set.
1765 #
1766 # Notes: A file may declare more than one package (a
1767 # main class plus a small error class). The
1768 # first package is the primary one; routines
1769 # owned by later packages are ignored rather
1770 # than aborting the extraction.
1771 # --------------------------------------------------
1772 361 ▶sub _extract_package_name {
Subroutine _extract_package_name: called CC=7 SCAR=19.5
1773 361 my ($self, $document) = @_;
1774  
1775 361 ▶ if(!defined($document)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $document) 60 301 361
1776 60 $document = $self->{_document};
1777 }
1778 361 ▶ my $pkgs = $document->find('PPI::Statement::Package') || [];
Condition100%2/2
$document->find('PPI::Statement::Package') || []
l!l
3538
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
1779 361 ▶ if(@$pkgs == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pkgs == 0) 8 353 361
1780 8 my $package_stmt = $document->find_first('PPI::Statement::Package');
1781 8 ▶ return $package_stmt ? $package_stmt->namespace() : '';
Branch: 1 branch
BranchTrueFalseTotal
$package_stmt ? : 0 8 8
1782 }
1783 353 ▶ $self->{_package_name} //= $pkgs->[0]->namespace();
Condition66%2/3
$self->{"_package_name"} //= $pkgs->[0]->namespace
l!l&&r!l&&!r
612920
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
1784 353 ▶ if(@$pkgs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pkgs > 1) 1 352 353
1785 1 $self->{_multiple_packages} = 1;
1786 1 $self->_log(" Multiple packages; analyzing " . $pkgs->[0]->namespace() . " only");
1787 }
1788 353 return $pkgs->[0]->namespace();
1789 }
1790  
1791 # True when a node belongs to the primary (first) package of the document.
1792 # PPI nodes carry a location, so each routine belongs to the last package
1793 # declaration at or before its line. A single-package file trivially passes.
1794 1457 ▶sub _in_primary_package {
Subroutine _in_primary_package: called CC=8 SCAR=21.2
1795 1457 my ($self, $node, $pkgs) = @_;
1796  
1797 1457 ▶ return 1 unless($pkgs && @$pkgs > 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $pkgs and @$pkgs > 1 1454 3 1457
Condition66%2/3
$pkgs and @$pkgs > 1
!ll&&!rl&&r
014543
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
1798  
1799 3 my $loc = eval { $node->location };
1800 3 ▶ return 1 unless(ref($loc) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $loc eq "ARRAY" 0 3 3
1801  
1802 3 my $owner = $pkgs->[0];
1803 3 for my $pkg (@$pkgs) {
1804 6 my $pkg_loc = eval { $pkg->location };
1805 6 ▶ next unless(ref($pkg_loc) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pkg_loc eq "ARRAY" 0 6 6
1806 6 ▶ last if($pkg_loc->[0] > $loc->[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg_loc->[0] > $loc->[0] 1 5 6
1807 5 $owner = $pkg;
1808 }
1809  
1810 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
1811 3 return $owner->namespace() eq $primary;
1812 }
1813  
1814 # --------------------------------------------------
1815 # _find_methods
1816 #
1817 # Purpose: Locate all subroutine and method
1818 # declarations in a PPI document,
1819 # including Moose-style method modifiers
1820 # and Perl 5.38 class/method syntax.
1821 #
1822 # Entry: $document - a PPI::Document.
1823 #
1824 # Exit: Returns an arrayref of method hashrefs,
1825 # each containing: name, node, body, pod,
1826 # type, and optionally modifier, class,
1827 # and fields keys.
1828 # Private methods (names beginning with
1829 # _) are excluded unless include_private
1830 # was set in new(), except for _new,
1831 # _init, and _build which are always
1832 # included.
1833 #
1834 # Side effects: Logs progress and warnings to stdout
1835 # when verbose is set.
1836 #
1837 # Notes: Duplicate method names are silently
1838 # deduplicated — the second occurrence
1839 # is dropped with a verbose warning.
1840 # Class/method detection is regex-based
1841 # and may misbehave on complex code.
1842 # --------------------------------------------------
1843 304 ▶sub _find_methods {
Subroutine _find_methods: called CC=22 SCAR=34.0
1844 304 my ($self, $document) = @_;
1845  
1846 304 ▶ my $subs = $document->find('PPI::Statement::Sub') || [];
Condition100%2/2
$document->find('PPI::Statement::Sub') || []
l!l
3013
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
1847 304 ▶ my $pkgs = $document->find('PPI::Statement::Package') || [];
Condition100%2/2
$document->find('PPI::Statement::Package') || []
l!l
2968
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
1848 # Only fetch statements that begin with a Moose modifier keyword —
1849 # fetching ALL PPI::Statement nodes on a large file returns thousands
1850 # of nodes and then discards nearly all of them in the loop below.
1851 427615 ▶ my $sub_decls = $document->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1852 427615 ▶ $_[1]->isa('PPI::Statement')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement") and $_[1]->content =~ /^\s*(?:before|after|around)\b/ 43418 384197 427615
1853 && $_[1]->content =~ /^\s*(?:before|after|around)\b/
1854 304 ▶ }) || [];
Condition100%2/2
$document->find(sub { $_[1]->isa('PPI::Statement') and $_[1]->content =~ /^\s*(?:before|after|around)\b/; } ) || []
l!l
3301
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
1855  
1856 304 my @methods;
1857 304 foreach my $sub (@$subs) {
1858 1454 my $name = $sub->name();
1859  
1860 1454 ▶ next unless defined $name; # Skip anonymous routines
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1454 1454
1861 # A file may define helper packages after the main one; their subs
1862 # are not part of the module under test.
1863 1454 ▶ next unless $self->_in_primary_package($sub, $pkgs);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_primary_package($sub, $pkgs) 2 1452 1454
1864 1452 ▶ next if $name =~ /^(BEGIN|END|DESTROY|AUTOLOAD|CHECK|INIT|UNITCHECK)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(BEGIN|END|DESTROY|AUTOLOAD|CHECK|INIT|UNITCHECK)$/ 8 1444 1452
1865 1444 ▶ next if $name =~ /::/; # cross-package sub (e.g. sub DB::DB { })
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::/ 3 1441 1444
1866  
1867 # Skip private methods unless explicitly included, or they're special
1868 1441 ▶ if ($name =~ /^_/ && $name !~ /^_(new|init|build)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/ and not $name =~ /^_(new|init|build)/) 660 781 1441
Condition100%3/3
$name =~ /^_/ and not $name =~ /^_(new|init|build)/
!ll&&!rl&&r
76318660
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
1869 660 ▶ next unless $self->{include_private};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"include_private"} 658 2 660
1870 }
1871  
1872 # Get the POD before this sub
1873 783 my $pod = $self->_extract_pod_before($sub);
1874  
1875 783 push @methods, {
1876 name => $name,
1877 node => $sub,
1878 body => $sub->content(),
1879 pod => $pod,
1880 type => 'sub',
1881 };
1882 }
1883  
1884 # Look for class { method } syntax (Perl 5.38+). The regex scans the
1885 # whole document, so it is only safe when there is a single package.
1886 304 my $content = $document->content();
1887 304 ▶ if (@$pkgs <= 1 && $content =~ /\bclass\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pkgs <= 1 and $content =~ /\bclass\b/) 104 200 304
Condition100%3/3
@$pkgs <= 1 and $content =~ /\bclass\b/
!ll&&!rl&&r
1199104
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
1888 104 $self->_log(' Detecting class/method syntax...');
1889 # Strip POD blocks and line comments before the regex scan so that
1890 # patterns like class Name { inside documentation examples or
1891 # the comment # find "class Name {" blocks don't produce
1892 # spurious method names (e.g. the keyword 'if').
1893 104 (my $code_only = $content) =~ s/^=\w[^\n]*.*?^=cut[^\n]*\n//gms;
1894 104 $code_only =~ s/\s*#[^\n]*//g;
1895 104 $self->_extract_class_methods($code_only, \@methods);
1896 }
1897  
1898 # Process method modifiers (Moose)
1899 304 foreach my $decl (@$sub_decls) {
1900 3 ▶ next unless $self->_in_primary_package($decl, $pkgs);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_primary_package($decl, $pkgs) 0 3 3
1901 3 my $content = $decl->content;
1902 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
1903 0 my ($modifier, $method_name) = ($1, $2);
1904 0 my $full_name = "${modifier}_$method_name";
1905  
1906 # Look for the actual sub definition that follows
1907 0 my $next_sib = $decl->next_sibling;
1908 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
1909 0 $next_sib = $next_sib->next_sibling;
1910 }
1911  
1912 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
1913 0 my $pod = $self->_extract_pod_before($decl); # POD might be before modifier
1914 0 push @methods, {
1915 name => $full_name,
1916 node => $next_sib,
1917 body => $next_sib->content,
1918 pod => $pod,
1919 type => 'modifier',
1920 original_method => $method_name,
1921 modifier => $modifier,
1922 };
1923 0 $self->_log(" Found method modifier: $full_name");
1924 }
1925 }
1926 }
1927  
1928 # Prevent silent duplicate method overwrites
1929 304 my %seen;
1930 @methods = grep {
1931 304 my $n = $_->{name};
1932 783 ▶ if ($seen{$n}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$n}++) { } 1 782 783
1933 1 $self->_log(" WARNING: duplicate method '$n' ignored");
1934 1 0;
1935 } else {
1936 782 1;
1937 }
1938 } @methods;
1939  
1940 304 return \@methods;
1941 }
1942  
1943 # --------------------------------------------------
1944 # _extract_class_methods
1945 #
1946 # Purpose: Extract method declarations from
1947 # Perl 5.38 class { method {} } syntax
1948 # by regex-based scanning of the class
1949 # body content.
1950 #
1951 # Entry: $content - full document source string.
1952 # $methods - arrayref to push discovered
1953 # method hashrefs onto
1954 # (modified in place).
1955 #
1956 # Exit: Returns nothing. Appends to $methods.
1957 #
1958 # Side effects: Logs class and method discoveries
1959 # to stdout when verbose is set.
1960 #
1961 # Notes: This is experimental — regex-based
1962 # class body parsing may misbehave on
1963 # complex or nested class declarations.
1964 # Class body boundaries are tracked by
1965 # simple brace counting, which will
1966 # fail on unbalanced braces in strings
1967 # or heredocs.
1968 # --------------------------------------------------
1969 104 ▶sub _extract_class_methods {
Subroutine _extract_class_methods: called CC=8 SCAR=40.4
1970 104 my ($self, $content, $methods) = @_;
1971  
1972 # EXPERIMENTAL: regex-based parsing, may misbehave on complex code
1973  
1974 # Simple pattern: find "class Name {" blocks
1975 # This won't handle all edge cases but will work for simple classes
1976 104 ▶ while ($content =~ /class\s+(\w+)\s*\{/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /class\s+(\w+)\s*\{/g) 0 104 104
1977 0 my $class_name = $1;
1978 0 my $start_pos = pos($content);
1979  
1980 # Find the matching closing brace. $start_pos is just after the
1981 # opening '{' consumed by the regex above, so back up one
1982 # character to hand the brace itself to extract_bracketed.
1983 0 require Text::Balanced;
1984 0 my $extracted = Text::Balanced::extract_bracketed(substr($content, $start_pos - 1), '{}');
1985  
1986 0 ▶ next unless defined $extracted; # unbalanced braces, skip class
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 0 0
1987  
1988 0 my $class_body = substr($extracted, 1, length($extracted) - 2);
1989  
1990 0 $self->_log(" Found class $class_name");
1991  
1992 # Extract field declarations from class
1993 0 my $fields = $self->_extract_field_declarations($class_body);
1994  
1995 # Find methods in the class body
1996 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
1997 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
1998  
1999 # Skip private unless configured
2000 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
2001 0 ▶ next unless $self->{include_private};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"include_private"} 0 0 0
2002 }
2003  
2004 # Reconstruct as sub for analysis
2005 0 my $signature = $sig_with_parens;
2006 0 $signature =~ s/^\(//;
2007 0 $signature =~ s/\)$//;
2008  
2009 # Build a fake sub declaration
2010 0 my $fake_sub = "sub $method_name($signature) { }";
2011  
2012 0 push @$methods, {
2013 name => $method_name,
2014 node => undef,
2015 body => $fake_sub, # Just the signature for now
2016 is_stub => 1,
2017 pod => '',
2018 type => 'method',
2019 class => $class_name,
2020 fields => $fields,
2021 };
2022  
2023 0 $self->_log(" Found method $method_name in class $class_name");
2024 }
2025 }
2026 }
2027  
2028 # --------------------------------------------------
2029 # _extract_pod_before
2030 #
2031 # Purpose: Collect the POD documentation that
2032 # appears immediately before a
2033 # subroutine in the PPI document, by
2034 # walking backwards through siblings.
2035 #
2036 # Entry: $sub - a PPI node (typically a
2037 # PPI::Statement::Sub).
2038 #
2039 # Exit: Returns a string containing all POD
2040 # content found before the sub, with
2041 # inline parameter comments converted
2042 # to =item format. Returns an empty
2043 # string if no POD is found.
2044 #
2045 # Side effects: None.
2046 #
2047 # Notes: Stops walking backwards on the first
2048 # non-POD, non-whitespace, non-separator,
2049 # non-include node encountered.
2050 # Walking is capped at $POD_WALK_LIMIT
2051 # steps to prevent runaway processing
2052 # on pathological documents.
2053 # --------------------------------------------------
2054 783 ▶sub _extract_pod_before {
Subroutine _extract_pod_before: called CC=30 SCAR=35.2
2055 783 my ($self, $sub) = @_;
2056  
2057 783 ▶ my $name = eval { $sub->can('name') ? $sub->name : undef };
Branch: 1 branch
BranchTrueFalseTotal
$sub->can('name') ? : 783 0 783
2058 783 my $pod = '';
2059 783 my $current = $sub->previous_sibling();
2060 783 my $skipped_declaration = 0;
2061 783 my $steps = 0;
2062  
2063 # Walk backwards collecting POD.
2064 # Stop after the first pod token so that a =cut before =head1 METHODS
2065 # prevents class-level POD from being mistaken for method-specific POD.
2066 783 ▶ while($current && $steps++ < $POD_WALK_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current and $steps++ < $POD_WALK_LIMIT) 2254 5 2259
Condition66%2/3
$current and $steps++ < $POD_WALK_LIMIT
!ll&&!rl&&r
502254
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
2067 2254 ▶ if ($current->isa('PPI::Token::Pod')) {
Branch: 5 branches
BranchTrueFalseTotal
if ($current->isa('PPI::Token::Pod')) { } 524 1730 2254
elsif ($current->isa('PPI::Token::Comment')) { } 437 1293 1730
elsif ($current->isa('PPI::Token::Whitespace') or $current->isa('PPI::Token::Separator')) { } 979 314 1293
elsif ($current->isa('PPI::Statement::Include')) { } 57 257 314
elsif ($current->isa('PPI::Statement::Variable') and $current->content =~ /\A\s*(?:my|our)\b/ and not $current->content =~ /\bsub\b/) { } 3 254 257
2068 524 my $content = $current->content();
2069 # A file-scoped declaration may sit between a routine's POD and
2070 # its sub (Mutation::Feedback declares a regex constant there).
2071 # Accept such a POD block only when it documents this routine.
2072 524 ▶ 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) 524 0 524
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
52130
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
2073 || (defined($name) && $content =~ /^=head[1-6]\s+\Q$name\E\b/m)) {
2074 524 $pod = $content . $pod;
2075 }
2076 524 last; # Only take the immediately adjacent pod block
2077 } elsif ($current->isa('PPI::Token::Comment')) {
2078 # Include comments that might contain parameter info
2079 437 my $comment = $current->content();
2080 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
2081 0 $pod .= "=item \$$1\n$2\n\n";
2082 }
2083 ▶ } 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
9790314
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
2084 $current->isa('PPI::Token::Separator')) {
2085 # Skip whitespace and separators
2086 } elsif ($current->isa('PPI::Statement::Include')) {
2087 # allow 'use strict', 'use warnings' between POD and sub
2088 ▶ } 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
25403
$current->isa("PPI::Statement::Variable") and $current->content =~ /\A\s*(?:my|our)\b/
!ll&&!rl&&r
25403
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
2089 && $current->content() =~ /\A\s*(?:my|our)\b/
2090 && $current->content() !~ /\bsub\b/) {
2091 # A simple file-scoped declaration: the POD is still this
2092 # routine's documentation.
2093 3 $skipped_declaration = 1;
2094 } else {
2095 # Hit non-POD, non-whitespace - stop
2096 254 last;
2097 }
2098 1476 $current = $current->previous_sibling();
2099 }
2100  
2101 # A POD token can span several routines (and the module-level sections
2102 # before the first one). Keep the section that documents this routine,
2103 # stopping at the next heading that belongs to another method. Headings
2104 # such as SYNOPSIS or NOTES belong to the routine and must be kept.
2105 783 ▶ 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) 524 259 783
Condition66%4/6
$pod and defined $name and $pod =~ /^=head([1-6])\s+\Q$name\E\b/m
!ll&&!rl&&r
2590524
$pod and defined $name
!ll&&!rl&&r
2590524
MC/DC33%1/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 table50%2/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
2106 524 my ($level, $start) = ($1, $-[0]);
2107 524 my $section = substr($pod, $start);
2108  
2109 524 my $method_names = {};
2110 524 ▶ if(my $doc = eval { $sub->document }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = eval { do { $sub->document } }) 524 0 524
2111 524 ▶ $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
2912330
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
2112 233 my %names;
2113 233 my $subs = $doc->find('PPI::Statement::Sub');
2114 233 ▶ for my $method (@{ $subs || [] }) {
Condition50%1/2
$subs || []
l!l
2330
MC/DC0%0/1
$subs || []
$subs
Truth table50%1/2
A
A= $subs
Aresult
11
00
2115 1236 my $method_name = eval { $method->name };
2116 1236 ▶ $names{$method_name} = 1 if defined($method_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $method_name 1236 0 1236
2117 }
2118 233 \%names;
2119 };
2120 }
2121  
2122 524 my $cut;
2123 524 ▶ while($section =~ /^=head([1-6])\s+(.+?)\s*$/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /^=head([1-6])\s+(.+?)\s*$/gm) 2386 518 2904
2124 2386 my ($heading_level, $title) = ($1, $2);
2125 2386 ▶ next if($-[0] == 0); # the routine's own heading
Branch: 1 branch
BranchTrueFalseTotal
if $-[0] == 0 524 1862 2386
2126 1862 ▶ if($heading_level < $level
Branch: 1 branch
BranchTrueFalseTotal
if ($heading_level < $level or $heading_level == $level and $method_names->{$title}) 6 1856 1862
Condition83%5/6
$heading_level < $level or $heading_level == $level and $method_names->{$title}
l!l&&r!l&&!r
061856
$heading_level == $level and $method_names->{$title}
!ll&&!rl&&r
185336
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
2127 || ($heading_level == $level && $method_names->{$title})) {
2128 6 $cut = $-[0];
2129 6 last;
2130 }
2131 }
2132 524 ▶ if(defined($cut)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cut) 6 518 524
2133 6 $section = substr($section, 0, $cut);
2134 6 $section =~ s/\s+\z//;
2135 }
2136 524 $pod = $section;
2137 }
2138  
2139 783 return $pod;
2140 }
2141  
2142 # --------------------------------------------------
2143 # _pod_documents_file_write
2144 #
2145 # Purpose: Decide whether a routine's POD documents
2146 # creating or writing a file as part of
2147 # its contract, so the generated schema can
2148 # turn off the harness's leftover-file
2149 # check for it.
2150 # Entry: $pod - the routine's raw POD text, or
2151 # undef when it has none.
2152 # Exit: Returns 1 if the SIDE EFFECTS section
2153 # says a file is created, written,
2154 # overwritten or updated; 0 otherwise.
2155 # Side effects: None.
2156 # Notes: Only the SIDE EFFECTS section is examined.
2157 # A file mentioned while describing an
2158 # argument or an example is not a
2159 # documented side effect.
2160 # --------------------------------------------------
2161 772 ▶sub _pod_documents_file_write {
Subroutine _pod_documents_file_write: called CC=7 SCAR=19.5
2162 772 my ($self, $pod) = @_;
2163  
2164 772 ▶ return 0 unless defined($pod) && length($pod);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod and length $pod 253 519 772
Condition100%3/3
defined $pod and length $pod
!ll&&!rl&&r
1252519
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
2165  
2166 519 my ($section) = $pod =~ /^=head[1-4]\s+SIDE\s+EFFECTS\b(.*?)(?=^=head[1-4]\b|\z)/mis;
2167 519 ▶ return 0 unless defined($section) && length($section);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section and length $section 451 68 519
Condition66%2/3
defined $section and length $section
!ll&&!rl&&r
451068
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
2168  
2169 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
2170 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
2171  
2172 58 return 0;
2173 }
2174  
2175 # --------------------------------------------------
2176 # _analyze_method
2177 #
2178 # Purpose: Perform full multi-source analysis of
2179 # a single method and produce a complete
2180 # schema hashref, combining POD analysis,
2181 # code pattern detection, signature
2182 # analysis, validator schema extraction,
2183 # confidence scoring, relationship
2184 # detection, and modern Perl feature
2185 # extraction.
2186 #
2187 # Entry: $method - a method hashref as produced
2188 # by _find_methods, containing
2189 # at minimum: name, body, pod.
2190 #
2191 # Exit: Returns a schema hashref containing:
2192 # function, input, output, _confidence,
2193 # _analysis, _notes, and optionally:
2194 # new, accessor, relationships,
2195 # _yamltest_hints, _attributes,
2196 # _modern_features, _fields, _model,
2197 # _low_confidence.
2198 #
2199 # Side effects: Logs progress to stdout when verbose
2200 # is set. May carp or croak if
2201 # strict_pod is enabled and POD/code
2202 # disagreements are found.
2203 #
2204 # Notes: This is the central analysis entry
2205 # point — it orchestrates all other
2206 # analysis helpers and merges their
2207 # results. The non-invasive reasoning
2208 # layer (Model::Method, Analyzer::*)
2209 # runs after the main schema is built
2210 # and attaches metadata only.
2211 # --------------------------------------------------
2212 773 ▶sub _analyze_method {
Subroutine _analyze_method: called CC=58 SCAR=42.4
2213 773 my ($self, $method) = @_;
2214 773 my $code = $method->{body};
2215 773 my $pod = $method->{pod};
2216  
2217 # Extract modern features
2218 773 my $attributes = $self->_extract_subroutine_attributes($code);
2219 773 my $postfix_derefs = $self->_analyze_postfix_dereferencing($code);
2220 773 my $fields = $self->_extract_field_declarations($code);
2221  
2222 # If this method came from a class, use those field declarations
2223 0 ▶ if ($method->{fields} && keys %{$method->{fields}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"fields"} and keys %{$$method{"fields"};}) 0 773 773
Condition33%1/3
$method->{"fields"} and keys %{$$method{"fields"};}
!ll&&!rl&&r
77300
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
2224 0 $fields = $method->{fields};
2225 }
2226  
2227 my $schema = {
2228 function => $method->{name},
2229 773 _confidence => {
2230 'input' => {},
2231 'output' => {}
2232 },
2233 input => {},
2234 output => {},
2235 setup => undef,
2236 transforms => {},
2237 };
2238  
2239 # Analyze different sources
2240 773 my $pod_params = $self->_analyze_pod($pod);
2241 773 my $code_params = $self->_analyze_code($code, $method);
2242  
2243 # Neither analysis may hand a reference string to the schema as a parameter.
2244 773 $self->_sanitize_parameter_names($pod_params, 'POD analysis');
2245 773 $self->_sanitize_parameter_names($code_params, 'code analysis');
2246  
2247 # Validate POD/code agreement if strict mode is enabled.
2248 # Skip when there is no POD at all — strict_pod checks accuracy of
2249 # existing documentation, not whether every method is documented.
2250 773 ▶ if ($self->{strict_pod} && $pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict_pod"} and $pod) 375 398 773
Condition100%3/3
$self->{"strict_pod"} and $pod
!ll&&!rl&&r
33959375
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
2251 my @validation_errors = $self->_validate_pod_code_agreement(
2252 $pod_params,
2253 $code_params,
2254 $method->{name},
2255 {
2256 375 ignore_self => 1,
2257 allow_renames => 1,
2258 }
2259 );
2260  
2261 375 ▶ if (@validation_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@validation_errors) 30 345 375
2262 30 my $error_msg = "POD/Code disagreement in method '$method->{name}':\n " .
2263 join("\n ", @validation_errors);
2264  
2265 # Add to schema for reference even if we croak
2266 30 $schema->{_pod_validation_errors} = \@validation_errors;
2267  
2268 # Either croak immediately or log based on configuration
2269 30 ▶ if($self->{strict_pod} == $STRICT_POD_FATAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'strict_pod'} == $STRICT_POD_FATAL) { } 4 26 30
2270 4 croak("[POD STRICT] $error_msg");
2271 } else { # 1 = warnings
2272 26 carp("[POD STRICT] $error_msg");
2273 # Continue with analysis, but mark as problematic
2274 26 $schema->{_pod_disagreement} = 1;
2275 }
2276 }
2277 371 $schema->{_strict_pod_level} = $self->{strict_pod};
2278 }
2279  
2280 769 my $validator_params = $self->_extract_validator_schema($code);
2281  
2282 769 ▶ if ($validator_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator_params) { } 14 755 769
2283 14 $schema->{input} = $validator_params->{input};
2284 14 $schema->{input_style} = 'hash';
2285 14 $schema->{_confidence}{input} = { 'factors' => [ 'Determined from validator' ], 'level' => 'high' };
2286 $schema->{_analysis}{confidence_factors}{input} = [
2287 14 'Input schema extracted from validator'
2288 ];
2289 # =head4 Input spec overrides take highest priority — apply them on top of
2290 # the validator schema so authors can tune test constraints (optional,
2291 # memberof, matches) without altering the runtime validation call.
2292 14 for my $name (keys %$pod_params) {
2293 21 ▶ next unless ($pod_params->{$name}{_source} // '') eq SOURCE_INPUT_SPEC;
Branch: 1 branch
BranchTrueFalseTotal
unless ($pod_params->{$name}{"_source"} // "") eq "input_spec" 0 21 21
Condition50%1/2
$pod_params->{$name}{"_source"} // ""
l!l
210
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
2294 21 my $pod_p = $pod_params->{$name};
2295 21 ▶ $schema->{input}{$name} //= {};
Condition50%1/2
$schema->{"input"}{$name} //= {}
l!l
210
MC/DC0%0/1
$schema->{"input"}{$name} //= {}
$schema->{"input"}{$name}
Truth table50%1/2
A
A= $schema->{"input"}{$name}
Aresult
11
00
2296 21 ▶ $schema->{input}{$name}{optional} = $pod_p->{optional} if defined $pod_p->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"optional"} 15 6 21
2297 21 ▶ $schema->{input}{$name}{memberof} = $pod_p->{memberof} if defined $pod_p->{memberof};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"memberof"} 0 21 21
2298 21 ▶ $schema->{input}{$name}{matches} = $pod_p->{matches} if defined $pod_p->{matches};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"matches"} 0 21 21
2299 21 ▶ $schema->{input}{$name}{type} = $pod_p->{type} if defined $pod_p->{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"type"} 21 0 21
2300 21 ▶ $schema->{input}{$name}{min} = $pod_p->{min} if defined $pod_p->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"min"} 3 18 21
2301 21 ▶ $schema->{input}{$name}{max} = $pod_p->{max} if defined $pod_p->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pod_p->{"max"} 3 18 21
2302  
2303 # These constraints are documented, even though the field's
2304 # _source stays the type-checking marker (SOURCE_VALIDATOR).
2305 21 for my $key (qw(min max memberof matches nomatch notmemberof)) {
2306 126 ▶ next unless defined $pod_p->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod_p->{$key} 120 6 126
2307 6 $schema->{input}{$name}{_constraint_source}{$key} = SOURCE_INPUT_SPEC;
2308 }
2309 }
2310  
2311 # Not every validator extractor fills in per-parameter provenance, so
2312 # stamp the one source value here too. The harness treats a
2313 # 'validator' source as proof that the routine checks types; POD and
2314 # code-derived sources describe intent only.
2315 14 ▶ $schema->{input}{$_}{_source} //= SOURCE_VALIDATOR for keys %{$schema->{input}};
Condition50%1/2
$schema->{"input"}{$_}{"_source"} //= "validator"
l!l
043
MC/DC0%0/1
$schema->{"input"}{$_}{"_source"} //= "validator"
$schema->{"input"}{$_}{"_source"}
Truth table50%1/2
A
A= $schema->{"input"}{$_}{"_source"}
Aresult
11
00
2316 } else {
2317 # Merge field declarations into code_params before merging analyses
2318 755 ▶ if (keys %$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$fields) 0 755 755
2319 0 $self->_merge_field_declarations($code_params, $fields);
2320 }
2321  
2322 # A lone documented input parameter may be the whole argument list
2323 # under another name; the helper decides from the body and folds the
2324 # code's facts about that argument onto the documented name.
2325 755 $self->_positional_pod_input($pod_params, $code_params, $code);
2326  
2327 # Merge analyses
2328 755 $schema->{input} = $self->_merge_parameter_analyses(
2329 $pod_params,
2330 $code_params,
2331 );
2332 }
2333  
2334 # ----------------------------------------
2335 # Legacy Output Analysis (unchanged)
2336 # ----------------------------------------
2337  
2338 $schema->{output} = $self->_analyze_output(
2339 $method->{pod},
2340 $method->{body},
2341 $method->{name}
2342 769 );
2343  
2344  
2345 # Detect accessor methods
2346 769 $self->_detect_accessor_methods($method, $schema);
2347  
2348 # Detect if this is an instance method that needs object instantiation
2349 # Constructors never require object instantiation
2350 769 my $needs_object = $self->_needs_object_instantiation($method->{name}, $method->{body}, $method);
2351 769 ▶ if($method->{name} ne 'new' && $needs_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"name"} ne "new" and $needs_object) 379 390 769
Condition100%3/3
$method->{"name"} ne "new" and $needs_object
!ll&&!rl&&r
95295379
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
2352 379 $schema->{new} = $needs_object;
2353 379 $self->_log(" NEW: Method requires object instantiation: $needs_object");
2354 }
2355  
2356 # Calculate confidences
2357 769 my $input_confidence = $schema->{_confidence}{'input'};
2358 769 ▶ if(!ref($input_confidence)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $input_confidence) 0 769 769
2359 0 $input_confidence = $schema->{_confidence}{'input'} = $self->_calculate_input_confidence($schema->{input});
2360 }
2361 769 my $output_confidence = $schema->{_confidence}{'output'} = $self->_calculate_output_confidence($schema->{output});
2362  
2363 # Add metadata
2364 769 $schema->{_notes} = $self->_generate_notes($schema->{input});
2365  
2366 # Add analytics
2367 769 ▶ $schema->{_analysis} ||= {};
Condition100%2/2
$schema->{"_analysis"} ||= {}
l!l
14755
MC/DC100%1/1
$schema->{"_analysis"} ||= {}
$schema->{"_analysis"}
Truth table100%2/2
A
A= $schema->{"_analysis"}
Aresult
11
00
2368 769 $schema->{_analysis}{input_confidence} = $input_confidence->{level};
2369 769 $schema->{_analysis}{output_confidence} = $output_confidence->{level};
2370 769 ▶ $schema->{_analysis}{confidence_factors} ||= {};
Condition100%2/2
$schema->{"_analysis"}{"confidence_factors"} ||= {}
l!l
14755
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
2371 769 ▶ $schema->{_analysis}{confidence_factors}{input} ||= $input_confidence->{factors};
Condition100%3/3
$schema->{"_analysis"}{"confidence_factors"}{"input"} ||= $input_confidence->{"factors"}
l!l&&r!l&&!r
146749
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
2372 769 ▶ $schema->{_analysis}{confidence_factors}{output} ||= $output_confidence->{factors};
Condition33%1/3
$schema->{"_analysis"}{"confidence_factors"}{"output"} ||= $output_confidence->{"factors"}
l!l&&r!l&&!r
07690
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
2373  
2374 769 foreach my $mode('input', 'output') {
2375 1538 $self->_set_defaults($schema, $mode);
2376 }
2377  
2378 # Optionally store detailed per-parameter analysis
2379 769 ▶ if ($input_confidence->{per_parameter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_confidence->{"per_parameter"}) 0 769 769
2380 0 $schema->{_analysis}{per_parameter_scores} = $input_confidence->{per_parameter};
2381 }
2382  
2383 # Calculate overall confidence (for backward compatibility)
2384 769 my $input_level = $input_confidence->{level};
2385 769 my $output_level = $output_confidence->{level};
2386  
2387 769 my %level_rank = (
2388 none => 0,
2389 very_low => 1,
2390 low => 2,
2391 medium => 3,
2392 high => 4
2393 );
2394  
2395 # Overall is the lower of input and output
2396 769 ▶ $input_level //= 'none';
Condition100%2/2
$input_level //= "none"
l!l
89680
MC/DC100%1/1
$input_level //= "none"
$input_level
Truth table100%2/2
A
A= $input_level
Aresult
11
00
2397 769 ▶ $output_level //= 'none';
Condition50%1/2
$output_level //= "none"
l!l
7690
MC/DC0%0/1
$output_level //= "none"
$output_level
Truth table50%1/2
A
A= $output_level
Aresult
11
00
2398 769 ▶ 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} ? : 668 101 769
2399  
2400 769 $schema->{_analysis}{overall_confidence} = $overall;
2401  
2402 # Analyze parameter relationships
2403 769 my $relationships = $self->_analyze_relationships($method);
2404 769 ▶ if ($relationships && @{$relationships}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relationships and @{$relationships;}) 7 762 769
Condition66%2/3
$relationships and @{$relationships;}
!ll&&!rl&&r
07627
MC/DC50%1/2
$relationships and @{$relationships;}
$relationships @{$relationships;}
Truth table66%2/3
A and B
A= $relationshipsB= @{$relationships;}
ABresult
0X0
100
111
2405 7 $schema->{relationships} = $relationships;
2406 7 $self->_log(" Found " . scalar(@$relationships) . " parameter relationships");
2407 }
2408  
2409 # Store modern feature info in schema
2410 769 ▶ $schema->{_attributes} = $attributes if keys %$attributes;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$attributes 0 769 769
2411 769 ▶ $schema->{_modern_features}{postfix_dereferencing} = $postfix_derefs if keys %$postfix_derefs;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$postfix_derefs 0 769 769
2412 769 ▶ $schema->{_fields} = $fields if keys %$fields;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$fields 0 769 769
2413  
2414 # Store class info if this is a class method
2415 769 ▶ if ($method->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"class"}) 0 769 769
2416 0 $schema->{_class} = $method->{class};
2417 }
2418  
2419 769 my $hints = $self->_extract_test_hints($method, $schema);
2420 769 $self->_extract_pod_examples($pod, $hints);
2421  
2422 769 for my $k (qw(boundary_values invalid_inputs valid_inputs equivalence_classes)) {
2423 3076 my %seen;
2424 $hints->{$k} = [
2425 189 ▶ grep { !$seen{ defined $_ ? $_ : '__undef__' }++ }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 189 0 189
2426 3076 @{ $hints->{$k} }
2427 ];
2428 }
2429  
2430 # --------------------------------------------------
2431 # YAML test hints: numeric boundaries
2432 # --------------------------------------------------
2433 769 ▶ if ($self->_method_has_numeric_intent($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_method_has_numeric_intent($schema)) 187 582 769
2434 187 ▶ $schema->{_yamltest_hints} ||= {};
Condition50%1/2
$schema->{"_yamltest_hints"} ||= {}
l!l
0187
MC/DC0%0/1
$schema->{"_yamltest_hints"} ||= {}
$schema->{"_yamltest_hints"}
Truth table50%1/2
A
A= $schema->{"_yamltest_hints"}
Aresult
11
00
2435  
2436 # Do not override existing hints
2437 187 ▶ $schema->{_yamltest_hints}{boundary_values} ||= [];
Condition50%1/2
$schema->{"_yamltest_hints"}{"boundary_values"} ||= []
l!l
0187
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
2438  
2439 0 ▶ my %seen = map { (defined $_ ? $_ : '__undef__') => 1 }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
2440 187 @{ $schema->{_yamltest_hints}{boundary_values} };
2441  
2442 187 foreach my $v (@{ $self->_numeric_boundary_values }) {
2443 934 ▶ my $key = defined $v ? $v : '__undef__';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 933 1 934
2444 934 ▶ push @{ $schema->{_yamltest_hints}{boundary_values} }, $v unless $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$key}++ 934 0 934
2445 }
2446  
2447 187 $self->_log(' HINTS: Added numeric boundary values');
2448 }
2449  
2450 769 ▶ if (keys %$hints) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$hints) 769 0 769
2451 769 ▶ $schema->{_yamltest_hints} ||= {};
Condition100%2/2
$schema->{"_yamltest_hints"} ||= {}
l!l
187582
MC/DC100%1/1
$schema->{"_yamltest_hints"} ||= {}
$schema->{"_yamltest_hints"}
Truth table100%2/2
A
A= $schema->{"_yamltest_hints"}
Aresult
11
00
2452 769 foreach my $k (keys %$hints) {
2453 $schema->{_yamltest_hints}{$k} = $hints->{$k}
2454 3076 ▶ unless exists $schema->{_yamltest_hints}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"_yamltest_hints"}{$k} 2889 187 3076
2455 }
2456 }
2457  
2458 769 ▶ 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) 681 88 769
Condition100%3/3
$level_rank{$overall} < $level_rank{$LEVEL_MEDIUM} and $level_rank{$overall} < $self->{"confidence_threshold"} * 4
!ll&&!rl&&r
979681
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
2459 ($level_rank{$overall} < ($self->{confidence_threshold} * 4))) {
2460 681 $schema->{_low_confidence} = 1
2461 }
2462  
2463 # ----------------------------------------
2464 # Non-invasive reasoning layer
2465 # ----------------------------------------
2466  
2467 my $method_model = App::Test::Generator::Model::Method->new(
2468 name => $method->{name},
2469 source => $method->{body},
2470 769 );
2471  
2472 769 my $return_analyzer = App::Test::Generator::Analyzer::Return->new();
2473 769 $return_analyzer->analyze($method_model);
2474  
2475 # Let model learn from finalized schema
2476 769 ▶ if ($schema->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}) 769 0 769
2477 769 $method_model->absorb_legacy_output($schema->{output});
2478 }
2479  
2480 769 $method_model->resolve_return_type();
2481 769 $method_model->resolve_classification();
2482 769 $method_model->resolve_confidence();
2483  
2484 # Attach only metadata
2485 $schema->{_model} = {
2486 769 classification => $method_model->classification,
2487 confidence => $method_model->confidence,
2488 };
2489  
2490 # ----------------------------------------
2491 # Return Meta Analysis (Non-invasive)
2492 # ----------------------------------------
2493  
2494 769 my $meta = App::Test::Generator::Analyzer::ReturnMeta->new();
2495 769 my $analysis = $meta->analyze($schema);
2496  
2497 769 $schema->{_analysis}{stability_score} = $analysis->{stability_score};
2498 769 $schema->{_analysis}{consistency_score} = $analysis->{consistency_score};
2499 769 $schema->{_analysis}{risk_flags} = $analysis->{risk_flags};
2500  
2501 # ----------------------------------------
2502 # Side Effect Analysis (Non-invasive)
2503 # ----------------------------------------
2504  
2505 769 my $se = App::Test::Generator::Analyzer::SideEffect->new();
2506  
2507 769 my $effects = $se->analyze($method);
2508  
2509 769 $schema->{_analysis}{side_effects} = $effects;
2510  
2511 # A routine whose documented side effects include creating or writing a
2512 # file is doing its job when it leaves that file behind, so the harness's
2513 # leftover-file check must not report it. Record the contract here for
2514 # _write_schema to emit as check_files: 0.
2515 769 ▶ if($self->_pod_documents_file_write($pod)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pod_documents_file_write($pod)) 9 760 769
2516 9 $schema->{_analysis}{side_effects}{writes_file} = 1;
2517 }
2518  
2519 # ----------------------------------------
2520 # Complexity Analysis (Non-invasive)
2521 # ----------------------------------------
2522  
2523 769 my $cx = App::Test::Generator::Analyzer::Complexity->new();
2524 769 my $complexity = $cx->analyze($method);
2525  
2526 769 $schema->{_analysis}{complexity} = $complexity;
2527  
2528 769 return $schema;
2529 }
2530  
2531 # --------------------------------------------------
2532 # _method_has_numeric_intent
2533 #
2534 # Purpose: Determine whether a method schema
2535 # has numeric intent — either a numeric
2536 # output type or at least one required
2537 # numeric input parameter — to decide
2538 # whether to add standard numeric
2539 # boundary hint values.
2540 #
2541 # Entry: $schema - schema hashref as built by
2542 # _analyze_method.
2543 #
2544 # Exit: Returns 1 if numeric intent is
2545 # detected, 0 otherwise.
2546 #
2547 # Side effects: None.
2548 # --------------------------------------------------
2549 769 ▶sub _method_has_numeric_intent {
Subroutine _method_has_numeric_intent: called CC=9 SCAR=22.0
2550 769 my ($self, $schema) = @_;
2551  
2552 # Numeric output
2553 769 ▶ 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 610 769
Condition83%5/6
$schema->{"output"} and $schema->{"output"}{"type"} and $schema->{"output"}{"type"} =~ /^(number|integer)$/
!ll&&!rl&&r
30580159
$schema->{"output"} and $schema->{"output"}{"type"}
!ll&&!rl&&r
030739
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
2554  
2555 # Numeric inputs
2556 610 ▶ foreach my $p (values %{ $schema->{input} || {} }) {
Condition100%2/2
$schema->{'input'} || {}
l!l
57535
MC/DC100%1/1
$schema->{'input'} || {}
$schema->{'input'}
Truth table100%2/2
A
A= $schema->{'input'}
Aresult
11
00
2557 595 ▶ next if $p->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"optional"} 284 311 595
2558 311 ▶ return 1 if ($p->{type} && $p->{type} =~ /^(number|integer)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} and $p->{"type"} =~ /^(number|integer)$/ 28 283 311
Condition66%2/3
$p->{"type"} and $p->{"type"} =~ /^(number|integer)$/
!ll&&!rl&&r
028328
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
2559 }
2560  
2561 582 return 0;
2562 }
2563  
2564 # --------------------------------------------------
2565 # _numeric_boundary_values
2566 #
2567 # Purpose: Return the standard set of numeric
2568 # boundary values used as test hints
2569 # for methods with numeric intent.
2570 #
2571 # Entry: None.
2572 #
2573 # Exit: Returns an arrayref of boundary
2574 # values: [-1, 0, 1, 2, 100].
2575 #
2576 # Side effects: None.
2577 # --------------------------------------------------
2578 186 ▶sub _numeric_boundary_values {
Subroutine _numeric_boundary_values: called CC=1 SCAR=0.0
2579 186 return [ -1, 0, 1, 2, 100 ];
2580 }
2581  
2582 # --------------------------------------------------
2583 # _detect_accessor_methods
2584 #
2585 # Purpose: Detect whether a method is a getter,
2586 # setter, or combined getter/setter
2587 # accessor by analysing assignment and
2588 # return patterns involving $self->{...}.
2589 #
2590 # Entry: $method - method hashref containing
2591 # at minimum 'body' and
2592 # optionally 'pod'.
2593 # $schema - schema hashref (modified
2594 # in place).
2595 #
2596 # Exit: Returns nothing. Modifies $schema in
2597 # place, setting accessor, input,
2598 # input_style, output, and _confidence
2599 # keys as appropriate.
2600 #
2601 # Side effects: Croaks if a getter/setter has more
2602 # than one argument, or if a setter
2603 # returns non-self data.
2604 # Logs detections to stdout when
2605 # verbose is set.
2606 #
2607 # Notes: Four accessor patterns are detected
2608 # in order: (1) combined getter/setter
2609 # with shift, (2) combined getter/setter
2610 # with validated input, (3) getter only,
2611 # (4) setter that returns $self. Methods
2612 # accessing multiple $self fields are
2613 # skipped immediately.
2614 # --------------------------------------------------
2615 769 ▶sub _detect_accessor_methods {
Subroutine _detect_accessor_methods: called CC=62 SCAR=50.4
2616 769 my ($self, $method, $schema) = @_;
2617  
2618 769 my $body = $method->{body};
2619  
2620 # Normalize whitespace for regex sanity
2621 769 my $code = $body;
2622 769 $code =~ s/\s+/ /g;
2623  
2624 # If a method touches more than one $self->{...}, it’s not an accessor.
2625 769 my %fields_seen;
2626 769 ▶ while ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}/g) 402 769 1171
2627 402 $fields_seen{$1}++;
2628 }
2629 769 ▶ if (keys(%fields_seen) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %fields_seen > 1) 45 724 769
2630 45 $self->_log(" Skipping accessor detection: multiple fields accessed");
2631 45 return;
2632 }
2633  
2634 # A store guarded by the presence of an argument is the same get/set
2635 # idiom as `if (@_)`, just written against a named lexical:
2636 #
2637 # my $status = shift;
2638 # return $self->{status} = $status if(defined($status));
2639 # ...
2640 # return $self->{status} || 200;
2641 #
2642 # CGI::Info::status is this shape, and it was being typed as a plain
2643 # function because every branch below only looked for `if (@_)`.
2644 724 my $conditional_arg = 0;
2645 724 ▶ while($code =~ /\bmy\s+\$(\w+)\s*=\s*shift\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\bmy\s+\$(\w+)\s*=\s*shift\b/g) 80 703 783
2646 80 my $arg = $1;
2647 # The invocant is shifted too, but a test on $self says nothing
2648 # about whether an argument was supplied.
2649 80 ▶ next if($arg eq 'self');
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "self" 24 56 80
2650 56 ▶ 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 35 56
2651 21 $conditional_arg = 1;
2652 21 last;
2653 }
2654 }
2655 # `if (@_)` stays the strongest signal; a named-argument guard is
2656 # weaker evidence and must not turn a heuristic miss into a croak.
2657 # The guard must test the argument itself: CGI::Info::protocol reads
2658 # if($ENV{'SCRIPT_URI'}) and stores a value it computed, which says
2659 # nothing about how the method is called. Reading any `if($x)` as an
2660 # argument guard typed it as a getset accessor, and the harness then
2661 # set a value the routine never looks at.
2662 724 ▶ 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 717 724
2663  
2664 # -------------------------------
2665 # Getter/Setter combo
2666 # -------------------------------
2667 724 ▶ 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 724 724
elsif ($arg_guard || $conditional_arg and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x and $code =~ /return\b/) { } 6 718 724
elsif ($code =~ /return\s+\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*;/) { } 38 680 718
elsif ($code =~ /return\s+\$self\b/ and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/) { } 6 1332 1338
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
72310
$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
72301
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
2668 # Require get/set of the same property
2669 $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*shift\s*;/ &&
2670 $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$1\E['"]?\s*\}\s*;/ &&
2671 $code =~ /if\s*\(\s*\@_/
2672 ) {
2673 0 my $property = $1;
2674  
2675 0 ▶ if(!defined($property)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $property) 0 0 0
2676 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
2677 0 $property = $1;
2678 }
2679 }
2680  
2681 $schema->{accessor} = {
2682 0 type => 'getset',
2683 property => $property,
2684 };
2685  
2686 0 $self->_log(" Detected getter/setter accessor for property: $property");
2687  
2688 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
2689  
2690 0 $schema->{input_style} = 'hash';
2691  
2692 $schema->{_confidence}{input} = {
2693 0 level => 'high',
2694 factors => ['Detected combined getter/setter accessor'],
2695 };
2696 0 ▶ if (my $pod = $method->{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pod = $method->{"pod"}) 0 0 0
2697 0 ▶ if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) 0 0 0
2698 0 my $class = $1;
2699 $schema->{output} = {
2700 0 type => 'object',
2701 isa => $class,
2702 };
2703 0 $schema->{input}{$property} = {
2704 type => 'object',
2705 isa => $class,
2706 optional => 1,
2707 };
2708  
2709 $schema->{_confidence}{output} = {
2710 0 level => 'high',
2711 factors => ['POD specifies UserAgent object'],
2712 };
2713 }
2714 }
2715 ▶ } 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
71806
$arg_guard || $conditional_arg and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x
!ll&&!rl&&r
696226
$arg_guard or $conditional_arg
l!l&&r!l&&!r
721696
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
2716 $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*(?:shift|\@_|\$_\[\d+\]|\$\w+)\b/x &&
2717 $code =~ /return\b/
2718 ) {
2719 # -------------------------------
2720 # Getter/Setter (validated input)
2721 # -------------------------------
2722 6 my $property = $1;
2723  
2724 # A named-argument guard is weaker evidence than `if (@_)`, so when
2725 # the method declares more than one parameter assume this is not an
2726 # accessor rather than croaking further down on an ambiguous match.
2727 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
2728 2 (ref($schema->{input}) eq 'HASH') && (scalar(keys %{$schema->{input}}) > 1)) {
2729 0 $self->_log(" Skipping conditional accessor: more than one argument");
2730 0 return;
2731 }
2732  
2733 6 ▶ if(!defined($property)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $property) 6 0 6
2734 6 ▶ if($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=/) 6 0 6
2735 6 $property = $1;
2736 }
2737 }
2738 6 ▶ if ($code =~ /validate_strict/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /validate_strict/) { } 2 4 6
2739 2 push @{ $schema->{_confidence}{input}{factors} }, 'Setter uses Params::Validate::Strict';
2740 } else {
2741 # ---------------------------------------
2742 # Detect object input via blessed($arg)
2743 # ---------------------------------------
2744 4 ▶ if ($code =~ /blessed\s*\(\s*\$(\w+)\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /blessed\s*\(\s*\$(\w+)\s*\)/) { } 0 4 4
2745 0 my $param = $1;
2746  
2747 0 $self->_log(" Detected object input via blessed(\$$param)");
2748  
2749 $schema->{input} = {
2750 0 $param => {
2751 type => 'object',
2752 optional => 1,
2753 }
2754 };
2755  
2756 $schema->{_confidence}{input} = {
2757 0 level => 'high',
2758 factors => ['Input validated by Scalar::Util::blessed'],
2759 };
2760 } else {
2761 # fallback ONLY if nothing known
2762 $schema->{input} ||= {
2763 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
2764 };
2765 }
2766 };
2767 $schema->{accessor} = {
2768 6 type => 'getset',
2769 property => $property,
2770 };
2771  
2772 6 $self->_log(" Detected getter/setter accessor for property: $property");
2773 6 ▶ if (my $pod = $method->{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pod = $method->{"pod"}) 2 4 6
2774 2 ▶ if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\b(LWP::UserAgent(?:::\w+)*)\b/) 0 2 2
2775 0 my $class = $1;
2776 $schema->{output} = {
2777 0 type => 'object',
2778 isa => $class,
2779 };
2780 0 $schema->{input}{$property} = {
2781 type => 'object',
2782 isa => $class,
2783 optional => 1,
2784 };
2785  
2786 $schema->{_confidence}{output} = {
2787 0 level => 'high',
2788 factors => ['POD specifies UserAgent object'],
2789 };
2790 }
2791 }
2792 # Set position 0 on whichever single input parameter already exists.
2793 # The parameter may be named differently from the stored property
2794 # (e.g. $dir for the logdir property), so find the existing key
2795 # rather than creating a new entry keyed by $property — doing the
2796 # latter produces a duplicate-position collision between the real
2797 # param and the spurious $property key.
2798 6 ▶ if(ref($schema->{input}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"input"} eq "HASH") 6 0 6
2799 6 my @input_keys = keys %{$schema->{input}};
2800 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
2801 0 croak(__PACKAGE__, ': A getset accessor function can have at most one argument');
2802 } elsif(@input_keys == 1) {
2803 6 $schema->{input}{$input_keys[0]}{position} = 0;
2804 } else {
2805 0 $schema->{input}{$property}{position} = 0;
2806 }
2807 }
2808 } elsif ($code =~ /return\s+\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*;/) {
2809 # -------------------------------
2810 # Getter
2811 # -------------------------------
2812 38 my $property = $1;
2813  
2814 # Don't flag mutators like
2815 # sub foo {
2816 # my $self = shift;
2817 # $self->{bar} = shift;
2818 # return $self->{bar};
2819 # }
2820 # Only exclude if the property is being set FROM EXTERNAL INPUT
2821 38 ▶ 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+\])/) 37 1 38
2822 37 my @returns = $code =~ /return\b/g;
2823 37 my @self_returns = $code =~ /return\s+\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}/g;
2824 # it's a getter
2825 37 ▶ if (scalar(@returns) == scalar(@self_returns)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @returns == scalar @self_returns) 35 2 37
2826 # all returns are returning $self->{$property}, so it's a getter
2827 $schema->{accessor} = {
2828 35 type => 'getter',
2829 property => $property,
2830 };
2831  
2832 35 $self->_log(" Detected getter accessor for property: $property");
2833  
2834 # A memoised property's initializer is the only evidence of
2835 # what the getter returns:
2836 # $self->{proposals} //= [ map { ... } @{ $self->survivors } ];
2837 # Without it the expression looks like a scalar, and the
2838 # harness validates the returned arrayref as a string.
2839 35 ▶ $schema->{output} ||= {};
Condition50%1/2
$schema->{"output"} ||= {}
l!l
350
MC/DC0%0/1
$schema->{"output"} ||= {}
$schema->{"output"}
Truth table50%1/2
A
A= $schema->{"output"}
Aresult
11
00
2840 35 ▶ 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 32 35
elsif ($code =~ m[\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*//=\s*\{]) { } 0 32 32
2841 3 $schema->{output}{type} = 'arrayref';
2842 } elsif($code =~ /\$self\s*->\s*\{\s*['"]?\Q$property\E['"]?\s*\}\s*\/\/=\s*\{/) {
2843 0 $schema->{output}{type} = 'hashref';
2844 }
2845  
2846 $schema->{_confidence}{output} = {
2847 35 level => 'high',
2848 factors => ['Detected getter method'],
2849 };
2850 35 delete $schema->{input};
2851 }
2852 }
2853 ▶ } elsif (
Condition100%3/3
$code =~ /return\s+\$self\b/ and $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/
!ll&&!rl&&r
658166
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
2854 $code =~ /return\s+\$self\b/ &&
2855 $code =~ /\$self\s*->\s*\{\s*['"]?([^}'"]+)['"]?\s*\}\s*=\s*\$(\w+)\s*;/
2856 ) {
2857 # -------------------------------
2858 # Setter
2859 # -------------------------------
2860 6 my ($property, $param) = ($1, $2);
2861  
2862 $schema->{accessor} = {
2863 6 type => 'setter',
2864 property => $property,
2865 param => $param,
2866 };
2867  
2868 6 $self->_log(" Detected setter accessor for property: $property");
2869  
2870 # A setter that hands the argument to something else before storing
2871 # it, such as CGI::Info::set_logger's
2872 #
2873 # $self->{'logger'} = Log::Abstraction->new($logger);
2874 #
2875 # does not constrain what the argument may be, so the 'string' default
2876 # would invent cases like "a hash ref must die" that the routine
2877 # rightly accepts. Claim 'string' only when every assignment stores the
2878 # argument itself.
2879 6 my $param_type = 'string';
2880 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
2881 7 ▶ if($1 =~ /(?:->|::)\w+\s*\([^)]*\$\Q$param\E\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 =~ /(?:->|::)\w+\s*\([^)]*\$\Q$param\E\b/) 1 6 7
2882 1 $param_type = 'any';
2883 1 last;
2884 }
2885 }
2886  
2887 $schema->{input} = {
2888 6 $param => { type => $param_type }, # safe default
2889 };
2890 6 $schema->{input_style} = 'hash';
2891  
2892 $schema->{_confidence}{input} = {
2893 6 level => 'high',
2894 factors => ['Detected setter/accessor method'],
2895 };
2896 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
2897 6 ▶ if($schema->{output}{type} ne 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"type"} ne "object") 0 6 6
2898 0 croak 'Setter can not return data other than $self';
2899 }
2900 6 ▶ if($schema->{output}{isa} ne $self->{_package_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"isa"} ne $self->{"_package_name"}) 0 6 6
2901 0 croak 'Setter can not return data other than $self';
2902 }
2903 0 } elsif(scalar(keys %{$schema->{output}}) != 0) {
2904 $self->_analysis_error(
2905 method => $method->{name},
2906 0 message => "Setter cannot return data",
2907 );
2908 }
2909 }
2910  
2911 724 ▶ if(exists($schema->{accessor})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"accessor"}) 47 677 724
2912 47 ▶ 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 35 47
Condition66%4/6
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"type"} =~ /setter|getset/ and $schema->{"input"}
!ll&&!rl&&r
35012
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"type"} =~ /setter|getset/
!ll&&!rl&&r
03512
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
2913 12 for my $param (keys %{ $schema->{input} }) {
2914 12 my $in = $schema->{input}{$param};
2915  
2916 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
2917 $schema->{output} = {
2918 type => 'object',
2919 2 ▶ ($in->{isa} ? (isa => $in->{isa}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$in->{'isa'} ? : 0 2 2
2920 };
2921  
2922 $schema->{_confidence}{output} = {
2923 2 level => 'high',
2924 factors => ['Output type propagated from setter input'],
2925 };
2926 }
2927 }
2928 }
2929  
2930 47 ▶ 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") 28 19 47
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
61328
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"property"} and $schema->{"accessor"}{"type"} =~ /getter|getset/
!ll&&!rl&&r
0641
$schema->{"accessor"}{"type"} and $schema->{"accessor"}{"property"}
!ll&&!rl&&r
0047
not defined $schema->{"output"}{"type"} or $schema->{"output"}{"type"} eq "string"
l!l&&r!l&&!r
02813
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
2931 ((!defined($schema->{output}{type})) || ($schema->{output}{type} eq 'string'))) {
2932 28 ▶ if (my $pod = $method->{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pod = $method->{"pod"}) 17 11 28
2933 # POD says "UserAgent object"
2934 17 ▶ if ($pod =~ /\bUser[- ]?Agent\b.*\bobject\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /\bUser[- ]?Agent\b.*\bobject\b/i) 1 16 17
2935 1 $schema->{output}{type} = 'object';
2936 1 $schema->{output}{isa} = 'LWP::UserAgent';
2937  
2938 1 push @{ $schema->{_confidence}{output}{factors} }, 'POD indicates UserAgent object';
2939  
2940 1 $schema->{_confidence}{output}{level} = 'high';
2941 }
2942 }
2943 }
2944 }
2945 }
2946  
2947 # --------------------------------------------------
2948 # _analysis_error
2949 #
2950 # Purpose: Report a fatal analysis error with
2951 # module, method, and file context,
2952 # then croak.
2953 #
2954 # Entry: Named args:
2955 # method - method name string.
2956 # message - error description string.
2957 #
2958 # Exit: Does not return — always croaks.
2959 #
2960 # Side effects: None beyond the croak.
2961 # --------------------------------------------------
2962 2 ▶sub _analysis_error {
Subroutine _analysis_error: called CC=5 SCAR=16.1
2963 2 my ($self, %args) = @_;
2964  
2965 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
2966 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
2967  
2968 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
2969 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
2970  
2971 2 croak join "\n",
2972 $msg,
2973 " Module: $module",
2974 " Method: $method",
2975 " File: $file",
2976 '';
2977 }
2978  
2979 # --------------------------------------------------
2980 # _extract_validator_schema
2981 #
2982 # Purpose: Try each supported validator extractor
2983 # in priority order and return the first
2984 # schema that yields a non-empty input
2985 # spec. Used to detect explicit
2986 # parameter validation declarations
2987 # before falling back to heuristic
2988 # code analysis.
2989 #
2990 # Entry: $code - method body source string.
2991 #
2992 # Exit: Returns a schema hashref on success,
2993 # or undef if no supported validator
2994 # call is detected.
2995 #
2996 # Side effects: None.
2997 #
2998 # Notes: Extractors tried in order:
2999 # Params::Validate::Strict,
3000 # Params::Validate,
3001 # MooseX::Params::Validate,
3002 # Type::Params.
3003 # --------------------------------------------------
3004 769 ▶sub _extract_validator_schema {
Subroutine _extract_validator_schema: called CC=6 SCAR=18.3
3005 769 my ($self, $code) = @_;
3006  
3007 769 for my $extractor ('_extract_pvs_schema', '_extract_pv_schema', '_extract_moosex_params_schema', '_extract_type_params_schema') {
3008 3043 my $res = $self->$extractor($code);
3009 3043 ▶ 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 {};} 14 3029 3043
Condition62%5/8
$res and ref $res eq "HASH" and keys %{$res->{"input"} or {};}
!ll&&!rl&&r
3029014
$res and ref $res eq "HASH"
!ll&&!rl&&r
3029014
$res->{'input'} || {}
l!l
140
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
3010 }
3011  
3012 755 return;
3013 }
3014  
3015 # --------------------------------------------------
3016 # _parse_schema_hash
3017 #
3018 # Purpose: Parse a PPI block node representing
3019 # a validator schema hash literal and
3020 # return a normalised schema structure
3021 # suitable for use as input spec.
3022 #
3023 # Entry: $hash - a PPI node with a children()
3024 # method, typically a
3025 # PPI::Structure::Block from
3026 # a validate_strict call.
3027 #
3028 # Exit: Returns a hashref with keys:
3029 # input - hashref of param specs
3030 # input_style - 'hash'
3031 # _confidence - confidence hashref
3032 # or undef if parsing fails.
3033 #
3034 # Side effects: None.
3035 # --------------------------------------------------
3036 0 ▶sub _parse_schema_hash {
Subroutine _parse_schema_hash: not called CC=27 SCAR=66.3
3037 0 my ($self, $hash) = @_;
3038  
3039 0 my %result;
3040  
3041 0 for my $child ($hash->children) {
3042 # skip whitespace and operators
3043 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
3044 0 my ($key, $val);
3045  
3046 my @tokens = grep {
3047 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
3048 !$_->isa('PPI::Token::Operator')
3049 } $child->children;
3050  
3051 0 ▶ for (my $i = 0; $i < @tokens - 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tokens - 1) 0 0 0
3052 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
3053 $tokens[$i+1]->isa('PPI::Structure::Constructor')) {
3054 0 $key = $tokens[$i]->content;
3055 0 $key =~ s/^['"]|['"]$//g;
3056 0 $val = $tokens[$i+1];
3057 0 last;
3058 }
3059 }
3060  
3061 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
3062  
3063 0 my %param;
3064 0 for my $inner ($val->children) {
3065 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
3066  
3067 my ($k, undef, $v) = grep {
3068 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
3069 !$_->isa('PPI::Token::Operator')
3070 } $inner->children;
3071  
3072 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
3073  
3074 0 my $keyname = $k->content;
3075 0 ▶ my $value = $v->can('content') ? $v->content : undef;
Branch: 1 branch
BranchTrueFalseTotal
$v->can('content') ? : 0 0 0
3076 0 ▶ $value =~ s/^['"]|['"]$//g if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
3077  
3078 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
3079 0 $param{type} = lc($value);
3080 } elsif ($keyname eq 'optional') {
3081 0 ▶ $param{optional} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
3082 ▶ } 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
3083 0 $param{$keyname} = 0 + $value;
3084 } elsif ($keyname eq 'matches') {
3085 0 $param{matches} = qr/$value/;
3086 }
3087 }
3088  
3089 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
3090 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
3091  
3092 0 $result{$key} = \%param;
3093 }
3094 }
3095  
3096 return {
3097 0 input => \%result,
3098 input_style => 'hash',
3099 _confidence => {
3100 input => {
3101 level => 'high',
3102 factors => ['Input schema extracted from validator'],
3103 },
3104 },
3105 };
3106 }
3107  
3108 # --------------------------------------------------
3109 # _ppi
3110 #
3111 # Purpose: Return a PPI::Document for a code
3112 # string, using a per-instance cache
3113 # to avoid re-parsing the same string
3114 # multiple times during a single
3115 # analysis pass.
3116 #
3117 # Entry: $code - either a string of Perl source
3118 # code, or an object that
3119 # already has a find() method
3120 # (returned as-is).
3121 #
3122 # Exit: Returns a PPI::Document, or the
3123 # original object if it already
3124 # supports find().
3125 #
3126 # Side effects: Populates $self->{_ppi_cache}.
3127 # --------------------------------------------------
3128 16 ▶sub _ppi {
Subroutine _ppi: called CC=5 SCAR=16.2
3129 16 my ($self, $code) = @_;
3130  
3131 16 ▶ return $code if ref($code) && $code->can('find');
Branch: 1 branch
BranchTrueFalseTotal
if ref $code and $code->can("find") 1 15 16
Condition66%2/3
ref $code and $code->can("find")
!ll&&!rl&&r
1501
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
3132  
3133 15 ▶ $self->{_ppi_cache} ||= {};
Condition100%2/2
$self->{"_ppi_cache"} ||= {}
l!l
213
MC/DC100%1/1
$self->{"_ppi_cache"} ||= {}
$self->{"_ppi_cache"}
Truth table100%2/2
A
A= $self->{"_ppi_cache"}
Aresult
11
00
3134 15 ▶ 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
1140
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
3135 }
3136  
3137 # --------------------------------------------------
3138 # _extract_pvs_schema
3139 #
3140 # Purpose: Detect and extract a parameter schema
3141 # from a Params::Validate::Strict
3142 # validate_strict() call in the method
3143 # body.
3144 #
3145 # Entry: $code - method body source string.
3146 #
3147 # Exit: Returns a schema hashref with input,
3148 # style, and source keys on success,
3149 # or undef if no validate_strict call
3150 # is found or parsing fails.
3151 #
3152 # Side effects: None.
3153 # --------------------------------------------------
3154 769 ▶sub _extract_pvs_schema {
Subroutine _extract_pvs_schema: called CC=15 SCAR=29.7
3155 769 my ($self, $code) = @_;
3156  
3157 769 ▶ return unless $code =~ /\bvalidate_strict\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\bvalidate_strict\s*\(/ 758 11 769
3158  
3159 11 ▶ my $doc = $self->_ppi($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->_ppi($code) or return 11 0 11
3160  
3161 23846 ▶ my $calls = $doc->find(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
3162 23846 ▶ $_[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" 1989 21857 23846
Condition100%6/6
$_[1]->isa('PPI::Token::Word') && ($_[1]->content eq 'validate_strict' || $_[1]->content eq 'Params::Validate::Strict::validate_strict')
!ll&&!rl&&r
21857197811
$_[1]->content eq "validate_strict" or $_[1]->content eq "Params::Validate::Strict::validate_strict"
l!l&&r!l&&!r
291978
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
3163 11 ▶ }) 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 11 0 11
3164  
3165 11 for my $call (@$calls) {
3166 11 my $list = $call->parent();
3167 11 ▶ while ($list && !$list->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and not $list->isa("PPI::Structure::List")) 58 11 69
Condition66%2/3
$list and not $list->isa("PPI::Structure::List")
!ll&&!rl&&r
11058
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
3168 58 $list = $list->parent();
3169 }
3170 11 ▶ if(!defined($list)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 11 0 11
3171 11 my $next = $call->next_sibling();
3172 11 ▶ next unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 0 11 11
3173 11 ▶ if($next->content() =~ /schema\s*=>\s*(\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->content =~ /schema\s*=>\s*(\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})/s) 11 0 11
3174 11 my $schema_text = $1;
3175 11 ▶ next if $schema_text =~ $UNSAFE_KEYWORD_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $schema_text =~ /$UNSAFE_KEYWORD_RE/ 0 11 11
3176 11 my $compartment = Safe->new();
3177 11 $compartment->permit_only(qw(:base_core :base_mem :base_orig));
3178  
3179 11 my $schema_str = "my \$schema = $schema_text";
3180 11 my $schema = $compartment->reval($schema_str);
3181 11 ▶ if($self->_usable_validator_schema($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_usable_validator_schema($schema)) 10 1 11
3182 return {
3183 10 input => $schema,
3184 style => 'hash',
3185 source => SOURCE_VALIDATOR
3186 }
3187 }
3188 }
3189 }
3190 1 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 1 0 1
3191  
3192 0 my ($schema_block) = grep { $_->isa('PPI::Structure::Block') } $list->children;
3193  
3194 0 ▶ next unless $schema_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_block 0 0 0
3195  
3196 0 my $schema = $self->_extract_schema_hash_from_block($schema_block);
3197 0 ▶ return $self->_normalize_validator_schema($schema) if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 0 0
3198 }
3199  
3200 1 return;
3201 }
3202  
3203 # --------------------------------------------------
3204 # _extract_pv_schema
3205 #
3206 # Purpose: Detect and extract a parameter schema
3207 # from a Params::Validate validate()
3208 # call in the method body.
3209 #
3210 # Entry: $code - method body source string.
3211 #
3212 # Exit: Returns a schema hashref with input,
3213 # style, and source keys on success,
3214 # or undef if no validate() call is
3215 # found or parsing fails.
3216 #
3217 # Side effects: None.
3218 # --------------------------------------------------
3219 759 ▶sub _extract_pv_schema {
Subroutine _extract_pv_schema: called CC=18 SCAR=33.7
3220 759 my ($self, $code) = @_;
3221  
3222 759 ▶ return unless $code =~ /\bvalidate\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\bvalidate\s*\(/ 758 1 759
3223  
3224 1 ▶ my $doc = $self->_ppi($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->_ppi($code) or return 1 0 1
3225  
3226 263 ▶ my $calls = $doc->find(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
3227 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
3228 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
3229  
3230 1 for my $call (@$calls) {
3231 1 my $list = $call->parent;
3232 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
3233 4 $list = $list->parent;
3234 }
3235 1 ▶ if(!defined($list)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 1 0 1
3236 1 my $next = $call->next_sibling();
3237 1 my ($arglist, $schema_text) = $self->_parse_pv_call($next);
3238  
3239 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
3240 1 my $compartment = Safe->new();
3241 1 $compartment->permit_only(qw(:base_core :base_mem :base_orig));
3242  
3243 1 my $schema_str = "my \$schema = $schema_text";
3244 1 my $schema = $compartment->reval($schema_str);
3245  
3246 1 ▶ if($self->_usable_validator_schema($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_usable_validator_schema($schema)) 1 0 1
3247 1 foreach my $arg(keys %{$schema}) {
3248 3 my $field = $schema->{$arg};
3249 3 ▶ if(my $type = $field->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $field->{"type"}) 3 0 3
3250 3 ▶ if($type eq 'ARRAYREF') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAYREF') { } 1 2 3
elsif ($type eq 'SCALAR') { } 2 0 2
3251 1 $field->{'type'} = 'arrayref';
3252 } elsif($type eq 'SCALAR') {
3253 2 $field->{'type'} = 'string';
3254 }
3255 }
3256 3 delete $field->{'callbacks'};
3257 }
3258  
3259 return {
3260 1 input => $schema,
3261 style => 'hash',
3262 source => SOURCE_VALIDATOR
3263 }
3264 }
3265 }
3266 }
3267 0 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
3268  
3269 0 my ($schema_block) = grep { $_->isa('PPI::Structure::Block') } $list->children;
3270  
3271 0 ▶ next unless $schema_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_block 0 0 0
3272  
3273 0 my $schema = $self->_extract_schema_hash_from_block($schema_block);
3274 0 ▶ return $self->_normalize_validator_schema($schema) if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 0 0
3275 }
3276  
3277 0 return;
3278 }
3279  
3280 # --------------------------------------------------
3281 # _parse_pv_call
3282 #
3283 # Purpose: Split a Params::Validate call argument
3284 # string into its two components: the
3285 # first argument (typically \@_) and
3286 # the schema hash string.
3287 #
3288 # Entry: $string - the raw argument string
3289 # from the validate() call,
3290 # including outer parentheses.
3291 #
3292 # Exit: Returns a two-element list:
3293 # ($first_arg, $hash_str)
3294 # or an empty list if no comma is found
3295 # at brace depth zero (malformed call).
3296 #
3297 # Side effects: None.
3298 # --------------------------------------------------
3299 2 ▶sub _parse_pv_call {
Subroutine _parse_pv_call: called CC=6 SCAR=19.0
3300 2 my ($self, $string) = @_;
3301  
3302 # Remove outer parentheses and whitespace
3303 2 $string =~ s/^\s*\(\s*//;
3304 2 $string =~ s/\s*\)\s*$//;
3305  
3306 # Find the first comma at brace-depth 0, jumping over each balanced
3307 # {...} block in one step via extract_bracketed rather than
3308 # counting depth character by character
3309 2 require Text::Balanced;
3310 2 my $rest = $string;
3311 2 my $comma_pos = 0;
3312 2 my $found_comma = 0;
3313  
3314 2 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 10 0 10
3315 10 ▶ if (substr($rest, 0, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) eq "{") 0 10 10
3316 # extract_bracketed advances $rest past the extracted block
3317 # in place, so $rest must not be re-truncated afterwards
3318 0 my $extracted = Text::Balanced::extract_bracketed($rest, '{}');
3319 0 ▶ return unless defined $extracted; # Broken source code
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 0 0
3320 0 $comma_pos += length $extracted;
3321 0 next;
3322 }
3323 10 ▶ if (substr($rest, 0, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) eq ",") 2 8 10
3324 2 $found_comma = 1;
3325 2 last;
3326 }
3327 8 $comma_pos++;
3328 8 $rest = substr($rest, 1);
3329 }
3330  
3331 2 ▶ return unless $found_comma;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_comma 0 2 2
3332  
3333 2 my $first_arg = substr($string, 0, $comma_pos);
3334 2 my $hash_str = substr($string, $comma_pos + 1);
3335  
3336 # Trim whitespace
3337 2 $first_arg =~ s/^\s+|\s+$//g;
3338 2 $hash_str =~ s/^\s+|\s+$//g;
3339  
3340 2 return ($first_arg, $hash_str);
3341 }
3342  
3343 # --------------------------------------------------
3344 # _extract_moosex_params_schema
3345 #
3346 # Purpose: Detect and extract a parameter schema
3347 # from a MooseX::Params::Validate
3348 # validated_hash() call in the method
3349 # body.
3350 #
3351 # Entry: $code - method body source string.
3352 #
3353 # Exit: Returns a schema hashref with input,
3354 # style, and source keys on success,
3355 # or undef if no validated_hash() call
3356 # is found or parsing fails.
3357 #
3358 # Side effects: None.
3359 # --------------------------------------------------
3360 sub _extract_moosex_params_schema
3361 758 ▶{
Subroutine _extract_moosex_params_schema: called CC=23 SCAR=36.9
3362 758 my ($self, $code) = @_;
3363  
3364 758 ▶ return unless $code =~ /\bvalidated_hash\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\bvalidated_hash\s*\(/ 757 1 758
3365  
3366 1 ▶ my $doc = $self->_ppi($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->_ppi($code) or return 1 0 1
3367  
3368 212 ▶ my $calls = $doc->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
3369 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
3370 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
3371  
3372 1 for my $call (@$calls) {
3373 1 my $list = $call->parent();
3374 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
3375 4 $list = $list->parent;
3376 }
3377 1 ▶ if(!defined($list)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 1 0 1
3378 1 my $next = $call->next_sibling();
3379 1 my ($arglist, $schema_text) = $self->_parse_pv_call($next);
3380  
3381 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
3382 1 my $compartment = Safe->new();
3383 1 $compartment->permit_only(qw(:base_core :base_mem :base_orig));
3384  
3385 1 my $schema_str = "my \$schema = { $schema_text }";
3386 1 $schema_str =~ s/ArrayRef\[(.+?)\]/arrayref, element_type => $1/g;
3387 1 my $schema = $compartment->reval($schema_str);
3388  
3389 1 ▶ if($self->_usable_validator_schema($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_usable_validator_schema($schema)) 1 0 1
3390 1 foreach my $arg(keys %{$schema}) {
3391 3 my $field = $schema->{$arg};
3392 3 ▶ if(my $isa = delete $field->{'isa'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $isa = delete $field->{"isa"}) 3 0 3
3393 3 $field->{'type'} = $isa;
3394 }
3395 3 ▶ if(exists($field->{'required'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{'required'}) { } 1 2 3
3396 1 my $required = delete $field->{'required'};
3397 1 ▶ $field->{'optional'} = $required ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$required ? : 1 0 1
3398 } else {
3399 2 $field->{'optional'} = 1;
3400 }
3401 3 ▶ if(ref($field->{'default'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field->{"default"} eq "CODE") 1 2 3
3402 1 delete $field->{'default'}; # TODO
3403 }
3404 }
3405  
3406 1 foreach my $arg(keys %{$schema}) {
3407 3 my $field = $schema->{$arg};
3408 3 ▶ if(my $type = $field->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $field->{"type"}) 3 0 3
3409 3 ▶ if($type eq 'ARRAYREF') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAYREF') { } 0 3 3
elsif ($type eq 'SCALAR') { } 0 3 3
3410 0 $field->{'type'} = 'arrayref';
3411 } elsif($type eq 'SCALAR') {
3412 0 $field->{'type'} = 'string';
3413 }
3414 }
3415 3 delete $field->{'callbacks'};
3416 }
3417  
3418 return {
3419 1 input => $schema,
3420 style => 'hash',
3421 source => SOURCE_VALIDATOR
3422 }
3423 }
3424 }
3425 }
3426 0 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
3427  
3428 0 my ($schema_block) = grep { $_->isa('PPI::Structure::Block') } $list->children;
3429  
3430 0 ▶ next unless $schema_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_block 0 0 0
3431  
3432 0 my $schema = $self->_extract_schema_hash_from_block($schema_block);
3433 0 ▶ return $self->_normalize_validator_schema($schema) if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 0 0
3434 }
3435  
3436 0 return;
3437 }
3438  
3439 # --------------------------------------------------
3440 # _extract_schema_hash_from_block
3441 #
3442 # Purpose: Extract a parameter schema hashref from
3443 # a PPI::Structure::Block node representing
3444 # the schema argument to a validator call
3445 # such as validate_strict({ ... }).
3446 #
3447 # Entry: $block - a PPI::Structure::Block node.
3448 #
3449 # Exit: Returns a hashref of parameter name to
3450 # spec hashref, or undef if parsing fails.
3451 #
3452 # Side effects: None.
3453 #
3454 # Notes: Delegates to _parse_schema_hash which
3455 # expects a PPI node with a children()
3456 # method. This method exists to provide
3457 # a clear semantic name at the call site.
3458 # --------------------------------------------------
3459 0 ▶sub _extract_schema_hash_from_block {
Subroutine _extract_schema_hash_from_block: not called CC=6 SCAR=37.4
3460 0 my ($self, $block) = @_;
3461  
3462 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
3463  
3464 0 my $result = $self->_parse_schema_hash($block);
3465  
3466 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
3467  
3468 0 return $result->{input};
3469 }
3470  
3471 # --------------------------------------------------
3472 # _usable_validator_schema
3473 #
3474 # Purpose: Decide whether a schema hash evaluated
3475 # from source code is a real parameter
3476 # schema, or the remains of a runtime
3477 # expression that was never meant to be
3478 # evaluated at all.
3479 #
3480 # Entry: $schema - value returned by
3481 # Safe->reval() on the schema
3482 # text found in the call.
3483 #
3484 # Exit: Returns true when the schema is a
3485 # non-empty hash of named parameters to
3486 # spec hashes, false otherwise.
3487 #
3488 # Side effects: None.
3489 #
3490 # Notes: CGI::Info::params validates each
3491 # parameter inside a loop with
3492 #
3493 # validate_strict({
3494 # schema => { $key => $schema },
3495 # args => { $key => $value },
3496 # });
3497 #
3498 # Evaluating { $key => $schema } with
3499 # both lexicals undefined yields
3500 # { '' => undef }. Accepting that as the
3501 # method's input gave the schema an empty
3502 # parameter name, which the harness
3503 # generator rightly refuses to turn into
3504 # a Perl variable, so no harness could be
3505 # generated for the routine at all.
3506 # --------------------------------------------------
3507 13 ▶sub _usable_validator_schema {
Subroutine _usable_validator_schema: called CC=6 SCAR=18.3
3508 13 my ($self, $schema) = @_;
3509  
3510 13 ▶ return 0 unless ref($schema) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 13 13
3511  
3512 13 my @names = keys %$schema;
3513 13 ▶ return 0 unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 13 13
3514  
3515 13 for my $name (@names) {
3516 40 ▶ return 0 unless length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 1 39 40
3517 39 ▶ return 0 unless ref($schema->{$name}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema->{$name} eq "HASH" 0 39 39
3518 }
3519  
3520 12 return 1;
3521 }
3522  
3523 # --------------------------------------------------
3524 # _normalize_validator_schema
3525 #
3526 # Purpose: Normalise a raw validator schema
3527 # hashref (as extracted from PPI) into
3528 # the standard input spec format used
3529 # throughout the extractor.
3530 #
3531 # Entry: $schema - hashref of parameter name
3532 # to raw spec hashref, as
3533 # produced by
3534 # _extract_schema_hash_from_block.
3535 #
3536 # Exit: Returns a hashref with keys:
3537 # input_style - 'hash'
3538 # input - normalised param specs
3539 # Each param spec gains an explicit
3540 # optional key and _source / _type_confidence
3541 # metadata.
3542 #
3543 # Side effects: None.
3544 # --------------------------------------------------
3545 0 ▶sub _normalize_validator_schema {
Subroutine _normalize_validator_schema: not called CC=3 SCAR=24.8
3546 0 my ($self, $schema) = @_;
3547  
3548 0 my %input;
3549  
3550 0 for my $name (keys %$schema) {
3551 0 my $spec = $schema->{$name};
3552  
3553 $input{$name} = {
3554 %$spec,
3555 0 ▶ optional => exists $spec->{optional} ? $spec->{optional} : 0,
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{'optional'} ? : 0 0 0
3556 _source => SOURCE_VALIDATOR,
3557 _type_confidence => 'high',
3558 };
3559 }
3560  
3561 return {
3562 0 input_style => 'hash',
3563 input => \%input,
3564 };
3565 }
3566  
3567 # --------------------------------------------------
3568 # _extract_type_params_schema
3569 #
3570 # Purpose: Detect and extract a parameter schema
3571 # from a Type::Params signature_for()
3572 # declaration for the current method,
3573 # located in the module-level document.
3574 #
3575 # Entry: $code - method body source string
3576 # (used to extract the function
3577 # name for lookup).
3578 #
3579 # Exit: Returns a schema hashref on success,
3580 # or undef if no signature_for
3581 # declaration is found or compilation
3582 # fails.
3583 #
3584 # Side effects: May fork a child process to compile
3585 # the signature in isolation.
3586 # --------------------------------------------------
3587 760 ▶sub _extract_type_params_schema {
Subroutine _extract_type_params_schema: called CC=6 SCAR=18.0
3588 760 my ($self, $code) = @_;
3589  
3590 760 ▶ my $function = $self->_extract_function_name($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $function = $self->_extract_function_name($code) or return 760 0 760
3591  
3592 760 ▶ my $doc = $self->{_document} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $self->{"_document"} or return 759 1 760
3593 759 ▶ 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 755 759
3594  
3595 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
3596  
3597 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
3598  
3599 3 return $self->_build_schema_from_meta($meta);
3600 }
3601  
3602 # --------------------------------------------------
3603 # _extract_function_name
3604 #
3605 # Purpose: Extract the subroutine name from the
3606 # start of a method body string, used
3607 # to look up its Type::Params signature.
3608 #
3609 # Entry: $code - method body source string.
3610 #
3611 # Exit: Returns the subroutine name string,
3612 # or undef if no 'sub name' declaration
3613 # is found.
3614 #
3615 # Side effects: None.
3616 # --------------------------------------------------
3617 761 ▶sub _extract_function_name {
Subroutine _extract_function_name: called CC=2 SCAR=6.9
3618 761 my ($self, $code) = @_;
3619 761 ▶ return $1 if $code =~ /^\s*sub\s+([a-zA-Z0-9_]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /^\s*sub\s+([a-zA-Z0-9_]+)/ 759 2 761
3620 2 return;
3621 }
3622  
3623 # --------------------------------------------------
3624 # _find_signature_statement
3625 #
3626 # Purpose: Search a PPI document for a
3627 # signature_for statement that
3628 # corresponds to a named function.
3629 #
3630 # Entry: $doc - PPI::Document to search.
3631 # $function - function name string.
3632 #
3633 # Exit: Returns the matching PPI::Statement
3634 # node, or undef if none is found.
3635 #
3636 # Side effects: None.
3637 # --------------------------------------------------
3638 757 ▶sub _find_signature_statement {
Subroutine _find_signature_statement: called CC=4 SCAR=14.0
3639 757 my ($self, $doc, $function) = @_;
3640  
3641 my $statements = $doc->find(
3642 2102451 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
3643 2102451 ▶ $_[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/ 216197 1886254 2102451
3644 }
3645 757 ▶ ) 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 754 757
3646  
3647 3 foreach my $stmt (@$statements) {
3648 3 my $content = $stmt->content;
3649 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
3650 3 return $stmt;
3651 }
3652 }
3653  
3654 0 return;
3655 }
3656  
3657 # --------------------------------------------------
3658 # _extract_signature_expression
3659 #
3660 # Purpose: Extract the Type::Params signature
3661 # expression (everything after =>) from
3662 # a signature_for statement node.
3663 #
3664 # Entry: $stmt - PPI::Statement node.
3665 # $function - function name string,
3666 # used in the match pattern.
3667 #
3668 # Exit: Returns the signature expression
3669 # string, or undef if the pattern
3670 # does not match.
3671 #
3672 # Side effects: None.
3673 # --------------------------------------------------
3674 3 ▶sub _extract_signature_expression {
Subroutine _extract_signature_expression: called CC=2 SCAR=7.4
3675 3 my ($self, $stmt, $function) = @_;
3676  
3677 3 my $content = $stmt->content;
3678  
3679 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
3680 3 return $1;
3681 }
3682  
3683 0 return;
3684 }
3685  
3686 # --------------------------------------------------
3687 # _compile_signature_isolated
3688 #
3689 # Purpose: Compile and evaluate a Type::Params
3690 # signature expression in an isolated
3691 # process to extract parameter metadata
3692 # without polluting the current process.
3693 #
3694 # Only runs when the caller passed
3695 # allow_signature_exec => 1 to new().
3696 # Extracting parameter types from a
3697 # Type::Params signature_for() declaration
3698 # requires actually building the type
3699 # objects at runtime -- there is no purely
3700 # static way to do it -- so this is real
3701 # execution of an excerpt of the target
3702 # module's own source. Every other code
3703 # path in this module is static (PPI-only)
3704 # analysis that never runs the target's
3705 # code, so this one feature must be opted
3706 # into explicitly rather than triggered
3707 # implicitly by extract_all().
3708 #
3709 # A Safe compartment was previously tried
3710 # first as a "fast path" before falling
3711 # back to this subprocess unconditionally.
3712 # It was removed: Type::Params and
3713 # Types::Common pull in XS modules (e.g.
3714 # B.pm via Type::Params), and Safe cannot
3715 # host XS/dynamic loading at all, so the
3716 # compartment never succeeded for any real
3717 # signature_for() declaration -- it was
3718 # dead code that gave a false impression of
3719 # sandboxing while every real call fell
3720 # through to the unconditional subprocess
3721 # below.
3722 #
3723 # Entry: $function - function name string.
3724 # $signature_expr - Type::Params
3725 # signature expression
3726 # string.
3727 #
3728 # Exit: Returns a decoded JSON hashref
3729 # containing parameters and returns
3730 # metadata on success.
3731 # Returns undef without running anything if
3732 # allow_signature_exec was not enabled.
3733 # Croaks on unsafe expressions, timeout,
3734 # or compile errors.
3735 #
3736 # Side effects: May fork a child process with a
3737 # memory limit applied via
3738 # BSD::Resource if available.
3739 # Memory limiting is best-effort and
3740 # silently skipped on platforms where
3741 # BSD::Resource is unavailable.
3742 # --------------------------------------------------
3743 12 ▶sub _compile_signature_isolated {
Subroutine _compile_signature_isolated: called CC=13 SCAR=26.2
3744 12 my ($self, $function, $signature_expr) = @_;
3745  
3746 12 ▶ unless ($self->{allow_signature_exec}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"allow_signature_exec"}) 2 10 12
3747 carp "Skipping Type::Params signature_for($function) extraction: ",
3748 'allow_signature_exec => 1 was not passed to new() ',
3749 '(this would execute code from the target module)'
3750 2 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
3751 2 return;
3752 }
3753  
3754 # Remove comments
3755 10 $signature_expr =~ s/#.*$//mg;
3756  
3757 # Reject obviously dangerous constructs. This is defense in depth
3758 # only, not a real security boundary -- it is a denylist of literal
3759 # tokens and cannot catch e.g. a symbolic-ref call built by string
3760 # concatenation. The actual control here is the allow_signature_exec
3761 # opt-in above: this code must never run against a module the caller
3762 # has not already decided to trust enough to execute.
3763 # Both checks unified into one croak so the message and class are consistent
3764 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
3765 0 croak 'Unsafe signature expression -- rejected to prevent code execution';
3766 }
3767  
3768 10 my $payload = <<'PERL';
3769 use strict;
3770 use warnings;
3771 use Type::Params -sigs;
3772 use Types::Common -types;
3773 use JSON::MaybeXS;
3774  
3775 # Apply address-space limit passed from parent via env. Done here (in the
3776 # child) rather than in the parent so the parent's memory is never capped.
3777 if (my $limit = $ENV{_ATG_RLIMIT_AS}) {
3778 eval {
3779 require BSD::Resource;
3780 BSD::Resource::setrlimit(BSD::Resource::RLIMIT_AS(), $limit, $limit);
3781 };
3782 }
3783  
3784 # Stub sub so Perl can parse it
3785 sub FUNCTION_NAME {}
3786  
3787 # Create the Type::Params signature object
3788 my $sig = signature_for FUNCTION_NAME => SIGNATURE_EXPR;
3789  
3790 # Extract parameters — guard against older Type::Params (< 2.x) where
3791 # signature_for() installs the constraint but returns undef rather than
3792 # the signature object, so ->parameters() would die.
3793 my @sig_params = (defined $sig && ref $sig && $sig->can('parameters'))
3794 ? @{ $sig->parameters || [] }
3795 : ();
3796 my $pos = 0;
3797 my @params;
3798  
3799 # if ($sig->method) {
3800 # The $self value
3801 # push @params, {
3802 # name => 'arg0',
3803 # optional => 0,
3804 # position => $pos++,
3805 # };
3806 # }
3807  
3808 for my $p (@sig_params) {
3809 my $name = ($p->can('name') && defined($p->name) && length($p->name))
3810 ? $p->name
3811 : "arg$pos";
3812 push @params, {
3813 name => $name,
3814 optional => $p->optional ? 1 : 0,
3815 position => $pos,
3816 type => $p->type->name
3817 };
3818 $pos++;
3819 }
3820  
3821 # Extract return type
3822 my $returns;
3823 if (my $r = $sig->returns_scalar) {
3824 $returns = {
3825 context => 'scalar',
3826 type => $r ? $r->name : 'unknown',
3827 };
3828 } elsif ($r = $sig->returns_list) {
3829 $returns = {
3830 context => 'list',
3831 type => $r ? $r->name : 'unknown',
3832 };
3833 }
3834  
3835 print encode_json({
3836 parameters => \@params,
3837 returns => $returns,
3838 });
3839 PERL
3840  
3841 # Substitute function name and signature expression
3842 10 $payload =~ s/FUNCTION_NAME/$function/g;
3843 10 $payload =~ s/SIGNATURE_EXPR/$signature_expr/;
3844  
3845 # Run in an isolated Perl process
3846 10 my ($wtr, $rdr, $err) = (undef, undef, gensym);
3847 10 local %ENV;
3848  
3849 # Pass the memory limit to the child via env so the child can apply
3850 # setrlimit on itself after exec. Must NOT call setrlimit here in the
3851 # parent: setrlimit(RLIMIT_AS) constrains the parent's own address
3852 # space, and when the parent later tries to allocate memory for test
3853 # framework teardown it would OOM and crash inside the Test::Builder
3854 # subtest context, producing a "context destroyed" error.
3855 10 $ENV{_ATG_RLIMIT_AS} = $MEMORY_LIMIT_BYTES;
3856  
3857 10 my $pid = open3($wtr, $rdr, $err, $^X, '-T');
3858  
3859 10 print $wtr $payload;
3860 10 close $wtr;
3861  
3862 0 ▶ local $SIG{ALRM} = sub { croak 'Signature compile timeout' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
3863 10 eval { alarm($SIGNATURE_TIMEOUT_SECS) }; # no-op on Windows
3864  
3865 10 my $stdout = do { local $/; <$rdr> };
3866 10 my $stderr = do { local $/; <$err> };
3867  
3868 10 eval { alarm 0 };
3869  
3870 10 waitpid($pid, 0);
3871  
3872 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
3873 1 ▶ carp "Error compiling signature:\n$stderr" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
3874 1 return;
3875 }
3876  
3877 # Child may be killed by the kernel OOM killer (SIGKILL) before it can
3878 # write anything to stdout or stderr. Guard both cases so we degrade
3879 # gracefully rather than croaking with "malformed JSON".
3880 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
3881 4 ▶ carp 'Signature subprocess produced no output' if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 4 4
3882 4 return;
3883 }
3884  
3885 5 my $result = eval { decode_json($stdout) };
3886 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 2 5
3887 3 ▶ carp "Error decoding signature output: $@" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 3 3
3888 3 return;
3889 }
3890 2 return $result;
3891 }
3892  
3893 # --------------------------------------------------
3894 # _build_schema_from_meta
3895 #
3896 # Purpose: Convert the parameter and return type
3897 # metadata produced by
3898 # _compile_signature_isolated into a
3899 # standard schema hashref.
3900 #
3901 # Entry: $meta - hashref with 'parameters'
3902 # arrayref and optional
3903 # 'returns' hashref, as decoded
3904 # from the isolated compile
3905 # JSON output.
3906 #
3907 # Exit: Returns a schema hashref with input,
3908 # output, style, source, _notes, and
3909 # _confidence keys.
3910 #
3911 # Side effects: None.
3912 #
3913 # Notes: Unknown Type::Params type names are
3914 # mapped to 'string' with a note added
3915 # and confidence downgraded to 'medium'.
3916 # --------------------------------------------------
3917 2 ▶sub _build_schema_from_meta {
Subroutine _build_schema_from_meta: called CC=9 SCAR=24.3
3918 2 my ($self, $meta) = @_;
3919  
3920 2 my %type_map = (
3921 Num => 'number',
3922 Int => 'integer',
3923 Str => 'string',
3924 Bool => 'boolean',
3925 Object => 'object',
3926 ArrayRef => 'array',
3927 HashRef => 'object',
3928 );
3929  
3930 2 my $input;
3931 2 my $position = 0;
3932 2 my $confidence = 'high';
3933 2 my @notes = ('Type::Params detected');
3934  
3935 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
3936 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
3937  
3938 4 ▶ if (!exists $type_map{$p->{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $type_map{$p->{"type"}}) 0 4 4
3939 0 push @notes, "Unknown type $p->{type}, defaulting to string";
3940 0 $confidence = 'medium';
3941 }
3942  
3943 $input->{"arg$position"} = {
3944 type => $type,
3945 position => $position,
3946 4 ▶ optional => $p->{optional} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$p->{'optional'} ? : 0 4 4
3947 };
3948  
3949 4 $position++;
3950 }
3951  
3952 2 my $output;
3953  
3954 2 ▶ if (my $ret = $meta->{returns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = $meta->{"returns"}) 2 0 2
3955 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
3956  
3957 2 ▶ if (!exists $type_map{$ret->{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $type_map{$ret->{"type"}}) 0 2 2
3958 0 push @notes, "Unknown return type $ret->{type}, defaulting to string";
3959 0 $confidence = 'medium';
3960 }
3961  
3962 $output = {
3963 2 type => $type,
3964 "_$ret->{context}_context" => { type => $type },
3965 };
3966 }
3967  
3968 return {
3969 2 input => $input,
3970 output => $output,
3971 style => 'hash',
3972 source => SOURCE_VALIDATOR,
3973 _notes => \@notes,
3974 _confidence => {
3975 input => $confidence,
3976 },
3977 };
3978 }
3979  
3980 # --------------------------------------------------
3981 # _analyze_pod
3982 #
3983 # Purpose: Parse POD documentation for a method
3984 # and extract parameter names, types,
3985 # constraints, and optionality from
3986 # multiple POD patterns.
3987 #
3988 # Entry: $pod - string of POD content as
3989 # returned by _extract_pod_before.
3990 # May be undef or empty.
3991 #
3992 # Exit: Returns a hashref of parameter name
3993 # to parameter spec hashref. Returns an
3994 # empty hashref if no POD is provided
3995 # or no parameters are found.
3996 #
3997 # Side effects: Carps when a semantic type is
3998 # detected, advising the caller to
3999 # set config->properties.
4000 # Logs progress to stdout when
4001 # verbose is set.
4002 #
4003 # Notes: Three pattern strategies are tried
4004 # in order: (1) named Parameters section,
4005 # (2) inline $name - type format,
4006 # (3) =over/=item list. Parameters found
4007 # earlier take precedence over later
4008 # discoveries. Default values from POD
4009 # are merged in last.
4010 # --------------------------------------------------
4011 773 ▶sub _analyze_pod {
Subroutine _analyze_pod: called CC=115 SCAR=59.7
4012 773 my ($self, $pod) = @_;
4013  
4014 773 ▶ return {} unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 252 521 773
4015  
4016 521 my %params;
4017 521 my $position_counter = 0;
4018  
4019 # Check for positional arguments in method signature
4020 # Pattern: =head2 method_name($arg1, $arg2, $arg3)
4021 521 ▶ if ($pod =~ /=head2\s+\w+\s*\(([^)]+)\)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=head2\s+\w+\s*\(([^)]+)\)/s) 34 487 521
4022 34 my $sig = $1;
4023 # Extract parameter names in order
4024 34 my @sig_params = $sig =~ /\$(\w+)/g;
4025  
4026 # Skip $self or $class
4027 34 ▶ 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 34 34
Condition33%1/3
@sig_params and $sig_params[0] =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
0340
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
4028  
4029 # Assign positions
4030 34 foreach my $param (@sig_params) {
4031 46 ▶ $params{$param}{position} //= $position_counter;
Condition33%1/3
$params{$param}{"position"} //= $position_counter
l!l&&r!l&&!r
0460
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
4032 46 $self->_log(" POD: $param has position $params{$param}{position}");
4033 46 $position_counter++;
4034 }
4035 }
4036  
4037 521 $self->_log(" POD: Found $position_counter unnamed parameters to add to the position list");
4038  
4039 # Pattern 1: Parse line-by-line in Parameters section
4040 # First, extract the Parameters section
4041 521 my $param_section;
4042 521 ▶ if($pod =~ /(?:Parameters?|Arguments?|Inputs?):?\s*\n((?:\s*\$.*\n)+)/si) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pod =~ /(?:Parameters?|Arguments?|Inputs?):?\s*\n((?:\s*\$.*\n)+)/is) { } 48 473 521
elsif ($pod =~ /^=head\d+\s+(?:Parameters?|Arguments?|Inputs?)\b.*?\n(.*?)(?=^=head|\Z)/ism) { } 400 73 473
4043 48 $param_section = $1;
4044 } elsif ($pod =~ /^=head\d+\s+(?:Parameters?|Arguments?|Inputs?)\b.*?\n(.*?)(?=^=head|\Z)/msi) {
4045 400 $param_section = $1;
4046 }
4047 521 ▶ if($param_section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_section) 448 73 521
4048 448 my $param_order = 0;
4049  
4050 448 $self->_log(" POD: Scan for named parameters in '$param_section'");
4051 # Now parse each line that starts with $varname
4052 448 foreach my $line (split /\n/, $param_section) {
4053 3343 ▶ if ($line =~ /C<\$(\w+)>\s*\((Required|Mandatory)\)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /C<\$(\w+)>\s*\((Required|Mandatory)\)/i) 0 3343 3343
4054 0 $params{$1}{optional} = 0;
4055 0 $self->_log(" POD: $1 marked required from item header");
4056 }
4057  
4058 # Match: $name - type (constraints), description
4059 # or: $name - type, description
4060 # or: $name - type
4061 3343 ▶ 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) 80 3263 3343
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
7283263
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
4062 ($line =~ /^\s*C<\$(\w+)>\s*-\s*(\w+)(?:\s*\(([^)]+)\))?\s*,?\s*(.*)$/i)) {
4063 80 my ($name, $type, $constraint, $desc) = ($1, lc($2), $3, $4);
4064  
4065 # Clean up
4066 80 ▶ $desc =~ s/^\s+|\s+$//g if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 72 8 80
4067  
4068 # Skip common non-parameters
4069 80 ▶ 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 80 80
4070  
4071 80 ▶ $params{$name} ||= { _source => SOURCE_POD };
Condition100%2/2
$params{$name} ||= {"_source", "pod"}
l!l
2555
MC/DC100%1/1
$params{$name} ||= {"_source", "pod"}
$params{$name}
Truth table100%2/2
A
A= $params{$name}
Aresult
11
00
4072  
4073 # If we haven't already assigned a position from the signature, use order in Parameters section
4074 80 ▶ unless (exists $params{$name}{position}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{$name}{"position"}) 55 25 80
4075 55 $params{$name}{position} = $param_order++;
4076 55 $self->_log(" POD: $name has position $params{$name}{position} (from Parameters order)");
4077 }
4078  
4079 # Normalize type names
4080 80 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 80 80
4081 80 ▶ $type = 'number' if $type eq 'num' || $type eq 'float';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "num" or $type eq "float" 0 80 80
Condition33%1/3
$type eq "num" or $type eq "float"
l!l&&r!l&&!r
0080
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
4082 80 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 80 80
4083 80 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 1 79 80
4084 80 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 80 80
4085  
4086 80 $params{$name}{type} = $type;
4087  
4088 # Parse constraints
4089 80 ▶ if($constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint) 9 71 80
4090 9 $self->_parse_constraints($params{$name}, $constraint);
4091 }
4092  
4093 # Check for optional/required in description OR constraint.
4094 # Use word boundaries to avoid matching "optionally" as "optional".
4095 80 ▶ my $full_text = ($constraint || '') . ' ' . ($desc || '');
Condition100%4/4
$constraint || ''
l!l
971
$desc || ''
l!l
728
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
4096 80 ▶ if ($full_text =~ /\boptional\b/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($full_text =~ /\boptional\b/i) { } 10 70 80
elsif ($full_text =~ /required|mandatory/i) { } 4 66 70
4097 10 $params{$name}{optional} = 1;
4098 10 $self->_log(" POD: $name marked as optional");
4099 } elsif ($full_text =~ /required|mandatory/i) {
4100 4 $params{$name}{optional} = 0;
4101 4 $self->_log(" POD: $name marked as required");
4102 }
4103  
4104 # Detect semantic types:
4105 80 ▶ 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 76 80
4106 # TODO: ensure properties is set to 1 in $config
4107 4 carp('Manually set config->properties to 1 in ', $self->{'input_file'});
4108 4 $params{$name}{semantic} = lc($1);
4109 }
4110  
4111 # Look for regex patterns
4112 80 ▶ if ($desc && $desc =~ m{matches?\s+(/[^/]+/|qr/.+?/)}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc and $desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i) 6 74 80
Condition100%3/3
$desc and $desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i
!ll&&!rl&&r
8666
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
4113 6 $params{$name}{matches} = $1;
4114 6 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4115 }
4116  
4117 80 ▶ if(my $values = $self->_prose_memberof($desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($desc)) 0 80 80
4118 0 $params{$name}{memberof} = $values;
4119 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4120 0 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4121 }
4122  
4123 80 ▶ $self->_log(" POD: Found parameter '$name' in parameters section, type=$type" .
Branch: 2 branches
BranchTrueFalseTotal
$constraint ? : 9 71 80
$desc ? : 72 8 80
4124 ($constraint ? " ($constraint)" : '') .
4125 ($desc ? " - $desc" : ''));
4126 }
4127 }
4128 }
4129  
4130 # Pattern 2: Also try the inline format in case Parameters: section wasn't found
4131 521 ▶ 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) 58 521 579
4132 58 my ($name, $type, $constraint, $desc) = ($1, lc($2), $3, $4);
4133  
4134 # Only process if we haven't already found this param in the Parameters section
4135 58 ▶ next if exists $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$name} 58 0 58
4136  
4137 # Clean up description - remove leading/trailing whitespace
4138 0 ▶ $desc =~ s/^\s+|\s+$//g if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 0 0 0
4139  
4140 # Skip common words that aren't parameters
4141 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
4142  
4143 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
4144  
4145 # Normalize type names
4146 0 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 0 0
4147 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
4148 0 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 0 0
4149 0 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 0 0
4150 0 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 0 0
4151  
4152 0 $params{$name}{type} = $type;
4153  
4154 # Parse constraints
4155 0 ▶ if ($constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint) 0 0 0
4156 0 $self->_parse_constraints($params{$name}, $constraint);
4157 }
4158  
4159 # Check for optional/required in description.
4160 # Use word boundaries to avoid matching "optionally" as "optional".
4161 0 ▶ if ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 0 0 0
4162 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
4163 0 $params{$name}{optional} = 1;
4164 } elsif ($desc =~ /required|mandatory/i) {
4165 0 $params{$name}{optional} = 0;
4166 }
4167  
4168 # Look for regex patterns in description
4169 0 ▶ if ($desc =~ m{matches?\s+(/[^/]+/|qr/.+?/)}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i) 0 0 0
4170 0 $params{$name}{matches} = $1;
4171 }
4172 }
4173  
4174 0 ▶ if(my $values = $self->_prose_memberof($desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($desc)) 0 0 0
4175 0 $params{$name}{memberof} = $values;
4176 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4177 0 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4178 }
4179  
4180 0 ▶ $self->_log(" POD: Found parameter '$name' in the inline documentation, type=$type" .
Branch: 1 branch
BranchTrueFalseTotal
$constraint ? : 0 0 0
4181 ($constraint ? " ($constraint)" : ''));
4182 }
4183  
4184 # Pattern 3: Parse =over /=item list of arguments, e.g. `=item * C<$name>`.
4185 # The sigil is required here: `=item * C<field>` without one usually
4186 # documents a returned key or an internal field, not an argument.
4187 #
4188 # Inside an Arguments section the sigil is optional: some modules write
4189 # `=item * C<mutation_results>` for a real argument. Add the sigil for
4190 # those items only, so a Returns list elsewhere still cannot name an
4191 # argument.
4192 521 my $item_pod = $pod;
4193 521 $item_pod =~ s{
4194 (?:^=head[2-6]\s+arguments?\b)
4195 (.*?)
4196 (?=^=head[1-6]\b|\z)
4197 }{
4198 287 my $section = $&;
4199 287 $section =~ s{^=item(\s+\*\s*)C<\s*([A-Za-z_]\w*)\s*>}{
4200 146 '=item' . $1 . 'C<$' . $2 . '>'
4201 }gme;
4202 287 $section;
4203 }msgeix;
4204  
4205 521 ▶ 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) 341 521 862
4206 341 my $name = $1;
4207 341 my $desc = $2;
4208  
4209 # Never allow empty or undefined parameter names
4210 341 ▶ next unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 341 341
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
00341
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
4211  
4212 341 $desc =~ s/^\s+|\s+$//g;
4213  
4214 # Skip common non-parameters
4215 341 ▶ 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 329 341
4216  
4217 329 ▶ $params{$name} ||= { _source => SOURCE_POD };
Condition100%2/2
$params{$name} ||= {"_source", "pod"}
l!l
2327
MC/DC100%1/1
$params{$name} ||= {"_source", "pod"}
$params{$name}
Truth table100%2/2
A
A= $params{$name}
Aresult
11
00
4218  
4219 # Explicit typed form only:
4220 # $param - type (constraints)
4221 329 ▶ 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 285 329
4222 44 my $type = lc($1);
4223 44 my $constraint = $2;
4224  
4225 # Normalize type names
4226 44 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 44 44
4227 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
4228 44 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 44 44
4229 44 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 44 44
4230 44 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 44 44
4231  
4232 44 $params{$name}{type} = $type;
4233  
4234 44 ▶ if ($constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint) 12 32 44
4235 12 $self->_parse_constraints($params{$name}, $constraint);
4236 }
4237  
4238 44 $self->_log(" POD: Explicit type '$type' for $name");
4239 } else {
4240 # Heuristic inference from description text
4241 285 ▶ if ($desc =~ /\bstring\b/i) {
Branch: 5 branches
BranchTrueFalseTotal
if ($desc =~ /\bstring\b/i) { } 24 261 285
elsif ($desc =~ /\b(int|integer)\b/i) { } 1 260 261
elsif ($desc =~ /\b(num|number|float)\b/i) { } 6 254 260
elsif ($desc =~ /\b(bool|boolean)\b/i) { } 0 254 254
elsif ($desc =~ /\bhashref\b/i) { } 66 188 254
4242 24 $params{$name}{type} = 'string';
4243 } elsif ($desc =~ /\b(int|integer)\b/i) {
4244 1 $params{$name}{type} = 'integer';
4245 } elsif ($desc =~ /\b(num|number|float)\b/i) {
4246 6 $params{$name}{type} = 'number';
4247 } elsif ($desc =~ /\b(bool|boolean)\b/i) {
4248 0 $params{$name}{type} = 'boolean';
4249 } elsif ($desc =~ /\bhashref\b/i) {
4250 # "either a path or an already decoded hashref": the harness
4251 # can build a hashref, so prefer it over the default string.
4252 66 $params{$name}{type} = 'hashref';
4253 }
4254 }
4255  
4256 # Check for optional/required in description.
4257 # Use word boundaries to avoid matching "optionally" as "optional".
4258 # Ignore code spans: a description that mentions the parameter name
4259 # C<optional> (CoverageGuidedFuzzer's schema) is not saying the
4260 # parameter itself is optional, and can still say "Required."
4261 329 my $optionality_text = $desc;
4262 329 $optionality_text =~ s/\b[CBIL]<[^>]*>//g;
4263 329 ▶ if ($optionality_text =~ /\boptional\b/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($optionality_text =~ /\boptional\b/i) { } 91 238 329
elsif ($optionality_text =~ /\b(?:required|mandatory)\b/i) { } 104 134 238
4264 91 $params{$name}{optional} = 1;
4265 } elsif ($optionality_text =~ /\b(?:required|mandatory)\b/i) {
4266 104 $params{$name}{optional} = 0;
4267 }
4268  
4269 # Look for regex patterns
4270 329 ▶ if ($desc =~ m{matches?\s+(/[^/]+/|qr/.+?/)}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ m[matches?\s+(/[^/]+/|qr/.+?/)]i) 0 329 329
4271 0 $params{$name}{matches} = $1;
4272 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4273 }
4274  
4275 329 ▶ if(my $values = $self->_prose_memberof($desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($desc)) 8 321 329
4276 8 $params{$name}{memberof} = $values;
4277 8 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4278 8 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4279 }
4280  
4281 329 $self->_log(" POD: Found parameter '$name' from =item list");
4282 }
4283  
4284 # Extract default values from POD
4285 521 my $pod_defaults = $self->_extract_defaults_from_pod($pod);
4286 521 foreach my $param (keys %$pod_defaults) {
4287 21 ▶ if (exists $params{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$param}) 15 6 21
4288 15 $params{$param}{_default} = $pod_defaults->{$param};
4289 15 ▶ $params{$param}{optional} = 1 unless defined $params{$param}{optional};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$param}{"optional"} 7 8 15
4290 $self->_log(sprintf(" POD: %s has default value: %s",
4291 $param,
4292 15 ▶ defined($pod_defaults->{$param}) ? $pod_defaults->{$param} : 'undef'
Branch: 1 branch
BranchTrueFalseTotal
defined $pod_defaults->{$param} ? : 14 1 15
4293 ));
4294 }
4295 }
4296  
4297 # Default undocumented optionality: documented params are REQUIRED unless stated otherwise
4298 521 for my $name (keys %params) {
4299 428 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 0 428 428
4300  
4301 # TODO: if optionality was never explicitly set, assume required.
4302 # Currently disabled as it breaks some schemas — revisit in a future pass.
4303 # if (!exists $params{$name}{optional}) {
4304 # $params{$name}{optional} = 0;
4305 # $self->_log(" POD: $name assumed required (no optional/default specified)");
4306 # }
4307 }
4308  
4309 # Pattern 0: =head3|4 Input formal spec — highest-priority type source.
4310 # Runs last so positional matching can use positions set by earlier patterns.
4311 # Accepts positional array format: [ {type=>'...'}, ... ]
4312 # and named hash format: { name => {type=>'...'}, ... }
4313 521 ▶ if ($pod =~ /=head[34]\s+Input\b(.*?)(?==head|\z)/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=head[34]\s+Input\b(.*?)(?==head|\z)/is) 369 152 521
4314 369 my $block = $1;
4315 369 $block =~ s/\A\s+//;
4316 # The spec may follow introductory prose and comment lines: start at
4317 # the first line that opens the structure.
4318 369 ▶ $block =~ s/\A.*?^[ \t]+(?=[\[\{])//ms unless($block =~ /\A[\[\{]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $block =~ /\A[\[\{]/ 16 353 369
4319  
4320 369 ▶ if ($block =~ /\A\[/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block =~ /\A\[/) { } 4 365 369
elsif ($block =~ /\A\{/) { } 350 15 365
4321 # Positional format: each {…} maps to the param at that array index.
4322 4 my $idx = 0;
4323 4 ▶ while ($block =~ /\{([^}]*)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /\{([^}]*)\}/g) 6 4 10
4324 6 my $spec = $1;
4325 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
4326 keys %params;
4327 6 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 6 6
4328 0 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4329 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
4330 0 $params{$name}{type} = $t;
4331 0 $self->_log(" POD: $name type '$t' from =head Input (positional $idx)");
4332 }
4333 0 ▶ if(defined(my $opt = _spec_optionality($spec))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $opt = _spec_optionality($spec))) 0 0 0
4334 0 $params{$name}{optional} = $opt;
4335 }
4336 }
4337 6 $idx++;
4338 }
4339 } elsif ($block =~ /\A\{/) {
4340 # Named format: each 'name => {…}' entry maps directly by name.
4341 350 ▶ while ($block =~ /\b(\w+)\s*=>\s*\{([^}]*)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /\b(\w+)\s*=>\s*\{([^}]*)\}/g) 582 350 932
4342 582 my ($name, $spec) = ($1, $2);
4343 582 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 210 372 582
4344 372 ▶ $params{$name} //= { _source => SOURCE_INPUT_SPEC };
Condition100%2/2
$params{$name} //= {"_source", "input_spec"}
l!l
29082
MC/DC100%1/1
$params{$name} //= {"_source", "input_spec"}
$params{$name}
Truth table100%2/2
A
A= $params{$name}
Aresult
11
00
4345 372 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4346 372 ▶ if (my $t = $self->_map_formal_input_type($spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->_map_formal_input_type($spec)) 372 0 372
4347 372 $params{$name}{type} = $t;
4348 372 $self->_log(" POD: $name type '$t' from =head Input (named)");
4349 }
4350 372 ▶ if(defined(my $opt = _spec_optionality($spec))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $opt = _spec_optionality($spec))) 192 180 372
4351 192 $params{$name}{optional} = $opt;
4352 }
4353 372 ▶ if ($spec =~ /\bmemberof\s*=>\s*\[([^\]]*)\]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bmemberof\s*=>\s*\[([^\]]*)\]/i) 4 368 372
4354 4 my $list_str = $1;
4355 4 my @vals;
4356 4 ▶ while ($list_str =~ /['"]([^'"]*)['"]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_str =~ /['"]([^'"]*)['"]/g) 8 4 12
4357 8 push @vals, $1;
4358 }
4359 4 ▶ if(@vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 4 0 4
4360 4 $params{$name}{memberof} = \@vals;
4361 # The list is documentation: the code may accept more.
4362 4 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4363 }
4364 }
4365 372 ▶ if ($spec =~ /\bmin\s*=>\s*(\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bmin\s*=>\s*(\d+)/i) 46 326 372
4366 46 $params{$name}{min} = $1 + 0;
4367 }
4368 372 ▶ if ($spec =~ /\bmax\s*=>\s*(\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bmax\s*=>\s*(\d+)/i) 29 343 372
4369 29 $params{$name}{max} = $1 + 0;
4370 }
4371 # A documented size is a claim about valid input, not a check
4372 # the routine has been seen to make: NameCase's spec says
4373 # min => 1 while its code accepts []. The harness still uses
4374 # the bound to build valid data, but generates no outside case.
4375 372 ▶ if(exists($params{$name}{min}) || exists($params{$name}{max})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$name}{"min"} or exists $params{$name}{"max"}) 59 313 372
Condition100%3/3
exists $params{$name}{"min"} or exists $params{$name}{"max"}
l!l&&r!l&&!r
4613313
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
4376 59 $params{$name}{_source} = SOURCE_INPUT_SPEC;
4377 }
4378 372 ▶ if ($spec =~ /\bisa\s*=>\s*['"]([^'"]+)['"]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\bisa\s*=>\s*['"]([^'"]+)['"]/i) 36 336 372
4379 36 $params{$name}{isa} = $1;
4380 }
4381 }
4382 # Two or more documented names signal a hash/named API, and
4383 # positional info from signature analysis is then not meaningful:
4384 # it causes "param X missing position" errors when params are
4385 # mixed. A lone name may instead label the first positional
4386 # argument, which _positional_pod_input decides once the body is
4387 # known.
4388 350 delete $params{$_}{position} for keys %params;
4389 }
4390  
4391 # Some input specs are a bare list of `name => { ... }` entries with no
4392 # outer braces, as Database::Join's new() has. Parsing the whole bare
4393 # list would read unrelated examples in the block, so only the
4394 # unambiguous required flag is taken from it; the parameter itself is
4395 # described by the validator, signature or prose. A structured spec
4396 # was read above, and only top-level entries count: a nested
4397 # properties entry's required flag belongs to that property.
4398 369 ▶ unless($block =~ /\A[\[\{]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block =~ /\A[\[\{]/) 15 354 369
4399 15 for my $entry (_top_level_hash_entries($block)) {
4400 2 my ($bare_name, $bare_spec) = @{$entry};
4401 2 ▶ next if $bare_name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $bare_name =~ /^(self|class|pkg|proto|klass)$/i 0 2 2
4402 2 my $own = _own_spec_text($bare_spec);
4403 2 ▶ next unless $own =~ /\brequired\s*=>\s*(0|1)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $own =~ /\brequired\s*=>\s*(0|1)/i 1 1 2
4404 1 ▶ next if $own =~ /\boptional\s*=>\s*(?:0|1)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $own =~ /\boptional\s*=>\s*(?:0|1)/i 0 1 1
4405 1 ▶ $params{$bare_name} //= { _source => SOURCE_INPUT_SPEC };
Condition50%1/2
$params{$bare_name} //= {"_source", "input_spec"}
l!l
01
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
4406 1 $params{$bare_name}{optional} = 1 - ($1 + 0);
4407 }
4408 }
4409 }
4410  
4411 # Documented value lists in =item prose, e.g.
4412 # =item * C<$colour> - one of red, green or blue
4413 # Run after the formal =head Input spec, which can be the only place a
4414 # parameter is named (strict_pod is such a parameter): the prose list
4415 # then still reaches it. Only parameters already found are updated:
4416 # a Returns section also uses =item, and its entries are outputs.
4417 521 ▶ 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) 442 521 963
4418 442 ▶ my ($name, $text) = ($1 // $2, $3);
Condition66%2/3
$1 // $2
l!l&&r!l&&!r
44110
MC/DC0%0/2
$1 // $2
$1 $2
Truth table66%2/3
A // B
A= $1B= $2
ABresult
1X1
011
000
4419 442 ▶ next unless $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{$name} 109 333 442
4420 333 ▶ next if defined($params{$name}{memberof});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{$name}{"memberof"} 8 325 333
4421  
4422 325 ▶ if(my $values = $self->_prose_memberof($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($text)) 0 325 325
4423 0 $params{$name}{memberof} = $values;
4424 0 $params{$name}{_source} = SOURCE_POD; # documentation, not a check
4425 0 $self->_log(" POD: $name memberof from prose: " . join(', ', @{$values}));
4426 }
4427 }
4428  
4429 521 $self->_sanitize_parameter_names(\%params, 'POD analysis');
4430 521 return \%params;
4431 }
4432  
4433 # --------------------------------------------------
4434 # _map_formal_input_type
4435 #
4436 # Purpose: Extract and normalise the type string
4437 # from a parameter spec fragment such as
4438 # "type => 'scalar | scalarref'".
4439 # Handles union types by returning the
4440 # canonical ATG type for the first
4441 # recognised alternative.
4442 #
4443 # Entry: $spec - text content of a { } block
4444 # from a =head3|4 Input spec.
4445 #
4446 # Exit: Canonical type string, or undef when
4447 # no 'type' key is present or the value
4448 # is not a recognised type name.
4449 # --------------------------------------------------
4450 377 ▶sub _map_formal_input_type {
Subroutine _map_formal_input_type: called CC=5 SCAR=16.1
4451 377 my ($self, $spec) = @_;
4452 # Accept both quoted type => 'scalar' and unquoted Params::Validate
4453 # constants type => OBJECT (no quotes around the constant name).
4454 377 ▶ return undef unless $spec =~ /\btype\s*=>\s*(?:['"]([^'"]+)['"]|([A-Z_]+))/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ /\btype\s*=>\s*(?:['"]([^'"]+)['"]|([A-Z_]+))/i 1 376 377
4455 376 ▶ my $raw = lc(defined($1) ? $1 : $2);
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 136 240 376
4456 376 $raw =~ s/\s+//g;
4457  
4458 376 my %map = (
4459 scalar => 'string',
4460 scalarref => 'string',
4461 str => 'string',
4462 string => 'string',
4463 int => 'integer',
4464 integer => 'integer',
4465 num => 'number',
4466 number => 'number',
4467 float => 'number',
4468 bool => 'boolean',
4469 boolean => 'boolean',
4470 array => 'arrayref',
4471 arrayref => 'arrayref',
4472 hash => 'hashref',
4473 hashref => 'hashref',
4474 object => 'object',
4475 any => 'any',
4476 undef => 'undef',
4477 coderef => 'coderef',
4478 );
4479  
4480 376 for my $t (split /\|/, $raw) {
4481 376 ▶ return $map{$t} if exists $map{$t};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map{$t} 375 1 376
4482 }
4483 1 return undef;
4484 }
4485  
4486 # --------------------------------------------------
4487 # _analyze_output
4488 #
4489 # Purpose: Orchestrate analysis of a method's
4490 # return value by combining POD return
4491 # section parsing, code return statement
4492 # analysis, boolean detection, context
4493 # detection, void detection, chaining
4494 # detection, and error convention
4495 # detection.
4496 #
4497 # Entry: $pod - POD string for the method.
4498 # $code - method body source string.
4499 # $method_name - name of the method being
4500 # analysed, used for
4501 # boolean heuristics.
4502 #
4503 # Exit: Returns a hashref describing the
4504 # output type and behaviour, or an empty
4505 # hashref if nothing could be determined.
4506 # Keys include: type, value, isa, and
4507 # various _* metadata keys.
4508 #
4509 # Side effects: Logs progress to stdout when
4510 # verbose is set.
4511 # --------------------------------------------------
4512 771 ▶sub _analyze_output {
Subroutine _analyze_output: called CC=3 SCAR=11.0
4513 771 my ($self, $pod, $code, $method_name) = @_;
4514  
4515 771 my %output;
4516  
4517 771 $self->_analyze_output_from_pod(\%output, $pod);
4518 771 $self->_analyze_output_from_code(\%output, $code, $method_name);
4519 771 $self->_enhance_boolean_detection(\%output, $pod, $code, $method_name);
4520 771 $self->_detect_list_context(\%output, $code);
4521 771 $self->_detect_void_context(\%output, $code, $method_name);
4522 771 $self->_detect_chaining_pattern(\%output, $code);
4523 771 $self->_detect_error_conventions(\%output, $code);
4524  
4525 771 ▶ $self->_validate_output(\%output) if keys %output;
Branch: 1 branch
BranchTrueFalseTotal
if keys %output 745 26 771
4526  
4527 # Don't return empty output
4528 771 ▶ return (keys %output) ? \%output : {};
Branch: 1 branch
BranchTrueFalseTotal
keys %output ? : 745 26 771
4529 }
4530  
4531 # --------------------------------------------------
4532 # _analyze_output_from_pod
4533 #
4534 # Purpose: Parse the POD documentation for a
4535 # method's return value and populate
4536 # an output hashref with type, value,
4537 # and behaviour information.
4538 #
4539 # Entry: $output - hashref to populate
4540 # (modified in place).
4541 # $pod - POD string for the method.
4542 #
4543 # Exit: Returns nothing. Modifies $output
4544 # in place.
4545 #
4546 # Side effects: Logs detections to stdout when
4547 # verbose is set.
4548 #
4549 # Notes: Two patterns are tried: (1) a
4550 # 'Returns:' section of up to 3 lines,
4551 # and (2) an inline 'returns X' phrase.
4552 # The section pattern takes precedence.
4553 # --------------------------------------------------
4554 769 ▶sub _analyze_output_from_pod {
Subroutine _analyze_output_from_pod: called CC=61 SCAR=54.0
4555 769 my ($self, $output, $pod) = @_;
4556 769 my %VALID_OUTPUT_TYPES = map { $_ => 1 }
4557 qw(string integer number float boolean arrayref hashref object coderef void undef);
4558  
4559 769 ▶ if ($pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod) 517 252 769
4560 # Pattern 0: =head4 Output formal spec (highest priority — explicit over heuristic)
4561 # The outer container shape determines the return type:
4562 # (...) — list/array of items
4563 # [...] — arrayref (bare [] = empty/void, skip)
4564 # {...} — hashref spec; look for type => inside, or isa => for object
4565 517 ▶ if($pod =~ /=head4\s+Output\b(.*?)(?==head|\z)/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /=head4\s+Output\b(.*?)(?==head|\z)/is) 359 158 517
4566 359 my $block = $1;
4567 359 $block =~ s/^\s+//;
4568 359 ▶ if($block =~ /^\(/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($block =~ /^\(/) { } 2 357 359
elsif ($block =~ /^\[/) { } 0 357 357
elsif ($block =~ /^\{/) { } 357 0 357
4569 2 $output->{type} = 'array';
4570 2 $self->_log(" OUTPUT: type 'array' from =head4 Output list notation");
4571 } elsif($block =~ /^\[/) {
4572 0 ▶ unless($block =~ /^\[\s*\]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block =~ /^\[\s*\]/) 0 0 0
4573 0 $output->{type} = 'arrayref';
4574 0 $self->_log(" OUTPUT: type 'arrayref' from =head4 Output arrayref notation");
4575 }
4576 } elsif($block =~ /^\{/) {
4577 357 ▶ if($block =~ /type\s*=>\s*['"]?(\w[\w:]*?)['"]?\s*[,}]/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block =~ /type\s*=>\s*['"]?(\w[\w:]*?)['"]?\s*[,}]/i) { } 354 3 357
elsif ($block =~ /\bisa\s*=>/) { } 0 3 3
4578 354 my $type = lc($1);
4579 354 ▶ $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" 97 257 354
Condition83%5/6
$type eq "scalar" or $type eq "scalarref" or $type eq "str"
l!l&&r!l&&!r
970257
$type eq "scalar" or $type eq "scalarref"
l!l&&r!l&&!r
943257
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
4580 354 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 354 354
4581 354 ▶ $type = 'number' if $type eq 'float' || $type eq 'num';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "float" or $type eq "num" 0 354 354
Condition33%1/3
$type eq "float" or $type eq "num"
l!l&&r!l&&!r
00354
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
4582 354 ▶ $type = 'boolean' if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 0 354 354
4583 354 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 354 354
4584 354 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 354 354
4585 354 ▶ if($VALID_OUTPUT_TYPES{$type}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($VALID_OUTPUT_TYPES{$type}) { } 354 0 354
elsif ($block =~ /\bisa\s*=>/) { } 0 0 0
4586 354 $output->{type} = $type;
4587 354 $self->_log(" OUTPUT: type '$type' from =head4 Output formal spec");
4588 } elsif($block =~ /\bisa\s*=>/) {
4589 0 $output->{type} = 'object';
4590 0 $self->_log(" OUTPUT: type 'object' from =head4 Output isa spec");
4591 }
4592 } elsif($block =~ /\bisa\s*=>/) {
4593 0 $output->{type} = 'object';
4594 0 $self->_log(" OUTPUT: type 'object' from =head4 Output isa spec");
4595 }
4596 }
4597 }
4598  
4599 # Pattern 1: Returns: section
4600 # Up to 3 lines
4601 517 ▶ 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 487 517
4602 30 my $returns_desc = $1;
4603 30 $returns_desc =~ s/^\s+|\s+$//g;
4604  
4605 30 $self->_log(" OUTPUT: Found Returns section: $returns_desc");
4606  
4607 # Try to infer type from description (skip if Pattern 0 already set type)
4608 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
4609 3 $output->{type} = 'string';
4610 ▶ } 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
4611 6 $output->{type} = 'integer';
4612 ▶ } 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
4613 2 $output->{type} = 'number';
4614 ▶ } 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
4615 7 $output->{type} = 'boolean';
4616 ▶ } 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
4617 0 $output->{type} = 'arrayref';
4618 ▶ } 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
4619 1 $output->{type} = 'hashref';
4620 ▶ } 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
4621 2 $output->{type} = 'object';
4622 ▶ } 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
4623 0 $output->{type} = 'undef';
4624 }
4625  
4626 # Look for specific values
4627 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
4628 0 $output->{value} = 1;
4629 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
4630 0 $output->{type} = 'boolean';
4631 } else {
4632 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
4633 }
4634 0 $self->_log(" OUTPUT: Returns 1 on success");
4635 } elsif ($returns_desc =~ /\b0\s+(?:on\s+failure|if\s+fail)\b/i) {
4636 0 $output->{alt_value} = 0;
4637 } elsif ($returns_desc =~ /dies\s+on\s+(?:error|failure)/i) {
4638 0 $output->{_STATUS} = 'LIVES';
4639 0 $self->_log(' OUTPUT: Should not die on success');
4640 }
4641 30 ▶ if ($returns_desc =~ /\b(true|false)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returns_desc =~ /\b(true|false)\b/i) 3 27 30
4642 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
4643 }
4644 30 ▶ if ($returns_desc =~ /\bundef\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returns_desc =~ /\bundef\b/i) 0 30 30
4645 0 $output->{optional} = 1;
4646 }
4647 }
4648  
4649 # Pattern 2: Inline "returns X"
4650 517 ▶ 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) 45 472 517
Condition100%3/3
not $output->{"type"} and $pod =~ /returns?\s+(?:an?\s+)?(\w+)/i
!ll&&!rl&&r
3779545
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
4651 45 my $type = lc($1);
4652  
4653 45 ▶ $type = 'boolean' if $type =~ /^(true|false|bool)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(true|false|bool)$/ 10 35 45
4654 # Skip if it's just a number (like "returns 1")
4655 45 ▶ $type = 'integer' if $type eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 0 45 45
4656 45 ▶ $type = 'number' if $type =~ /^(num|float)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(num|float)$/ 0 45 45
4657 45 ▶ $type = 'arrayref' if $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 45 45
4658 45 ▶ $type = 'hashref' if $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 45 45
4659  
4660 45 ▶ if($type =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^\d+$/) 0 45 45
4661 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
4662 # Try hard to guess if the result is a boolean
4663 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
4664 0 $type = 'boolean';
4665 } elsif($pod =~ /return 0 .+ 1 on success/) {
4666 0 $type = 'boolean';
4667 } else {
4668 0 $type = 'integer';
4669 }
4670 } else {
4671 0 $type = 'integer';
4672 }
4673 }
4674  
4675 45 ▶ $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 45 45
Condition33%1/3
not $type and $pod =~ /returns?\s+.+\slist\b/i
!ll&&!rl&&r
4500
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
4676 # $output->{type} = $type if $type && $type !~ /^\d+$/;
4677 45 ▶ if ($VALID_OUTPUT_TYPES{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VALID_OUTPUT_TYPES{$type}) { } 13 32 45
4678 13 $output->{type} = $type;
4679 13 $self->_log(" OUTPUT: Inferred type from POD: $type");
4680 } else {
4681 32 $self->_log(" OUTPUT: POD return type '$type' is not a valid type, ignoring");
4682 }
4683 }
4684 }
4685 }
4686  
4687 # --------------------------------------------------
4688 # _extract_defaults_from_pod
4689 #
4690 # Purpose: Extract default values for parameters
4691 # from POD documentation using multiple
4692 # pattern strategies.
4693 #
4694 # Entry: $pod - POD string for the method.
4695 # May be undef or empty.
4696 #
4697 # Exit: Returns a hashref of parameter name
4698 # to cleaned default value. Returns an
4699 # empty hashref if no POD is provided
4700 # or no defaults are found.
4701 #
4702 # Side effects: None.
4703 #
4704 # Notes: Three strategies are tried: (1) lines
4705 # containing 'Default:' or 'Defaults to:',
4706 # (2) lines containing 'Optional, default',
4707 # (3) inline $name - type, default value
4708 # format. Parameter names are inferred
4709 # by scanning backwards from the default
4710 # phrase to the nearest $variable.
4711 # --------------------------------------------------
4712 527 ▶sub _extract_defaults_from_pod {
Subroutine _extract_defaults_from_pod: called CC=9 SCAR=22.2
4713 527 my ($self, $pod) = @_;
4714  
4715 527 ▶ return {} unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 1 526 527
4716  
4717 526 my %defaults;
4718  
4719 # Pattern 1: Default: 'value' or Defaults to: 'value'
4720 526 ▶ 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 526 564
4721 38 my $default_text = $1;
4722 38 my $match_pos = pos($pod);
4723 38 $default_text =~ s/^\s+|\s+$//g;
4724  
4725 # Look backwards in the POD to find the parameter name
4726 38 my $context = substr($pod, 0, $match_pos);
4727 38 my @param_matches = ($context =~ /\$(\w+)/g);
4728 38 ▶ my $param = $param_matches[-1] if @param_matches; # Last parameter before default
Branch: 1 branch
BranchTrueFalseTotal
if @param_matches 38 0 38
4729  
4730 38 ▶ if ($param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) 38 0 38
4731 # Always clean the default value - let _clean_default_value handle everything
4732 38 ▶ if ($default_text =~ /(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_text =~ /(\w+)\s*=\s*(.+)$/) { } 0 38 38
4733 # Has explicit param = value format in the default text
4734 0 my ($p, $value) = ($1, $2);
4735 0 $defaults{$p} = $self->_clean_default_value($value);
4736 } else {
4737 # Just a value, associate with the found param
4738 38 $defaults{$param} = $self->_clean_default_value($default_text, 0); # NOT from code
4739 }
4740 }
4741 }
4742  
4743 # Pattern 2: Optional, default 'value'
4744 526 ▶ 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) 29 526 555
4745 29 my $default_text = $1;
4746 29 my $match_pos = pos($pod);
4747 29 $default_text =~ s/^\s+|\s+$//g;
4748  
4749 # Look backwards for parameter name
4750 29 my $context = substr($pod, 0, $match_pos);
4751 29 my @param_matches = ($context =~ /\$(\w+)/g);
4752 29 ▶ if (@param_matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param_matches) 29 0 29
4753 29 my $param = $param_matches[-1]; # Last parameter before the default
4754 29 $defaults{$param} = $self->_clean_default_value($default_text, 0);
4755 }
4756 }
4757  
4758 # Pattern 3: In parameter descriptions: $param - type, default 'value'
4759 526 ▶ 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 526 527
4760 1 my ($param, $value) = ($1, $2);
4761 1 $defaults{$param} = $self->_clean_default_value($value, 0);
4762 }
4763  
4764 526 return \%defaults;
4765 }
4766  
4767 # --------------------------------------------------
4768 # _analyze_output_from_code
4769 #
4770 # Purpose: Analyse return statements in a method
4771 # body to infer the output type by
4772 # counting and classifying each return
4773 # expression.
4774 #
4775 # Entry: $output - hashref to populate
4776 # (modified in place).
4777 # $code - method body source string.
4778 # $method_name - method name string.
4779 #
4780 # Exit: Returns nothing. Modifies $output
4781 # in place.
4782 #
4783 # Side effects: Logs detections to stdout when
4784 # verbose is set.
4785 # --------------------------------------------------
4786 sub _analyze_output_from_code
4787 769 ▶{
Subroutine _analyze_output_from_code: called CC=73 SCAR=48.7
4788 769 my ($self, $output, $code, $method_name) = @_;
4789  
4790 769 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 769 0 769
4791 # Early boolean detection - check for consistent 1/0 returns
4792 769 my @all_returns = $code =~ /return\s+([^;]+);/g;
4793 769 ▶ if (@all_returns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@all_returns) 671 98 769
4794 671 my $boolean_count = 0;
4795 671 my $total_count = scalar(@all_returns);
4796  
4797 671 foreach my $ret (@all_returns) {
4798 941 $ret =~ s/^\s+|\s+$//g;
4799 # Match 0 or 1, even with conditions
4800 941 ▶ $boolean_count++ if ($ret =~ /^(?:0|1)(?:\s|$)/);
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /^(?:0|1)(?:\s|$)/ 112 829 941
4801 }
4802  
4803 # If most returns are 0 or 1, strongly suggest boolean
4804 671 ▶ 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") 14 657 671
Condition100%3/3
$boolean_count >= 2 and $boolean_count >= $total_count * "0.8"
!ll&&!rl&&r
6431414
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
4805 14 ▶ unless ($output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->{"type"}) 4 10 14
4806 4 $output->{type} = 'boolean';
4807 4 $self->_log(" OUTPUT: Early detection - $boolean_count/$total_count returns are 0/1, setting boolean");
4808 }
4809 }
4810 }
4811  
4812 769 my @return_statements;
4813  
4814 769 ▶ 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 754 769
elsif ($code =~ /return\s+bless/s) { } 63 691 754
elsif ($code =~ /return\s*\(\s*[^)]+\s*,\s*[^)]+\s*\)\s*;/) { } 1 690 691
elsif ($code =~ /return\s+bless[^,]+,\s*__PACKAGE__/) { } 0 690 690
elsif ($code =~ /return\s*\(([^)]+)\)/) { } 11 679 690
elsif ($code =~ /return\s+\$self\s*;/ and $code =~ /\$self\s*->\s*\{[^}]+\}\s*=/) { } 11 1334 1345
4815 # Detect blessed refs
4816 15 $output->{type} = 'object';
4817 15 ▶ if($method_name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name eq 'new') { } 14 1 15
4818 # If we found the new() method, the object we're returning should be a sensible one
4819 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
4820 14 $output->{isa} = $package_stmt->namespace();
4821 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
4822 }
4823 } else {
4824 1 $output->{isa} = $1;
4825 }
4826 15 $self->_log(" OUTPUT: Bless found, inferring type from code is $output->{isa}");
4827 } elsif ($code =~ /return\s+bless/s) {
4828 63 $output->{type} = 'object';
4829 63 ▶ if($method_name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name eq 'new') { } 60 3 63
4830 60 $output->{isa} = $self->_extract_package_name();
4831 60 $self->_log(" OUTPUT: Bless found, inferring type from code is $output->{isa}");
4832 } else {
4833 3 $self->_log(' OUTPUT: Bless found, inferring type from code is object');
4834 }
4835 } elsif ($code =~ /return\s*\(\s*[^)]+\s*,\s*[^)]+\s*\)\s*;/) {
4836 # Detect array context returns - must end with semicolon to be actual return
4837 1 $output->{type} = 'array'; # Not arrayref - actual array
4838 1 $self->_log(' OUTPUT: Found array contect return');
4839 } elsif ($code =~ /return\s+bless[^,]+,\s*__PACKAGE__/) {
4840 # Detect: bless {}, __PACKAGE__
4841 0 $output->{type} = 'object';
4842 # Get package name from the extractor's stored document
4843 0 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 0 0 0
4844 0 my $pkg = $self->{_document}->find_first('PPI::Statement::Package');
4845 0 ▶ $output->{isa} = $pkg ? $pkg->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 0 0 0
4846 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
4847 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
4848 }
4849 } elsif ($code =~ /return\s*\(([^)]+)\)/) {
4850 11 my $content = $1;
4851 11 ▶ if ($content =~ /,/) { # Has comma = multiple values
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /,/) 6 5 11
4852 6 $output->{type} = 'array';
4853 }
4854 ▶ } 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
666211
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
4855 # Returns $self for chaining
4856 11 $output->{type} = 'object';
4857 11 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 11 0 11
4858 11 my $pkg = $self->{_document}->find_first('PPI::Statement::Package');
4859 11 ▶ $output->{isa} = $pkg ? $pkg->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 11 0 11
4860 11 ▶ $self->_log(' OUTPUT: Object chained into __PACKAGE__: ' . ($output->{isa} || 'UNKNOWN'));
Condition50%1/2
$output->{'isa'} || 'UNKNOWN'
l!l
110
MC/DC0%0/1
$output->{'isa'} || 'UNKNOWN'
$output->{'isa'}
Truth table50%1/2
A
A= $output->{'isa'}
Aresult
11
00
4861 11 ▶ $self->{_package_name} //= $output->{isa};
Condition33%1/3
$self->{"_package_name"} //= $output->{"isa"}
l!l&&r!l&&!r
1100
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
4862 }
4863 }
4864  
4865 # Find all return statements
4866 769 ▶ while ($code =~ /return\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+([^;]+);/g) 941 769 1710
4867 941 my $return_expr = $1;
4868 941 push @return_statements, $return_expr;
4869 }
4870  
4871 769 ▶ if (@return_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@return_statements) { } 671 98 769
4872 671 $self->_log(' OUTPUT: Found ' . scalar(@return_statements) . ' return statement(s)');
4873  
4874 # Analyze return patterns
4875 671 my %return_types;
4876  
4877 671 ▶ if($output->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"type"}) 356 315 671
4878 356 $return_types{$output->{'type'}} += 3; # Add weighting to what's already been found
4879 }
4880 671 my $min;
4881 671 foreach my $ret (@return_statements) {
4882 941 $ret =~ s/^\s+|\s+$//g;
4883  
4884 # Literal values
4885 941 ▶ if ($ret eq '1' || $ret eq '0') {
Branch: 16 branches
BranchTrueFalseTotal
if ($ret eq '1' or $ret eq '0') { } 69 872 941
elsif ($ret =~ /^['"]/) { } 54 818 872
elsif ($ret =~ /^-?\d+$/) { } 106 712 818
elsif ($ret =~ /^-?\d+\.\d+$/) { } 0 712 712
elsif ($ret eq 'undef') { } 1 711 712
elsif ($ret =~ /^\[/) { } 15 696 711
elsif ($ret =~ /^\{/) { } 20 676 696
elsif ($ret =~ m[ # Numeric expressions (heuristic, medium confidence) # Don't match -> (?: \+ | -\b | \* | / | % | \+\+ | -- ) ]x) { } 172 504 676
elsif ($ret =~ /\|\|\s*\d+\b/) { } 1 503 504
elsif ($ret =~ /^length[\s\(]/) { } 1 502 503
elsif ($ret =~ /^pos[\s\(]/) { } 0 502 502
elsif ($ret =~ /^index[\s\(]/) { } 0 502 502
elsif ($ret =~ /^rindex[\s\(]/) { } 0 502 502
elsif ($ret =~ /^ord[\s\(]/) { } 0 502 502
elsif ($ret =~ /=/ and $ret =~ /\$\w+/) { } 82 420 502
elsif ($ret =~ /\$\w+/) { } 356 64 420
Condition100%3/3
$ret eq 1 or $ret eq 0
l!l&&r!l&&!r
4425872
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
4886 69 $return_types{boolean}++;
4887 } elsif ($ret =~ /^['"]/) {
4888 54 $return_types{string}++;
4889 } elsif ($ret =~ /^-?\d+$/) {
4890 106 $return_types{integer}++;
4891 } elsif ($ret =~ /^-?\d+\.\d+$/) {
4892 0 $return_types{number}++;
4893 } elsif ($ret eq 'undef') {
4894 1 $return_types{undef}++;
4895 } elsif ($ret =~ /^\[/) {
4896 # Data structures
4897 15 $return_types{arrayref}++;
4898 } elsif ($ret =~ /^\{/) {
4899 20 $return_types{hashref}++;
4900 } elsif ($ret =~ m{
4901 # Numeric expressions (heuristic, medium confidence)
4902 # Don't match ->
4903 (?:
4904 \+ | -\b | \* | / | %
4905 | \+\+ | --
4906 )
4907 }x) {
4908 172 $return_types{number} += 2;
4909 } elsif ($ret =~ /\|\|\s*\d+\b/) {
4910 # Logical-or fallback with numeric literal (e.g. $x || 200)
4911 1 $return_types{integer} += 2;
4912 1 $self->_log(" OUTPUT: Numeric fallback expression detected");
4913 } elsif($ret =~ /^length[\s\(]/) {
4914 1 $return_types{integer}++;
4915 1 $min = 0;
4916 } elsif($ret =~ /^pos[\s\(]/) {
4917 0 $return_types{integer}++;
4918 0 $min = 0;
4919 } elsif($ret =~ /^index[\s\(]/) {
4920 0 $return_types{integer}++;
4921 0 $min = -1;
4922 } elsif($ret =~ /^rindex[\s\(]/) {
4923 0 $return_types{integer}++;
4924 0 $min = -1;
4925 } elsif($ret =~ /^ord[\s\(]/) {
4926 0 $return_types{integer}++;
4927 ▶ } elsif ($ret =~ /=/ && $ret =~ /\$\w+/) {
Condition100%3/3
$ret =~ /=/ and $ret =~ /\$\w+/
!ll&&!rl&&r
419182
MC/DC100%2/2
$ret =~ /=/ and $ret =~ /\$\w+/
$ret =~ /=/ $ret =~ /\$\w+/
Truth table100%3/3
A and B
A= $ret =~ /=/B= $ret =~ /\$\w+/
ABresult
0X0
100
111
4928 # Assignment returning a value (e.g. $self->{status} = $status)
4929 # If assignment involves a numeric literal or variable, assume numeric intent.
4930 # $1, $2 and friends are regex captures - strings, not numeric
4931 # literals - so drop them before looking for digits. CGI::Info::script_dir
4932 # returns "$self->{script_path} =~ $dir_regex ? $1 : ..." and the
4933 # "1" in $1 was being read as a numeric literal.
4934 82 (my $numeric_ret = $ret) =~ s/\$\d+//g;
4935 82 ▶ if ($numeric_ret =~ /\b\d+\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric_ret =~ /\b\d+\b/) { } 19 63 82
4936 19 $return_types{integer} += 2;
4937 19 $self->_log(" OUTPUT: Assignment with numeric value detected");
4938 } else {
4939 63 $return_types{scalar}++;
4940 }
4941 }
4942 # Variables/expressions
4943 elsif ($ret =~ /\$\w+/) {
4944 # \@_ is this routine's own argument list being passed
4945 # along, not an arrayref being returned. CGI::Info::get_cookie
4946 # returns $self->cookie(\@_) and was typed as returning an
4947 # arrayref because of it.
4948 356 (my $ref_ret = $ret) =~ s/\\\@_\b//g;
4949 356 ▶ if ($ref_ret =~ /\\\@/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($ref_ret =~ /\\\@/) { } 0 356 356
elsif ($ref_ret =~ /\\\%/) { } 0 356 356
elsif ($ret =~ /bless/) { } 22 334 356
elsif ($ret =~ /^\{[^}]*\}$/) { } 0 334 334
elsif ($ret =~ /^\[[^\]]*\]$/) { } 0 334 334
4950 0 $return_types{arrayref}++;
4951 } elsif ($ref_ret =~ /\\\%/) {
4952 0 $return_types{hashref}++;
4953 } elsif ($ret =~ /bless/) {
4954 22 $return_types{object} += 2; # Heigher weight
4955 } elsif ($ret =~ /^\{[^}]*\}$/) {
4956 0 $return_types{hashref}++;
4957 } elsif ($ret =~ /^\[[^\]]*\]$/) {
4958 0 $return_types{arrayref}++;
4959 } else {
4960 334 $return_types{scalar}++;
4961 }
4962 }
4963 }
4964  
4965 # Determine most common return type
4966 671 ▶ if (keys %return_types) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %return_types) 658 13 671
4967 658 my ($most_common) = sort { $return_types{$b} <=> $return_types{$a} } keys %return_types;
4968 # Prefer integer over scalar if numeric returns dominate
4969 658 ▶ if ($return_types{integer} && (!$return_types{string})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_types{"integer"} and not $return_types{"string"}) 121 537 658
Condition66%2/3
$return_types{"integer"} and not $return_types{"string"}
!ll&&!rl&&r
5370121
MC/DC50%1/2
$return_types{"integer"} and not $return_types{"string"}
$return_types{"integer"} not $return_types{"string"}
Truth table66%2/3
A and not(B)
A= $return_types{"integer"}B= not $return_types{"string"}
ABresult
0X0
100
111
4970 121 ▶ if (!$output->{type} || $output->{type} eq 'scalar') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $output->{"type"} or $output->{"type"} eq "scalar") 104 17 121
Condition66%2/3
not $output->{"type"} or $output->{"type"} eq "scalar"
l!l&&r!l&&!r
104017
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
4971 104 $output->{type} = 'integer';
4972 104 $self->_log(" OUTPUT: Numeric returns dominate, forcing integer");
4973 104 ▶ $output->{_type_confidence} ||= 'low';
Condition50%1/2
$output->{"_type_confidence"} ||= "low"
l!l
0104
MC/DC0%0/1
$output->{"_type_confidence"} ||= "low"
$output->{"_type_confidence"}
Truth table50%1/2
A
A= $output->{"_type_confidence"}
Aresult
11
00
4974 104 ▶ if(defined($min)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 1 103 104
4975 1 $output->{min} = $min;
4976 }
4977 }
4978 }
4979 658 ▶ unless ($output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->{"type"}) 198 460 658
4980 198 $output->{type} = $most_common;
4981  
4982 # Assign confidence for inferred numeric expressions
4983 198 ▶ if ($most_common eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($most_common eq "number") 33 165 198
4984 33 ▶ $output->{_type_confidence} ||= 'medium';
Condition50%1/2
$output->{"_type_confidence"} ||= "medium"
l!l
033
MC/DC0%0/1
$output->{"_type_confidence"} ||= "medium"
$output->{"_type_confidence"}
Truth table50%1/2
A
A= $output->{"_type_confidence"}
Aresult
11
00
4985 33 ▶ if(defined($min)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 0 33 33
4986 0 $output->{min} = $min;
4987 }
4988 }
4989  
4990 198 $self->_log(" OUTPUT: Inferred type from code: $most_common");
4991 }
4992 }
4993  
4994 # Check for consistent single value returns
4995 671 ▶ if (@return_statements == 1 && $return_statements[0] eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (@return_statements == 1 and $return_statements[0] eq 1) 31 640 671
Condition100%3/3
@return_statements == 1 and $return_statements[0] eq 1
!ll&&!rl&&r
10653431
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
4996 31 $output->{value} = 1;
4997 31 ▶ $output->{type} = 'boolean' if !$output->{type} || $output->{type} eq 'scalar';
Branch: 1 branch
BranchTrueFalseTotal
if not $output->{"type"} or $output->{"type"} eq "scalar" 0 31 31
Condition33%1/3
not $output->{"type"} or $output->{"type"} eq "scalar"
l!l&&r!l&&!r
0031
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
4998 31 ▶ $self->_log(" OUTPUT: Type already set to '$output->{type}', overriding with boolean") if($output->{'type'});
Branch: 1 branch
BranchTrueFalseTotal
if $output->{"type"} 31 0 31
4999 }
5000 } else {
5001 # No explicit return - might return nothing or implicit undef
5002 98 $self->_log(" OUTPUT: No explicit return statement found");
5003 }
5004 }
5005 }
5006  
5007 # --------------------------------------------------
5008 # _enhance_boolean_detection
5009 #
5010 # Purpose: Apply additional boolean-specific
5011 # detection heuristics using a weighted
5012 # scoring system, to override weak
5013 # type assignments when there is strong
5014 # evidence of a boolean return.
5015 #
5016 # Entry: $output - output hashref
5017 # (modified in place).
5018 # $pod - POD string.
5019 # $code - method body source string.
5020 # $method_name - method name string.
5021 #
5022 # Exit: Returns nothing. Modifies $output
5023 # in place, setting type to 'boolean'
5024 # if the score reaches
5025 # $BOOLEAN_SCORE_THRESHOLD.
5026 #
5027 # Side effects: Logs scoring details to stdout when
5028 # verbose is set.
5029 #
5030 # Notes: Only fires when output type is
5031 # not yet set or is 'unknown'. Does not
5032 # override explicitly set types.
5033 # --------------------------------------------------
5034 769 ▶sub _enhance_boolean_detection {
Subroutine _enhance_boolean_detection: called CC=22 SCAR=46.4
5035 769 my ($self, $output, $pod, $code, $method_name) = @_;
5036  
5037 769 my $boolean_score = 0; # Track evidence for boolean return
5038  
5039 769 ▶ return unless !$output->{type} || $output->{type} eq 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
unless not $output->{"type"} or $output->{"type"} eq "unknown" 736 33 769
Condition66%2/3
not $output->{"type"} or $output->{"type"} eq "unknown"
l!l&&r!l&&!r
330736
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
5040  
5041 # Look for stronger boolean indicators
5042 33 ▶ if ($pod && !$output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod and not $output->{"type"}) 9 24 33
Condition66%2/3
$pod and not $output->{"type"}
!ll&&!rl&&r
2409
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
5043 # Common boolean return patterns in POD
5044 9 ▶ 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 9 9
5045 0 $boolean_score += 30;
5046 0 $self->_log(' OUTPUT: Strong boolean indicator in POD (+30)');
5047 }
5048  
5049 # Check for method names that suggest boolean returns
5050 9 ▶ if ($pod =~ /(?:method|sub)\s+(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /(?:method|sub)\s+(\w+)/) 0 9 9
5051 0 my $inferred_method_name = $1;
5052 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
5053 0 $boolean_score += 20;
5054 0 $self->_log(" OUTPUT: Inferred method name '$inferred_method_name' suggests boolean return (+20)");
5055 }
5056 }
5057 }
5058  
5059 # Analyze code for boolean patterns
5060 33 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 33 0 33
5061 # Count boolean return idioms
5062 33 my $true_returns = () = $code =~ /return\s+1\s*;/g;
5063 33 my $false_returns = () = $code =~ /return\s+0\s*;/g;
5064  
5065 33 ▶ if ($true_returns + $false_returns >= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($true_returns + $false_returns >= 2) { } 0 33 33
elsif ($true_returns + $false_returns == 1) { } 0 33 33
5066 0 $boolean_score += 40;
5067 0 $self->_log(' OUTPUT: Multiple 1/0 returns suggest boolean (+40)');
5068 } elsif ($true_returns + $false_returns == 1) {
5069 0 $boolean_score += 10;
5070 0 $self->_log(' OUTPUT: Single 1/0 return (+10)');
5071 }
5072  
5073 # Ternary operators that return booleans
5074 33 ▶ 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 33 33
5075 0 $boolean_score += 25;
5076 0 $self->_log(' OUTPUT: Ternary with 1/0 suggests boolean (+25)');
5077 }
5078  
5079 # Check for common boolean method patterns
5080 33 ▶ if ($code =~ /return\s+[!\$\@\%]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+[!\$\@\%]/) 6 27 33
5081 # Returns negation or existence check
5082 6 $boolean_score += 15;
5083 6 $self->_log(' OUTPUT: Returns negation/existence check (+15)');
5084 }
5085 }
5086  
5087 # Check method name for boolean indicators
5088 33 ▶ if ($method_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name) 33 0 33
5089 33 ▶ 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 33 33
5090 0 $boolean_score += 25;
5091 0 $self->_log(" OUTPUT: Method name '$method_name' suggests boolean return (+25)");
5092 }
5093 33 ▶ if ($method_name =~ /_ok$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /_ok$/) 0 33 33
5094 0 $boolean_score += 30;
5095 0 $self->_log(" OUTPUT: Method name '$method_name' ends with '_ok' (+30)");
5096 }
5097 }
5098  
5099 # Apply boolean type if we have strong evidence
5100 # Override weak type assignments (like 'array' from false positive)
5101 33 ▶ if($boolean_score >= $BOOLEAN_SCORE_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean_score >= $BOOLEAN_SCORE_THRESHOLD) 0 33 33
5102 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
5103 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
5104 0 $output->{type} = 'boolean';
5105 0 $self->_log(" OUTPUT: Boolean score $boolean_score >= $BOOLEAN_SCORE_THRESHOLD, setting type to boolean (was: $old_type)");
5106 }
5107 }
5108 }
5109  
5110 # --------------------------------------------------
5111 # _detect_list_context
5112 #
5113 # Purpose: Detect methods that return different
5114 # values depending on calling context
5115 # via wantarray, and methods that
5116 # return explicit lists.
5117 #
5118 # Entry: $output - output hashref (modified
5119 # in place).
5120 # $code - method body source string.
5121 #
5122 # Exit: Returns nothing. Modifies $output
5123 # in place, setting _context_aware,
5124 # _list_context, _scalar_context,
5125 # _list_return, and/or type keys.
5126 #
5127 # Side effects: Logs detections to stdout when
5128 # verbose is set.
5129 # --------------------------------------------------
5130 769 ▶sub _detect_list_context {
Subroutine _detect_list_context: called CC=16 SCAR=27.9
5131 769 my ($self, $output, $code) = @_;
5132 769 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 769 769
5133  
5134 # Check for wantarray usage
5135 769 ▶ if ($code =~ /wantarray/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /wantarray/) 7 762 769
5136 7 $output->{_context_aware} = 1;
5137 7 $self->_log(' OUTPUT: Method uses wantarray - context sensitive');
5138  
5139 # Debug: show what we're matching against
5140 7 ▶ if ($code =~ /(wantarray[^;]+;)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(wantarray[^;]+;)/s) 6 1 7
5141 6 $self->_log(" DEBUG wantarray line: $1");
5142 }
5143  
5144 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
5145 # Pattern 1: wantarray ? (list, items) : scalar_value (with parens)
5146 1 my ($list_return, $scalar_return) = ($1, $2);
5147 1 $self->_log(" DEBUG list (with parens): [$list_return], scalar: [$scalar_return]");
5148  
5149 1 $output->{_list_context} = $self->_infer_type_from_expression($list_return);
5150 1 $output->{_scalar_context} = $self->_infer_type_from_expression($scalar_return);
5151 1 $self->_log(' OUTPUT: Detected context-dependent returns (parenthesized)');
5152 } elsif ($code =~ /wantarray\s*\?\s*([^:]+?)\s*:\s*([^;]+)/s) {
5153 # Pattern 2: wantarray ? @array : scalar (no parens around list)
5154 5 my ($list_return, $scalar_return) = ($1, $2);
5155 # Clean up
5156 5 $list_return =~ s/^\s+|\s+$//g;
5157 5 $scalar_return =~ s/^\s+|\s+$//g;
5158  
5159 5 $self->_log(" DEBUG list (no parens): [$list_return], scalar: [$scalar_return]");
5160  
5161 5 $output->{_list_context} = $self->_infer_type_from_expression($list_return);
5162 5 $output->{_scalar_context} = $self->_infer_type_from_expression($scalar_return);
5163 5 $self->_log(' OUTPUT: Detected context-dependent returns (non-parenthesized)');
5164 } elsif ($code =~ /return[^;]*unless\s+wantarray.*?return\s*\(([^)]+)\)/s) {
5165 # Pattern 3: return unless wantarray; return (list);
5166 1 $output->{_list_context} = { type => 'array' };
5167 1 $self->_log(' OUTPUT: Detected list context return after wantarray check');
5168 }
5169 }
5170  
5171 # Detect explicit list returns (multiple values in parentheses)
5172 # Avoid false positives from function calls
5173 769 ▶ if ($code =~ /return\s*\(\s*([^)]+)\s*\)\s*;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s*\(\s*([^)]+)\s*\)\s*;/) 2 767 769
5174 2 my $content = $1;
5175  
5176 # Count commas outside of nested structures, jumping over each
5177 # balanced bracketed block in one step via extract_bracketed
5178 2 require Text::Balanced;
5179 2 my $comma_count = 0;
5180 2 my $rest = $content;
5181 2 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 29 2 31
5182 29 ▶ if (substr($rest, 0, 1) =~ /[(\[{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) =~ /[(\[{]/) 4 25 29
5183 4 my $extracted = Text::Balanced::extract_bracketed($rest, '(){}[]');
5184 4 ▶ last unless defined $extracted; # Unbalanced brackets
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 4 4
5185 4 next;
5186 }
5187 25 ▶ $comma_count++ if substr($rest, 0, 1) eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if substr($rest, 0, 1) eq "," 2 23 25
5188 25 $rest = substr($rest, 1);
5189 }
5190  
5191 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
5192 # Multiple values returned
5193 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
5194 1 $output->{type} = 'array';
5195 1 $output->{_list_return} = $comma_count + 1;
5196 1 $self->_log(' OUTPUT: Returns list of ' . ($comma_count + 1) . ' values');
5197 }
5198 }
5199 }
5200 }
5201  
5202 # --------------------------------------------------
5203 # _detect_void_context
5204 #
5205 # Purpose: Detect methods that return nothing
5206 # meaningful (void context), methods
5207 # that always return 1 as a success
5208 # indicator, and methods whose name
5209 # suggests void context (setters,
5210 # mutators, loggers).
5211 #
5212 # Entry: $output - output hashref
5213 # (modified in place).
5214 # $code - method body source string.
5215 # $method_name - method name string.
5216 #
5217 # Exit: Returns nothing. Modifies $output
5218 # in place, setting _void_context,
5219 # _success_indicator, and/or type.
5220 #
5221 # Side effects: Logs detections to stdout when
5222 # verbose is set.
5223 # --------------------------------------------------
5224 769 ▶sub _detect_void_context {
Subroutine _detect_void_context: called CC=17 SCAR=28.3
5225 769 my ($self, $output, $code, $method_name) = @_;
5226 769 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 769 769
5227  
5228 769 $self->_log(" DEBUG _detect_void_context called for $method_name");
5229  
5230 # Methods that typically don't return meaningful values
5231 769 my $void_patterns = {
5232 'setter' => qr/^set_\w+$/,
5233 'mutator' => qr/^(?:add|remove|delete|clear|reset|update)_/,
5234 'logger' => qr/^(?:log|debug|warn|error|info)$/,
5235 'printer' => qr/^(?:print|say|dump)_/,
5236 };
5237  
5238 # Check if method name suggests void context
5239 769 foreach my $type (keys %$void_patterns) {
5240 3042 ▶ if ($method_name =~ $void_patterns->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /$void_patterns->{$type}/) 22 3020 3042
5241 22 $output->{_void_context_hint} = $type;
5242 22 $self->_log(" OUTPUT: Method name suggests $type (typically void context)");
5243 22 last;
5244 }
5245 }
5246  
5247 # Analyze return statements
5248 769 my @returns = $code =~ /return\s*([^;]*);/g;
5249  
5250 769 $self->_log(' DEBUG Found ' . scalar(@returns) . ' return statements');
5251  
5252 # Count different return patterns
5253 769 my $no_value_returns = 0;
5254 769 my $true_returns = 0;
5255 769 my $self_returns = 0;
5256  
5257 769 foreach my $ret (@returns) {
5258 1096 $ret =~ s/^\s+|\s+$//g;
5259 1096 $self->_log(" DEBUG return value: [$ret]");
5260 1096 ▶ $no_value_returns++ if $ret eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq "" 13 1083 1096
5261 1096 ▶ $no_value_returns++ if($ret =~ /^(if|unless)\s/);
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /^(if|unless)\s/ 10 1086 1096
5262 1096 ▶ $true_returns++ if $ret eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq 1 44 1052 1096
5263 1096 ▶ $self_returns++ if $ret eq '$self';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq "\$self" 13 1083 1096
5264 1096 ▶ if ($ret =~ /\?\s*1\s*:\s*0\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret =~ /\?\s*1\s*:\s*0\b/) 25 1071 1096
5265 # Strong boolean signal: ternary returning 1/0
5266 25 $true_returns++;
5267 # $self->_log(" OUTPUT: Ternary 1:0 return detected, treating as boolean (+40)");
5268 25 $self->_log(' OUTPUT: Ternary 1:0 return detected, treating as boolean');
5269 }
5270 }
5271  
5272 769 my $total_returns = scalar(@returns);
5273  
5274 769 $self->_log(" DEBUG no_value=$no_value_returns, true=$true_returns, self=$self_returns, total=$total_returns");
5275  
5276 # Void context indicators
5277 769 ▶ 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) { } 10 759 769
elsif ($true_returns > 0 and $true_returns == $total_returns and $total_returns >= 1) { } 47 1424 1471
Condition100%3/3
$no_value_returns > 0 and $no_value_returns == $total_returns
!ll&&!rl&&r
7481110
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
5278 10 $output->{_void_context} = 1;
5279 10 $output->{type} = 'void'; # This should override any previous type
5280 10 $self->_log(' OUTPUT: All returns are empty - void context method');
5281 ▶ } 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
712047
$true_returns > 0 and $true_returns == $total_returns
!ll&&!rl&&r
6902247
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
5282 # Methods that always return true (success indicator)
5283 47 $output->{_success_indicator} = 1;
5284 # Don't override type if already set to boolean
5285 47 ▶ unless ($output->{type} && $output->{type} eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->{"type"} and $output->{"type"} eq "boolean") 13 34 47
Condition66%2/3
$output->{"type"} and $output->{"type"} eq "boolean"
!ll&&!rl&&r
01334
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
5286 13 $output->{type} = 'boolean';
5287 }
5288 47 $self->_log(' OUTPUT: Always returns 1 - success indicator pattern');
5289 }
5290 }
5291  
5292 # --------------------------------------------------
5293 # _detect_chaining_pattern
5294 #
5295 # Purpose: Detect methods that return $self for
5296 # fluent interface chaining, by counting
5297 # the proportion of return statements
5298 # that return $self.
5299 #
5300 # Entry: $output - output hashref (modified
5301 # in place).
5302 # $code - method body source string.
5303 #
5304 # Exit: Returns nothing. Modifies $output
5305 # in place, setting type to 'object',
5306 # _returns_self to 1, and isa to the
5307 # current package name when the
5308 # proportion of $self returns is >= 0.8.
5309 #
5310 # Side effects: Logs detection to stdout when
5311 # verbose is set.
5312 # --------------------------------------------------
5313 769 ▶sub _detect_chaining_pattern {
Subroutine _detect_chaining_pattern: called CC=10 SCAR=23.4
5314 769 my ($self, $output, $code) = @_;
5315 769 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 769 769
5316  
5317 # Count returns of $self
5318 769 my $self_returns = 0;
5319 769 my $total_returns = 0;
5320  
5321 769 ▶ while ($code =~ /return\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+([^;]+);/g) 941 769 1710
5322 941 my $ret = $1;
5323 941 $ret =~ s/^\s+|\s+$//g;
5324 941 $total_returns++;
5325 941 ▶ $self_returns++ if $ret eq '$self';
Branch: 1 branch
BranchTrueFalseTotal
if $ret eq "\$self" 13 928 941
5326 }
5327  
5328 # If most/all returns are $self, it's a chaining method
5329 769 ▶ if ($self_returns > 0 && $total_returns > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_returns > 0 and $total_returns > 0) 13 756 769
Condition66%2/3
$self_returns > 0 and $total_returns > 0
!ll&&!rl&&r
756013
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
5330 13 my $ratio = $self_returns / $total_returns;
5331  
5332 13 ▶ if ($ratio >= 0.8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ratio >= "0.8") 11 2 13
5333 11 $output->{type} = 'object';
5334 11 $output->{_returns_self} = 1;
5335  
5336 # Get the class name
5337 11 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 11 0 11
5338 11 my $pkg = $self->{_document}->find_first('PPI::Statement::Package');
5339 11 ▶ $output->{isa} = $pkg ? $pkg->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 11 0 11
5340 11 ▶ $self->{_package_name} //= $output->{isa};
Condition33%1/3
$self->{"_package_name"} //= $output->{"isa"}
l!l&&r!l&&!r
1100
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
5341 }
5342  
5343 11 $self->_log(" OUTPUT: Chainable method - returns \$self ($self_returns/$total_returns returns)");
5344 }
5345 }
5346 }
5347  
5348 # --------------------------------------------------
5349 # _detect_error_conventions
5350 #
5351 # Purpose: Analyse how a method signals errors
5352 # by detecting patterns such as
5353 # 'return undef if', implicit bare
5354 # returns, empty list returns, 0/1
5355 # boolean error patterns, and eval
5356 # exception handling.
5357 #
5358 # Entry: $output - output hashref (modified
5359 # in place).
5360 # $code - method body source string.
5361 #
5362 # Exit: Returns nothing. Modifies $output
5363 # in place, setting _error_handling,
5364 # _error_return, and
5365 # _success_failure_pattern keys.
5366 #
5367 # Side effects: Logs detections to stdout when
5368 # verbose is set.
5369 # --------------------------------------------------
5370 769 ▶sub _detect_error_conventions {
Subroutine _detect_error_conventions: called CC=21 SCAR=30.5
5371 769 my ($self, $output, $code) = @_;
5372  
5373 769 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 769 769
5374  
5375 769 $self->_log(' DEBUG _detect_error_conventions called');
5376  
5377 769 my %error_patterns;
5378  
5379 # Pattern 1: return undef if/unless condition
5380 769 ▶ while ($code =~ /return\s+undef\s+(?:if|unless)\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+undef\s+(?:if|unless)\s+([^;]+);/g) 4 769 773
5381 4 push @{$error_patterns{undef_on_error}}, $1;
5382 4 $self->_log(" DEBUG Found 'return undef' pattern");
5383 }
5384  
5385 # Pattern 2: return if/unless (implicit undef)
5386 769 ▶ while ($code =~ /return\s+(?:if|unless)\s+([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+(?:if|unless)\s+([^;]+);/g) 13 769 782
5387 13 push @{$error_patterns{implicit_undef}}, $1;
5388 13 $self->_log(" DEBUG Found implicit undef pattern");
5389 }
5390  
5391 # Pattern 3: return () - matches with or without conditions
5392 769 ▶ if ($code =~ /return\s*\(\s*\)\s*(?:if|unless|;)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s*\(\s*\)\s*(?:if|unless|;)/) 3 766 769
5393 3 $error_patterns{empty_list} = 1;
5394 3 $self->_log(" DEBUG Found empty list return");
5395 }
5396  
5397 # Pattern 4: return 0/1 pattern (indicates boolean with error handling)
5398 769 my $zero_returns = 0;
5399 769 my $one_returns = 0;
5400 # Match "return 0" or "return 1" followed by anything (condition or semicolon)
5401 769 ▶ while ($code =~ /return\s+(0|1)\s*(?:;|if|unless)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /return\s+(0|1)\s*(?:;|if|unless)/g) 124 769 893
5402 124 ▶ if ($1 eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '0') { } 67 57 124
5403 67 $zero_returns++;
5404 } else {
5405 57 $one_returns++;
5406 }
5407 }
5408  
5409 769 ▶ if ($zero_returns > 0 && $one_returns > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zero_returns > 0 and $one_returns > 0) 15 754 769
Condition100%3/3
$zero_returns > 0 and $one_returns > 0
!ll&&!rl&&r
7302415
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
5410 15 $error_patterns{zero_on_error} = 1;
5411 15 $self->_log(" DEBUG Found 0/1 return pattern ($zero_returns zeros, $one_returns ones)");
5412 }
5413  
5414 # Pattern 5: Exception handling with eval
5415 769 ▶ if ($code =~ /eval\s*\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /eval\s*\{/) 30 739 769
5416 # Check if there's error handling after eval
5417 30 ▶ 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 10 30
5418 20 $error_patterns{exception_handling} = 1;
5419 20 $self->_log(' DEBUG Found exception handling with eval');
5420 }
5421 }
5422  
5423 # Detect success/failure return pattern
5424 769 my @all_returns = $code =~ /return\s+([^;]+);/g;
5425 769 my $has_undef = grep { /^\s*undef\s*(?:if|unless|$)/ } @all_returns;
5426 769 ▶ my $has_value = grep { !/^\s*undef\s*$/ && !/^\s*$/ } @all_returns;
Condition66%2/3
/^\s*undef\s*$/ || /^\s*$/
l!l&&r!l&&!r
10940
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
5427  
5428 769 ▶ if ($has_undef && $has_value && scalar(@all_returns) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_undef and $has_value and scalar @all_returns >= 2) 3 766 769
Condition66%4/6
$has_undef and $has_value and scalar @all_returns >= 2
!ll&&!rl&&r
76603
$has_undef and $has_value
!ll&&!rl&&r
76603
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
5429 3 $output->{_success_failure_pattern} = 1;
5430 3 $self->_log(" OUTPUT: Uses success/failure return pattern");
5431 }
5432  
5433 # Store error conventions in output
5434 769 ▶ if(scalar(keys %error_patterns)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %error_patterns) { } 48 721 769
5435 48 $output->{_error_handling} = \%error_patterns;
5436  
5437 # Determine primary error convention
5438 48 ▶ if ($error_patterns{undef_on_error}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($error_patterns{'undef_on_error'}) { } 2 46 48
elsif ($error_patterns{'implicit_undef'}) { } 12 34 46
elsif ($error_patterns{'empty_list'}) { } 3 31 34
elsif ($error_patterns{'zero_on_error'}) { } 15 16 31
5439 2 $output->{_error_return} = 'undef';
5440 2 $self->_log(" OUTPUT: Returns undef on error");
5441 } elsif ($error_patterns{implicit_undef}) {
5442 12 $output->{_error_return} = 'undef';
5443 12 $self->_log(" OUTPUT: Returns implicit undef on error");
5444 } elsif ($error_patterns{empty_list}) {
5445 3 $output->{_error_return} = 'empty_list';
5446 3 $self->_log(" OUTPUT: Returns empty list on error");
5447 } elsif ($error_patterns{zero_on_error}) {
5448 15 $output->{_error_return} = 'false';
5449 15 $self->_log(" OUTPUT: Returns 0/false on error");
5450 }
5451  
5452 48 ▶ if ($error_patterns{exception_handling}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_patterns{"exception_handling"}) 20 28 48
5453 20 $self->_log(" OUTPUT: Has exception handling");
5454 }
5455 } else {
5456 721 delete $output->{_error_handling};
5457 }
5458 }
5459  
5460 # --------------------------------------------------
5461 # _infer_type_from_expression
5462 #
5463 # Purpose: Infer the data type of a return
5464 # expression string by matching it
5465 # against common Perl literal and
5466 # variable patterns.
5467 #
5468 # Entry: $expr - return expression string,
5469 # trimmed of leading and
5470 # trailing whitespace.
5471 # May be undef.
5472 #
5473 # Exit: Returns a type hashref of the form
5474 # { type => '...' } and optionally
5475 # { min => N }. Defaults to
5476 # { type => 'scalar' } when no
5477 # pattern matches.
5478 #
5479 # Side effects: None.
5480 # --------------------------------------------------
5481 28 ▶sub _infer_type_from_expression {
Subroutine _infer_type_from_expression: called CC=25 SCAR=32.3
5482 28 my ($self, $expr) = @_;
5483  
5484 28 ▶ return { type => 'scalar' } unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 1 27 28
5485  
5486 27 $expr =~ s/^\s+|\s+$//g;
5487  
5488 # Check for multiple comma-separated values (indicates array/list)
5489 27 ▶ if ($expr =~ /,/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /,/) 3 24 27
5490 3 require Text::Balanced;
5491 3 my $comma_count = 0;
5492 3 my $rest = $expr;
5493 3 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 35 3 38
5494 35 ▶ if (substr($rest, 0, 1) =~ /[(\[{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) =~ /[(\[{]/) 4 31 35
5495 4 my $extracted = Text::Balanced::extract_bracketed($rest, '(){}[]');
5496 4 ▶ last unless defined $extracted; # Unbalanced brackets
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 4 4
5497 4 next;
5498 }
5499 31 ▶ $comma_count++ if substr($rest, 0, 1) eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if substr($rest, 0, 1) eq "," 3 28 31
5500 31 $rest = substr($rest, 1);
5501 }
5502  
5503 3 ▶ if ($comma_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma_count > 0) 2 1 3
5504 2 return { type => 'array' };
5505 }
5506 }
5507  
5508 # Check for @ prefix (array)
5509 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
5510 7 return { type => 'array' };
5511 }
5512  
5513 # Check for scalar() function - returns count
5514 18 ▶ if ($expr =~ /scalar\s*\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /scalar\s*\(/) 2 16 18
5515 2 return { type => 'integer', min => 0 };
5516 }
5517  
5518 # Check for array reference
5519 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
5520 3 return { type => 'arrayref' };
5521 }
5522  
5523 # Check for hash reference
5524 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
5525 2 return { type => 'hashref' };
5526 }
5527  
5528 # Check for hash
5529 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
5530 0 return { type => 'hash' };
5531 }
5532  
5533 # Check for strings
5534 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
5535 1 return { type => 'string' };
5536 }
5537  
5538 # Check for booleans first — must come before the integer check
5539 # since /^-?\d+$/ would otherwise match 0 and 1 as integers
5540 10 ▶ if($expr =~ /^[01]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^[01]$/) 2 8 10
5541 2 return { type => 'boolean' };
5542 }
5543  
5544 # Check for integers
5545 8 ▶ if($expr =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^-?\d+$/) 2 6 8
5546 2 return { type => 'integer' };
5547 }
5548  
5549 6 ▶ if ($expr =~ /^-?\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^-?\d+\.\d+$/) 1 5 6
5550 1 return { type => 'number' };
5551 }
5552  
5553 # Check for objects
5554 5 ▶ if ($expr =~ /bless/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /bless/) 0 5 5
5555 0 return { type => 'object' };
5556 }
5557  
5558 5 ▶ if($expr =~ /\blength\s*\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\blength\s*\(/) 1 4 5
5559 1 return { type => 'integer', min => 0 };
5560 }
5561  
5562 # Default to scalar
5563 4 return { type => 'scalar' };
5564 }
5565  
5566 # --------------------------------------------------
5567 # _detect_chaining_from_pod
5568 #
5569 # Purpose: Check POD documentation for explicit
5570 # indications that a method is chainable
5571 # or part of a fluent interface.
5572 #
5573 # Entry: $output - output hashref (modified
5574 # in place).
5575 # $pod - POD string for the method.
5576 #
5577 # Exit: Returns nothing. Sets _returns_self
5578 # in $output if chaining keywords are
5579 # found.
5580 #
5581 # Side effects: Logs detection to stdout when
5582 # verbose is set.
5583 # --------------------------------------------------
5584 0 ▶sub _detect_chaining_from_pod {
Subroutine _detect_chaining_from_pod: not called CC=6 SCAR=37.4
5585 0 my ($self, $output, $pod) = @_;
5586 0 ▶ return unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 0 0 0
5587  
5588 # Look for explicit chaining documentation
5589 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
5590 $pod =~ /chainable/i ||
5591 $pod =~ /fluent\s+interface/i ||
5592 $pod =~ /method\s+chaining/i) {
5593  
5594 0 $output->{_returns_self} = 1;
5595 0 $self->_log(" OUTPUT: POD indicates chainable/fluent interface");
5596 }
5597 }
5598  
5599 # --------------------------------------------------
5600 # _validate_output
5601 #
5602 # Purpose: Apply basic sanity checks to the
5603 # assembled output hashref and warn
5604 # about suspicious type combinations,
5605 # normalising clearly invalid types to
5606 # 'string'.
5607 #
5608 # Entry: $output - output hashref (modified
5609 # in place).
5610 #
5611 # Exit: Returns nothing. May modify type key
5612 # in $output. Logs warnings to stdout
5613 # when verbose is set.
5614 #
5615 # Side effects: None.
5616 # --------------------------------------------------
5617 744 ▶sub _validate_output {
Subroutine _validate_output: called CC=9 SCAR=22.2
5618 744 my ($self, $output) = @_;
5619  
5620 # Warn about suspicious combinations
5621 744 ▶ 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"}) 29 715 744
Condition100%6/6
defined $output->{"type"} and $output->{"type"} eq "boolean" and not defined $output->{"value"}
!ll&&!rl&&r
6843129
defined $output->{"type"} and $output->{"type"} eq "boolean"
!ll&&!rl&&r
567960
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
5622 29 $self->_log(' WARNING Boolean type without value - may want to set value: 1');
5623 }
5624 744 ▶ 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 744 744
Condition66%4/6
$output->{"value"} and defined $output->{"type"} and $output->{"type"} ne "boolean"
!ll&&!rl&&r
713310
$output->{"value"} and defined $output->{"type"}
!ll&&!rl&&r
713031
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
5625 0 $self->_log(" WARNING Value set but type is not boolean: $output->{type}");
5626 }
5627 744 my %valid_types = map { $_ => 1 } qw(string integer number boolean array arrayref hashref object void);
5628 744 ▶ if(exists $output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $output->{"type"}) 739 5 744
5629 739 ▶ if(!$valid_types{$output->{type}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $valid_types{$output->{"type"}}) 120 619 739
5630 120 $self->_log(" WARNING Output value type is unknown: '$output->{type}', setting to string");
5631 120 $output->{type} = 'string';
5632 }
5633 }
5634 }
5635  
5636 # --------------------------------------------------
5637 # _prose_memberof
5638 #
5639 # Purpose: Recognise a documented list of allowed values written as
5640 # prose, such as "one of red, green or blue", "must be one
5641 # of C<low>, C<moderate> or C<high>", "valid values are 1,
5642 # 5 and 111" or "either C<host> or C<port>".
5643 # The list is documentation, not a check the routine was
5644 # seen to make, so callers mark the parameter
5645 # _source => 'pod': the harness uses the values as valid
5646 # data and generates no must-die case outside the set.
5647 # Entry: $text - a parameter description or constraint, possibly
5648 # containing POD formatting codes.
5649 # Exit: Arrayref of the documented values, or undef when the text
5650 # holds no recognisable list. Every value must be a single
5651 # token, so prose like "one of the four recognised keys set
5652 # to a true value also adds ..." is not mistaken for a list.
5653 # --------------------------------------------------
5654 764 ▶sub _prose_memberof {
Subroutine _prose_memberof: called CC=10 SCAR=23.0
5655 764 my ($self, $text) = @_;
5656  
5657 764 ▶ return undef unless defined($text) && length($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 37 727 764
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
037727
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
5658  
5659 727 my $plain = $text;
5660 727 $plain =~ s/\b[CLIB]<([^<>]*)>/$1/g; # drop POD formatting
5661 727 $plain =~ s/\bE<([^<>]*)>/$1/g;
5662 727 $plain =~ s/\A\s+//;
5663 727 $plain =~ s/\s+\z//;
5664  
5665 727 my $list;
5666 727 ▶ 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) { } 6 721 727
elsif ($plain =~ /\b(?:must\s+be\s+)?either\s+(.+)$/i) { } 1 720 721
elsif ($plain =~ /\b(?:also\s+)?accepts\s+the\s+strings?\s+(.+)$/i) { } 4 716 720
5667 6 $list = $1;
5668 } elsif($plain =~ /\b(?:must\s+be\s+)?either\s+(.+)$/i) {
5669 1 $list = $1;
5670 } elsif($plain =~ /\b(?:also\s+)?accepts\s+the\s+strings?\s+(.+)$/i) {
5671 # "Also accepts the strings off, warn, and fatal."
5672 4 $list = $1;
5673 } else {
5674 716 return undef;
5675 }
5676  
5677 11 $list =~ s/\.(?:\s|\z).*\z//s; # stop at the end of the sentence
5678 11 $list =~ s/\s*\.\s*\z//;
5679 11 $list =~ s/\s+\z//;
5680  
5681 11 my @values = grep { length } split(/\s*,\s*(?:(?:and|or)\s+)?|\s+(?:and|or)\s+/i, $list);
5682 11 ▶ return undef if(@values < 2 || @values > 20);
Branch: 1 branch
BranchTrueFalseTotal
if @values < 2 or @values > 20 2 9 11
Condition66%2/3
@values < 2 or @values > 20
l!l&&r!l&&!r
209
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
5683  
5684 9 for my $value (@values) {
5685 24 $value =~ s/\A\s+|\s+\z//g;
5686 24 $value =~ s/\A['"]|['"]\z//g;
5687 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
5688 }
5689  
5690 8 return \@values;
5691 }
5692  
5693 # --------------------------------------------------
5694 # _parse_constraints
5695 #
5696 # Purpose: Parse a constraint string extracted
5697 # from POD documentation and populate
5698 # min, max, or other constraint fields
5699 # in a parameter hashref.
5700 #
5701 # Entry: $param - hashref for the parameter
5702 # being annotated (modified
5703 # in place).
5704 # $constraint - the constraint string,
5705 # e.g. '3-50', 'positive',
5706 # '>= 0', 'min 3'.
5707 #
5708 # Exit: Returns nothing. Modifies $param in
5709 # place by setting min and/or max keys.
5710 #
5711 # Side effects: Logs min/max values to stdout when
5712 # verbose is set, and marks the constraints as
5713 # documented (documentation only).
5714 # --------------------------------------------------
5715 30 ▶sub _parse_constraints {
Subroutine _parse_constraints: called CC=23 SCAR=34.6
5716 30 my ($self, $param, $constraint) = @_;
5717  
5718 # A documented list of values, e.g. "(one of red, green)"
5719 30 ▶ if(my $values = $self->_prose_memberof($constraint)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $self->_prose_memberof($constraint)) 0 30 30
5720 0 $param->{memberof} = $values;
5721 }
5722  
5723 # Range: "3-50" or "1-100 chars"
5724 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
5725 6 $param->{min} = $1;
5726 6 $param->{max} = $2;
5727 }
5728 elsif ($constraint =~ /(\d+)\s*\.\.\s*(\d+)/) {
5729 # Range: 0..19
5730 1 $param->{min} = $1;
5731 1 $param->{max} = $2;
5732 }
5733 # Minimum: "min 3" or "at least 5"
5734 elsif ($constraint =~ /(?:min|minimum|at least)\s*(\d+)/i) {
5735 4 $param->{min} = $1;
5736 }
5737 # Maximum: "max 50" or "up to 100"
5738 elsif ($constraint =~ /(?:max|maximum|up to)\s*(\d+)/i) {
5739 3 $param->{max} = $1;
5740 }
5741 # Positive
5742 elsif ($constraint =~ /positive/i) {
5743 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
5744 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
5745 }
5746 # Non-negative
5747 elsif ($constraint =~ /non-negative/i) {
5748 1 $param->{min} = 0;
5749 } elsif($constraint =~ /^(\S+)\s+(.+)$/) {
5750 8 my ($op, $val) = ($1, $2);
5751 8 ▶ if(looks_like_number($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($val)) 0 8 8
5752 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
5753 0 $param->{max} = $val - 1;
5754 } elsif ($op eq '<=') {
5755 0 $param->{max} = $val;
5756 } elsif ($op eq '>') {
5757 0 $param->{min} = $val + 1;
5758 } elsif ($op eq '>=') {
5759 0 $param->{min} = $val;
5760 }
5761 }
5762 }
5763  
5764 30 ▶ if(defined($param->{max})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"max"}) 10 20 30
5765 10 $self->_log(" Set max to $param->{max}");
5766 }
5767 30 ▶ if(defined($param->{min})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"min"}) 14 16 30
5768 14 $self->_log(" Set min to $param->{min}");
5769 }
5770  
5771 # These constraints come from prose ("3-50 chars", "at least 5"): they
5772 # describe valid input rather than a check the code was seen to make, so
5773 # the harness uses them as data hints and expects no croak outside them.
5774 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
5775 17 $param->{_source} = SOURCE_POD;
5776 }
5777 }
5778  
5779 # --------------------------------------------------
5780 # _analyze_code
5781 #
5782 # Purpose: Analyse a method's source code using
5783 # pattern matching to infer parameter
5784 # names, types, constraints, defaults,
5785 # and optionality. Orchestrates all
5786 # per-parameter code analysis helpers.
5787 #
5788 # Entry: $code - method body source string.
5789 # $method - method hashref (used for
5790 # constructor-specific logic
5791 # when extracting parameters
5792 # from @_ patterns).
5793 #
5794 # Exit: Returns a hashref of parameter name
5795 # to parameter spec hashref, with as
5796 # much type and constraint information
5797 # as could be inferred from the code.
5798 #
5799 # Side effects: Logs progress and warnings to stdout
5800 # when verbose is set.
5801 #
5802 # Notes: Analysis is capped at max_parameters
5803 # to prevent runaway processing on
5804 # pathological methods. Falls back to
5805 # classic @_ extraction if signature
5806 # extraction found no parameters.
5807 # --------------------------------------------------
5808 775 ▶sub _analyze_code {
Subroutine _analyze_code: called CC=27 SCAR=38.5
5809 775 my ($self, $code, $method) = @_;
5810  
5811 775 my %params;
5812  
5813 # Safety check - limit parameter analysis to prevent runaway processing
5814 775 my $param_count = 0;
5815  
5816 # Extract parameter names from various signature styles
5817 775 $self->_extract_parameters_from_signature(\%params, $code);
5818  
5819 # Params::Get: get_params('key', \@_) passes the param name as a string,
5820 # not as a $var in the signature, so run this unconditionally as a second
5821 # pass after the early-returning signature parsers have finished.
5822 775 ▶ if($code =~ /Params::Get/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /Params::Get/) 13 762 775
5823 13 my $pos = scalar keys %params;
5824 13 ▶ while($code =~ /get_params\s*\(\s*['"](\w+)['"]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /get_params\s*\(\s*['"](\w+)['"]/g) 7 13 20
5825 7 my $name = $1;
5826 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
5827 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
5828 7 $self->_log(" CODE: Found Params::Get parameter '$name'");
5829 }
5830 }
5831  
5832 775 $self->_extract_defaults_from_code(\%params, $code, $method);
5833  
5834 # Infer types from defaults
5835 775 foreach my $param (keys %params) {
5836 510 ▶ if ($params{$param}{_default} && !$params{$param}{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$param}{"_default"} and not $params{$param}{"type"}) 33 477 510
Condition66%2/3
$params{$param}{"_default"} and not $params{$param}{"type"}
!ll&&!rl&&r
477033
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
5837 33 my $default = $params{$param}{_default};
5838 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
5839 6 $params{$param}{type} = 'hashref';
5840 6 $self->_log(" CODE: $param type inferred as hashref from default");
5841 } elsif (ref($default) eq 'ARRAY') {
5842 2 $params{$param}{type} = 'arrayref';
5843 2 $self->_log(" CODE: $param type inferred as arrayref from default");
5844 }
5845 }
5846 }
5847  
5848 775 ▶ 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 775 775
elsif ($code =~ /(?:croak|die)\(.*\)\s+if\s*\(\s*scalar\(\@_\)\s*==\s*0\s*\)/s) { } 0 775 775
5849 0 my $required_count = $1;
5850 0 my @param_names = sort { $params{$a}{position} <=> $params{$b}{position} } keys %params;
5851 0 for my $i (0 .. $required_count-1) {
5852 0 $params{$param_names[$i]}{optional} = 0;
5853 0 $self->_log(" CODE: $param_names[$i] marked required due to croak scalar check");
5854 }
5855 } elsif ($code =~ /(?:croak|die)\(.*\)\s+if\s*\(\s*scalar\(\@_\)\s*==\s*0\s*\)/s) {
5856 0 foreach my $param (keys %params) {
5857 0 $params{$param}{optional} = 0;
5858 0 $self->_log(" CODE: $param: all parameters are required due to 'scalar(@_) == 0' check");
5859 }
5860 }
5861  
5862 # Analyze each parameter (with safety limit)
5863 775 foreach my $param (keys %params) {
5864 510 ▶ if ($param_count++ > $self->{max_parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_count++ > $self->{"max_parameters"}) 0 510 510
5865 0 $self->_log(" WARNING: Max parameters ($self->{max_parameters}) exceeded, skipping remaining");
5866 0 last;
5867 }
5868  
5869 510 my $p = \$params{$param};
5870  
5871 510 $self->_analyze_parameter_type($p, $param, $code);
5872 510 $self->_analyze_parameter_constraints($p, $param, $code);
5873 510 $self->_analyze_parameter_validation($p, $param, $code);
5874 510 $self->_analyze_advanced_types($p, $param, $code);
5875  
5876 # Defined checks
5877 510 ▶ if ($code =~ /defined\s*\(\s*\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /defined\s*\(\s*\$$param\s*\)/) 42 468 510
5878 42 $$p->{optional} = 0;
5879 42 $self->_log(" CODE: $param is required (defined check)");
5880 }
5881  
5882 # Determine optional/required and numeric type from code
5883 510 ▶ if ($code =~ /\s*\$$param\s*(?:\/\/|\|\|)=/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ m[\s*\$$param\s*(?://|\|\|)=]) { } 21 489 510
elsif ($code =~ m[\s*\$$param\s*(?:[\+\-\*\%]|/(?!/)|(?:\+\+)|(?:--)|(?:[\+\-\*\%]=|/(?!/)=)|\+\$|\$[+-])]) { } 134 355 489
elsif ($code =~ /\$\b$param\b\s*(?:\+0|\*1)/) { } 0 355 355
5884 # e.g. $var //= 5; or $var ||= 5;
5885 21 $$p->{optional} = 1;
5886 21 $self->_log(" CODE: $param is optional (default value assigned in code)");
5887 } elsif ($code =~ /\s*\$$param\s*(?:[\+\-\*\%]|\/(?!\/)|(?:\+\+)|(?:--)|(?:[\+\-\*\%]=|\/(?!\/)=)|\+\$|\$[+-])/ ) {
5888 # Covers arithmetic usage:
5889 # $x + $param, $param++, $param--, $x += $param, $x -= $param, etc.
5890 134 $$p->{optional} = 0;
5891 134 ▶ $$p->{type} //= 'number';
Condition50%1/2
$$p->{"type"} //= "number"
l!l
1340
MC/DC0%0/1
$$p->{"type"} //= "number"
$$p->{"type"}
Truth table50%1/2
A
A= $$p->{"type"}
Aresult
11
00
5892 134 $self->_log(" CODE: $param is required (used in arithmetic context)");
5893 } elsif ($code =~ /\$\b$param\b\s*(?:\+0|\*1)/) {
5894 # Forces numeric context, e.g., "$param + 0" or "$param * 1"
5895 0 $$p->{optional} = 0;
5896 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
5897 0 $self->_log(" CODE: $param is required (numeric context)");
5898 }
5899  
5900 # Required parameter checks (undef causes error)
5901  
5902 # Style 1: block form
5903 510 ▶ 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 510 510
5904 0 my $block = $1;
5905 0 ▶ if ($block =~ /\b(croak|die|confess)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /\b(croak|die|confess)\b/) 0 0 0
5906 0 $$p->{optional} = 0;
5907 0 $self->_log(" CODE: $param is required (undef causes error)");
5908 }
5909 }
5910  
5911 # Style 2: postfix unless
5912 510 ▶ 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*\)/) 18 492 510
5913 18 $$p->{optional} = 0;
5914 18 $self->_log(" CODE: $param is required (postfix undef check)");
5915 }
5916  
5917 # Exists checks for hash keys
5918 510 ▶ if ($code =~ /exists\s*\(\s*\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /exists\s*\(\s*\$$param\s*\)/) 0 510 510
5919 0 $$p->{type} = 'hashkey';
5920 0 $self->_log(" CODE: $param is a hash key");
5921 }
5922  
5923 # Scalar context for arrays
5924 510 ▶ if ($code =~ /scalar\s*\(\s*\@?\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /scalar\s*\(\s*\@?\$$param\s*\)/) 1 509 510
5925 1 $$p->{type} = 'array';
5926 1 $self->_log(" CODE: $param used in scalar context (array)");
5927 }
5928  
5929 510 $self->_extract_error_constraints($p, $param, $code);
5930 }
5931  
5932 775 $self->_sanitize_parameter_names(\%params, 'code analysis');
5933 775 return \%params;
5934 }
5935  
5936 # --------------------------------------------------
5937 # _analyze_parameter_type
5938 #
5939 # Purpose: Infer the type of a single parameter
5940 # from ref() checks, isa() calls,
5941 # bless patterns, array/hash operations,
5942 # and numeric operator usage in the
5943 # method body.
5944 #
5945 # Entry: $p_ref - reference to the parameter
5946 # hashref (modified in place
5947 # via the referenced hash).
5948 # $param - parameter name string.
5949 # $code - method body source string.
5950 #
5951 # Exit: Returns nothing. Modifies the
5952 # referenced parameter hashref.
5953 #
5954 # Side effects: Logs detections to stdout when
5955 # verbose is set.
5956 # --------------------------------------------------
5957 510 ▶sub _analyze_parameter_type {
Subroutine _analyze_parameter_type: called CC=23 SCAR=31.7
5958 510 my ($self, $p_ref, $param, $code) = @_;
5959 510 my $p = $$p_ref;
5960  
5961 # Type inference from ref() checks
5962 510 ▶ 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) { } 40 470 510
elsif ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]([^'"]+)['"]\s*\)/i) { } 3 467 470
elsif ($code =~ /bless\s+.*\$$param/) { } 6 461 467
5963 40 my $reftype = lc($1);
5964 40 ▶ $p->{type} = $reftype eq 'array' ? 'arrayref' :
Branch: 2 branches
BranchTrueFalseTotal
$reftype eq 'array' ? : 5 35 40
$reftype eq 'hash' ? : 35 0 35
5965 $reftype eq 'hash' ? 'hashref' :
5966 'scalar';
5967 40 $self->_log(" CODE: $param is $p->{type} (ref check)");
5968 }
5969 # ISA checks for objects
5970 elsif ($code =~ /\$$param\s*->\s*isa\s*\(\s*['"]([^'"]+)['"]\s*\)/i) {
5971 3 $p->{type} = 'object';
5972 3 $p->{isa} = $1;
5973 3 $self->_log(" CODE: $param is object of class $1");
5974 }
5975 # Blessed references
5976 elsif ($code =~ /bless\s+.*\$$param/) {
5977 6 $p->{type} = 'object';
5978 6 $self->_log(" CODE: $param is blessed object");
5979 }
5980 # Array/hash operations
5981 510 ▶ if (!$p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 456 54 510
5982 456 ▶ 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 451 456
elsif ($code =~ /\%\{\s*\$$param\s*\}/ or $code =~ /\$$param\s*->\s*\{/) { } 27 424 451
Condition66%2/3
$code =~ /\@\{\s*\$$param\s*\}/ or $code =~ /push\s*\(\s*\@?\$$param/
l!l&&r!l&&!r
50451
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
5983 5 $p->{type} = 'arrayref';
5984 ▶ } elsif ($code =~ /\%\{\s*\$$param\s*\}/ || $code =~ /\$$param\s*->\s*\{/) {
Condition66%2/3
$code =~ /\%\{\s*\$$param\s*\}/ or $code =~ /\$$param\s*->\s*\{/
l!l&&r!l&&!r
027424
MC/DC50%1/2
$code =~ /\%\{\s*\$$param\s*\}/ or $code =~ /\$$param\s*->\s*\{/
$code =~ /\%\{\s*\$$param\s*\}/ $code =~ /\$$param\s*->\s*\{/
Truth table66%2/3
A or B
A= $code =~ /\%\{\s*\$$param\s*\}/B= $code =~ /\$$param\s*->\s*\{/
ABresult
1X1
011
000
5985 27 $p->{type} = 'hashref';
5986 }
5987 }
5988  
5989 # Infer type from the default value if type is unknown
5990 510 ▶ if (!$p->{type} && exists $p->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->{"type"} and exists $p->{"_default"}) 32 478 510
Condition100%3/3
not $p->{"type"} and exists $p->{"_default"}
!ll&&!rl&&r
8639232
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
5991 32 my $default = $p->{_default};
5992 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
5993 0 $p->{type} = 'hashref';
5994 0 $self->_log(" CODE: $param type inferred as hashref from default");
5995 } elsif (ref($default) eq 'ARRAY') {
5996 0 $p->{type} = 'arrayref';
5997 0 $self->_log(" CODE: $param type inferred as arrayref from default");
5998 }
5999 }
6000  
6001 # ------------------------------------------------------------
6002 # Heuristic numeric inference (low confidence)
6003 # ------------------------------------------------------------
6004 510 ▶ if (!$p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->{"type"}) 424 86 510
6005 # An explicit looks_like_number($param) check is a direct
6006 # numeric-type assertion by the author, stronger evidence than
6007 # incidental arithmetic adjacency (e.g. $param is only ever
6008 # used inside a defined-or default before the arithmetic, so
6009 # the arithmetic-operator check below never sees $param itself
6010 # next to an operator).
6011 424 ▶ if ($code =~ /\blooks_like_number\s*\(\s*\$$param\s*\)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ /\blooks_like_number\s*\(\s*\$$param\s*\)/) { } 13 411 424
elsif ($code =~ m[\$$param\s*(?:[\+\-\*\%]|/(?!/))] or $code =~ m[(?:[\+\-\*\%]|/(?!/))\s*\$$param] or $code =~ /\bint\s*\(\s*\$$param\s*\)/ or $code =~ /\babs\s*\(\s*\$$param\s*\)/) { } 99 312 411
elsif ($code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ or $code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/) { } 33 279 312
6012 13 $p->{type} = 'number';
6013 13 $p->{_type_confidence} = 'heuristic';
6014 13 $self->_log(" CODE: $param inferred as number (looks_like_number check)");
6015 }
6016 # Numeric operators: + - * / % **
6017 # Use \/(?!\/) to exclude // (defined-or) from matching as division.
6018 ▶ elsif (
Condition88%8/9
$code =~ m[\$$param\s*(?:[\+\-\*\%]|/(?!/))] or $code =~ m[(?:[\+\-\*\%]|/(?!/))\s*\$$param] or $code =~ /\bint\s*\(\s*\$$param\s*\)/ or $code =~ /\babs\s*\(\s*\$$param\s*\)/
l!l&&r!l&&!r
990312
$code =~ m[\$$param\s*(?:[\+\-\*\%]|/(?!/))] or $code =~ m[(?:[\+\-\*\%]|/(?!/))\s*\$$param] or $code =~ /\bint\s*\(\s*\$$param\s*\)/
l!l&&r!l&&!r
972312
$code =~ m[\$$param\s*(?:[\+\-\*\%]|/(?!/))] or $code =~ m[(?:[\+\-\*\%]|/(?!/))\s*\$$param]
l!l&&r!l&&!r
6928314
MC/DC75%3/4
$code =~ m[\$$param\s*(?:[\+\-\*\%]|/(?!/))] or $code =~ m[(?:[\+\-\*\%]|/(?!/))\s*\$$param] or $code =~ /\bint\s*\(\s*\$$param\s*\)/ or $code =~ /\babs\s*\(\s*\$$param\s*\)/
$code =~ m[\$$param\s*(?:[\+\-\*\%]|/(?!/))] $code =~ m[(?:[\+\-\*\%]|/(?!/))\s*\$$param] $code =~ /\bint\s*\(\s*\$$param\s*\)/ $code =~ /\babs\s*\(\s*\$$param\s*\)/
Truth table80%4/5
A or B or C or D
A= $code =~ m[\$$param\s*(?:[\+\-\*\%]|/(?!/))]B= $code =~ m[(?:[\+\-\*\%]|/(?!/))\s*\$$param]C= $code =~ /\bint\s*\(\s*\$$param\s*\)/D= $code =~ /\babs\s*\(\s*\$$param\s*\)/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
6019 $code =~ /\$$param\s*(?:[\+\-\*\%]|\/(?!\/))/ ||
6020 $code =~ /(?:[\+\-\*\%]|\/(?!\/))\s*\$$param/ ||
6021 $code =~ /\bint\s*\(\s*\$$param\s*\)/ ||
6022 $code =~ /\babs\s*\(\s*\$$param\s*\)/
6023 ) {
6024 99 $p->{type} = 'number';
6025 99 $p->{_type_confidence} = 'heuristic';
6026 99 $self->_log(" CODE: $param inferred as number (numeric operator)");
6027 }
6028 # Numeric comparison
6029 ▶ elsif (
Condition100%3/3
$code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ or $code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
l!l&&r!l&&!r
2112279
MC/DC100%2/2
$code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ or $code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
$code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ $code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
Truth table100%3/3
A or B
A= $code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/B= $code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
ABresult
1X1
011
000
6030 $code =~ /\$$param\s*(?:==|!=|<=|>=|<|>)/ ||
6031 $code =~ /(?:==|!=|<=|>=|<|>)\s*\$$param/
6032 ) {
6033 33 $p->{type} = 'number';
6034 33 $p->{_type_confidence} = 'heuristic';
6035 33 $self->_log(" CODE: $param inferred as number (numeric comparison)");
6036 }
6037 }
6038 }
6039  
6040 # --------------------------------------------------
6041 # _analyze_advanced_types
6042 #
6043 # Purpose: Apply enhanced type detection to a
6044 # single parameter, checking for
6045 # DateTime objects, file handles,
6046 # coderefs, and enum-like constraints
6047 # beyond what basic type inference
6048 # can determine.
6049 #
6050 # Entry: $p_ref - reference to the parameter
6051 # hashref (modified in place
6052 # via the referenced hash).
6053 # $param - the parameter name string.
6054 # $code - method body source string.
6055 #
6056 # Exit: Returns nothing. Modifies the
6057 # referenced parameter hashref in place.
6058 #
6059 # Side effects: Logs detections to stdout when
6060 # verbose is set.
6061 #
6062 # Notes: Delegates to four specialised
6063 # detectors: _detect_datetime_type,
6064 # _detect_filehandle_type,
6065 # _detect_coderef_type, and
6066 # _detect_enum_type. Each detector
6067 # returns early on first match so
6068 # detectors are implicitly prioritised
6069 # in that order.
6070 # --------------------------------------------------
6071 511 ▶sub _analyze_advanced_types {
Subroutine _analyze_advanced_types: called CC=1 SCAR=0.0
6072 511 my ($self, $p_ref, $param, $code) = @_;
6073  
6074 # Dereference once to get the hash reference
6075 511 my $p = $$p_ref;
6076  
6077 # Now pass the dereferenced hash to the detection methods
6078 511 $self->_detect_datetime_type($p, $param, $code);
6079 511 $self->_detect_filehandle_type($p, $param, $code);
6080 511 $self->_detect_coderef_type($p, $param, $code);
6081 511 $self->_detect_enum_type($p, $param, $code);
6082 }
6083  
6084 # --------------------------------------------------
6085 # _detect_datetime_type
6086 #
6087 # Purpose: Detect DateTime objects, Time::Piece
6088 # objects, date strings, ISO 8601
6089 # strings, and UNIX timestamps by
6090 # analysing code patterns involving
6091 # the parameter.
6092 #
6093 # Entry: $p - parameter hashref (modified
6094 # in place).
6095 # $param - parameter name string.
6096 # $code - method body source string.
6097 #
6098 # Exit: Returns nothing. Modifies $p in place,
6099 # setting type, isa, semantic, min,
6100 # matches, and/or format keys.
6101 # Returns immediately on first match.
6102 #
6103 # Side effects: Logs detections to stdout when
6104 # verbose is set.
6105 # --------------------------------------------------
6106 510 ▶sub _detect_datetime_type {
Subroutine _detect_datetime_type: called CC=14 SCAR=28.8
6107 510 my ($self, $p, $param, $code) = @_;
6108  
6109 # Validate param is just a simple word
6110 510 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 510 510
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00510
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
6111  
6112 # DateTime object detection via isa/UNIVERSAL checks
6113 510 ▶ 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 509 510
6114 1 $p->{type} = 'object';
6115 1 $p->{isa} = 'DateTime';
6116 1 $p->{semantic} = 'datetime_object';
6117 1 $self->_log(" ADVANCED: $param is DateTime object");
6118 1 return;
6119 }
6120  
6121 # Check for DateTime method calls
6122 509 ▶ 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 508 509
6123 1 $p->{type} = 'object';
6124 1 $p->{isa} = 'DateTime';
6125 1 $p->{semantic} = 'datetime_object';
6126 1 $self->_log(" ADVANCED: $param uses DateTime methods");
6127 1 return;
6128 }
6129  
6130 # Time::Piece detection
6131 508 ▶ 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 508 508
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
00508
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
6132 $code =~ /\$$param\s*->\s*(strftime|epoch|year|mon|mday)/) {
6133 0 $p->{type} = 'object';
6134 0 $p->{isa} = 'Time::Piece';
6135 0 $p->{semantic} = 'timepiece_object';
6136 0 $self->_log(" ADVANCED: $param is Time::Piece object");
6137 0 return;
6138 }
6139  
6140 # String date/time patterns via regex matching
6141 508 ▶ 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 507 508
6142 1 $p->{type} = 'string';
6143 1 $p->{semantic} = 'date_string';
6144 1 $p->{format} = 'YYYY-MM-DD or similar';
6145 1 $self->_log(" ADVANCED: $param validated as date string pattern");
6146 1 return;
6147 }
6148  
6149 # ISO 8601 date pattern
6150 507 ▶ if ($code =~ /\$$param\s*=~\s*\/.*?[Tt].*?[Zz].*?\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[\$$param\s*=~\s*/.*?[Tt].*?[Zz].*?/]) 1 506 507
6151 1 $p->{type} = 'string';
6152 1 $p->{semantic} = 'iso8601_string';
6153 1 $p->{matches} = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z?$/';
6154 1 $self->_log(" ADVANCED: $param validated as ISO 8601 datetime");
6155 1 return;
6156 }
6157  
6158 # UNIX timestamp detection (numeric with specific range)
6159 506 ▶ 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 505 506
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
10505
$code =~ /\$$param\s*>\s*\d{9,}/ or $code =~ /time\(\s*\)\s*-\s*\$$param/
l!l&&r!l&&!r
10505
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
6160 $code =~ /time\(\s*\)\s*-\s*\$$param/ ||
6161 $code =~ /\$$param\s*-\s*time\(\s*\)/) {
6162 1 $p->{type} = 'integer';
6163 1 $p->{semantic} = 'unix_timestamp';
6164 1 $p->{min} = 0;
6165 1 $self->_log(" ADVANCED: $param appears to be UNIX timestamp");
6166 1 return;
6167 }
6168  
6169 # Date parsing with strptime or similar
6170 505 ▶ 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 505 505
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
00505
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
6171 $code =~ /DateTime::Format::\w+\s*->\s*parse_datetime\s*\(\s*\$$param/) {
6172 0 $p->{type} = 'string';
6173 0 $p->{semantic} = 'datetime_parseable';
6174 0 $self->_log(" ADVANCED: $param is parsed as datetime");
6175 0 return;
6176 }
6177 }
6178  
6179 # --------------------------------------------------
6180 # _detect_filehandle_type
6181 #
6182 # Purpose: Detect file handle parameters and
6183 # file path string parameters by
6184 # analysing I/O operations, file test
6185 # operators, and path manipulation
6186 # patterns involving the parameter.
6187 #
6188 # Entry: $p - parameter hashref (modified
6189 # in place).
6190 # $param - parameter name string.
6191 # $code - method body source string.
6192 #
6193 # Exit: Returns nothing. Modifies $p in place,
6194 # setting type, isa, and semantic keys.
6195 # Returns immediately on first match.
6196 #
6197 # Side effects: Logs detections to stdout when
6198 # verbose is set.
6199 # --------------------------------------------------
6200 510 ▶sub _detect_filehandle_type {
Subroutine _detect_filehandle_type: called CC=13 SCAR=30.5
6201 510 my ($self, $p, $param, $code) = @_;
6202  
6203 510 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 510 510
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00510
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
6204  
6205 # File handle operations
6206 510 ▶ if ($code =~ /(?:open|close|read|print|say|sysread|syswrite)\s*\(?\s*\$$param/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:open|close|read|print|say|sysread|syswrite)\s*\(?\s*\$$param/) 1 509 510
6207 1 $p->{type} = 'object';
6208 1 $p->{isa} = 'IO::Handle';
6209 1 $p->{semantic} = 'filehandle';
6210 1 $self->_log(" ADVANCED: $param is a file handle");
6211 1 return;
6212 }
6213  
6214 # Filehandle-specific operations
6215 509 ▶ 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 508 509
6216 1 $p->{type} = 'object';
6217 1 $p->{isa} = 'IO::Handle';
6218 1 $p->{semantic} = 'filehandle';
6219 1 $self->_log(" ADVANCED: $param uses filehandle methods");
6220 1 return;
6221 }
6222  
6223 # File test operators. A -d test is a directory requirement: the value
6224 # has to name an existing directory, which no spelling pattern can
6225 # capture, so it gets its own semantic rather than a filepath pattern.
6226 508 ▶ if ($code =~ /(?:-[frwxoOeszlpSbctugkTBMAC])\s+\$$param/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(?:-[frwxoOeszlpSbctugkTBMAC])\s+\$$param/) 2 506 508
6227 2 $p->{type} = 'string';
6228 2 ▶ if ($code =~ /-d\s+\$$param/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /-d\s+\$$param/) { } 1 1 2
6229 1 $p->{semantic} = 'directory';
6230 1 $self->_log(" ADVANCED: $param is tested as a directory");
6231 } else {
6232 1 $p->{semantic} = 'filepath';
6233 1 $self->_log(" ADVANCED: $param is tested as file path");
6234 }
6235 2 return;
6236 }
6237  
6238 # File::Spec operations or path manipulation
6239 506 ▶ 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 506 506
Condition33%1/3
$code =~ /File::(?:Spec|Basename)::\w+\s*\(\s*\$$param/ or $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/
l!l&&r!l&&!r
00506
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
6240 $code =~ /(?:basename|dirname|fileparse)\s*\(\s*\$$param/) {
6241 0 $p->{type} = 'string';
6242 0 $p->{semantic} = 'filepath';
6243 0 $self->_log(" ADVANCED: $param manipulated as file path");
6244 0 return;
6245 }
6246  
6247 # Path validation patterns
6248 # Only match a literal path assigned or defaulted to this variable
6249 506 ▶ 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 506 506
Condition66%2/3
defined $p->{"_default"} and $p->{"_default"} =~ m[^([A-Za-z]:\\|/|\./|\.\./)]
!ll&&!rl&&r
465410
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
6250 0 $p->{type} = 'string';
6251 0 $p->{semantic} = 'filepath';
6252 0 $self->_log(" ADVANCED: $param default looks like a path");
6253 0 return;
6254 }
6255  
6256 # IO::File detection
6257 506 ▶ 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 506 506
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
00506
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
6258 $code =~ /IO::File\s*->\s*new\s*\(\s*\$$param/) {
6259 0 $p->{type} = 'object';
6260 0 $p->{isa} = 'IO::File';
6261 0 $p->{semantic} = 'filehandle';
6262 0 $self->_log(" ADVANCED: $param is IO::File object");
6263 0 return;
6264 }
6265 }
6266  
6267 # --------------------------------------------------
6268 # _detect_coderef_type
6269 #
6270 # Purpose: Detect coderef and callback parameters
6271 # by analysing ref() checks, invocation
6272 # patterns, and parameter naming
6273 # conventions.
6274 #
6275 # Entry: $p - parameter hashref (modified
6276 # in place).
6277 # $param - parameter name string.
6278 # $code - method body source string.
6279 #
6280 # Exit: Returns nothing. Modifies $p in place,
6281 # setting type and semantic keys.
6282 # Returns immediately on first match.
6283 #
6284 # Side effects: Logs detections to stdout when
6285 # verbose is set.
6286 # --------------------------------------------------
6287 510 ▶sub _detect_coderef_type {
Subroutine _detect_coderef_type: called CC=11 SCAR=26.0
6288 510 my ($self, $p, $param, $code) = @_;
6289  
6290 510 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 510 510
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00510
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
6291  
6292 # ref() check for CODE, for a plain variable or a named argument
6293 510 ▶ 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 509 510
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
10509
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
6294 || $code =~ /ref\s*\(\s*\$args\{\s*['"]?$param['"]?\s*\}\s*\)\s*eq\s*['"]CODE['"]/i) {
6295 1 $p->{type} = 'coderef';
6296 1 $p->{semantic} = 'callback';
6297 1 $self->_log(" ADVANCED: $param is coderef (ref check)");
6298 1 return;
6299 }
6300  
6301 # Invocation as coderef - note the escaped @ in \@_
6302 509 ▶ 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 507 509
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
20507
$code =~ /\$$param\s*->\s*\(/ or $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/
l!l&&r!l&&!r
20507
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
6303 $code =~ /\$$param\s*->\s*\(\s*\@_\s*\)/ ||
6304 $code =~ /&\s*\{\s*\$$param\s*\}/) {
6305 2 $p->{type} = 'coderef';
6306 2 $p->{semantic} = 'callback';
6307 2 $self->_log(" ADVANCED: $param invoked as coderef");
6308 2 return;
6309 }
6310  
6311 # Parameter name suggests callback
6312 507 ▶ 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 504 507
6313 3 $p->{type} = 'coderef';
6314 3 $p->{semantic} = 'callback';
6315 3 $self->_log(" ADVANCED: $param name suggests coderef");
6316 3 return;
6317 }
6318  
6319 # Blessed coderef (unusual but valid)
6320 504 ▶ 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 504 504
Condition66%2/3
$code =~ /blessed\s*\(\s*\$$param\s*\)/ and $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i
!ll&&!rl&&r
50130
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
6321 $code =~ /ref\s*\(\s*\$$param\s*\)\s*eq\s*['"]CODE['"]/i) {
6322 0 $p->{type} = 'object';
6323 0 $p->{isa} = 'blessed_coderef';
6324 0 $p->{semantic} = 'callback';
6325 0 $self->_log(" ADVANCED: $param is blessed coderef");
6326 0 return;
6327 }
6328 }
6329  
6330 # --------------------------------------------------
6331 # _detect_enum_type
6332 #
6333 # Purpose: Detect enum-like parameters whose
6334 # valid values are a fixed set, by
6335 # analysing validation patterns
6336 # including regex alternations, hash
6337 # lookups, grep checks, given/when,
6338 # if/elsif chains, and smart match.
6339 #
6340 # Entry: $p - parameter hashref (modified
6341 # in place).
6342 # $param - parameter name string.
6343 # $code - method body source string.
6344 #
6345 # Exit: Returns nothing. Modifies $p in place,
6346 # setting type, enum, and semantic keys.
6347 # Returns immediately on first match.
6348 #
6349 # Side effects: Logs detections to stdout when
6350 # verbose is set.
6351 #
6352 # Notes: Requires at least 3 if/elsif branches
6353 # for pattern 5 to avoid false positives
6354 # from ordinary conditional code.
6355 # --------------------------------------------------
6356 510 ▶sub _detect_enum_type {
Subroutine _detect_enum_type: called CC=24 SCAR=36.1
6357 510 my ($self, $p, $param, $code) = @_;
6358  
6359 510 ▶ return unless defined $param && $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param and $param =~ /^\w+$/ 0 510 510
Condition33%1/3
defined $param and $param =~ /^\w+$/
!ll&&!rl&&r
00510
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
6360  
6361 # Pattern 1: die/croak unless value is in list
6362 # die 'Invalid status' unless $status =~ /^(active|inactive|pending)$/;
6363 510 ▶ if ($code =~ /unless\s+\$$param\s*=~\s*\/\^?\(([^)]+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[unless\s+\$$param\s*=~\s*/\^?\(([^)]+)\)]) 6 504 510
6364 6 my $values = $1;
6365 6 my @enum_values = split(/\|/, $values);
6366 6 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 2 4 6
6367 6 $p->{enum} = \@enum_values;
6368 6 $p->{semantic} = 'enum';
6369 6 $self->_log(" ADVANCED: $param is enum with values: " . join(', ', @enum_values));
6370 6 return;
6371 }
6372  
6373 # Pattern 2: Hash lookup for validation
6374 # my %valid = map { $_ => 1 } qw(red green blue);
6375 # die unless $valid{$param};
6376 504 ▶ if ($code =~ /\%(\w+)\s*=.*?qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\%(\w+)\s*=.*?qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) 1 503 504
6377 1 my $hash_name = $1;
6378 1 my $values_str = $2;
6379 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
6380 1 my @enum_values = split(/\s+/, $values_str);
6381 1 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 1 0 1
6382 1 $p->{enum} = \@enum_values;
6383 1 $p->{semantic} = 'enum';
6384 1 $self->_log(" ADVANCED: $param validated via hash lookup: " . join(', ', @enum_values));
6385 1 return;
6386 }
6387 }
6388  
6389 # Pattern 3: Array grep validation
6390 # die unless grep { $_ eq $param } qw(foo bar baz);
6391 503 ▶ if ($code =~ /grep\s*\{[^}]*\$$param[^}]*\}\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /grep\s*\{[^}]*\$${param}[^}]*\}\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) 1 502 503
6392 1 my $values_str = $1;
6393 1 my @enum_values = split(/\s+/, $values_str);
6394 1 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 1 0 1
6395 1 $p->{enum} = \@enum_values;
6396 1 $p->{semantic} = 'enum';
6397 1 $self->_log(" ADVANCED: $param validated via grep: " . join(', ', @enum_values));
6398 1 return;
6399 }
6400  
6401 # Pattern 4: Given/when (Perl 5.10+)
6402 502 ▶ if ($code =~ /given\s*\(\s*\$$param\s*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /given\s*\(\s*\$$param\s*\)/) 1 501 502
6403 1 my @enum_values;
6404 1 ▶ while ($code =~ /when\s*\(\s*['"]([^'"]+)['"]\s*\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /when\s*\(\s*['"]([^'"]+)['"]\s*\)/g) 2 1 3
6405 2 push @enum_values, $1;
6406 }
6407 1 ▶ if (@enum_values >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@enum_values >= 2) 1 0 1
6408 1 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 1 0 1
6409 1 $p->{enum} = \@enum_values;
6410 1 $p->{semantic} = 'enum';
6411 1 $self->_log(" ADVANCED: $param has enum values from given/when: " .
6412 join(', ', @enum_values));
6413 1 return;
6414 }
6415 }
6416  
6417 # Pattern 5: Multiple if/elsif checking specific values
6418 501 my @if_values;
6419 501 ▶ 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 501 508
6420 7 push @if_values, $1;
6421 }
6422 501 ▶ 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 501 506
6423 5 push @if_values, $1;
6424 }
6425 501 ▶ if (@if_values >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@if_values >= 3) 2 499 501
6426 2 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 2 0 2
6427 2 $p->{enum} = \@if_values;
6428 2 $p->{semantic} = 'enum';
6429 2 $self->_log(" ADVANCED: $param appears to be enum from if/elsif: " .
6430 join(', ', @if_values));
6431 2 return;
6432 }
6433  
6434 # Pattern 6: Smart match (~~) with array
6435 499 ▶ if ($code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ or $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) 0 499 499
Condition33%1/3
$code =~ /\$$param\s*~~\s*\[([^\]]+)\]/ or $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/
l!l&&r!l&&!r
00499
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
6436 $code =~ /\$$param\s*~~\s*qw\s*[\(\[<{]([^)\]>}]+)[\)\]>}]/) {
6437 0 my $values_str = $1;
6438 0 my @enum_values;
6439 0 ▶ if ($values_str =~ /['"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($values_str =~ /['"]/) { } 0 0 0
6440 0 @enum_values = $values_str =~ /['"](.*?)['"]/g;
6441 } else {
6442 0 @enum_values = split(/\s+/, $values_str);
6443 }
6444 0 ▶ if (@enum_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@enum_values) 0 0 0
6445 0 ▶ $p->{type} = 'string' unless $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"type"} 0 0 0
6446 0 $p->{enum} = \@enum_values;
6447 0 $p->{semantic} = 'enum';
6448 0 $self->_log(" ADVANCED: $param validated with smart match: " .
6449 join(', ', @enum_values));
6450 0 return;
6451 }
6452 }
6453 }
6454  
6455 # --------------------------------------------------
6456 # _extract_error_constraints
6457 #
6458 # Purpose: Extract invalid-value constraints and
6459 # error messages from die/croak patterns
6460 # referencing a specific parameter, and
6461 # infer numeric bounds from comparisons
6462 # with literals.
6463 #
6464 # Entry: $p_ref - reference to the parameter
6465 # hashref (modified in place).
6466 # $param - parameter name string.
6467 # $code - method body source string.
6468 #
6469 # Exit: Returns nothing. May add _invalid,
6470 # _errors, min, and/or max to the
6471 # referenced parameter hashref.
6472 #
6473 # Side effects: Logs detections to stdout when
6474 # verbose is set.
6475 # --------------------------------------------------
6476 510 ▶sub _extract_error_constraints {
Subroutine _extract_error_constraints: called CC=17 SCAR=28.4
6477 510 my ($self, $p, $param, $code) = @_;
6478  
6479 # Look for die/croak/confess with a condition involving this param
6480 510 ▶ 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 510 574
6481 (?:die|croak|confess) # error call
6482 \s*
6483 (?:
6484 ["']([^"']+)["'] # captured error message
6485 |
6486 q[qw]?\s*[\(\[]([^)\]]+)[\)\]] # q(), qq(), qw()
6487 )?
6488 \s*
6489 if\s+
6490 (.+?) # condition
6491 \s*;
6492 /gsx) {
6493  
6494 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
6495 64 my $condition = $3;
6496  
6497 # Only keep conditions that reference this parameter
6498 64 ▶ next unless $condition =~ /\$$param\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $condition =~ /\$$param\b/ 25 39 64
6499  
6500 # Initialize storage
6501 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
6502 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
6503  
6504 # Normalize condition (strip surrounding parens)
6505 39 $condition =~ s/^\(|\)$//g;
6506 39 $condition =~ s/\s+/ /g;
6507  
6508 # Try to extract a meaningful invalid constraint
6509 39 my $constraint;
6510  
6511 # Examples:
6512 # $age <= 0
6513 # $x eq ''
6514 # length($s) < 3
6515 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
6516 7 $constraint = "$1 $2";
6517 }
6518 elsif ($condition =~ /length\s*\(\s*\$$param\s*\)\s*([<>=!]+)\s*(\d+)/) {
6519 4 $constraint = "length $1 $2";
6520 }
6521 elsif ($condition =~ /\$$param\s*==\s*0/) {
6522 0 $constraint = '== 0';
6523 }
6524  
6525 # Store results
6526 39 ▶ push @{ $$p->{_invalid} }, $constraint if $constraint;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint 11 28 39
6527 39 ▶ push @{ $$p->{_errors} }, $message if defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if defined $message 34 5 39
6528  
6529 39 ▶ $self->_log(
Branch: 1 branch
BranchTrueFalseTotal
defined $message ? : 34 5 39
6530 " ERROR: $param invalid when [$condition]" .
6531 (defined $message ? " => '$message'" : '')
6532 );
6533 }
6534  
6535 # Numeric comparison with literal
6536 510 ▶ if ($code =~ /\b\Q$param\E\s*(<=|<|>=|>)\s*(-?\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\b\Q$param\E\s*(<=|<|>=|>)\s*(-?\d+)/) 21 489 510
6537 21 my ($op, $num) = ($1, $2);
6538  
6539 # Mark required
6540 21 $$p->{optional} = 0;
6541  
6542 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
6543 1 $$p->{min} = $num + 1;
6544 } elsif ($op eq '<') {
6545 5 $$p->{min} = $num;
6546 } elsif ($op eq '>=') {
6547 2 $$p->{max} = $num - 1;
6548 } elsif ($op eq '>') {
6549 13 $$p->{max} = $num;
6550 }
6551  
6552 21 $self->_log(" ERROR: $param normalized constraint from '$op $num'");
6553 }
6554 }
6555  
6556 # --------------------------------------------------
6557 # _extract_parameters_from_signature
6558 #
6559 # Purpose: Extract parameter names and positions
6560 # from a method's signature, trying
6561 # modern Perl subroutine signatures
6562 # first and falling back to traditional
6563 # @_ extraction styles.
6564 #
6565 # Entry: $params - hashref to populate with
6566 # parameter specs (modified
6567 # in place).
6568 # $code - method body source string.
6569 #
6570 # Exit: Returns nothing. Populates $params.
6571 #
6572 # Side effects: Logs detections to stdout when
6573 # verbose is set.
6574 #
6575 # Notes: Three traditional styles are
6576 # supported: (1) my ($self, ...) = @_,
6577 # (2) my $self = shift; my $x = shift,
6578 # (3) my $x = $_[N]. $self and $class
6579 # are always excluded from the returned
6580 # parameters.
6581 # --------------------------------------------------
6582 1544 ▶sub _extract_parameters_from_signature {
Subroutine _extract_parameters_from_signature: called CC=24 SCAR=35.7
6583 1544 my ($self, $params, $code) = @_;
6584  
6585 # Modern Style: Subroutine signatures with attributes
6586 # Handle multi-line signatures
6587 # sub foo :attr1 :attr2(val) (
6588 # $self,
6589 # $x :Type,
6590 # $y = default
6591 # ) { }
6592  
6593 # Try to match signature after attributes
6594 # Look for the parameter list - it's the last (...) before the opening brace
6595 # that contains sigils ($, %, @)
6596 1544 ▶ 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 1538 1544
6597 6 my $potential_sig = $1;
6598  
6599 # Check if this looks like parameters (has sigils)
6600 6 ▶ if ($potential_sig =~ /[\$\%\@]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($potential_sig =~ /[\$\%\@]/) 6 0 6
6601 6 $self->_log(" SIG: Found modern signature: ($potential_sig)");
6602 6 $self->_parse_modern_signature($params, $potential_sig);
6603 6 return;
6604 }
6605 }
6606  
6607 # Direct-index style: my $self = $_[0]; my $arg = $_[1]; ...
6608 # Must be checked before Style 1 to avoid matching @_ inside closures
6609 # defined in the body of a method that uses this style.
6610 1538 ▶ if($code =~ /my\s+\$(?:self|class)\s*=\s*\$_\[0\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(?:self|class)\s*=\s*\$_\[0\]/) 138 1400 1538
6611 138 my $pos = 0;
6612 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
6613 140 my $name = $1;
6614 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
6615 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
6616 2 $self->_log(" CODE: Found direct-index parameter '\$$name' at \$_[$2]");
6617 }
6618 138 return;
6619 }
6620  
6621 # Traditional Style 1: my ($self, $arg1, $arg2) = @_;
6622 1400 ▶ if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) { } 802 598 1400
elsif ($code =~ /my\s+\$self\s*=\s*shift/) { } 56 542 598
6623 802 my $sig = $1;
6624 802 my $pos = 0;
6625  
6626 802 ▶ while ($sig =~ /\$(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig =~ /\$(\w+)/g) 1560 802 2362
6627 1560 my $name = $1;
6628  
6629 1560 ▶ next if $name =~ /^(self|class|pkg|proto|klass)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(self|class|pkg|proto|klass)$/i 724 836 1560
6630  
6631 836 ▶ $params->{$name} //= {
Condition50%1/2
$params->{$name} //= {"_source", "code", "optional", 1}
l!l
0836
MC/DC0%0/1
$params->{$name} //= {"_source", "code", "optional", 1}
$params->{$name}
Truth table50%1/2
A
A= $params->{$name}
Aresult
11
00
6632 _source => SOURCE_CODE,
6633 optional => 1,
6634 };
6635  
6636 836 ▶ $params->{$name}{position} = $pos unless exists $params->{$name}{position};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$name}{"position"} 836 0 836
6637  
6638 836 $pos++;
6639 }
6640 802 return;
6641 } elsif ($code =~ /my\s+\$self\s*=\s*shift/) {
6642 # Traditional Style 2: my $self = shift; my $arg1 = shift;
6643 56 my @shifts;
6644 56 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*shift/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*shift/g) 66 56 122
6645 66 push @shifts, $1;
6646 }
6647 56 ▶ 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 56 0 56
Condition33%1/3
@shifts and $shifts[0] =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
0056
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
6648 56 my $pos = 0;
6649 56 foreach my $param (@shifts) {
6650 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
6651 }
6652 56 return;
6653 }
6654  
6655 # Traditional Style 3: Function parameters (no $self)
6656 542 ▶ if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s*\(\s*([^)]+)\)\s*=\s*\@_/s) 0 542 542
6657 0 my $sig = $1;
6658 0 my @param_names = $sig =~ /\$(\w+)/g;
6659 0 my $pos = 0;
6660 0 foreach my $param (@param_names) {
6661 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
6662 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
6663 }
6664 }
6665  
6666 # De-duplicate
6667 542 my %seen;
6668 542 foreach my $param (keys %$params) {
6669 0 ▶ if ($seen{$param}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$param}++) 0 0 0
6670 0 $self->_log(" WARNING: Duplicate parameter '$param' found");
6671 }
6672 }
6673 }
6674  
6675 # --------------------------------------------------
6676 # _parse_modern_signature
6677 #
6678 # Purpose: Parse a Perl 5.20+ subroutine
6679 # signature string into individual
6680 # parameter specs, respecting nested
6681 # structures when splitting on commas.
6682 #
6683 # Entry: $params - hashref to populate
6684 # (modified in place).
6685 # $sig - signature string with outer
6686 # parentheses already removed.
6687 #
6688 # Exit: Returns nothing. Populates $params
6689 # via _parse_signature_parameter.
6690 #
6691 # Side effects: Logs parsing details to stdout when
6692 # verbose is set.
6693 # --------------------------------------------------
6694 8 ▶sub _parse_modern_signature {
Subroutine _parse_modern_signature: called CC=12 SCAR=24.9
6695 8 my ($self, $params, $sig) = @_;
6696  
6697 8 $self->_log(" DEBUG: Parsing signature: [$sig]");
6698  
6699 # Split signature by commas, but respect nested structures (e.g. a
6700 # default value containing a hashref/arrayref literal)
6701 8 require Text::Balanced;
6702 8 my @parts;
6703 8 my $current = '';
6704 8 my $rest = $sig;
6705  
6706 8 ▶ while (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 173 8 181
6707 173 ▶ if (substr($rest, 0, 1) =~ /[(\[{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) =~ /[(\[{]/) 1 172 173
6708 # extract_bracketed advances $rest past the extracted block
6709 # in place, so $rest must not be re-truncated afterwards
6710 1 my $extracted = Text::Balanced::extract_bracketed($rest, '(){}[]');
6711 1 ▶ last unless defined $extracted; # Unbalanced brackets
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extracted 0 1 1
6712 1 $current .= $extracted;
6713 1 next;
6714 }
6715 172 ▶ if (substr($rest, 0, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rest, 0, 1) eq ",") 15 157 172
6716 15 push @parts, $current;
6717 15 $current = '';
6718 15 $rest = substr($rest, 1);
6719 15 next;
6720 }
6721 157 $current .= substr($rest, 0, 1);
6722 157 $rest = substr($rest, 1);
6723 }
6724 8 ▶ push @parts, $current if $current =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $current =~ /\S/ 8 0 8
6725  
6726 8 my $position = 0;
6727  
6728 8 foreach my $part (@parts) {
6729 23 $part =~ s/^\s+|\s+$//g;
6730  
6731 # Skip empty parts
6732 23 ▶ next unless $part;
Branch: 1 branch
BranchTrueFalseTotal
unless $part 0 23 23
6733  
6734 # Parse different parameter types
6735 23 my $param_info = $self->_parse_signature_parameter($part, $position);
6736  
6737 23 ▶ if ($param_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_info) 23 0 23
6738 23 my $name = $param_info->{name};
6739  
6740 # Skip self/class
6741 23 ▶ if ($name =~ /^(self|class|pkg|proto|klass)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(self|class|pkg|proto|klass)$/i) 8 15 23
6742 8 next;
6743 }
6744  
6745 15 $params->{$name} = $param_info;
6746 $self->_log(" SIG: $name has position $position" .
6747 ($param_info->{optional} ? ' (optional)' : '') .
6748 15 ▶ ($param_info->{_default} ? ", default: $param_info->{_default}" : ''));
Branch: 2 branches
BranchTrueFalseTotal
$param_info->{'optional'} ? : 1 14 15
$param_info->{'_default'} ? : 1 14 15
6749 15 $position++;
6750 }
6751 }
6752 }
6753  
6754 # --------------------------------------------------
6755 # _parse_signature_parameter
6756 #
6757 # Purpose: Parse a single parameter declaration
6758 # from a modern Perl signature, handling
6759 # type constraints, default values,
6760 # plain scalars, and slurpy array/hash
6761 # parameters.
6762 #
6763 # Entry: $part - a single parameter string
6764 # (one comma-separated
6765 # element from the signature).
6766 # $position - zero-based position index
6767 # of this parameter.
6768 #
6769 # Exit: Returns a parameter info hashref on
6770 # success, or undef if the string does
6771 # not match any known pattern.
6772 #
6773 # Side effects: None.
6774 #
6775 # Notes: Six patterns are tried in order:
6776 # (1) :Type with default,
6777 # (2) :Type without default,
6778 # (3) default without type,
6779 # (4) plain $name,
6780 # (5) slurpy @name,
6781 # (6) slurpy %name.
6782 # --------------------------------------------------
6783 30 ▶sub _parse_signature_parameter {
Subroutine _parse_signature_parameter: called CC=20 SCAR=38.2
6784 30 my ($self, $part, $position) = @_;
6785  
6786 30 my %info = (
6787 _source => SOURCE_SIGNATURE,
6788 position => $position,
6789 optional => 0,
6790 );
6791  
6792 # Pattern 1: Type constraint WITH default: $name :Type = default
6793 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
6794 1 my ($name, $constraint, $default) = ($1, $2, $3);
6795 1 $default =~ s/^\s+|\s+$//g;
6796  
6797 1 $info{name} = $name;
6798 1 $info{optional} = 1;
6799 1 $info{_default} = $self->_clean_default_value($default, 1);
6800  
6801 # Apply type constraint
6802 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
6803 0 $info{type} = 'integer';
6804 } elsif ($constraint =~ /^(Num|Number)$/i) {
6805 1 $info{type} = 'number';
6806 } elsif ($constraint =~ /^(Str|String)$/i) {
6807 0 $info{type} = 'string';
6808 } elsif ($constraint =~ /^(Bool|Boolean)$/i) {
6809 0 $info{type} = 'boolean';
6810 } elsif ($constraint =~ /^(Array|ArrayRef)$/i) {
6811 0 $info{type} = 'arrayref';
6812 } elsif ($constraint =~ /^(Hash|HashRef)$/i) {
6813 0 $info{type} = 'hashref';
6814 } else {
6815 0 $info{type} = 'object';
6816 0 $info{isa} = $constraint;
6817 }
6818  
6819 1 return \%info;
6820 } elsif ($part =~ /^\$(\w+)\s*:\s*(\w+)\s*$/s) {
6821 # Pattern 2: Type constraint WITHOUT default: $name :Type
6822 1 my ($name, $constraint) = ($1, $2);
6823 1 $info{name} = $name;
6824 1 $info{optional} = 0;
6825  
6826 # Apply type constraint (same as above)
6827 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
6828 1 $info{type} = 'integer';
6829 } elsif ($constraint =~ /^(Num|Number)$/i) {
6830 0 $info{type} = 'number';
6831 } elsif ($constraint =~ /^(Str|String)$/i) {
6832 0 $info{type} = 'string';
6833 } elsif ($constraint =~ /^(Bool|Boolean)$/i) {
6834 0 $info{type} = 'boolean';
6835 } elsif ($constraint =~ /^(Array|ArrayRef)$/i) {
6836 0 $info{type} = 'arrayref';
6837 } elsif ($constraint =~ /^(Hash|HashRef)$/i) {
6838 0 $info{type} = 'hashref';
6839 } else {
6840 0 $info{type} = 'object';
6841 0 $info{isa} = $constraint;
6842 }
6843  
6844 1 return \%info;
6845 } elsif ($part =~ /^\$(\w+)\s*=\s*(.+)$/s) {
6846 # Pattern 3: Default WITHOUT type: $name = default
6847 2 my ($name, $default) = ($1, $2);
6848 2 $default =~ s/^\s+|\s+$//g;
6849  
6850 2 $info{name} = $name;
6851 2 $info{optional} = 1;
6852 2 $info{_default} = $self->_clean_default_value($default, 1);
6853 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
6854  
6855 2 return \%info;
6856 }
6857  
6858 # Pattern 4: Plain parameter: $name
6859 elsif ($part =~ /^\$(\w+)$/s) {
6860 23 $info{name} = $1;
6861 23 $info{optional} = 0;
6862 23 return \%info;
6863 }
6864  
6865 # Pattern 5: Array parameter: @name
6866 elsif ($part =~ /^\@(\w+)$/s) {
6867 1 $info{name} = $1;
6868 1 $info{type} = 'array';
6869 1 $info{slurpy} = 1;
6870 1 $info{optional} = 1;
6871 1 return \%info;
6872 }
6873  
6874 # Pattern 6: Hash parameter: %name
6875 elsif ($part =~ /^\%(\w+)$/s) {
6876 1 $info{name} = $1;
6877 1 $info{type} = 'hash';
6878 1 $info{slurpy} = 1;
6879 1 $info{optional} = 1;
6880 1 return \%info;
6881 }
6882  
6883 1 return undef;
6884 }
6885  
6886 # --------------------------------------------------
6887 # _infer_type_from_default
6888 #
6889 # Purpose: Infer a parameter type from its
6890 # default value when no explicit type
6891 # annotation is available.
6892 #
6893 # Entry: $default - the cleaned default value
6894 # scalar, hashref, or
6895 # arrayref. May be undef.
6896 #
6897 # Exit: Returns a type string ('hashref',
6898 # 'arrayref', 'integer', 'number',
6899 # 'boolean', 'string'), or undef if
6900 # $default is undef.
6901 #
6902 # Side effects: None.
6903 # --------------------------------------------------
6904 11 ▶sub _infer_type_from_default {
Subroutine _infer_type_from_default: called CC=8 SCAR=21.2
6905 11 my ($self, $default) = @_;
6906  
6907 11 ▶ return undef unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 1 10 11
6908  
6909 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
6910 1 return 'hashref';
6911 } elsif (ref($default) eq 'ARRAY') {
6912 1 return 'arrayref';
6913 } elsif ($default =~ /^-?\d+$/) {
6914 5 return 'integer';
6915 } elsif ($default =~ /^-?\d+\.\d+$/) {
6916 1 return 'number';
6917 ▶ } 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
6918 0 return 'boolean';
6919 } else {
6920 2 return 'string';
6921 }
6922 }
6923  
6924 # --------------------------------------------------
6925 # _extract_subroutine_attributes
6926 #
6927 # Purpose: Extract Perl subroutine attributes
6928 # (e.g. :lvalue, :method, :Returns(Int))
6929 # from a method's source string.
6930 #
6931 # Entry: $code - method body source string.
6932 #
6933 # Exit: Returns a hashref of attribute name
6934 # to value (1 for flag-only attributes,
6935 # the attribute argument string for
6936 # attributes with values).
6937 # Returns an empty hashref if no
6938 # attributes are found.
6939 #
6940 # Side effects: Logs detections to stdout when
6941 # verbose is set.
6942 # --------------------------------------------------
6943 777 ▶sub _extract_subroutine_attributes {
Subroutine _extract_subroutine_attributes: called CC=10 SCAR=23.0
6944 777 my ($self, $code) = @_;
6945  
6946 777 my %attributes;
6947  
6948 # Extract all attributes from the sub declaration
6949 # Attributes are :name or :name(value) between sub name and either ( or {
6950 # Pattern: sub name ATTRIBUTES ( params ) { }
6951 # or: sub name ATTRIBUTES { }
6952  
6953 # First, find the attributes section (everything between sub name and ( or { )
6954 777 my $attr_section = '';
6955  
6956 777 ▶ if($code =~ /sub\s+\w+\s+((?::\w+(?:\([^)]*\))?\s*)+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /sub\s+\w+\s+((?::\w+(?:\([^)]*\))?\s*)+)/s) 3 774 777
6957 3 $attr_section = $1;
6958 }
6959  
6960 # Parse individual attributes from the section
6961 777 ▶ if($attr_section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_section) 3 774 777
6962 3 ▶ while($attr_section =~ /:(\w+)(?:\(([^)]*)\))?/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_section =~ /:(\w+)(?:\(([^)]*)\))?/g) 4 3 7
6963 4 my ($name, $value) = ($1, $2);
6964  
6965 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
6966 1 $attributes{$name} = $value;
6967 1 $self->_log(" ATTR: Found attribute :$name($value)");
6968 } else {
6969 3 $attributes{$name} = 1;
6970 3 $self->_log(" ATTR: Found attribute :$name");
6971 }
6972 }
6973 }
6974  
6975 # Process common attributes
6976 777 ▶ if ($attributes{Returns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{"Returns"}) 1 776 777
6977 1 my $return_type = $attributes{Returns};
6978 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
6979 1 $self->_log(" ATTR: Method declares return type: $return_type");
6980 }
6981 }
6982  
6983 777 ▶ if ($attributes{lvalue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{"lvalue"}) 2 775 777
6984 2 $self->_log(" ATTR: Method is lvalue (can be assigned to)");
6985 }
6986  
6987 777 ▶ if ($attributes{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{"method"}) 1 776 777
6988 1 $self->_log(' ATTR: Method explicitly marked as :method');
6989 }
6990  
6991 777 return \%attributes;
6992 }
6993  
6994 # --------------------------------------------------
6995 # _analyze_postfix_dereferencing
6996 #
6997 # Purpose: Detect usage of Perl 5.20+ postfix
6998 # dereferencing syntax in a method body
6999 # and record which dereference forms
7000 # are used.
7001 #
7002 # Entry: $code - method body source string.
7003 #
7004 # Exit: Returns a hashref whose keys are
7005 # dereference form names (array_deref,
7006 # hash_deref, scalar_deref, code_deref,
7007 # array_slice, hash_slice) with value 1
7008 # when detected.
7009 # Returns an empty hashref if no
7010 # postfix dereferencing is found.
7011 #
7012 # Side effects: Logs detections to stdout when
7013 # verbose is set.
7014 # --------------------------------------------------
7015 779 ▶sub _analyze_postfix_dereferencing {
Subroutine _analyze_postfix_dereferencing: called CC=7 SCAR=19.5
7016 779 my ($self, $code) = @_;
7017  
7018 779 my %derefs;
7019  
7020 # Array dereference: $ref->@*
7021 779 ▶ if ($code =~ /\$\w+\s*->\s*\@\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\@\*/) 1 778 779
7022 1 $derefs{array_deref} = 1;
7023 1 $self->_log(" MODERN: Uses postfix array dereferencing (->@*)");
7024 }
7025  
7026 # Hash dereference: $ref->%*
7027 779 ▶ if ($code =~ /\$\w+\s*->\s*\%\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\%\*/) 1 778 779
7028 1 $derefs{hash_deref} = 1;
7029 1 $self->_log(" MODERN: Uses postfix hash dereferencing (->%*)");
7030 }
7031  
7032 # Scalar dereference: $ref->$*
7033 779 ▶ if ($code =~ /\$\w+\s*->\s*\$\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\$\*/) 1 778 779
7034 1 $derefs{scalar_deref} = 1;
7035 1 $self->_log(' MODERN: Uses postfix scalar dereferencing (->$*)');
7036 }
7037  
7038 # Code dereference: $ref->&*
7039 779 ▶ if ($code =~ /\$\w+\s*->\s*\&\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\&\*/) 0 779 779
7040 0 $derefs{code_deref} = 1;
7041 0 $self->_log(" MODERN: Uses postfix code dereferencing (->&*)");
7042 }
7043  
7044 # Array element: $ref->@[0,2,4]
7045 779 ▶ if ($code =~ /\$\w+\s*->\s*\@\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\@\[/) 1 778 779
7046 1 $derefs{array_slice} = 1;
7047 1 $self->_log(" MODERN: Uses postfix array slice (->@[...])");
7048 }
7049  
7050 # Hash element: $ref->%{key1,key2}
7051 779 ▶ if ($code =~ /\$\w+\s*->\s*\%\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*->\s*\%\{/) 1 778 779
7052 1 $derefs{hash_slice} = 1;
7053 1 $self->_log(" MODERN: Uses postfix hash slice (->%{...})");
7054 }
7055  
7056 779 return \%derefs;
7057 }
7058  
7059 # --------------------------------------------------
7060 # _extract_field_declarations
7061 #
7062 # Purpose: Extract Perl 5.38 field declarations
7063 # from a class body or method source
7064 # string, capturing field names,
7065 # :param attributes, default values,
7066 # and :isa type constraints.
7067 #
7068 # Entry: $code - source string potentially
7069 # containing 'field $name ...'
7070 # declarations.
7071 #
7072 # Exit: Returns a hashref of field name to
7073 # field_info hashref. Returns an empty
7074 # hashref if no field declarations
7075 # are found.
7076 #
7077 # Side effects: Logs detections to stdout when
7078 # verbose is set.
7079 # --------------------------------------------------
7080 778 ▶sub _extract_field_declarations {
Subroutine _extract_field_declarations: called CC=7 SCAR=19.5
7081 778 my ($self, $code) = @_;
7082  
7083 778 my %fields;
7084  
7085 # Pattern: field $name :param;
7086 # Pattern: field $name :param(name);
7087 # Pattern: field $name = default;
7088 # More lenient pattern to catch various formats
7089 778 ▶ while ($code =~ /^\s*field\s+\$(\w+)\s*([^;]*);/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\s*field\s+\$(\w+)\s*([^;]*);/gm) 4 778 782
7090 4 my ($name, $modifiers) = ($1, $2);
7091  
7092 4 $self->_log(" FIELD: Found field \$$name with modifiers: [$modifiers]");
7093  
7094 4 my %field_info = (
7095 name => $name,
7096 _source => SOURCE_FIELD
7097 );
7098  
7099 # Check for :param attribute
7100 4 ▶ if ($modifiers =~ /:param(?:\(([^)]+)\))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers =~ /:param(?:\(([^)]+)\))?/) 4 0 4
7101 4 $field_info{is_param} = 1;
7102  
7103 4 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 1 3 4
7104 # Explicit parameter name
7105 1 $field_info{param_name} = $1;
7106 } else {
7107 # Implicit - field name is param name
7108 3 $field_info{param_name} = $name;
7109 }
7110  
7111 4 $self->_log(" FIELD: $name maps to parameter: $field_info{param_name}");
7112 }
7113  
7114 # Check for default value - must come before type constraint check
7115 4 ▶ if ($modifiers =~ /=\s*([^:;]+)(?::|;|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers =~ /=\s*([^:;]+)(?::|;|$)/) 1 3 4
7116 1 my $default = $1;
7117 1 $default =~ s/\s+$//;
7118 1 $field_info{_default} = $self->_clean_default_value($default, 1);
7119 1 $field_info{optional} = 1;
7120 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
7121 }
7122  
7123 # Check for type constraints
7124 4 ▶ if ($modifiers =~ /:isa\(([^)]+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers =~ /:isa\(([^)]+)\)/) 1 3 4
7125 1 $field_info{isa} = $1;
7126 1 $field_info{type} = 'object';
7127 1 $self->_log(" FIELD: $name has type constraint: $1");
7128 }
7129  
7130 4 $fields{$name} = \%field_info;
7131 }
7132  
7133 778 return \%fields;
7134 }
7135  
7136 # --------------------------------------------------
7137 # _merge_field_declarations
7138 #
7139 # Purpose: Integrate Perl 5.38 field declarations
7140 # that carry the :param attribute into
7141 # the code parameter hashref, so they
7142 # appear as constructor parameters in
7143 # the generated schema.
7144 #
7145 # Entry: $params - hashref of parameters
7146 # extracted from code analysis
7147 # (modified in place).
7148 # $fields - hashref of field declarations
7149 # as returned by
7150 # _extract_field_declarations.
7151 #
7152 # Exit: Returns nothing. Modifies $params
7153 # in place.
7154 #
7155 # Side effects: Logs merges to stdout when verbose
7156 # is set.
7157 #
7158 # Notes: Only fields with is_param => 1 are
7159 # merged. The param_name key in the
7160 # field (which may differ from the
7161 # field name if :param(name) was used)
7162 # determines the parameter key.
7163 # --------------------------------------------------
7164 0 ▶sub _merge_field_declarations {
Subroutine _merge_field_declarations: not called CC=8 SCAR=42.8
7165 0 my ($self, $params, $fields) = @_;
7166  
7167 0 foreach my $field_name (keys %$fields) {
7168 0 my $field = $fields->{$field_name};
7169  
7170 # Only process fields that are parameters
7171 0 ▶ next unless $field->{is_param};
Branch: 1 branch
BranchTrueFalseTotal
unless $field->{"is_param"} 0 0 0
7172  
7173 0 my $param_name = $field->{param_name};
7174  
7175 # Create or update parameter info
7176 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
7177 0 my $p = $params->{$param_name};
7178  
7179 # Merge field information into parameter
7180 0 ▶ $p->{_source} = SOURCE_FIELD unless $p->{_source};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"_source"} 0 0 0
7181 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
7182  
7183 0 ▶ if ($field->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"_default"}) 0 0 0
7184 0 $p->{_default} = $field->{_default};
7185 0 $p->{optional} = 1;
7186 }
7187  
7188 0 ▶ if ($field->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"isa"}) 0 0 0
7189 0 $p->{isa} = $field->{isa};
7190 0 $p->{type} = 'object';
7191 }
7192  
7193 0 $self->_log(" MERGED: Field $field_name -> parameter $param_name");
7194 }
7195 }
7196  
7197 # --------------------------------------------------
7198 # _extract_defaults_from_code
7199 #
7200 # Purpose: Scan a method body for default value
7201 # assignment patterns and populate the
7202 # optional and _default fields of
7203 # known parameters.
7204 #
7205 # Entry: $params - hashref of parameters
7206 # (modified in place).
7207 # $code - method body source string.
7208 # $method - method hashref, used for
7209 # constructor-specific
7210 # exclusions of $class and
7211 # $self.
7212 #
7213 # Exit: Returns nothing. Modifies $params
7214 # in place.
7215 #
7216 # Side effects: Logs detections to stdout when
7217 # verbose is set.
7218 #
7219 # Notes: Eight default patterns are tried.
7220 # Only parameters already present in
7221 # $params are updated — this method
7222 # does not add new parameters.
7223 # Falls back to extracting all @_
7224 # assignments if $params is empty
7225 # after the main pass.
7226 # --------------------------------------------------
7227 775 ▶sub _extract_defaults_from_code {
Subroutine _extract_defaults_from_code: called CC=35 SCAR=37.5
7228 775 my ($self, $params, $code, $method) = @_;
7229  
7230 # Pattern 1: my $param = value;
7231 775 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*([^;]+);/g) 952 775 1727
7232 952 my ($param, $value) = ($1, $2);
7233 952 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 945 7 952
7234 7 ▶ next if $value =~ /->/; # deref/method call, not a default value
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /->/ 1 6 7
7235  
7236 6 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
7237 6 $params->{$param}{optional} = 1;
7238 6 $self->_log(" CODE: $param has default: " . $self->_format_default($params->{$param}{_default}));
7239 }
7240  
7241 # Pattern 2: $param = value unless defined $param;
7242 775 ▶ 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 775 778
7243 3 my ($param, $value) = ($1, $2);
7244 3 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 3 3
7245  
7246 3 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
7247 3 $params->{$param}{optional} = 1;
7248 3 $self->_log(" CODE: $param has default (unless): " . $self->_format_default($params->{$param}{_default}));
7249 }
7250  
7251 # Pattern 3: $param = value unless $param;
7252 775 ▶ 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 775 776
7253 1 my ($param, $value) = ($1, $2);
7254 1 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 1 1
7255  
7256 1 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
7257 1 $params->{$param}{optional} = 1;
7258 1 $self->_log(" CODE: $param has default (unless): " . $self->_format_default($params->{$param}{_default}));
7259 }
7260  
7261 # Pattern 4: $param = $param || 'default';
7262 775 ▶ while ($code =~ /\$(\w+)\s*=\s*\$\1\s*\|\|\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*=\s*\$\1\s*\|\|\s*([^;]+);/g) 7 775 782
7263 7 my ($param, $value) = ($1, $2);
7264 7 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 7 7
7265  
7266 7 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
7267 7 $params->{$param}{optional} = 1;
7268 7 $self->_log(" CODE: $param has default (||): " . $self->_format_default($params->{$param}{_default}));
7269 }
7270  
7271 # Pattern 5: $param ||= 'default';
7272 775 ▶ while ($code =~ /\$(\w+)\s*\|\|=\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*\|\|=\s*([^;]+);/g) 19 775 794
7273 19 my ($param, $value) = ($1, $2);
7274 19 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 6 13 19
7275  
7276 13 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
7277 13 $params->{$param}{optional} = 1;
7278 13 $self->_log(" CODE: $param has default (||=): " . $self->_format_default($params->{$param}{_default}));
7279 }
7280  
7281 # Pattern 6: $param //= 'default';
7282 775 ▶ while ($code =~ /\$(\w+)\s*\/\/=\s*([^;]+);/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[\$(\w+)\s*//=\s*([^;]+);]g) 8 775 783
7283 8 my ($param, $value) = ($1, $2);
7284 8 ▶ next unless exists $params->{$param}; # Using -> because $params is a reference
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 8 8
7285  
7286 8 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
7287  
7288 8 $params->{$param}{optional} = 1;
7289 8 $self->_log(" CODE: $param has default (//=): " . $self->_format_default($params->{$param}{_default}));
7290 }
7291  
7292 # Pattern 7: $param = defined $param ? $param : 'default';
7293 775 ▶ 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 775 779
7294 4 my ($param, $value) = ($1, $2);
7295  
7296 # Create param entry if it doesn't exist
7297 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
7298  
7299 4 my $cleaned = $self->_clean_default_value($value, 1);
7300  
7301 4 $params->{$param}{_default} = $cleaned;
7302 4 $params->{$param}{optional} = 1;
7303 4 $self->_log(" CODE: $param has default (ternary): " . $self->_format_default($params->{$param}{_default}));
7304 }
7305  
7306 # Pattern 8: $param = $args{param} || 'default';
7307 775 ▶ 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 775 775
7308 0 my ($param, $value) = ($1, $2);
7309 0 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 0 0
7310  
7311 0 $params->{$param}{_default} = $self->_clean_default_value($value, 1);
7312 0 $params->{$param}{optional} = 1;
7313 0 $self->_log(" CODE: $param has default (from args): " . $self->_format_default($params->{$param}{_default}));
7314 }
7315  
7316 # Pattern for non-empty hashref
7317 775 ▶ while ($code =~ /\$(\w+)\s*\|\|=\s*\{[^}]+\}/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$(\w+)\s*\|\|=\s*\{[^}]+\}/gs) 1 775 776
7318 1 my $param = $1;
7319 1 ▶ next unless exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 0 1 1
7320  
7321 # Return empty hashref as placeholder (can't evaluate complex hashrefs)
7322 1 $params->{$param}{_default} = {};
7323 1 $params->{$param}{optional} = 1;
7324 1 $self->_log(" CODE: $param has hashref default (||=)");
7325 }
7326  
7327 # Fallback: extract parameters from classic Perl body styles
7328 # Only run if signature extraction found nothing AND the code does not use
7329 # the direct-index ($_[0]) style — that style is used for no-param methods
7330 # whose empty %params would otherwise trigger this fallback and pick up
7331 # my (...) = @_ from inner closures as if they were method params.
7332 # TODO: On constructors, use $class to help to determine the output type
7333 775 ▶ 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\]/) 385 390 775
Condition100%3/3
keys %{$params;} || $code =~ /my\s+\$(?:self|class|pkg|proto|klass)\s*=\s*\$_\[0\]/
l!l&&r!l&&!r
32268385
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
7334 385 my $position = 0;
7335  
7336 # Style 1: my ($a, $b) = @_;
7337 385 ▶ while ($code =~ /my\s*\(\s*([^)]+)\s*\)\s*=\s*\@_/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s*\(\s*([^)]+)\s*\)\s*=\s*\@_/g) 98 385 483
7338 98 my @vars = $1 =~ /\$(\w+)/g;
7339 98 foreach my $var (@vars) {
7340 98 ▶ 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) { } 98 0 98
Condition33%1/3
$position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
0098
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
7341 # Invocant — skip it
7342 98 delete $params->{$var};
7343 } else {
7344 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
7345 0 $self->_log(" CODE: $var extracted from \@_ list assignment");
7346 }
7347 }
7348 }
7349  
7350 # Style 2: my $x = shift;
7351 385 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*shift\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*shift\b/g) 66 385 451
7352 66 my $var = $1;
7353 66 ▶ 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) { } 23 43 66
Condition66%2/3
$position == 0 and $var =~ /^(self|class|pkg|proto|klass)$/i
!ll&&!rl&&r
04323
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
7354 # Invocant — skip it
7355 23 delete $params->{$var};
7356 } else {
7357 43 ▶ $params->{$var} ||= { position => $position++ };
Condition50%1/2
$params->{$var} ||= {"position", $position++}
l!l
043
MC/DC0%0/1
$params->{$var} ||= {"position", $position++}
$params->{$var}
Truth table50%1/2
A
A= $params->{$var}
Aresult
11
00
7358 43 $self->_log(" CODE: $var is extracted from shift");
7359 }
7360 }
7361  
7362 # Style 3: my $x = $_[0];
7363 385 ▶ while ($code =~ /my\s+\$(\w+)\s*=\s*\$_\[(\d+)\]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /my\s+\$(\w+)\s*=\s*\$_\[(\d+)\]/g) 24 385 409
7364 24 my ($var, $index) = ($1, $2);
7365 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
7366 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
7367 24 $self->_log(" CODE: $var is extracted from \$_\[$index\]");
7368 }
7369 }
7370 }
7371 }
7372  
7373 # --------------------------------------------------
7374 # _format_default
7375 #
7376 # Purpose: Format a default value for display
7377 # in verbose log output.
7378 #
7379 # Entry: $default - the default value to
7380 # format. May be undef,
7381 # a scalar, a hashref, or
7382 # an arrayref.
7383 #
7384 # Exit: Returns a display string: 'undef'
7385 # for undef, 'HASH ref' / 'ARRAY ref'
7386 # for references, or the value itself
7387 # for scalars.
7388 #
7389 # Side effects: None.
7390 # --------------------------------------------------
7391 47 ▶sub _format_default {
Subroutine _format_default: called CC=3 SCAR=11.0
7392 47 my ($self, $default) = @_;
7393 47 ▶ return 'undef' unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 6 41 47
7394 41 ▶ return ref($default) . ' ref' if ref($default);
Branch: 1 branch
BranchTrueFalseTotal
if ref $default 10 31 41
7395 31 return $default;
7396 }
7397  
7398 # Resolve the right-hand side of a length comparison: a literal or a
7399 # Readonly/use-constant name.
7400 35 ▶sub _length_constraint_value {
Subroutine _length_constraint_value: called CC=3 SCAR=11.0
7401 35 my ($self, $rhs) = @_;
7402  
7403 35 ▶ return $rhs + 0 if $rhs =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $rhs =~ /^\d+$/ 17 18 35
7404 18 ▶ return undef unless $rhs =~ /^\$(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rhs =~ /^\$(\w+)$/ 0 18 18
7405 18 return $self->_module_constants()->{$1};
7406 }
7407  
7408 # Record the accepted length range. When the comparison is an error
7409 # condition (an `if` guard), the accepted range is its negation.
7410 35 ▶sub _record_length_constraint {
Subroutine _record_length_constraint: called CC=14 SCAR=37.0
7411 35 my ($self, $p, $op, $val, $invert) = @_;
7412  
7413 35 ▶ if($invert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invert) 7 28 35
7414 7 my %negated = ('<' => '>=', '<=' => '>', '>' => '<=', '>=' => '<');
7415 7 $op = $negated{$op};
7416 }
7417  
7418 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
7419 0 my $max = $val - 1;
7420 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
7421 } elsif($op eq '<=') {
7422 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
7423 } elsif($op eq '>') {
7424 0 my $min = $val + 1;
7425 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
7426 } elsif($op eq '>=') {
7427 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
7428 }
7429 }
7430  
7431 # Return ($op, $value) when a guard condition is exactly one length
7432 # comparison, or an empty list when the comparison is only one operand of a
7433 # compound condition. A compound condition enforces no bound of its own:
7434 # `croak ... if length($t) < 5 || $other` still croaks for long text when
7435 # $other is true, and `... if length($t) < 5 && $other` accepts short text
7436 # when $other is false. Only a condition that is nothing but the comparison
7437 # makes the negation of that comparison a real constraint.
7438 320 ▶sub _whole_condition_length_guard {
Subroutine _whole_condition_length_guard: called CC=10 SCAR=23.5
7439 320 my ($self, $param, $condition) = @_;
7440  
7441 320 $condition =~ s/^\s+//;
7442 320 $condition =~ s/\s+$//;
7443  
7444 # Drop balanced outer parentheses, as in `if (length($t) < 5)`.
7445 320 ▶ while($condition =~ /^\((.*)\)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition =~ /^\((.*)\)$/s) 1 320 321
7446 1 my $inner = $1;
7447 1 my $depth = 0;
7448 1 my $balanced = 1;
7449 1 foreach my $char (split(//, $inner)) {
7450 15 ▶ if($char eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '(') { } 1 14 15
elsif ($char eq ')') { } 1 13 14
7451 1 $depth++;
7452 } elsif($char eq ')') {
7453 1 $depth--;
7454 1 ▶ if($depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0) 0 1 1
7455 0 $balanced = 0;
7456 0 last;
7457 }
7458 }
7459 }
7460 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
7461 1 $condition = $inner;
7462 1 $condition =~ s/^\s+//;
7463 1 $condition =~ s/\s+$//;
7464 }
7465  
7466 320 ▶ 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+)$/ 285 35 320
7467  
7468 35 my ($op, $rhs) = ($1, $2);
7469 35 my $val = $self->_length_constraint_value($rhs);
7470 35 ▶ return unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 35 35
7471  
7472 35 return ($op, $val);
7473 }
7474  
7475 # --------------------------------------------------
7476 # _module_constants
7477 #
7478 # Purpose: Build and cache a hash of numeric
7479 # constant values declared in the target
7480 # module source, covering both Readonly
7481 # lexicals and 'use constant' barewords.
7482 # Used by _analyze_parameter_constraints
7483 # to resolve right-hand sides like
7484 # $MIN_NAME_LEN that are not literals.
7485 #
7486 # Entry: None (uses $self->{_document}).
7487 #
7488 # Exit: Returns a hashref { NAME => value }.
7489 # Returns {} if the PPI document is not
7490 # yet loaded.
7491 #
7492 # Side effects: Caches result in $self->{_constants}.
7493 # --------------------------------------------------
7494 18 ▶sub _module_constants {
Subroutine _module_constants: called CC=5 SCAR=16.7
7495 18 my ($self) = @_;
7496 18 ▶ return $self->{_constants} if exists $self->{_constants};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_constants"} 13 5 18
7497  
7498 5 my %c;
7499 5 my $doc = $self->{_document};
7500 5 ▶ unless ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 0 5 5
7501 0 $self->{_constants} = \%c;
7502 0 return \%c;
7503 }
7504  
7505 5 my $src = $doc->serialize();
7506  
7507 # Readonly my $CONST => numeric_value;
7508 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
7509 34 $c{$1} = $2;
7510 }
7511  
7512 # use constant CONST => numeric_value;
7513 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
7514 0 $c{$1} = $2;
7515 }
7516  
7517 5 $self->{_constants} = \%c;
7518 5 return \%c;
7519 }
7520  
7521 # --------------------------------------------------
7522 # _analyze_parameter_constraints
7523 #
7524 # Purpose: Infer min, max, and regex match
7525 # constraints for a single parameter
7526 # from length checks, numeric
7527 # comparisons, and regex match
7528 # patterns in the method body.
7529 #
7530 # Entry: $p_ref - reference to the parameter
7531 # hashref (modified in place).
7532 # $param - parameter name string.
7533 # $code - method body source string.
7534 #
7535 # Exit: Returns nothing. Modifies the
7536 # referenced parameter hashref.
7537 #
7538 # Side effects: Logs detections to stdout when
7539 # verbose is set.
7540 #
7541 # Notes: Numeric comparisons that appear
7542 # inside die/croak guard conditions
7543 # are excluded to avoid inferring
7544 # invalid-input ranges as valid
7545 # constraints.
7546 # --------------------------------------------------
7547 510 ▶sub _analyze_parameter_constraints {
Subroutine _analyze_parameter_constraints: called CC=26 SCAR=42.5
7548 510 my ($self, $p_ref, $param, $code) = @_;
7549 510 my $p = $$p_ref;
7550  
7551 # Do not treat comparisons inside die/croak/confess as valid constraints
7552 510 my $guarded = 0;
7553 510 ▶ 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) 56 454 510
7554 56 $guarded = 1;
7555 }
7556  
7557 # Length checks for strings. A comparison only describes the accepted
7558 # range when it guards an error: `croak ... unless length >= 5` accepts
7559 # >= 5, while `croak ... if length < 5` also accepts >= 5. Reading the
7560 # second condition literally would invent a max of 4 and can contradict
7561 # a min inferred from the first form.
7562 510 ▶ $p->{type} ||= 'string' if $code =~ /length\s*\(\s*\$$param\s*\)/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /length\s*\(\s*\$$param\s*\)/ 31 479 510
Condition50%1/2
$p->{"type"} ||= "string"
l!l
031
MC/DC0%0/1
$p->{"type"} ||= "string"
$p->{"type"}
Truth table50%1/2
A
A= $p->{"type"}
Aresult
11
00
7563  
7564 510 my $postfix_guards = $code;
7565 510 ▶ 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) 320 510 830
7566 320 my ($guard, $condition) = ($1, $2);
7567 320 my ($op, $val) = $self->_whole_condition_length_guard($param, $condition);
7568 320 ▶ next unless defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 285 35 320
7569 35 $self->_record_length_constraint($p, $op, $val, ($guard eq 'if'));
7570 35 $self->_log(" CODE: $param length constraint from $guard guard: $op $val");
7571 }
7572  
7573 510 my $block_guards = $code;
7574 510 ▶ 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 510 510
7575 0 my ($guard, $op, $rhs, $block) = ($1, $2, $3, $4);
7576 0 ▶ next unless $block =~ /\b(?:croak|die|confess)\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $block =~ /\b(?:croak|die|confess)\b/ 0 0 0
7577 0 my $val = $self->_length_constraint_value($rhs);
7578 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
7579 0 $self->_record_length_constraint($p, $op, $val, ($guard eq 'if'));
7580 0 $self->_log(" CODE: $param length constraint from $guard block: $op $rhs");
7581 }
7582  
7583 # Numeric range checks (only if NOT part of error guard)
7584 510 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $guarded and $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/) 16 494 510
Condition100%3/3
not $guarded and $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/
!ll&&!rl&&r
5643816
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
7585 !$guarded
7586 && $code =~ /\$$param\s*([<>]=?)\s*([+-]?(?:\d+\.?\d*|\.\d+))/
7587 ) {
7588 16 my ($op, $val) = ($1, $2);
7589 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
7590  
7591 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
7592 # Only set max if it tightens the range
7593 3 ▶ my $max = ($op eq '<') ? $val - 1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 3 0 3
7594 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
7595 ▶ } 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
7596 13 ▶ my $min = ($op eq '>') ? $val + 1 : $val;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '>' ? : 11 2 13
7597 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
7598 }
7599 }
7600  
7601 # Regex pattern matching with better capture
7602 510 ▶ if ($code =~ /\$$param\s*=~\s*((?:qr?\/[^\/]+\/|\$[\w:]+|\$\{\w+\}))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[\$$param\s*=~\s*((?:qr?/[^/]+/|\$[\w:]+|\$\{\w+\}))]) 0 510 510
7603 0 my $pattern = $1;
7604 0 ▶ $p->{type} ||= 'string';
Condition0%0/2
$p->{"type"} ||= "string"
l!l
00
MC/DC0%0/1
$p->{"type"} ||= "string"
$p->{"type"}
Truth table0%0/2
A
A= $p->{"type"}
Aresult
11
00
7605  
7606 # Clean up the pattern if it's a straightforward regex
7607 0 ▶ if ($pattern =~ /^qr?\/([^\/]+)\/$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ m[^qr?/([^/]+)/$]) { } 0 0 0
7608 0 $p->{matches} = "/$1/";
7609 } else {
7610 0 $p->{matches} = $pattern;
7611 }
7612 0 $self->_log(" CODE: $param matches pattern: $p->{matches}");
7613 }
7614 }
7615  
7616 # --------------------------------------------------
7617 # _analyze_parameter_validation
7618 #
7619 # Purpose: Determine optionality and extract
7620 # default values for a single parameter
7621 # by analysing explicit required checks
7622 # (die/croak unless defined) and default
7623 # assignment patterns in the method body.
7624 #
7625 # Entry: $p_ref - reference to the parameter
7626 # hashref (modified in place).
7627 # $param - parameter name string.
7628 # $code - method body source string.
7629 #
7630 # Exit: Returns nothing. Modifies the
7631 # referenced parameter hashref.
7632 #
7633 # Side effects: Logs detections to stdout when
7634 # verbose is set.
7635 #
7636 # Notes: Explicit required checks take highest
7637 # priority and override any default
7638 # value detected earlier.
7639 # --------------------------------------------------
7640 510 ▶sub _analyze_parameter_validation {
Subroutine _analyze_parameter_validation: called CC=23 SCAR=34.1
7641 510 my ($self, $p_ref, $param, $code) = @_;
7642 510 my $p = $$p_ref;
7643  
7644 # Required/optional checks
7645 510 my $is_required = 0;
7646  
7647 # Die/croak if not defined
7648 510 ▶ 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) 67 443 510
7649 67 $is_required = 1;
7650 }
7651  
7652 # Extract default values with the new method
7653 510 my $default_value = $self->_extract_default_value($param, $code);
7654 510 ▶ if (defined $default_value && !exists $p->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default_value and not exists $p->{"_default"}) 1 509 510
Condition100%3/3
defined $default_value and not exists $p->{"_default"}
!ll&&!rl&&r
479301
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
7655 1 $p->{optional} = 1;
7656 1 $p->{_default} = $default_value;
7657  
7658 # Try to infer type from default value if not already set
7659 1 ▶ unless ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 1 0 1
7660 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
7661 1 ▶ $p->{type} = $default_value =~ /\./ ? 'number' : 'integer';
Branch: 1 branch
BranchTrueFalseTotal
$default_value =~ /\./ ? : 0 1 1
7662 } elsif (ref($default_value) eq 'ARRAY') {
7663 0 $p->{type} = 'arrayref';
7664 } elsif (ref($default_value) eq 'HASH') {
7665 0 $p->{type} = 'hashref';
7666 } elsif ($default_value eq 'undef') {
7667 0 $p->{type} = 'scalar'; # undef can be any scalar
7668 ▶ } 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
7669 0 $p->{type} = 'string';
7670 }
7671 }
7672  
7673 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
7674 }
7675  
7676 # Also check for simple default assignment without condition
7677 # Pattern: $param = 'value';
7678 510 ▶ 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) 97 413 510
Condition100%6/6
not $default_value || exists $p->{"_default"} and $code =~ /\$$param\s*=\s*([^;{}]+?)(?:\s*[;}])/s
!ll&&!rl&&r
4237197
$default_value || exists $p->{'_default'}
l!l&&r!l&&!r
2814468
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
7679 97 my $assignment = $1;
7680 # Make sure it's not part of a larger expression
7681 97 ▶ if ($assignment !~ /\$$param/ && $assignment !~ /^shift/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($assignment =~ /\$$param/ or $assignment =~ /^shift/) 54 43 97
Condition66%2/3
$assignment =~ /\$$param/ or $assignment =~ /^shift/
l!l&&r!l&&!r
04354
MC/DC50%1/2
$assignment =~ /\$$param/ or $assignment =~ /^shift/
$assignment =~ /\$$param/ $assignment =~ /^shift/
Truth table66%2/3
A or B
A= $assignment =~ /\$$param/B= $assignment =~ /^shift/
ABresult
1X1
011
000
7682 54 my $possible_default = $assignment;
7683 54 $possible_default =~ s/\s*;\s*$//;
7684 54 $possible_default = $self->_clean_default_value($possible_default);
7685 54 ▶ if (defined $possible_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $possible_default) 21 33 54
7686 21 $p->{_default} = $possible_default;
7687 21 $p->{optional} = 1;
7688 21 $self->_log(" CODE: $param has unconditional default: $possible_default");
7689 }
7690 }
7691 }
7692  
7693 # A bare truthiness test ("if($dir) {...}") is a presence test: the
7694 # routine does something different when nothing was passed, so the
7695 # argument can be omitted. An explicit required check wins over this.
7696 510 ▶ 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 509 510
Condition100%3/3
not $is_required and $code =~ /\b(?:if|unless)\s*\(\s*\$$param\s*\)/
!ll&&!rl&&r
674421
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
7697 1 $p->{optional} = 1;
7698 1 $self->_log(" CODE: $param is optional (only used when truthy)");
7699 }
7700  
7701 # Explicit required check overrides default detection
7702 510 ▶ if ($is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required) 67 443 510
7703 67 $p->{optional} = 0;
7704 67 ▶ delete $p->{_default} if exists $p->{_default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{"_default"} 16 51 67
7705 67 $self->_log(" CODE: $param is required (validation check)");
7706 }
7707 }
7708  
7709 # --------------------------------------------------
7710 # _positional_pod_input
7711 #
7712 # Purpose: Recognise a lone documented input parameter that is really the
7713 # first positional argument under another name.
7714 #
7715 # Entry: $pod - parameters from _analyze_pod; mutated.
7716 # $code_params - parameters from code analysis; mutated.
7717 # $code - the subroutine body as source text.
7718 #
7719 # Exit: Returns the documented name when it is treated as position 0,
7720 # otherwise undef.
7721 #
7722 # Notes: Two or more documented names describe a hash of arguments, and
7723 # a body that reads an argument hash (my %args = @_, $args->{...},
7724 # $_[0]{...}) keeps the named style even with one name. A body
7725 # that shows no positional use at all -- a wrapper forwarding @_,
7726 # say -- proves nothing either way, so it stays named rather than
7727 # guessing.
7728 #
7729 # Side effects: On success sets position 0, drops stray documented names and
7730 # replaces the code analysis with the one argument, renamed.
7731 # --------------------------------------------------
7732 755 ▶sub _positional_pod_input {
Subroutine _positional_pod_input: called CC=7 SCAR=19.5
7733 755 my ($self, $pod, $code_params, $code) = @_;
7734  
7735 755 ▶ my @documented = grep { ($pod->{$_}{_source} // '') eq SOURCE_INPUT_SPEC } keys %$pod;
Condition100%2/2
$pod->{$_}{"_source"} // ""
l!l
44341
MC/DC100%1/1
$pod->{$_}{"_source"} // ""
$pod->{$_}{"_source"}
Truth table100%2/2
A
A= $pod->{$_}{"_source"}
Aresult
11
00
7736 755 ▶ return undef unless @documented == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @documented == 1 584 171 755
7737 171 ▶ return undef unless $self->_argument_style($code) eq 'positional';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_argument_style($code) eq "positional" 50 121 171
7738  
7739 121 my ($name) = @documented;
7740 121 $pod->{$name}{position} = 0;
7741  
7742 # The body names the same argument however it likes, so carry its facts
7743 # (a directory tested with -d, say) to the documented name.
7744 121 my @code_names = keys %$code_params;
7745 121 ▶ my $source = $code_params->{$name} ? $name
Branch: 2 branches
BranchTrueFalseTotal
$code_params->{$name} ? : 117 4 121
@code_names == 1 ? : 0 4 4
7746 : (@code_names == 1 ? $code_names[0] : undef);
7747  
7748 # Empty list, not {}: a hashref here would become a one-element list and
7749 # land in the hash as a "HASH(0x...)" key.
7750 121 ▶ %$code_params = $source ? ($name => $code_params->{$source}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$source ? : 117 4 121
7751 121 %$pod = ($name => $pod->{$name});
7752 121 $self->_log(" POD: single Input spec '$name' describes a positional argument");
7753  
7754 121 return $name;
7755 }
7756  
7757 # --------------------------------------------------
7758 # _argument_style
7759 #
7760 # Purpose: Classify how a subroutine reads its arguments.
7761 #
7762 # Entry: $code - the subroutine body as source text.
7763 #
7764 # Exit: 'named' when the body reads an argument hash, 'positional'
7765 # when it reads an argument by position, '' when neither shows.
7766 #
7767 # Notes: $self->{...} is object state, not an argument, so it is removed
7768 # before looking for hash dereferences. '' is not 'positional':
7769 # a wrapper that only forwards @_ proves nothing, so it keeps the
7770 # named style these heuristics started from. An invocant shift
7771 # is not an argument.
7772 #
7773 # Side effects: None.
7774 # --------------------------------------------------
7775 171 ▶sub _argument_style {
Subroutine _argument_style: called CC=9 SCAR=22.5
7776 171 my ($self, $code) = @_;
7777  
7778 171 ▶ return '' unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 171 171
7779  
7780 171 my $body = $code;
7781 171 $body =~ s/\$self\s*->\s*\{[^}]*\}//g;
7782  
7783 171 ▶ return 'named' if($body =~ /\bmy\s+%\w+\s*=[^;]*\@_/s);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\bmy\s+%\w+\s*=[^;]*\@_/s 0 171 171
7784 171 ▶ 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 5 166 171
7785 166 ▶ return 'named' if($body =~ /\$_\s*\[\s*\d+\s*\]\s*(?:->\s*)?\{/);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\$_\s*\[\s*\d+\s*\]\s*(?:->\s*)?\{/ 0 166 166
7786 166 ▶ return 'named' if($body =~ /\$\w+\s*->\s*\{/);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\$\w+\s*->\s*\{/ 45 121 166
7787  
7788 121 ▶ return 'positional' if($body =~ /\$_\s*\[\s*\d+\s*\]/);
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\$_\s*\[\s*\d+\s*\]/ 22 99 121
7789 99 ▶ 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 66 99
7790 66 ▶ 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 66 0 66
7791  
7792 0 return '';
7793 }
7794  
7795 # --------------------------------------------------
7796 # _merge_parameter_analyses
7797 #
7798 # Purpose: Merge parameter information from POD,
7799 # code, and signature analysis into a
7800 # single authoritative parameter hashref
7801 # for each parameter.
7802 #
7803 # Entry: $pod - hashref of parameters from POD
7804 # analysis.
7805 # $code - hashref of parameters from
7806 # code analysis.
7807 # $sig - hashref of parameters from
7808 # signature analysis (optional,
7809 # defaults to empty hashref).
7810 #
7811 # Exit: Returns a merged hashref of parameter
7812 # name to spec hashref. Each spec has
7813 # all available information combined,
7814 # with POD taking highest priority,
7815 # code second, and signature filling
7816 # remaining gaps.
7817 #
7818 # Side effects: Logs merged parameter details to
7819 # stdout when verbose is set.
7820 #
7821 # Notes: Position is determined by majority
7822 # vote across all sources, with the
7823 # lowest position winning ties. Optional
7824 # status is determined by
7825 # _determine_optional_status. The single
7826 # _source provenance marker is retained on
7827 # each merged parameter.
7828 # --------------------------------------------------
7829 # --------------------------------------------------
7830 # _sanitize_parameter_names
7831 #
7832 # Purpose: Drop keys that cannot be parameter names, and report them.
7833 #
7834 # Entry: $params - hashref keyed by parameter name; modified in place.
7835 # $where - the analysis stage, for the warning text.
7836 #
7837 # Exit: Number of keys dropped.
7838 #
7839 # Notes: A hashref used where a list of key/value pairs was meant
7840 # stringifies to "HASH(0x...)" and silently becomes a parameter.
7841 # Such a key is never a variable the routine can read, so the
7842 # schema would test an argument that does not exist. Perl hash
7843 # keys are always strings, so the string form is what we see.
7844 #
7845 # Side effects: Emits a carp warning for each dropped key.
7846 # --------------------------------------------------
7847 4353 ▶sub _sanitize_parameter_names {
Subroutine _sanitize_parameter_names: called CC=5 SCAR=16.2
7848 4353 my ($self, $params, $where) = @_;
7849  
7850 4353 my $dropped = 0;
7851 4353 for my $name (keys %$params) {
7852 3009 ▶ 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
203007
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
023007
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
7853 || $name =~ /\A(?:HASH|ARRAY|SCALAR|CODE|GLOB|REF|REGEXP|Regexp|LVALUE|VSTRING|FORMAT|IO)\(0x[0-9a-fA-F]+\)\z/
7854 || $name =~ /\s/;
7855 3009 ▶ next unless $invalid;
Branch: 1 branch
BranchTrueFalseTotal
unless $invalid 3007 2 3009
7856  
7857 2 carp(__PACKAGE__ . ": ignoring parameter key '$name' from $where: not a parameter name");
7858 2 delete $params->{$name};
7859 2 $dropped++;
7860 }
7861  
7862 4353 return $dropped;
7863 }
7864  
7865 755 ▶sub _merge_parameter_analyses {
Subroutine _merge_parameter_analyses: called CC=50 SCAR=45.3
7866 755 my ($self, $pod, $code, $sig) = @_;
7867  
7868 # Belt and braces: every analysis should already be clean, but a merge is
7869 # the last point before these keys become schema parameters.
7870 $self->_sanitize_parameter_names($_, 'parameter merge')
7871 755 for grep { ref($_) eq 'HASH' } ($pod, $code, $sig);
7872  
7873 755 my %merged;
7874  
7875 755 my @constraint_keys = qw(min max memberof matches nomatch notmemberof);
7876 755 my %is_constraint = map { $_ => 1 } @constraint_keys;
7877  
7878 # Start with all parameters from all sources
7879 755 my %all_params = map { $_ => 1 } (keys %$pod, keys %$code, keys %$sig);
7880  
7881 755 foreach my $param (keys %all_params) {
7882 657 my $p = $merged{$param} = {};
7883  
7884 # Collect position from all sources
7885 657 my @positions;
7886 657 ▶ push @positions, $pod->{$param}{position} if $pod->{$param} && defined $pod->{$param}{position};
Branch: 1 branch
BranchTrueFalseTotal
if $pod->{$param} and defined $pod->{$param}{"position"} 217 440 657
Condition100%3/3
$pod->{$param} and defined $pod->{$param}{"position"}
!ll&&!rl&&r
173267217
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
7887 657 ▶ 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 657 657
Condition33%1/3
$sig->{$param} and defined $sig->{$param}{"position"}
!ll&&!rl&&r
65700
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
7888 657 ▶ push @positions, $code->{$param}{position} if $code->{$param} && defined $code->{$param}{position};
Branch: 1 branch
BranchTrueFalseTotal
if $code->{$param} and defined $code->{$param}{"position"} 485 172 657
Condition66%2/3
$code->{$param} and defined $code->{$param}{"position"}
!ll&&!rl&&r
1720485
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
7889  
7890 # Use the most common position, or lowest if tie
7891 657 ▶ if (@positions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@positions) 490 167 657
7892 490 my %pos_count;
7893 490 $pos_count{$_}++ for @positions;
7894 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
7895 490 ▶ $p->{position} = $best_pos unless(exists($p->{position}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p->{"position"} 490 0 490
7896 }
7897  
7898 # POD has highest priority for type info and explicit declarations
7899 657 ▶ if ($pod->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod->{$param}) 484 173 657
7900 484 %$p = (%$p, %{$pod->{$param}});
7901 484 ▶ $p->{_source} //= SOURCE_POD;
Condition100%2/2
$p->{"_source"} //= "pod"
l!l
44341
MC/DC100%1/1
$p->{"_source"} //= "pod"
$p->{"_source"}
Truth table100%2/2
A
A= $p->{"_source"}
Aresult
11
00
7902  
7903 # Record which source each constraint came from, so a documented
7904 # minimum does not make a maximum enforced by the code tolerant.
7905 484 ▶ my $source = $pod->{$param}{_source} // SOURCE_POD;
Condition100%2/2
$pod->{$param}{"_source"} // "pod"
l!l
44341
MC/DC100%1/1
$pod->{$param}{"_source"} // "pod"
$pod->{$param}{"_source"}
Truth table100%2/2
A
A= $pod->{$param}{"_source"}
Aresult
11
00
7906 484 for my $key (@constraint_keys) {
7907 2904 ▶ next unless exists($pod->{$param}{$key}) && defined($pod->{$param}{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pod->{$param}{$key} and defined $pod->{$param}{$key} 2804 100 2904
Condition66%2/3
exists $pod->{$param}{$key} and defined $pod->{$param}{$key}
!ll&&!rl&&r
28040100
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
7908 100 ▶ $p->{_constraint_source}{$key} //= $source;
Condition33%1/3
$p->{"_constraint_source"}{$key} //= $source
l!l&&r!l&&!r
01000
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
7909 }
7910 }
7911  
7912 # Code analysis adds concrete evidence (but doesn't override POD explicit types)
7913 657 ▶ if ($code->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->{$param}) 485 172 657
7914 485 foreach my $key (keys %{$code->{$param}}) {
7915 1990 ▶ next if $key eq '_source';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_source" 418 1572 1990
7916 1572 ▶ next if $key eq 'position';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "position" 485 1087 1572
7917 # Formal input-spec declared this param without a type — author
7918 # intentionally left it unconstrained; don't let code heuristics
7919 # silently fill in a type that would cause wrong-type die tests.
7920 1087 ▶ next if $key eq 'type' && $pod->{$param} && ($pod->{$param}{_source} // '') eq SOURCE_INPUT_SPEC && !defined $pod->{$param}{type};
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "type" and $pod->{$param} and ($pod->{$param}{"_source"} // "") eq "input_spec" and not defined $pod->{$param}{"type"} 0 1087 1087
Condition90%10/11
$key eq "type" and $pod->{$param} and ($pod->{$param}{"_source"} // "") eq "input_spec" and not defined $pod->{$param}{"type"}
!ll&&!rl&&r
9671200
$key eq "type" and $pod->{$param} and ($pod->{$param}{"_source"} // "") eq "input_spec"
!ll&&!rl&&r
91255120
$key eq "type" and $pod->{$param}
!ll&&!rl&&r
82092175
$pod->{$param}{"_source"} // ""
l!l
15520
MC/DC0%0/4
$key eq "type" and $pod->{$param} and ($pod->{$param}{"_source"} // "") eq "input_spec" and not defined $pod->{$param}{"type"}
$key eq "type" $pod->{$param} $pod->{$param}{"_source"} not defined $pod->{$param}{"type"}
Truth table80%4/5
A and B and C and not(D)
A= $key eq "type"B= $pod->{$param}C= $pod->{$param}{"_source"}D= not defined $pod->{$param}{"type"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
7921  
7922 # Only override if POD didn't provide this info or it's a stronger signal
7923 1087 my $from_pod = exists $pod->{$param};
7924 1087 ▶ 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") 751 336 1087
Condition91%11/12
not exists $p->{$key} or $key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string"
l!l&&r!l&&!r
7456336
$key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string"
!ll&&!rl&&r
307296
$key eq "type" and $from_pod and $p->{"type"} eq "string"
!ll&&!rl&&r
18112635
$key eq "type" and $from_pod
!ll&&!rl&&r
1810161
MC/DC80%4/5
not exists $p->{$key} or $key eq "type" and $from_pod and $p->{"type"} eq "string" and $code->{$param}{$key} ne "string"
not exists $p->{$key} $key eq "type" $from_pod $p->{"type"} eq "string" $code->{$param}{$key} ne "string"
Truth table83%5/6
not(A) or B and C and D and E
A= not exists $p->{$key}B= $key eq "type"C= $from_podD= $p->{"type"} eq "string"E= $code->{$param}{$key} ne "string"
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
7925 ($key eq 'type' && $from_pod && $p->{type} eq 'string' &&
7926 $code->{$param}{$key} ne 'string')) {
7927 751 $p->{$key} = $code->{$param}{$key};
7928 751 ▶ $p->{_constraint_source}{$key} = SOURCE_CODE if $is_constraint{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $is_constraint{$key} 43 708 751
7929 }
7930 }
7931 485 ▶ $p->{_source} //= $code->{$param}{_source};
Condition100%3/3
$p->{"_source"} //= $code->{$param}{"_source"}
l!l&&r!l&&!r
31215617
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
7932 }
7933  
7934 # Signature fills in remaining gaps
7935 657 ▶ if ($sig->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->{$param}) 0 657 657
7936 0 foreach my $key (keys %{$sig->{$param}}) {
7937 0 ▶ next if $key eq '_source';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_source" 0 0 0
7938 0 ▶ next if $key eq 'position';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "position" 0 0 0
7939 0 ▶ next if defined($p->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{$key} 0 0 0
7940 0 $p->{$key} = $sig->{$param}{$key};
7941 0 ▶ $p->{_constraint_source}{$key} = SOURCE_SIGNATURE if $is_constraint{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $is_constraint{$key} 0 0 0
7942 }
7943 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
7944 }
7945  
7946 # The source is the single provenance decision: validator-backed
7947 # parameters are type-checked, POD/input-spec parameters are
7948 # documentation, and code/signature/field parameters are inferred.
7949  
7950 # Handle optional field with better logic
7951 657 $self->_determine_optional_status($p, $pod->{$param}, $code->{$param});
7952 }
7953  
7954 # Debug logging
7955 755 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 2 753 755
7956 2 ▶ foreach my $param (sort { ($merged{$a}{position} || 999) <=> ($merged{$b}{position} || 999) } keys %merged) {
Condition50%2/4
$merged{$a}{'position'} || 999
l!l
01
$merged{$b}{'position'} || 999
l!l
10
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
7957 2 my $p = $merged{$param};
7958 $self->_log(" MERGED $param: " .
7959 'pos=' . (defined($p->{position}) ? $p->{position} : 'none') .
7960 ", type=" . ($p->{type} || 'none') .
7961 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
7962 }
7963 }
7964  
7965 755 return \%merged;
7966 }
7967  
7968 # --------------------------------------------------
7969 # _determine_optional_status
7970 #
7971 # Purpose: Set the optional field on a merged
7972 # parameter spec based on evidence from
7973 # POD and code analysis, with POD taking
7974 # highest priority.
7975 #
7976 # Entry: $merged_param - the merged parameter
7977 # hashref (modified in
7978 # place).
7979 # $pod_param - parameter spec from
7980 # POD analysis, or undef.
7981 # $code_param - parameter spec from
7982 # code analysis, or undef.
7983 #
7984 # Exit: Returns nothing. Sets or leaves
7985 # $merged_param->{optional}.
7986 #
7987 # Side effects: None.
7988 # --------------------------------------------------
7989 661 ▶sub _determine_optional_status {
Subroutine _determine_optional_status: called CC=6 SCAR=17.9
7990 661 my ($self, $merged_param, $pod_param, $code_param) = @_;
7991  
7992 661 ▶ my $pod_optional = $pod_param ? $pod_param->{optional} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pod_param ? : 488 173 661
7993 661 ▶ my $code_optional = $code_param ? $code_param->{optional} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$code_param ? : 489 172 661
7994  
7995 # Explicit POD declaration wins
7996 661 ▶ if (defined $pod_optional) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $pod_optional) { } 308 353 661
elsif (defined $code_optional) { } 310 43 353
elsif (keys %$merged_param > 0) { } 43 0 43
7997 308 $merged_param->{optional} = $pod_optional;
7998 }
7999 # Code validation evidence
8000 elsif (defined $code_optional) {
8001 310 $merged_param->{optional} = $code_optional;
8002 }
8003 # Default: if we have any info about the param, assume required
8004 elsif (keys %$merged_param > 0) {
8005 43 $merged_param->{optional} = 0;
8006 }
8007 # Otherwise leave undef (unknown)
8008 }
8009  
8010  
8011 # --------------------------------------------------
8012 # _calculate_input_confidence
8013 #
8014 # Purpose: Calculate a confidence score and level
8015 # for the input parameter analysis,
8016 # based on how much type, constraint,
8017 # and semantic information was inferred
8018 # for each parameter.
8019 #
8020 # Entry: $params - hashref of merged parameter
8021 # specs as produced by
8022 # _merge_parameter_analyses.
8023 #
8024 # Exit: Returns a hashref with keys:
8025 # level - one of: none,
8026 # very_low, low,
8027 # medium, high
8028 # score - numeric average
8029 # across all params
8030 # factors - arrayref of
8031 # human-readable
8032 # factor strings
8033 # per_parameter - hashref of per-
8034 # parameter score
8035 # and factor detail
8036 # Returns { level => 'none', ... } if
8037 # no parameters were found.
8038 #
8039 # Side effects: None.
8040 # --------------------------------------------------
8041 481 ▶sub _calculate_input_confidence {
Subroutine _calculate_input_confidence: called CC=24 SCAR=31.8
8042 481 my ($self, $params) = @_;
8043  
8044 481 my @factors; # Track all confidence factors
8045  
8046 481 ▶ return { level => 'none', factors => ['No parameters found'] } unless keys %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$params 178 303 481
8047  
8048 303 my $total_score = 0;
8049 303 my $count = 0;
8050 303 my %param_details; # Store per-parameter analysis
8051  
8052 303 foreach my $param (keys %$params) {
8053 455 my $p = $params->{$param};
8054 455 my $score = 0;
8055 455 my @param_factors;
8056  
8057 # Type information
8058 455 ▶ if ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'type'}) { } 455 0 455
8059 455 ▶ 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'}) { } 49 406 455
elsif ($p->{'type'} eq 'string') { } 162 244 406
Condition100%9/9
$p->{"type"} eq "string" and $p->{"min"} || $p->{"max"} || $p->{"matches"}
!ll&&!rl&&r
24416249
$p->{"min"} or $p->{"max"} or $p->{"matches"}
l!l&&r!l&&!r
3910162
$p->{"min"} or $p->{"max"}
l!l&&r!l&&!r
318172
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
8060 49 $score += 25;
8061 49 push @param_factors, "Type: constrained string (+25)";
8062 } elsif ($p->{type} eq 'string') {
8063 162 $score += 10;
8064 162 push @param_factors, "Type: plain string (+10)";
8065 } else {
8066 244 $score += 30;
8067 244 push @param_factors, "Type: $p->{type} (+30)";
8068 }
8069 } else {
8070 0 push @param_factors, "No type information (-0)";
8071 }
8072  
8073 # Constraints
8074 455 ▶ if (defined $p->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"min"}) 62 393 455
8075 62 $score += 15;
8076 62 push @param_factors, 'Has min constraint (+15)';
8077 }
8078 455 ▶ if (defined $p->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"max"}) 39 416 455
8079 39 $score += 15;
8080 39 push @param_factors, "Has max constraint (+15)";
8081 }
8082 455 ▶ if (defined $p->{optional}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"optional"}) 450 5 455
8083 450 $score += 20;
8084 450 push @param_factors, "Optional/required explicitly defined (+20)";
8085 }
8086 455 ▶ if ($p->{matches}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"matches"}) 15 440 455
8087 15 $score += 20;
8088 15 push @param_factors, 'Has regex pattern constraint (+20)';
8089 }
8090 455 ▶ if ($p->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"isa"}) 41 414 455
8091 41 $score += 25;
8092 41 push @param_factors, "Specific class constraint: $p->{isa} (+25)";
8093 }
8094  
8095 # Position information
8096 455 ▶ if (defined $p->{position}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{"position"}) 312 143 455
8097 312 $score += 10;
8098 312 push @param_factors, "Position defined: $p->{position} (+10)";
8099 }
8100  
8101 # Default value
8102 455 ▶ if (exists $p->{_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p->{"_default"}) 32 423 455
8103 32 $score += 10;
8104 32 push @param_factors, "Has default value (+10)";
8105 }
8106  
8107 # Semantic information
8108 455 ▶ if ($p->{semantic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"semantic"}) 33 422 455
8109 33 $score += 15;
8110 33 push @param_factors, "Semantic type: $p->{semantic} (+15)";
8111 }
8112  
8113 455 $param_details{$param} = {
8114 score => $score,
8115 factors => \@param_factors
8116 };
8117  
8118 455 $total_score += $score;
8119 455 $count++;
8120 }
8121  
8122 303 ▶ my $avg = $count ? ($total_score / $count) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$count ? : 303 0 303
8123  
8124 # Build summary factors
8125 303 ▶ push @factors, sprintf("Analyzed %d parameter%s", $count, $count == 1 ? '' : 's');
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 230 73 303
8126 303 push @factors, sprintf("Average confidence score: %.1f", $avg);
8127  
8128 # Add top contributing factors
8129 303 my @sorted_params = sort { $param_details{$b}{score} <=> $param_details{$a}{score} } keys %param_details;
8130  
8131 303 ▶ if (@sorted_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_params) 303 0 303
8132 303 my $highest = $sorted_params[0];
8133 303 my $highest_score = $param_details{$highest}{score};
8134 303 push @factors, sprintf("Highest scoring parameter: \$$highest (score: %d)", $highest_score);
8135  
8136 303 ▶ if (@sorted_params > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_params > 1) 73 230 303
8137 73 my $lowest = $sorted_params[-1];
8138 73 my $lowest_score = $param_details{$lowest}{score};
8139 73 push @factors, sprintf("Lowest scoring parameter: \$$lowest (score: %d)", $lowest_score);
8140 }
8141 }
8142  
8143 # Determine confidence level
8144 303 my $level;
8145 303 ▶ if ($avg >= $CONFIDENCE_HIGH_THRESHOLD) {
Branch: 3 branches
BranchTrueFalseTotal
if ($avg >= $CONFIDENCE_HIGH_THRESHOLD) { } 217 86 303
elsif ($avg >= $CONFIDENCE_MEDIUM_THRESHOLD) { } 66 20 86
elsif ($avg >= $CONFIDENCE_LOW_THRESHOLD) { } 18 2 20
8146 217 $level = $LEVEL_HIGH;
8147 217 push @factors, "High confidence: comprehensive type and constraint information";
8148 } elsif ($avg >= $CONFIDENCE_MEDIUM_THRESHOLD) {
8149 66 $level = $LEVEL_MEDIUM;
8150 66 push @factors, "Medium confidence: some type or constraint information present";
8151 } elsif ($avg >= $CONFIDENCE_LOW_THRESHOLD) {
8152 18 $level = $LEVEL_LOW;
8153 18 push @factors, "Low confidence: minimal type information";
8154 } else {
8155 2 $level = $LEVEL_VERY_LOW;
8156 2 push @factors, "Very low confidence: little to no type information";
8157 }
8158  
8159 return {
8160 303 level => $level,
8161 score => $avg,
8162 factors => \@factors,
8163 per_parameter => \%param_details
8164 };
8165 }
8166  
8167 # --------------------------------------------------
8168 # _calculate_output_confidence
8169 #
8170 # Purpose: Calculate a confidence score and level
8171 # for the output analysis based on how
8172 # much return type, value, class,
8173 # context, and error convention
8174 # information was determined.
8175 #
8176 # Entry: $output - the output hashref as built
8177 # by _analyze_output.
8178 #
8179 # Exit: Returns a hashref with keys:
8180 # level - one of: none, very_low,
8181 # low, medium, high
8182 # score - numeric confidence score
8183 # factors - arrayref of factor strings
8184 # Returns { level => 'none', ... } if
8185 # output is empty.
8186 #
8187 # Side effects: None.
8188 # --------------------------------------------------
8189 783 ▶sub _calculate_output_confidence {
Subroutine _calculate_output_confidence: called CC=17 SCAR=28.3
8190 783 my ($self, $output) = @_;
8191  
8192 783 my @factors;
8193  
8194 783 ▶ return { level => 'none', factors => ['No return information found'] } unless keys %$output;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$output 27 756 783
8195  
8196 756 my $score = 0;
8197  
8198 # Type information
8199 756 ▶ if ($output->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{'type'}) { } 749 7 756
8200 749 $score += 30;
8201 749 push @factors, "Return type defined: $output->{type} (+30)";
8202 } else {
8203 7 push @factors, 'No return type information (-0)';
8204 }
8205  
8206 # Specific value known
8207 756 ▶ if (defined $output->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output->{"value"}) 34 722 756
8208 34 $score += 30;
8209 34 push @factors, "Specific return value: $output->{value} (+30)";
8210 }
8211  
8212 # Class information for objects
8213 756 ▶ if ($output->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"isa"}) 90 666 756
8214 90 $score += 30;
8215 90 push @factors, "Returns specific class: $output->{isa} (+30)";
8216 }
8217  
8218 # Context-aware returns
8219 756 ▶ if ($output->{_context_aware}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_context_aware"}) 7 749 756
8220 7 $score += 20;
8221 7 push @factors, "Context-aware return (wantarray) (+20)";
8222  
8223 7 ▶ if ($output->{_list_context}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_list_context"}) 7 0 7
8224 7 push @factors, " List context: $output->{_list_context}{type}";
8225 }
8226 7 ▶ if ($output->{_scalar_context}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_scalar_context"}) 6 1 7
8227 6 push @factors, " Scalar context: $output->{_scalar_context}{type}";
8228 }
8229 }
8230  
8231 # Error handling information
8232 756 ▶ if ($output->{_error_return}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_return"}) 33 723 756
8233 33 $score += 15;
8234 33 push @factors, "Error return convention documented: $output->{_error_return} (+15)";
8235 }
8236  
8237 # Success/failure pattern
8238 756 ▶ if ($output->{_success_failure_pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_success_failure_pattern"}) 5 751 756
8239 5 $score += 10;
8240 5 push @factors, 'Success/failure pattern detected (+10)';
8241 }
8242  
8243 # Chainable methods
8244 756 ▶ if ($output->{_returns_self}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_returns_self"}) 11 745 756
8245 11 $score += 15;
8246 11 push @factors, "Chainable method (fluent interface) (+15)";
8247 }
8248  
8249 # Void context
8250 756 ▶ if ($output->{_void_context}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_void_context"}) 11 745 756
8251 11 $score += 20;
8252 11 push @factors, "Void context method (no meaningful return) (+20)";
8253 }
8254  
8255 # Exception handling
8256 756 ▶ if ($output->{_error_handling} && $output->{_error_handling}{exception_handling}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_handling"} and $output->{"_error_handling"}{"exception_handling"}) 20 736 756
Condition100%3/3
$output->{"_error_handling"} and $output->{"_error_handling"}{"exception_handling"}
!ll&&!rl&&r
7082820
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
8257 20 $score += 10;
8258 20 push @factors, 'Exception handling present (+10)';
8259 }
8260  
8261 756 push @factors, sprintf("Total output confidence score: %d", $score);
8262  
8263 # Determine confidence level
8264 756 my $level;
8265 756 ▶ if ($score >= $CONFIDENCE_HIGH_THRESHOLD) {
Branch: 3 branches
BranchTrueFalseTotal
if ($score >= $CONFIDENCE_HIGH_THRESHOLD) { } 126 630 756
elsif ($score >= $CONFIDENCE_MEDIUM_THRESHOLD) { } 59 571 630
elsif ($score >= $CONFIDENCE_LOW_THRESHOLD) { } 568 3 571
8266 126 $level = $LEVEL_HIGH;
8267 126 push @factors, "High confidence: detailed return type and behavior";
8268 } elsif ($score >= $CONFIDENCE_MEDIUM_THRESHOLD) {
8269 59 $level = $LEVEL_MEDIUM;
8270 59 push @factors, "Medium confidence: return type defined";
8271 } elsif ($score >= $CONFIDENCE_LOW_THRESHOLD) {
8272 568 $level = $LEVEL_LOW;
8273 568 push @factors, "Low confidence: minimal return information";
8274 } else {
8275 3 $level = $LEVEL_VERY_LOW;
8276 3 push @factors, 'Very low confidence: little return information';
8277 }
8278  
8279 return {
8280 756 level => $level,
8281 score => $score,
8282 factors => \@factors
8283 };
8284 }
8285  
8286 # --------------------------------------------------
8287 # _generate_confidence_report
8288 #
8289 # Purpose: Generate a human-readable text report
8290 # of all confidence factors for a
8291 # schema, for debugging and review
8292 # purposes.
8293 #
8294 # Entry: $schema - schema hashref containing
8295 # a populated _analysis key.
8296 #
8297 # Exit: Returns a multi-line string report,
8298 # or nothing if $schema->{_analysis}
8299 # is absent.
8300 #
8301 # Side effects: None.
8302 # --------------------------------------------------
8303 sub _generate_confidence_report
8304 0 ▶{
Subroutine _generate_confidence_report: not called CC=10 SCAR=47.0
8305 0 my ($self, $schema) = @_;
8306  
8307 0 ▶ return unless $schema->{_analysis};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"_analysis"} 0 0 0
8308  
8309 0 my $analysis = $schema->{_analysis};
8310 0 my @report;
8311  
8312 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
8313 0 push @report, '=' x 60;
8314 0 push @report, '';
8315  
8316 0 push @report, "Overall Confidence: " . uc($analysis->{overall_confidence});
8317 0 push @report, '';
8318  
8319 0 ▶ if ($analysis->{confidence_factors}{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($analysis->{"confidence_factors"}{"input"}) 0 0 0
8320 push @report, (
8321 "Input Parameters:",
8322 " Confidence Level: " . uc($analysis->{input_confidence})
8323 0 );
8324 0 foreach my $factor (@{$analysis->{confidence_factors}{input}}) {
8325 0 push @report, " - $factor";
8326 }
8327 0 push @report, '';
8328 }
8329  
8330 0 ▶ if ($analysis->{confidence_factors}{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($analysis->{"confidence_factors"}{"output"}) 0 0 0
8331 push @report, 'Return Value:',
8332 0 " Confidence Level: " . uc($analysis->{output_confidence});
8333 0 foreach my $factor (@{$analysis->{confidence_factors}{output}}) {
8334 0 push @report, " - $factor";
8335 }
8336 0 push @report, '';
8337 }
8338  
8339 0 ▶ if ($analysis->{per_parameter_scores}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($analysis->{"per_parameter_scores"}) 0 0 0
8340 0 push @report, 'Per-Parameter Analysis:';
8341 0 foreach my $param (sort keys %{$analysis->{per_parameter_scores}}) {
8342 0 my $details = $analysis->{per_parameter_scores}{$param};
8343 0 push @report, " \$$param (score: $details->{score}):";
8344 0 foreach my $factor (@{$details->{factors}}) {
8345 0 push @report, " - $factor";
8346 }
8347 }
8348 0 push @report, '';
8349 }
8350  
8351 0 return join("\n", @report);
8352 }
8353  
8354 # --------------------------------------------------
8355 # _generate_notes
8356 #
8357 # Purpose: Generate human-readable advisory notes
8358 # about parameters whose type or
8359 # optionality could not be determined,
8360 # to guide manual schema review.
8361 #
8362 # Entry: $params - hashref of merged parameter
8363 # specs.
8364 #
8365 # Exit: Returns an arrayref of note strings.
8366 # Returns an empty arrayref if all
8367 # parameters have known types and
8368 # optionality.
8369 #
8370 # Side effects: None.
8371 # --------------------------------------------------
8372 772 ▶sub _generate_notes {
Subroutine _generate_notes: called CC=4 SCAR=13.9
8373 772 my ($self, $params) = @_;
8374  
8375 772 my @notes;
8376  
8377 772 foreach my $param (sort keys %$params) {
8378 683 my $p = $params->{$param};
8379  
8380 683 ▶ unless ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 101 582 683
8381 101 push @notes, "$param: type unknown - please review - will set to 'string' as a default";
8382 }
8383  
8384 683 ▶ unless (defined $p->{optional}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p->{"optional"}) 17 666 683
8385 17 push @notes, "$param: optional status unknown";
8386 # Don't automatically set - let it be undef if we don't know
8387 }
8388 }
8389  
8390 772 return \@notes;
8391 }
8392  
8393 # --------------------------------------------------
8394 # _set_defaults
8395 #
8396 # Purpose: Apply default type values to any
8397 # parameters in a schema mode (input
8398 # or output) whose type was not set
8399 # during analysis, setting them to
8400 # 'string' as a conservative fallback.
8401 #
8402 # Entry: $schema - the schema hashref being
8403 # built by _analyze_method.
8404 # $mode - either 'input' or 'output'.
8405 #
8406 # Exit: Returns nothing. Modifies $schema in
8407 # place by setting type => 'string' on
8408 # any parameter that lacks a type, and
8409 # downgrading input confidence to 'low'.
8410 #
8411 # Side effects: Logs type defaulting to stdout when
8412 # verbose is set.
8413 #
8414 # Notes: Called after all analysis is complete
8415 # so that genuine type unknowns can be
8416 # distinguished from analysis gaps.
8417 # --------------------------------------------------
8418 1538 ▶sub _set_defaults {
Subroutine _set_defaults: called CC=4 SCAR=13.9
8419 1538 my ($self, $schema, $mode) = @_;
8420  
8421 1538 my $params = $schema->{$mode};
8422  
8423 1538 foreach my $param (keys %$params) {
8424 1871 my $p = $params->{$param};
8425  
8426 1871 ▶ next unless(ref($p) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p eq "HASH" 1129 742 1871
8427 742 ▶ unless ($p->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->{"type"}) 148 594 742
8428 148 $self->_log(" DEBUG {$mode}{$param}: Setting to 'string' as a default");
8429 148 $p->{'type'} = 'string';
8430 148 $schema->{_confidence}{$mode}->{level} = 'low'; # Setting a default means it's a guess
8431 }
8432 }
8433 }
8434  
8435 # --------------------------------------------------
8436 # _analyze_relationships
8437 #
8438 # Purpose: Detect inter-parameter relationships
8439 # in a method's source code, including
8440 # mutually exclusive parameters, required
8441 # groups, conditional requirements,
8442 # dependencies, and value-based
8443 # constraints.
8444 #
8445 # Entry: $method - method hashref containing
8446 # at minimum a 'body' key
8447 # with the source string.
8448 #
8449 # Exit: Returns an arrayref of relationship
8450 # hashrefs. Returns an empty arrayref
8451 # if no parameters or no relationships
8452 # are found.
8453 #
8454 # Side effects: Logs detections to stdout when
8455 # verbose is set.
8456 #
8457 # Notes: Parameter names are extracted via
8458 # _extract_parameters_from_signature, so
8459 # every style it supports -- my (...) =
8460 # @_, shift-style (my $x = shift), direct-
8461 # index ($_[N]), and modern signatures --
8462 # is analysed for relationships, not just
8463 # the my (...) = @_ list-assignment form.
8464 # --------------------------------------------------
8465 769 ▶sub _analyze_relationships {
Subroutine _analyze_relationships: called CC=2 SCAR=6.9
8466 769 my ($self, $method) = @_;
8467  
8468 769 my $code = $method->{body};
8469 769 my @relationships;
8470  
8471 # Extract all parameter names from the method, using the same
8472 # multi-style detection used for schema population so shift-style
8473 # and modern-signature methods get relationship analysis too
8474 my %params;
8475 769 $self->_extract_parameters_from_signature(\%params, $code);
8476 769 my @param_names = sort { $params{$a}{position} <=> $params{$b}{position} } keys %params;
8477  
8478 769 ▶ return [] unless @param_names;
Branch: 1 branch
BranchTrueFalseTotal
unless @param_names 460 309 769
8479  
8480 # Detect mutually exclusive parameters
8481 309 push @relationships, @{$self->_detect_mutually_exclusive($code, \@param_names)};
8482  
8483 # Detect required groups (OR logic)
8484 309 push @relationships, @{$self->_detect_required_groups($code, \@param_names)};
8485  
8486 # Detect conditional requirements (IF-THEN)
8487 309 push @relationships, @{$self->_detect_conditional_requirements($code, \@param_names)};
8488  
8489 # Detect dependencies
8490 309 push @relationships, @{$self->_detect_dependencies($code, \@param_names)};
8491  
8492 # Detect value-based constraints
8493 309 push @relationships, @{$self->_detect_value_constraints($code, \@param_names)};
8494  
8495 # Deduplicate relationships
8496 309 my @unique = $self->_deduplicate_relationships(\@relationships);
8497  
8498 309 return \@unique;
8499 }
8500  
8501 # --------------------------------------------------
8502 # _deduplicate_relationships
8503 #
8504 # Purpose: Remove duplicate relationship entries
8505 # from the relationships list by
8506 # computing a canonical signature for
8507 # each relationship type.
8508 #
8509 # Entry: $relationships - arrayref of
8510 # relationship hashrefs.
8511 #
8512 # Exit: Returns a deduplicated list of
8513 # relationship hashrefs.
8514 #
8515 # Side effects: None.
8516 # --------------------------------------------------
8517 313 ▶sub _deduplicate_relationships {
Subroutine _deduplicate_relationships: called CC=9 SCAR=22.0
8518 313 my ($self, $relationships) = @_;
8519  
8520 313 my @unique;
8521 my %seen;
8522  
8523 313 foreach my $rel (@$relationships) {
8524 # Create a signature for this relationship
8525 26 my $sig;
8526 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
8527 9 $sig = join(':', 'mutex', sort @{$rel->{params}});
8528 } elsif ($rel->{type} eq 'required_group') {
8529 4 $sig = join(':', 'reqgroup', sort @{$rel->{params}});
8530 } elsif ($rel->{type} eq 'conditional_requirement') {
8531 7 $sig = join(':', 'condreq', $rel->{if}, $rel->{then_required});
8532 } elsif ($rel->{type} eq 'dependency') {
8533 3 $sig = join(':', 'dep', $rel->{param}, $rel->{requires});
8534 } elsif ($rel->{type} eq 'value_constraint') {
8535 2 $sig = join(':', 'valcon', $rel->{if}, $rel->{then}, $rel->{operator}, $rel->{value});
8536 } elsif ($rel->{type} eq 'value_conditional') {
8537 1 $sig = join(':', 'valcond', $rel->{if}, $rel->{equals}, $rel->{then_required});
8538 } else {
8539 0 $sig = join(':', $rel->{type}, %$rel);
8540 }
8541  
8542 26 ▶ unless ($seen{$sig}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$sig}++) 20 6 26
8543 20 push @unique, $rel;
8544 }
8545 }
8546  
8547 313 return @unique;
8548 }
8549  
8550 # --------------------------------------------------
8551 # _detect_mutually_exclusive
8552 #
8553 # Purpose: Detect pairs of parameters that cannot
8554 # be specified together, by searching
8555 # for die/croak/confess patterns
8556 # that fire when both are truthy.
8557 #
8558 # Entry: $code - method body source string.
8559 # $param_names - arrayref of parameter
8560 # name strings.
8561 #
8562 # Exit: Returns an arrayref of relationship
8563 # hashrefs of type 'mutually_exclusive'.
8564 # Returns an empty arrayref if none found.
8565 #
8566 # Side effects: Logs detections to stdout when
8567 # verbose is set.
8568 # --------------------------------------------------
8569 312 ▶sub _detect_mutually_exclusive {
Subroutine _detect_mutually_exclusive: called CC=18 SCAR=30.2
8570 312 my ($self, $code, $param_names) = @_;
8571  
8572 312 my @relationships;
8573  
8574 # Pattern 1: die/croak if $x && $y
8575 # Look for: die/croak ... if $param1 && $param2
8576 312 foreach my $param1 (@$param_names) {
8577 430 foreach my $param2 (@$param_names) {
8578 846 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 430 416 846
8579  
8580 # Check various patterns
8581 416 ▶ 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 404 416
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
66404
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
8582 $code =~ /(?:die|croak|confess)[^;]*if\s+\$$param2\s+&&\s+\$$param1/) {
8583  
8584 # Avoid duplicates (param1,param2 vs param2,param1)
8585 12 my $found_reverse = 0;
8586 12 foreach my $rel (@relationships) {
8587 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
8588 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
8589 6 $found_reverse = 1;
8590 6 last;
8591 }
8592 }
8593  
8594 12 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 6 6 12
8595  
8596 6 push @relationships, {
8597 type => 'mutually_exclusive',
8598 params => [$param1, $param2],
8599 description => "Cannot specify both $param1 and $param2"
8600 };
8601  
8602 6 $self->_log(" RELATIONSHIP: $param1 and $param2 are mutually exclusive");
8603 }
8604  
8605 # Pattern 2: die "Cannot specify both X and Y"
8606 410 ▶ 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 409 410
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
10409
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
8607 $code =~ /(?:die|croak|confess)\s+['"](Cannot|Can't)[^'"]*both[^'"]*$param2[^'"]*$param1/i) {
8608  
8609 1 my $found_reverse = 0;
8610 1 foreach my $rel (@relationships) {
8611 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
8612 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
8613 0 $found_reverse = 1;
8614 0 last;
8615 }
8616 }
8617  
8618 1 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 0 1 1
8619  
8620 1 push @relationships, {
8621 type => 'mutually_exclusive',
8622 params => [$param1, $param2],
8623 description => "Cannot specify both $param1 and $param2"
8624 };
8625  
8626 1 $self->_log(" RELATIONSHIP: $param1 and $param2 are mutually exclusive (from error message)");
8627 }
8628 }
8629 }
8630  
8631 312 return \@relationships;
8632 }
8633  
8634 # --------------------------------------------------
8635 # _detect_required_groups
8636 #
8637 # Purpose: Detect parameter groups where at least
8638 # one parameter must be specified (OR
8639 # logic), by searching for die/croak
8640 # patterns that fire unless any of the
8641 # group is truthy.
8642 #
8643 # Entry: $code - method body source string.
8644 # $param_names - arrayref of parameter
8645 # name strings.
8646 #
8647 # Exit: Returns an arrayref of relationship
8648 # hashrefs of type 'required_group'.
8649 # Returns an empty arrayref if none found.
8650 #
8651 # Side effects: Logs detections to stdout when
8652 # verbose is set.
8653 # --------------------------------------------------
8654 311 ▶sub _detect_required_groups {
Subroutine _detect_required_groups: called CC=18 SCAR=31.4
8655 311 my ($self, $code, $param_names) = @_;
8656  
8657 311 my @relationships;
8658  
8659 # Pattern 1: die/croak unless $x || $y
8660 311 foreach my $param1 (@$param_names) {
8661 428 foreach my $param2 (@$param_names) {
8662 842 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 428 414 842
8663  
8664 414 ▶ 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 408 414
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
33408
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
8665 $code =~ /(?:die|croak|confess)[^;]*unless\s+\$$param2\s+\|\|\s+\$$param1/) {
8666  
8667 # Avoid duplicates
8668 6 my $found_reverse = 0;
8669 6 foreach my $rel (@relationships) {
8670 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
8671 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
8672 3 $found_reverse = 1;
8673 3 last;
8674 }
8675 }
8676  
8677 6 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 3 3 6
8678  
8679 3 push @relationships, {
8680 type => 'required_group',
8681 params => [$param1, $param2],
8682 logic => 'or',
8683 description => "Must specify either $param1 or $param2"
8684 };
8685  
8686 3 $self->_log(" RELATIONSHIP: Must specify either $param1 or $param2");
8687 }
8688  
8689 # Pattern 2: die "Must specify either X or Y"
8690 411 ▶ 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 410 411
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
10410
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
8691 $code =~ /(?:die|croak|confess)\s+['"]Must\s+specify\s+either[^'"]*$param2[^'"]*or[^'"]*$param1/i) {
8692  
8693 1 my $found_reverse = 0;
8694 1 foreach my $rel (@relationships) {
8695 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
8696 (($rel->{params}[0] eq $param2 && $rel->{params}[1] eq $param1))) {
8697 0 $found_reverse = 1;
8698 0 last;
8699 }
8700 }
8701  
8702 1 ▶ next if $found_reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $found_reverse 0 1 1
8703  
8704 1 push @relationships, {
8705 type => 'required_group',
8706 params => [$param1, $param2],
8707 logic => 'or',
8708 description => "Must specify either $param1 or $param2"
8709 };
8710  
8711 1 $self->_log(" RELATIONSHIP: Must specify either $param1 or $param2 (from error message)");
8712 }
8713 }
8714 }
8715  
8716 311 return \@relationships;
8717 }
8718  
8719 # --------------------------------------------------
8720 # _detect_conditional_requirements
8721 #
8722 # Purpose: Detect IF-THEN parameter relationships
8723 # where one parameter being present
8724 # makes another required, by searching
8725 # for die/croak patterns of the form
8726 # 'die if $x && !$y'.
8727 #
8728 # Entry: $code - method body source string.
8729 # $param_names - arrayref of parameter
8730 # name strings.
8731 #
8732 # Exit: Returns an arrayref of relationship
8733 # hashrefs of type
8734 # 'conditional_requirement'.
8735 # Returns an empty arrayref if none found.
8736 #
8737 # Side effects: Logs detections to stdout when
8738 # verbose is set.
8739 # --------------------------------------------------
8740 311 ▶sub _detect_conditional_requirements {
Subroutine _detect_conditional_requirements: called CC=7 SCAR=19.6
8741 311 my ($self, $code, $param_names) = @_;
8742  
8743 311 my @relationships;
8744  
8745 311 foreach my $param1 (@$param_names) {
8746 428 foreach my $param2 (@$param_names) {
8747 842 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 428 414 842
8748  
8749 # Pattern 1: die if $x && !$y (if x then y required)
8750 414 ▶ 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 410 414
8751 4 push @relationships, {
8752 type => 'conditional_requirement',
8753 if => $param1,
8754 then_required => $param2,
8755 description => "When $param1 is specified, $param2 is required"
8756 };
8757  
8758 4 $self->_log(" RELATIONSHIP: $param1 requires $param2");
8759 }
8760  
8761 # Pattern 2: die if $x && !defined($y)
8762 414 ▶ 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 414 414
8763 0 push @relationships, {
8764 type => 'conditional_requirement',
8765 if => $param1,
8766 then_required => $param2,
8767 description => "When $param1 is specified, $param2 is required"
8768 };
8769  
8770 0 $self->_log(" RELATIONSHIP: $param1 requires $param2 (defined check)");
8771 }
8772  
8773 # Pattern 3: Error message "X requires Y"
8774 414 ▶ 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 410 414
8775 4 push @relationships, {
8776 type => 'conditional_requirement',
8777 if => $param1,
8778 then_required => $param2,
8779 description => "When $param1 is specified, $param2 is required"
8780 };
8781  
8782 4 $self->_log(" RELATIONSHIP: $param1 requires $param2 (from error message)");
8783 }
8784 }
8785 }
8786  
8787 311 return \@relationships;
8788 }
8789  
8790 # --------------------------------------------------
8791 # _detect_dependencies
8792 #
8793 # Purpose: Detect simple parameter dependencies
8794 # where one parameter requires another
8795 # to also be present, by combining
8796 # error message pattern matching with
8797 # code condition matching.
8798 #
8799 # Entry: $code - method body source string.
8800 # $param_names - arrayref of parameter
8801 # name strings.
8802 #
8803 # Exit: Returns an arrayref of relationship
8804 # hashrefs of type 'dependency'.
8805 # Returns an empty arrayref if none found.
8806 #
8807 # Side effects: Logs detections to stdout when
8808 # verbose is set.
8809 # --------------------------------------------------
8810 313 ▶sub _detect_dependencies {
Subroutine _detect_dependencies: called CC=6 SCAR=17.9
8811 313 my ($self, $code, $param_names) = @_;
8812  
8813 313 my @relationships;
8814  
8815 313 foreach my $param1 (@$param_names) {
8816 432 foreach my $param2 (@$param_names) {
8817 850 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 432 418 850
8818  
8819 # Pattern 1: Error message mentions "X requires Y" AND code checks $x && !$y
8820 # Split into two checks to be more flexible
8821 418 ▶ 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 412 418
Condition100%3/3
$code =~ /(?:die|croak|confess)\s+['"]\w*${param1}[^'"]*requires[^'"]*$param2/i and $code =~ /if\s+\$$param1\s+&&\s+!\$$param2/
!ll&&!rl&&r
41026
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
8822 ($code =~ /if\s+\$$param1\s+&&\s+!\$$param2/)) {
8823  
8824 6 push @relationships, {
8825 type => 'dependency',
8826 param => $param1,
8827 requires => $param2,
8828 description => "$param1 requires $param2 to be specified"
8829 };
8830  
8831 6 $self->_log(" RELATIONSHIP: $param1 depends on $param2");
8832 }
8833 }
8834 }
8835  
8836 313 return \@relationships;
8837 }
8838  
8839 # --------------------------------------------------
8840 # _detect_value_constraints
8841 #
8842 # Purpose: Detect value-based constraints between
8843 # parameters, such as 'if $ssl then
8844 # $port must equal 443' or 'if $mode
8845 # eq secure then $key is required'.
8846 #
8847 # Entry: $code - method body source string.
8848 # $param_names - arrayref of parameter
8849 # name strings.
8850 #
8851 # Exit: Returns an arrayref of relationship
8852 # hashrefs of type 'value_constraint'
8853 # or 'value_conditional'.
8854 # Returns an empty arrayref if none found.
8855 #
8856 # Side effects: Logs detections to stdout when
8857 # verbose is set.
8858 # --------------------------------------------------
8859 309 ▶sub _detect_value_constraints {
Subroutine _detect_value_constraints: called CC=7 SCAR=19.7
8860 309 my ($self, $code, $param_names) = @_;
8861  
8862 309 my @relationships;
8863  
8864 309 foreach my $param1 (@$param_names) {
8865 424 foreach my $param2 (@$param_names) {
8866 834 ▶ next if $param1 eq $param2;
Branch: 1 branch
BranchTrueFalseTotal
if $param1 eq $param2 424 410 834
8867  
8868 # Pattern 1: die if $x && $y != value
8869 410 ▶ 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 408 410
8870 2 my $value = $1;
8871 2 push @relationships, {
8872 type => 'value_constraint',
8873 if => $param1,
8874 then => $param2,
8875 operator => '==',
8876 value => $value,
8877 description => "When $param1 is specified, $param2 must equal $value"
8878 };
8879  
8880 2 $self->_log(" RELATIONSHIP: $param1 requires $param2 == $value");
8881 }
8882  
8883 # Pattern 2: die if $x && $y < value
8884 410 ▶ 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 410 410
8885 0 my $value = $1;
8886 0 push @relationships, {
8887 type => 'value_constraint',
8888 if => $param1,
8889 then => $param2,
8890 operator => '>=',
8891 value => $value,
8892 description => "When $param1 is specified, $param2 must be >= $value"
8893 };
8894  
8895 0 $self->_log(" RELATIONSHIP: $param1 requires $param2 >= $value");
8896 }
8897  
8898 # Pattern 3: die if $x eq 'value' && !$y
8899 410 ▶ 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 409 410
8900 1 my $value = $1;
8901 1 push @relationships, {
8902 type => 'value_conditional',
8903 if => $param1,
8904 equals => $value,
8905 then_required => $param2,
8906 description => "When $param1 equals '$value', $param2 is required"
8907 };
8908  
8909 1 $self->_log(" RELATIONSHIP: $param1='$value' requires $param2");
8910 }
8911 }
8912 }
8913  
8914 309 return \@relationships;
8915 }
8916  
8917 # Write a single method schema to a YAML file in output_dir.
8918 #
8919 # Entry: $method_name is a non-empty string; $schema is a hashref.
8920 # Exit: YAML file written to output_dir/$method_name.yml.
8921 # Side effects: Creates output_dir if it does not exist.
8922 # Notes: Croaks if output_dir was not set in new().
8923  
8924 531 ▶sub _write_schema {
Subroutine _write_schema: called CC=34 SCAR=35.4
8925 531 my ($self, $method_name, $schema) = @_;
8926  
8927 # output_dir is required here — croak early with a clear message
8928 # rather than letting make_path fail with a cryptic error
8929 531 ▶ 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 530 531
8930  
8931 530 ▶ make_path($self->{output_dir}) unless -d $self->{output_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"output_dir"} 6 524 530
8932  
8933 530 my $filename = "$self->{output_dir}/${method_name}.yml";
8934  
8935 530 local $YAML::XS::QuoteNumericStrings = 0;
8936  
8937 # Extract package name for module field
8938 530 my $package_name = '';
8939 530 ▶ if ($self->{_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_document"}) 528 2 530
8940 528 my $package_stmt = $self->{_document}->find_first('PPI::Statement::Package');
8941 528 ▶ $package_name = $package_stmt ? $package_stmt->namespace : '';
Branch: 1 branch
BranchTrueFalseTotal
$package_stmt ? : 523 5 528
8942 528 ▶ $self->{_package_name} //= $package_name;
Condition33%1/3
$self->{"_package_name"} //= $package_name
l!l&&r!l&&!r
52800
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
8943 }
8944  
8945 # Clean up schema for output - use the format expected by App::Test::Generator::Template
8946 530 my $output = {
8947 function => $method_name,
8948 module => $package_name,
8949 config => {
8950 close_stdin => 1,
8951 dedup => 1,
8952 test_nuls => 0,
8953 test_undef => 0,
8954 test_empty => 1,
8955 test_non_ascii => 0,
8956 test_security => 0
8957 }
8958 };
8959  
8960 # A file the POD documents the routine creating or writing is the
8961 # requested result, not a leftover, so turn that destructive check off.
8962 530 ▶ if($schema->{_analysis}{side_effects}{writes_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_analysis"}{"side_effects"}{"writes_file"}) 10 520 530
8963 10 $output->{config}{check_files} = 0;
8964 }
8965  
8966 # The routine has a precondition no generated value can meet, so there is
8967 # nothing meaningful to fuzz; the harness and all-fuzz report it skipped.
8968 530 ▶ $output->{skip} = $schema->{skip} if($schema->{skip});
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"skip"} 16 514 530
8969  
8970 # Process input parameters with advanced type handling
8971 530 ▶ if($schema->{'input'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"input"}) 513 17 530
8972 513 ▶ if(scalar(keys %{$schema->{'input'}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$schema{'input'};}) { } 352 161 513
8973 352 $output->{'input'} = {};
8974  
8975 352 foreach my $param_name (sort keys %{$schema->{'input'}}) {
8976 574 my $param = $schema->{'input'}{$param_name};
8977 574 ▶ if($param->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"name"}) 2 572 574
8978 2 my $name = delete $param->{name};
8979 2 ▶ if($name ne $param_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne $param_name) 0 2 2
8980 # Sanity check
8981 0 croak("BUG: Parameter name - expected $param_name, got $name");
8982 }
8983 }
8984 574 my $cleaned_param = $self->_serialize_parameter_for_yaml($param);
8985 574 $output->{'input'}{$param_name} = $cleaned_param;
8986 }
8987  
8988 # If some params have positions and others don't, treat the whole
8989 # input as a named (hash) API and strip all positions. Mixed
8990 # position state arises when a named-API method also happens to
8991 # have a Params::Get positional-key call alongside =head4 Input
8992 # named-block params that carry no position.
8993 352 my @with_pos = grep { defined $output->{input}{$_}{position} } sort keys %{$output->{input}};
8994 352 my @without_pos = grep { !defined $output->{input}{$_}{position} } sort keys %{$output->{input}};
8995 352 ▶ if (@with_pos && @without_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (@with_pos and @without_pos) 4 348 352
Condition100%3/3
@with_pos and @without_pos
!ll&&!rl&&r
522964
MC/DC100%2/2
@with_pos and @without_pos
@with_pos @without_pos
Truth table100%3/3
A and B
A= @with_posB= @without_pos
ABresult
0X0
100
111
8996 4 delete $output->{input}{$_}{position} for @with_pos;
8997 }
8998 } else {
8999 161 delete $output->{input};
9000 }
9001 }
9002  
9003 # Process output
9004 530 ▶ if($schema->{'output'} && (scalar(keys %{$schema->{'output'}}))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"} and scalar keys %{$$schema{"output"};}) 530 0 530
Condition50%1/2
$schema->{"output"} and scalar keys %{$$schema{"output"};}
!ll
0530
MC/DC0%0/1
$schema->{"output"} and scalar keys %{$$schema{"output"};}
$schema->{"output"}
Truth table50%1/2
A
A= $schema->{"output"}
Aresult
00
11
9005 530 ▶ 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) 482 48 530
Condition100%3/3
ref $schema->{"output"}{"_error_handling"} eq "HASH" and scalar keys %{$$schema{"output"}{"_error_handling"};} == 0
!ll&&!rl&&r
444482
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
9006 482 delete $schema->{output}{_error_handling};
9007 }
9008 # value/alt_value are inference metadata; Return::Set does not recognise them as validation rules
9009 530 my %out_spec = %{$schema->{'output'}};
9010 530 delete @out_spec{qw(value alt_value)};
9011 530 $output->{'output'} = \%out_spec;
9012 }
9013  
9014 530 ▶ if($schema->{'output'}{'type'} && ($schema->{'output'}{'type'} eq 'scalar')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"type"} and $schema->{"output"}{"type"} eq "scalar") 0 530 530
Condition66%2/3
$schema->{"output"}{"type"} and $schema->{"output"}{"type"} eq "scalar"
!ll&&!rl&&r
205100
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
9015 0 $schema->{'output'}{'type'} = 'string';
9016 0 $schema->{_confidence}{output}->{level} = 'low'; # A guess
9017 }
9018  
9019 # Add 'new' field if object instantiation is needed
9020 530 ▶ if ($schema->{new}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"new"}) 288 242 530
9021 # TODO: consider allowing parent class packages up the ISA chain
9022 288 ▶ if(ref($schema->{new}) || ($schema->{new} eq $package_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{'new'} or $schema->{'new'} eq $package_name) { } 256 32 288
Condition100%3/3
ref $schema->{"new"} or $schema->{"new"} eq $package_name
l!l&&r!l&&!r
11014632
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
9023 256 ▶ $output->{new} = $schema->{new} eq $package_name ? undef : $schema->{'new'};
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'new'} eq $package_name ? : 146 110 256
9024 } else {
9025 32 $self->_log(" NEW: Don't use $schema->{new} for object insantiation");
9026 32 delete $schema->{new};
9027 32 delete $output->{new};
9028 }
9029 }
9030  
9031 530 ▶ if(!defined($schema->{_confidence}{input}->{level})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema->{"_confidence"}{"input"}{"level"}) 473 57 530
9032 473 $schema->{_confidence}{input} = $self->_calculate_input_confidence($schema->{input});
9033 }
9034 530 ▶ if(!defined($schema->{_confidence}{output}->{level})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema->{"_confidence"}{"output"}{"level"}) 1 529 530
9035 1 $schema->{_confidence}{output} = $self->_calculate_output_confidence($schema->{output});
9036 }
9037  
9038 # Add relationships if detected
9039 530 ▶ if ($schema->{relationships} && @{$schema->{relationships}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"relationships"} and @{$schema->{"relationships"};}) 7 523 530
Condition66%2/3
$schema->{"relationships"} and @{$schema->{"relationships"};}
!ll&&!rl&&r
52307
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
9040 7 $output->{relationships} = $schema->{relationships};
9041 }
9042  
9043 530 ▶ if($schema->{accessor} && scalar(keys %{$schema->{accessor}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}) 20 510 530
Condition100%2/2
$schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}
!ll
51020
MC/DC100%1/1
$schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}
$schema->{"accessor"}
Truth table100%2/2
A
A= $schema->{"accessor"}
Aresult
00
11
9044 20 $output->{accessor} = $schema->{accessor};
9045 }
9046  
9047 530 open my $fh, '>', $filename;
9048 530 print $fh YAML::XS::Dump($output);
9049 530 print $fh $self->_generate_schema_comments($schema, $method_name);
9050 530 close $fh;
9051  
9052 my $rel_info = $schema->{relationships} ?
9053 530 ▶ ' [' . scalar(@{$schema->{relationships}}) . ' relationships]' : '';
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'relationships'} ? : 7 523 530
9054 $self->_log(" Wrote: $filename (input confidence: $schema->{_confidence}{input}->{level})" .
9055 530 ▶ ($schema->{new} ? " [requires: $schema->{new}]" : '') . $rel_info);
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'new'} ? : 256 274 530
9056 }
9057  
9058 # --------------------------------------------------
9059 # _generate_schema_comments
9060 #
9061 # Purpose: Generate the YAML comment block
9062 # appended to the end of each written
9063 # schema file, containing provenance,
9064 # confidence levels, parameter type
9065 # notes, relationship summaries, and
9066 # warnings about types requiring
9067 # special test setup.
9068 #
9069 # Entry: $schema - the schema hashref as
9070 # built by _analyze_method.
9071 # $method_name - the method name string,
9072 # used in the fuzz
9073 # command hint.
9074 #
9075 # Exit: Returns a string of YAML comment lines
9076 # beginning with a blank line and ending
9077 # with a trailing newline.
9078 #
9079 # Side effects: None.
9080 # --------------------------------------------------
9081 530 ▶sub _generate_schema_comments {
Subroutine _generate_schema_comments: called CC=26 SCAR=32.6
9082 530 my ($self, $schema, $method_name) = @_;
9083  
9084 530 my @comments;
9085  
9086 530 push @comments, '';
9087 530 push @comments, '# Generated by ' . ref($self);
9088 530 push @comments, "# Run: fuzz-harness-generator -r $self->{output_dir}/${method_name}.yml";
9089 530 push @comments, '#';
9090 530 push @comments, "# Input confidence: $schema->{_confidence}{input}->{level}";
9091 530 push @comments, "# Output confidence: $schema->{_confidence}{output}->{level}";
9092  
9093 # Add notes about parameters
9094 530 ▶ if ($schema->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"input"}) 513 17 530
9095 513 my @param_notes;
9096 513 foreach my $param_name (sort keys %{$schema->{input}}) {
9097 574 my $p = $schema->{input}{$param_name};
9098  
9099 574 ▶ if ($p->{semantic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"semantic"}) 34 540 574
9100 34 push @param_notes, "$param_name: $p->{semantic}";
9101 }
9102  
9103 574 ▶ if ($p->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"enum"}) 8 566 574
9104 8 push @param_notes, "$param_name: enum with " . scalar(@{$p->{enum}}) . " values";
9105 }
9106  
9107 574 ▶ if ($p->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"isa"}) 41 533 574
9108 41 push @param_notes, "$param_name: requires $p->{isa} object";
9109 }
9110 }
9111  
9112 513 ▶ if (@param_notes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param_notes) 71 442 513
9113 71 push @comments, '#';
9114 71 push @comments, '# Parameter types detected:';
9115 71 foreach my $note (sort @param_notes) {
9116 83 push @comments, "# - $note";
9117 }
9118 }
9119 }
9120  
9121 # Add relationship notes, sorted so their order does not depend on
9122 # detection order.
9123 530 ▶ if ($schema->{relationships} && @{$schema->{relationships}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"relationships"} and @{$schema->{"relationships"};}) 7 523 530
Condition66%2/3
$schema->{"relationships"} and @{$schema->{"relationships"};}
!ll&&!rl&&r
52307
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
9124 7 push @comments, (
9125 '#',
9126 '# Parameter relationships detected:'
9127 );
9128 my @relationship_notes = map {
9129 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
9130 7 } @{$schema->{relationships}};
9131 7 foreach my $desc (sort @relationship_notes) {
9132 16 push @comments, "# - $desc";
9133 }
9134 }
9135  
9136 # Add general notes
9137 530 ▶ if ($schema->{_notes} && scalar(@{$schema->{_notes}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_notes"} and scalar @{$schema->{"_notes"};}) 50 480 530
Condition100%2/2
$schema->{"_notes"} and scalar @{$schema->{"_notes"};}
!ll
2528
MC/DC100%1/1
$schema->{"_notes"} and scalar @{$schema->{"_notes"};}
$schema->{"_notes"}
Truth table100%2/2
A
A= $schema->{"_notes"}
Aresult
00
11
9138 50 push @comments, '#';
9139 50 push @comments, '# Notes:';
9140 50 foreach my $note (sort @{$schema->{_notes}}) {
9141 82 push @comments, "# - $note";
9142 }
9143 }
9144  
9145 530 ▶ if($schema->{_analysis}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_analysis"}) 530 0 530
9146 530 push @comments, (
9147 '#',
9148 '# Analysis:',
9149 '# TODO:',
9150 );
9151 # confidence_factors:
9152 # input:
9153 # - No parameters found
9154 # output:
9155 # - 'Return type defined: object (+30)'
9156 # - 'Total output confidence score: 30'
9157 # - 'Medium confidence: return type defined'
9158 # input_confidence: none
9159 # output_confidence: medium
9160 # overall_confidence: none
9161 }
9162  
9163 # Add warnings for complex types
9164 530 my @warnings;
9165 530 ▶ if ($schema->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"input"}) 513 17 530
9166 513 foreach my $param_name (sort keys %{$schema->{input}}) {
9167 574 my $p = $schema->{input}{$param_name};
9168  
9169 574 ▶ if ($p->{type} && $p->{type} eq 'coderef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"type"} and $p->{"type"} eq "coderef") 12 562 574
Condition66%2/3
$p->{"type"} and $p->{"type"} eq "coderef"
!ll&&!rl&&r
056212
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
9170 12 push @warnings, "Parameter '$param_name' is a coderef - you'll need to provide a sub {} in tests";
9171 }
9172  
9173 574 ▶ if ($p->{semantic} && $p->{semantic} eq 'filehandle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"semantic"} and $p->{"semantic"} eq "filehandle") 2 572 574
Condition100%3/3
$p->{"semantic"} and $p->{"semantic"} eq "filehandle"
!ll&&!rl&&r
540322
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
9174 2 push @warnings, "Parameter '$param_name' is a filehandle - consider using IO::String or mock";
9175 }
9176  
9177 574 ▶ if ($p->{isa} && $p->{isa} =~ /DateTime/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"isa"} and $p->{"isa"} =~ /DateTime/) 2 572 574
Condition100%3/3
$p->{"isa"} and $p->{"isa"} =~ /DateTime/
!ll&&!rl&&r
533392
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
9178 2 push @warnings, "Parameter '$param_name' requires DateTime - ensure DateTime is loaded";
9179 }
9180 }
9181 }
9182  
9183 530 ▶ if (@warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (@warnings) 16 514 530
9184 16 push @comments, '#';
9185 16 push @comments, '# WARNINGS - Manual test setup may be required:';
9186 16 foreach my $warning (sort @warnings) {
9187 16 push @comments, "# ! $warning";
9188 }
9189 }
9190  
9191 530 push @comments, '';
9192  
9193 530 return join("\n", @comments);
9194 }
9195  
9196 # --------------------------------------------------
9197 # _serialize_parameter_for_yaml
9198 #
9199 # Purpose: Convert a parameter spec hashref into
9200 # a cleaned, YAML-serialisable form
9201 # suitable for App::Test::Generator
9202 # consumption, handling semantic type
9203 # mappings, enum values, and object
9204 # class annotations.
9205 #
9206 # Entry: $param - parameter spec hashref as
9207 # produced by the merge and
9208 # analysis pipeline.
9209 #
9210 # Exit: Returns a new hashref containing only
9211 # the fields App::Test::Generator
9212 # understands, with internal _ keys
9213 # and semantic keys removed or converted.
9214 #
9215 # Side effects: None.
9216 #
9217 # Notes: Semantic types are mapped to
9218 # appropriate base types with additional
9219 # constraint and note fields.
9220 # The original $param hashref is not
9221 # modified.
9222 # --------------------------------------------------
9223 583 ▶sub _serialize_parameter_for_yaml {
Subroutine _serialize_parameter_for_yaml: called CC=57 SCAR=43.8
9224 583 my ($self, $param) = @_;
9225  
9226 583 my %cleaned;
9227  
9228 # Copy basic fields that App::Test::Generator expects
9229 583 foreach my $field (qw(type position optional min max matches default)) {
9230 4081 ▶ $cleaned{$field} = $param->{$field} if defined $param->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{$field} 1667 2414 4081
9231 }
9232  
9233 # Provenance drives the harness's tolerance and type-check decisions.
9234 583 ▶ $cleaned{_source} = $param->{_source} if defined($param->{_source}) && length($param->{_source});
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{"_source"} and length $param->{"_source"} 555 28 583
Condition66%2/3
defined $param->{"_source"} and length $param->{"_source"}
!ll&&!rl&&r
280555
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
9235  
9236 # Per-constraint provenance: a documented minimum and a code-enforced
9237 # maximum on the same field are separate decisions.
9238 583 ▶ if(ref($param->{_constraint_source}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param->{"_constraint_source"} eq "HASH") 87 496 583
9239 87 my %provenance;
9240 87 foreach my $key (qw(min max memberof matches nomatch notmemberof)) {
9241 522 my $source = $param->{_constraint_source}{$key};
9242 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
9243 }
9244 87 ▶ $cleaned{_constraint_source} = \%provenance if keys %provenance;
Branch: 1 branch
BranchTrueFalseTotal
if keys %provenance 87 0 87
9245 }
9246  
9247 # Handle advanced type mappings
9248 583 ▶ if(my $semantic = $param->{semantic}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $semantic = $param->{"semantic"}) 38 545 583
9249 38 ▶ if ($semantic eq 'datetime_object') {
Branch: 12 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 '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
9250 # DateTime objects: test generator needs to know how to create them
9251 2 $cleaned{type} = 'object';
9252 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
9253 2 $cleaned{_note} = 'Requires DateTime object';
9254 } elsif ($semantic eq 'timepiece_object') {
9255 0 $cleaned{type} = 'object';
9256 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
9257 0 $cleaned{_note} = 'Requires Time::Piece object';
9258 } elsif ($semantic eq 'date_string') {
9259 # Date strings: provide regex pattern
9260 1 $cleaned{type} = 'string';
9261 1 ▶ $cleaned{matches} ||= '/^\d{4}-\d{2}-\d{2}$/';
Condition50%1/2
$cleaned{"matches"} ||= "/^\\d{4}-\\d{2}-\\d{2}\$/"
l!l
01
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
9262 1 $cleaned{_example} = '2024-12-12';
9263 } elsif ($semantic eq 'iso8601_string') {
9264 1 $cleaned{type} = 'string';
9265 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
9266 1 $cleaned{_example} = '2024-12-12T10:30:00Z';
9267 } elsif ($semantic eq 'unix_timestamp') {
9268 2 $cleaned{type} = 'integer';
9269 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
9270 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
9271 2 $cleaned{_note} = 'UNIX timestamp';
9272 } elsif ($semantic eq 'datetime_parseable') {
9273 0 $cleaned{type} = 'string';
9274 0 $cleaned{_note} = 'Must be parseable as datetime';
9275 } elsif ($semantic eq 'filehandle') {
9276 # File handles: special handling needed
9277 2 $cleaned{type} = 'object';
9278 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
9279 2 $cleaned{_note} = 'File handle - may need mock in tests';
9280 } elsif ($semantic eq 'filepath') {
9281 # File paths: string with path pattern
9282 1 $cleaned{type} = 'string';
9283 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
9284 1 $cleaned{_note} = 'File path';
9285 } elsif ($semantic eq 'existing_file') {
9286 # The routine only tests that the file exists. Any existing
9287 # file satisfies that, so keep the semantic for the harness to
9288 # supply a temporary file instead of guessing at a path.
9289 10 $cleaned{type} = 'string';
9290 10 $cleaned{semantic} = 'existing_file';
9291 10 $cleaned{_note} = 'Existing file';
9292 } elsif ($semantic eq 'directory') {
9293 # The value must name an existing directory. No pattern can
9294 # express that and any generated path would fail the -d test,
9295 # so keep the semantic for the harness to act on instead of
9296 # guessing at the spelling.
9297 1 $cleaned{type} = 'string';
9298 1 $cleaned{semantic} = 'directory';
9299 1 $cleaned{_note} = 'Existing directory';
9300 } elsif ($semantic eq 'callback') {
9301 # Coderefs: mark as special type
9302 7 $cleaned{type} = 'coderef';
9303 7 $cleaned{_note} = 'CODE reference - provide sub { } in tests';
9304 } elsif ($semantic eq 'enum') {
9305 # Enum: keep as string but add valid values.
9306 # If the formal input spec explicitly declared a different type, honour
9307 # it — the enum values from code analysis are regex-internal alternates
9308 # (e.g. numeric format variants) that are meaningless as string choices.
9309 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') { } 4 4 8
Condition62%5/8
($param->{"_source"} // "") eq "input_spec" and defined $param->{"type"} and $param->{"type"} ne "string"
!ll&&!rl&&r
404
($param->{"_source"} // "") eq "input_spec" and defined $param->{"type"}
!ll&&!rl&&r
404
$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 table25%1/4
A and B and C
A= $param->{"_source"}B= defined $param->{"type"}C= $param->{"type"} ne "string"
ABCresult
0XX0
10X0
1100
1111
9310 # Formal spec wins; suppress spurious regex-derived enum.
9311 4 delete $cleaned{enum};
9312 } else {
9313 4 $cleaned{type} = 'string';
9314 4 ▶ if ($param->{enum} && ref($param->{enum}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"enum"} and ref $param->{"enum"} eq "ARRAY") 4 0 4
Condition33%1/3
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY"
!ll&&!rl&&r
004
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
9315 4 $cleaned{enum} = $param->{enum};
9316 4 $cleaned{_note} = 'Must be one of: ' . join(', ', @{$param->{enum}});
9317 }
9318 }
9319 }
9320 }
9321  
9322 # Handle memberof even if not marked with semantic.
9323 # enum and memberof are mutually exclusive — only set memberof when enum
9324 # is not already being output (avoids the "has both" validation error).
9325 # Also suppress when the formal input spec declared a non-string type —
9326 # the enum values are regex-internal alternates, not valid string members.
9327 583 ▶ 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
268161154
($param->{"_source"} // "") eq "input_spec" and defined $param->{"type"}
!ll&&!rl&&r
2680315
$param->{"_source"} // ""
l!l
55528
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
9328 583 ▶ 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 582 583
Condition88%8/9
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY" and not $cleaned{"enum"} and not $formal_non_string
!ll&&!rl&&r
57841
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY" and not $cleaned{"enum"}
!ll&&!rl&&r
57445
$param->{"enum"} and ref $param->{"enum"} eq "ARRAY"
!ll&&!rl&&r
57409
MC/DC75%3/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 table80%4/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
9329 1 $cleaned{memberof} = $param->{enum};
9330 }
9331 583 ▶ if($param->{memberof} && ref($param->{memberof}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"memberof"} and ref $param->{"memberof"} eq "ARRAY") 13 570 583
Condition66%2/3
$param->{"memberof"} and ref $param->{"memberof"} eq "ARRAY"
!ll&&!rl&&r
570013
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
9332 13 $cleaned{memberof} = $param->{memberof};
9333 }
9334  
9335 # Handle object class
9336 583 ▶ if ($param->{isa} && !$cleaned{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"isa"} and not $cleaned{"isa"}) 38 545 583
Condition100%3/3
$param->{"isa"} and not $cleaned{"isa"}
!ll&&!rl&&r
541438
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
9337 38 $cleaned{isa} = $param->{isa};
9338 }
9339  
9340 # Add format hints where available
9341 583 ▶ if ($param->{format}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"format"}) 1 582 583
9342 1 $cleaned{_format} = $param->{format};
9343 }
9344  
9345 # Patterns harvested from source or POD arrive as slash-delimited
9346 # literals ('/^abc$/'). Consumers compile what they are given with
9347 # qr//, which would take the delimiters to be part of the pattern, so
9348 # store the pattern body. It stays a plain string: a compiled Regexp
9349 # is written to YAML as a !!perl/regexp object that a YAML consumer
9350 # cannot read back, and the schema is a data file.
9351 583 ▶ if(defined(my $pattern = $cleaned{matches})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pattern = $cleaned{"matches"})) 19 564 583
9352 19 ▶ if((reftype($pattern) // '') eq 'REGEXP') {
Branch: 2 branches
BranchTrueFalseTotal
if ((builtin::reftype($pattern) // '') eq 'REGEXP') { } 0 19 19
elsif (not ref $pattern) { } 19 0 19
Condition50%1/2
builtin::reftype($pattern) // ""
l!l
019
MC/DC0%0/1
builtin::reftype($pattern) // ""
builtin::reftype($pattern)
Truth table50%1/2
A
A= builtin::reftype($pattern)
Aresult
11
00
9353 # Some extraction paths hand over a compiled pattern, possibly a
9354 # blessed subclass (e.g. a regexp from a Safe compartment).
9355 0 my ($body) = re::regexp_pattern($pattern);
9356 0 ▶ $cleaned{matches} = $body if defined($body);
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 0 0 0
9357 } elsif(!ref($pattern)) {
9358 19 (my $body = $pattern) =~ s{^/(.+)/$}{$1}s;
9359 19 my $compiled = eval { qr/$body/ };
9360 19 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 19 19
9361 0 $self->_log(" invalid matches pattern '$pattern': $@");
9362 } else {
9363 19 $cleaned{matches} = $body;
9364 }
9365 }
9366 }
9367  
9368 # Remove internal fields. The directory and existing-file semantics are
9369 # kept: the harness needs them to supply a path that exists.
9370 delete $cleaned{semantic}
9371 583 ▶ unless(($cleaned{semantic} // '') =~ /^(?:directory|existing_file)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless ($cleaned{"semantic"} // "") =~ /^(?:directory|existing_file)$/ 572 11 583
Condition100%2/2
$cleaned{"semantic"} // ""
l!l
11572
MC/DC100%1/1
$cleaned{"semantic"} // ""
$cleaned{"semantic"}
Truth table100%2/2
A
A= $cleaned{"semantic"}
Aresult
11
00
9372  
9373 583 return \%cleaned;
9374 }
9375  
9376 # --------------------------------------------------
9377 # _format_relationship
9378 #
9379 # Purpose: Format a relationship hashref as a
9380 # short human-readable description
9381 # string for use in YAML comments.
9382 #
9383 # Entry: $rel - relationship hashref as
9384 # produced by the relationship
9385 # detection methods.
9386 #
9387 # Exit: Returns a description string.
9388 # Returns 'Unknown relationship' for
9389 # unrecognised types.
9390 #
9391 # Side effects: None.
9392 # --------------------------------------------------
9393 10 ▶sub _format_relationship {
Subroutine _format_relationship: called CC=7 SCAR=19.5
9394 10 my $rel = $_[0];
9395  
9396 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
9397 2 return 'Mutually exclusive: ' . join(', ', @{$rel->{params}});
9398 } elsif ($rel->{type} eq 'required_group') {
9399 1 return "Required group (OR): " . join(', ', @{$rel->{params}});
9400 } elsif ($rel->{type} eq 'conditional_requirement') {
9401 2 return "If $rel->{if} then $rel->{then_required} required";
9402 } elsif ($rel->{type} eq 'dependency') {
9403 2 return "$rel->{param} depends on $rel->{requires}";
9404 } elsif ($rel->{type} eq 'value_constraint') {
9405 1 return "If $rel->{if} then $rel->{then} $rel->{operator} $rel->{value}";
9406 } elsif ($rel->{type} eq 'value_conditional') {
9407 1 return "If $rel->{if}='$rel->{equals}' then $rel->{then_required} required";
9408 }
9409 1 return 'Unknown relationship';
9410 }
9411  
9412 # --------------------------------------------------
9413 # _needs_object_instantiation
9414 #
9415 # Purpose: Determine whether a method requires
9416 # --------------------------------------------------
9417 # _ctor_representative_args
9418 #
9419 # Purpose: Given a new() method schema, return a
9420 # hashref of representative values for its
9421 # required parameters, suitable for use as
9422 # the new: key in an instance-method schema.
9423 # Returns {} for a no-arg constructor.
9424 # Returns undef when any required param is
9425 # of a type that cannot be represented as a
9426 # plain YAML scalar (e.g. coderef, object).
9427 #
9428 # Entry: $ctor_schema - schema hashref for new().
9429 #
9430 # Exit: Hashref of {param => value} or undef.
9431 # --------------------------------------------------
9432  
9433 # A parameter whose value the routine reads from disk is a precondition, not an
9434 # argument the harness can make up. Two cases are distinguishable:
9435 #
9436 # * the routine only tests that the file exists (`-e`/`-f`) and keeps the
9437 # path, as a constructor validating its argument does. Any existing file
9438 # will do, so the parameter is marked semantic => 'existing_file' and the
9439 # harness supplies a real temporary file.
9440 # * the routine parses or slurps the file's contents. No synthesized or
9441 # empty fixture can satisfy that, so the routine is reported as skipped
9442 # rather than failed on a call it could never make correctly.
9443 sub _requires_input_file
9444 769 ▶{
Subroutine _requires_input_file: called CC=15 SCAR=27.2
9445 769 my ($body, $input) = @_;
9446  
9447 769 ▶ return undef unless(ref($input) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 35 734 769
9448 734 ▶ return undef unless(defined($body) && length($body));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and length $body 0 734 734
Condition33%1/3
defined $body and length $body
!ll&&!rl&&r
00734
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
9449  
9450 734 foreach my $param (sort keys %{$input}) {
9451 666 my $p = quotemeta($param);
9452  
9453 # The value is often copied out of %args under another name first.
9454 666 my @names = ($p);
9455 666 ▶ 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 666 673
9456 7 ▶ push @names, quotemeta($1) if($1 ne $param);
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne $param 5 2 7
9457 }
9458 666 my $v = '(?:' . join('|', @names) . ')';
9459  
9460 # A file whose contents are read, not merely tested for existence.
9461 666 ▶ 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 648 666
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
99648
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
9462 if($body =~ /\b(?:PPI::Document->new|_load_schema|_slurp|read_file|slurp_file)\s*\(\s*\$$v\b/
9463 || $body =~ /\bopen\b\s*\(?[^;]*['"]<[^;]*\$$v\b/);
9464  
9465 648 ▶ 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
83637
$body =~ /-[ef]\s+\$args\{\s*['"]?${p}['"]?\s*\}/ or $body =~ /-[ef]\s+\$$v\b/
l!l&&r!l&&!r
71640
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
9466 || ($body =~ /-[ef]\s+\$$v\b/)
9467 || ($body =~ /-[ef]\s+\$self->\{\s*['"]?$p['"]?\s*\}/);
9468 # Object::Configure reads the file its caller's config_file names and
9469 # croaks when it is missing; that test is not in this body to be seen.
9470 648 ▶ $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
111636
$param eq 'config_file' && $body =~ /\bObject::Configure::configure\s*\(/
!ll&&!rl&&r
63601
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
9471 && ($body =~ /\bObject::Configure::configure\s*\(/);
9472 648 ▶ next unless($tests_existence);
Branch: 1 branch
BranchTrueFalseTotal
unless $tests_existence 636 12 648
9473  
9474 12 my $spec = $input->{$param};
9475 12 ▶ $spec->{semantic} = 'existing_file' if(ref($spec) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec eq "HASH" 12 0 12
9476 }
9477  
9478 716 return undef;
9479 }
9480  
9481 # The part of a formal-spec entry that describes the entry itself, without
9482 # any nested { ... } such as a properties map. The named and positional
9483 # parsers capture up to the first closing brace, so an unbalanced trailing
9484 # nested opening is dropped as well.
9485 sub _own_spec_text
9486 377 ▶{
Subroutine _own_spec_text: called CC=3 SCAR=11.0
9487 377 ▶ my $spec = $_[0] // '';
Condition50%1/2
$_[0] // ""
l!l
3770
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
9488  
9489 377 ▶ 1 while($spec =~ s/\{[^{}]*\}//g);
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/\{[^{}]*\}//g and do { '???' } 2 377 379
9490 377 $spec =~ s/\{.*\z//s;
9491  
9492 377 return $spec;
9493 }
9494  
9495 # The optional flag a formal-spec entry gives its own parameter: 0 or 1, or
9496 # undef when it says nothing. An explicit optional wins over required.
9497 sub _spec_optionality
9498 375 ▶{
Subroutine _spec_optionality: called CC=3 SCAR=11.0
9499 375 my $own = _own_spec_text($_[0]);
9500  
9501 375 ▶ return $1 + 0 if($own =~ /\boptional\s*=>\s*(0|1)/i);
Branch: 1 branch
BranchTrueFalseTotal
if $own =~ /\boptional\s*=>\s*(0|1)/i 192 183 375
9502 183 ▶ return 1 - $1 if($own =~ /\brequired\s*=>\s*(0|1)/i);
Branch: 1 branch
BranchTrueFalseTotal
if $own =~ /\brequired\s*=>\s*(0|1)/i 2 181 183
9503 181 return undef;
9504 }
9505  
9506 # The name => { ... } entries at brace depth 0 of a bare-list spec, as
9507 # [ name, body ] pairs with the body's nested braces intact.
9508 sub _top_level_hash_entries
9509 16 ▶{
Subroutine _top_level_hash_entries: called CC=10 SCAR=23.1
9510 16 my $text = $_[0];
9511  
9512 16 my (@entries, $pending, $name, $start);
9513 16 my $depth = 0;
9514 16 ▶ while($text =~ /\b(\w+)\s*=>\s*(?=\{)|([{}])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\b(\w+)\s*=>\s*(?=\{)|([{}])/g) 18 16 34
9515 18 ▶ if(defined($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 6 12 18
9516 6 ▶ $pending = $1 if($depth == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 4 2 6
9517 6 next;
9518 }
9519 12 ▶ if($2 eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "{") 6 6 12
9520 6 ▶ if(($depth == 0) && defined($pending)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 and defined $pending) 4 2 6
Condition66%2/3
$depth == 0 and defined $pending
!ll&&!rl&&r
204
MC/DC50%1/2
$depth == 0 and defined $pending
$depth == 0 defined $pending
Truth table66%2/3
A and B
A= $depth == 0B= defined $pending
ABresult
0X0
100
111
9521 4 ($name, $start) = ($pending, pos($text));
9522 }
9523 6 undef $pending;
9524 6 $depth++;
9525 6 next;
9526 }
9527 6 ▶ next unless($depth > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $depth > 0 0 6 6
9528 6 $depth--;
9529 6 ▶ if(($depth == 0) && defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 and defined $name) 4 2 6
Condition66%2/3
$depth == 0 and defined $name
!ll&&!rl&&r
204
MC/DC50%1/2
$depth == 0 and defined $name
$depth == 0 defined $name
Truth table66%2/3
A and B
A= $depth == 0B= defined $name
ABresult
0X0
100
111
9530 4 push @entries, [ $name, substr($text, $start, pos($text) - 1 - $start) ];
9531 4 undef $name;
9532 }
9533 }
9534  
9535 16 return @entries;
9536 }
9537  
9538 # True when a required arrayref constructor parameter is not a list of
9539 # values but a list of live objects: the constructor rejects an empty list,
9540 # calls methods on the elements, or checks that they are blessed. A YAML
9541 # fixture cannot contain such objects, so both new() and the instance methods
9542 # that need its result must be reported as skipped.
9543 #
9544 # Database::Join->new is the motivating case: its databases parameter must
9545 # hold one or more Database::Abstraction instances, and the constructor calls
9546 # columns() on each one to build its routing table.
9547 sub _constructor_requires_live_objects
9548 97 ▶{
Subroutine _constructor_requires_live_objects: called CC=15 SCAR=28.3
9549 97 my ($body, $ctor_schema) = @_;
9550  
9551 0 ▶ my $content = ref($body) ? eval { $body->content } : $body;
Branch: 1 branch
BranchTrueFalseTotal
ref $body ? : 0 97 97
9552 97 ▶ return undef unless(defined($content) && length($content));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 97 97
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
0097
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
9553  
9554 97 my $input = $ctor_schema->{input};
9555 97 ▶ return undef unless(ref($input) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 0 97 97
9556  
9557 97 for my $param (sort keys %$input) {
9558 139 my $spec = $input->{$param};
9559 139 ▶ next unless(ref($spec) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 0 139 139
9560 139 ▶ next if($spec->{optional});
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"optional"} 70 69 139
9561  
9562 69 ▶ my $type = $spec->{type} // '';
Condition50%1/2
$spec->{"type"} // ""
l!l
690
MC/DC0%0/1
$spec->{"type"} // ""
$spec->{"type"}
Truth table50%1/2
A
A= $spec->{"type"}
Aresult
11
00
9563 69 ▶ next unless(($type eq 'arrayref') || ($type eq 'array'));
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "arrayref" or $type eq "array" 66 3 69
Condition66%2/3
$type eq "arrayref" or $type eq "array"
l!l&&r!l&&!r
3066
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
9564  
9565 3 my $p = quotemeta($param);
9566 3 my $field = qr/\$\w+(?:->)?\{\s*['"]?$p['"]?\s*\}/;
9567  
9568 # `croak ... unless @{ $p->{databases} }`.
9569 3 my $empty_guard = ($content =~ /\bcroak\b[^;]*?\bunless\b[^;]*?\@\{\s*$field\s*\}/s);
9570  
9571 # An element is blessed or has a method called on it.
9572 3 ▶ 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
201
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
9573 ($content =~ /$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*->\s*\w+/)
9574 || ($content =~ /\bblessed\s*\(\s*$field\s*(?:->\s*)?\[\s*[^\]]+\s*\]\s*\)/);
9575  
9576 3 ▶ next unless($empty_guard || $elements_are_objects);
Branch: 1 branch
BranchTrueFalseTotal
unless $empty_guard or $elements_are_objects 1 2 3
Condition100%3/3
$empty_guard or $elements_are_objects
l!l&&r!l&&!r
111
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
9577  
9578 2 ▶ return $empty_guard
Branch: 1 branch
BranchTrueFalseTotal
$empty_guard ? : 1 1 2
9579 ? "needs a non-empty arrayref of live objects in '$param'"
9580 : "needs live objects in '$param'";
9581 }
9582  
9583 95 return undef;
9584 }
9585  
9586 # True when the constructor refuses to build the package itself. Two idioms
9587 # are recognised: an explicit abstract-class check (Class::Abstract and
9588 # friends), and a croak over a constructor argument that the schema does not
9589 # describe, which the harness therefore cannot pass.
9590 sub _constructor_refuses_direct_instantiation
9591 95 ▶{
Subroutine _constructor_refuses_direct_instantiation: called CC=10 SCAR=24.6
9592 95 my ($body, $ctor_schema) = @_;
9593  
9594 # _find_methods() keeps the body as source text, but accept a PPI node too.
9595 0 ▶ my $content = ref($body) ? eval { $body->content } : $body;
Branch: 1 branch
BranchTrueFalseTotal
ref $body ? : 0 95 95
9596 95 ▶ return 0 unless(defined($content) && length($content));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 95 95
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
0095
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
9597  
9598 95 ▶ return 1 if($content =~ /\bcheck_abstract\s*\(/);
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\bcheck_abstract\s*\(/ 0 95 95
9599 95 ▶ 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 94 95
Condition100%3/3
$content =~ /\bcroak\b/ and $content =~ /\beq\s+__PACKAGE__\b/
!ll&&!rl&&r
55391
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
9600  
9601 94 ▶ my %declared = (ref($ctor_schema->{input}) eq 'HASH') ? %{$ctor_schema->{input}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $ctor_schema->{'input'} eq 'HASH' ? : 94 0 94
9602 94 ▶ 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 93 94
9603 1 ▶ return 1 unless(exists($declared{$2}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $declared{$2} 1 0 1
9604 }
9605  
9606 93 return 0;
9607 }
9608  
9609 95 ▶sub _ctor_representative_args {
Subroutine _ctor_representative_args: called CC=8 SCAR=20.9
9610 95 my ($self, $ctor_schema) = @_;
9611  
9612 95 my %REP = (
9613 string => 'test',
9614 integer => 42,
9615 float => 3.14,
9616 boolean => 1,
9617 hashref => {},
9618 arrayref => [],
9619 any => 'test',
9620 );
9621  
9622 95 ▶ my $input = $ctor_schema->{input} // {};
Condition50%1/2
$ctor_schema->{"input"} // {}
l!l
950
MC/DC0%0/1
$ctor_schema->{"input"} // {}
$ctor_schema->{"input"}
Truth table50%1/2
A
A= $ctor_schema->{"input"}
Aresult
11
00
9623 95 my %args;
9624 95 for my $param_name (keys %$input) {
9625 107 ▶ my $spec = $input->{$param_name} // {};
Condition50%1/2
$input->{$param_name} // {}
l!l
1070
MC/DC0%0/1
$input->{$param_name} // {}
$input->{$param_name}
Truth table50%1/2
A
A= $input->{$param_name}
Aresult
11
00
9626 107 ▶ next if $spec->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"optional"} 48 59 107
9627 # File-path params require a real file to exist, and object/coderef params
9628 # cannot be represented as plain YAML scalars. Such a constructor cannot
9629 # be built by the harness: calling new() with no arguments leaves a
9630 # required parameter missing, so the harness must skip the routine
9631 # rather than fail on a call it could never have made correctly.
9632 59 ▶ if ($param_name =~ /(?:file|path|dir|filename)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_name =~ /(?:file|path|dir|filename)/i) 11 48 59
9633 11 return undef;
9634 }
9635 48 ▶ my $type = $spec->{type} // 'string';
Condition50%1/2
$spec->{"type"} // "string"
l!l
480
MC/DC0%0/1
$spec->{"type"} // "string"
$spec->{"type"}
Truth table50%1/2
A
A= $spec->{"type"}
Aresult
11
00
9636 48 ▶ unless (exists $REP{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $REP{$type}) 7 41 48
9637 7 return undef;
9638 }
9639 41 $args{$param_name} = $REP{$type};
9640 }
9641 77 return \%args;
9642 }
9643  
9644 # an object to be instantiated before
9645 # it can be called, and if so return
9646 # the package name to instantiate.
9647 #
9648 # Entry: $method_name - name of the method.
9649 # $method_body - method source string.
9650 # $method_info - method hashref from
9651 # _find_methods (optional,
9652 # for backward compat).
9653 #
9654 # Exit: Returns the package name string if
9655 # object instantiation is required.
9656 # Returns undef if the method is a
9657 # constructor, factory, singleton, or
9658 # pure class method.
9659 #
9660 # Side effects: Logs analysis decisions to stdout
9661 # when verbose is set.
9662 #
9663 # Notes: Orchestrates five detection sub-steps:
9664 # factory detection, singleton detection,
9665 # instance method detection, inheritance
9666 # check, and constructor requirements.
9667 # Instance method detection overrides
9668 # factory detection when both fire.
9669 # --------------------------------------------------
9670 774 ▶sub _needs_object_instantiation {
Subroutine _needs_object_instantiation: called CC=24 SCAR=32.0
9671 774 my ($self, $method_name, $method_body, $method_info) = @_;
9672  
9673 # Allow method_info to be optional for backward compatibility
9674 774 ▶ $method_info ||= {};
Condition50%1/2
$method_info ||= {}
l!l
7740
MC/DC0%0/1
$method_info ||= {}
$method_info
Truth table50%1/2
A
A= $method_info
Aresult
11
00
9675  
9676 774 my $doc = $self->{_document};
9677 774 ▶ return undef unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 774 774
9678  
9679 # Get the current package name
9680 774 my $package_stmt = $doc->find_first('PPI::Statement::Package');
9681 774 ▶ my $current_package = $package_stmt ? $package_stmt->namespace : 'UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
$package_stmt ? : 768 6 774
9682 774 ▶ $self->{_package_name} //= $current_package;
Condition66%2/3
$self->{"_package_name"} //= $current_package
l!l&&r!l&&!r
77220
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
9683  
9684 # Initialize result structure
9685 774 my $result = {
9686 package => $current_package,
9687 needs_object => 0,
9688 type => 'unknown',
9689 details => {},
9690 constructor_params => undef,
9691 };
9692  
9693 # Track whether we should explicitly skip object instantiation
9694 774 my $skip_object = 0;
9695  
9696 # Skip constructors and destructors
9697 774 ▶ if ($method_name eq 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name eq "new") 95 679 774
9698 95 $self->_log(" OBJECT: Constructor '$method_name' detected; skipping instantiation analysis");
9699 95 return undef;
9700 }
9701 679 ▶ if($method_name =~ /^(create|build|construct|init|DESTROY)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^(create|build|construct|init|DESTROY)$/i) 1 678 679
9702 1 $skip_object = 1;
9703 }
9704  
9705 # 1. Check for factory methods that return instances
9706 679 my $is_factory = $self->_detect_factory_method(
9707 $method_name, $method_body, $current_package, $method_info
9708 );
9709  
9710 # 2. Check for singleton patterns
9711 679 my $is_singleton = $self->_detect_singleton_pattern($method_name, $method_body);
9712 679 ▶ if ($is_singleton) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_singleton) 1 678 679
9713 1 $result->{needs_object} = 0; # Singleton methods return the singleton instance
9714 1 $result->{type} = 'singleton_accessor';
9715 1 $result->{details} = $is_singleton;
9716 1 $self->_log(" OBJECT: Detected singleton accessor '$method_name'");
9717 # Singleton accessors typically don't need object creation in tests
9718 # as they're called on the class, not instance
9719 1 $skip_object = 1;
9720 }
9721  
9722 # 3. Check if this is an instance method that needs an object
9723 679 my $is_instance_method = $self->_detect_instance_method($method_name, $method_body);
9724 679 ▶ 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"};}) 376 303 679
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
29211376
$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
376011
$is_instance_method->{"explicit_self"} or $is_instance_method->{"shift_self"} or $is_instance_method->{"accesses_object_data"}
l!l&&r!l&&!r
376011
$is_instance_method->{"explicit_self"} or $is_instance_method->{"shift_self"}
l!l&&r!l&&!r
3482811
$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
9725 ($is_instance_method->{explicit_self} ||
9726 $is_instance_method->{shift_self} ||
9727 $is_instance_method->{accesses_object_data} ||
9728 ($is_instance_method->{calls_instance_methods} &&
9729 0 scalar @{$is_instance_method->{calls_instance_methods}}))) {
9730  
9731 # Instance-only methods override factory detection
9732 376 ▶ if ($is_factory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_factory) 26 350 376
9733 26 $self->_log(
9734 " OBJECT: Instance-only method '$method_name' overrides factory detection"
9735 );
9736 }
9737  
9738 376 $result->{needs_object} = 1;
9739 376 $result->{type} = 'instance_method';
9740 376 $result->{details} = $is_instance_method;
9741  
9742 # 4. Check for inheritance - if parent class constructor should be used
9743 376 my $inheritance_info = $self->_check_inheritance_for_constructor(
9744 $current_package, $method_body
9745 );
9746 376 ▶ if ($inheritance_info && $inheritance_info->{use_parent_constructor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inheritance_info and $inheritance_info->{"use_parent_constructor"}) 24 352 376
Condition100%3/3
$inheritance_info and $inheritance_info->{"use_parent_constructor"}
!ll&&!rl&&r
15419824
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
9747 24 $result->{package} = $inheritance_info->{parent_class};
9748 24 $result->{details}{inheritance} = $inheritance_info;
9749 24 $self->_log(
9750 " OBJECT: Method '$method_name' uses parent class constructor: $inheritance_info->{parent_class}"
9751 );
9752 }
9753  
9754 # 5. Check if constructor needs specific parameters
9755 my $constructor_needs = $self->_detect_constructor_requirements(
9756 $current_package, $result->{package}
9757 376 );
9758 376 ▶ if ($constructor_needs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constructor_needs) 109 267 376
9759 109 $result->{constructor_params} = $constructor_needs;
9760 109 $result->{details}{constructor_requirements} = $constructor_needs;
9761 109 $self->_log(
9762 " OBJECT: Constructor for $result->{package} requires parameters"
9763 );
9764 }
9765  
9766 # Return the package name (or parent package) that needs instantiation
9767 376 return $result->{package};
9768 }
9769  
9770 # 6. Check for class methods that might need objects from other classes
9771 303 my $needs_other_object = $self->_detect_external_object_dependency($method_body);
9772 303 ▶ if ($needs_other_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_other_object) 6 297 303
9773 6 $result->{needs_object} = 1;
9774 6 $result->{type} = 'external_dependency';
9775 $result->{package} = $needs_other_object->{package}
9776 6 ▶ if $needs_other_object->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $needs_other_object->{"package"} 6 0 6
9777 6 $result->{details} = $needs_other_object;
9778  
9779 6 $self->_log(
9780 " OBJECT: Method '$method_name' depends on external object: $needs_other_object->{package}"
9781 );
9782 6 ▶ return $result->{package} if $result->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"package"} 6 0 6
9783 }
9784  
9785 # Factory method only if NOT instance-based
9786 297 ▶ if ($is_factory && !$skip_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_factory and not $skip_object) 2 295 297
Condition100%3/3
$is_factory and not $skip_object
!ll&&!rl&&r
29322
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
9787 2 $result->{needs_object} = 0;
9788 2 $result->{type} = 'factory';
9789 2 $result->{details} = $is_factory;
9790 $self->_log(
9791 " OBJECT: Detected factory method '$method_name' returns $is_factory->{returns_class} objects"
9792 2 ▶ ) if $is_factory->{returns_class};
Branch: 1 branch
BranchTrueFalseTotal
if $is_factory->{"returns_class"} 2 0 2
9793 }
9794  
9795 297 return undef;
9796 }
9797  
9798 # --------------------------------------------------
9799 # _detect_factory_method
9800 #
9801 # Purpose: Detect whether a method is a factory
9802 # that creates and returns object
9803 # instances rather than operating on
9804 # an existing instance.
9805 #
9806 # Entry: $method_name - method name string.
9807 # $method_body - method source string.
9808 # $current_package - current package name.
9809 # $method_info - method hashref
9810 # (optional).
9811 #
9812 # Exit: Returns a factory_info hashref on
9813 # detection, or undef if the method
9814 # is not a factory.
9815 # The hashref includes: returns_class,
9816 # confidence, and one of:
9817 # returns_blessed, returns_new,
9818 # returns_factory_result, pod_hint.
9819 #
9820 # Side effects: None.
9821 # --------------------------------------------------
9822 682 ▶sub _detect_factory_method {
Subroutine _detect_factory_method: called CC=19 SCAR=32.9
9823 682 my ($self, $method_name, $method_body, $current_package, $method_info) = @_;
9824  
9825 682 my %factory_info;
9826  
9827 # Check method name patterns
9828 682 ▶ if ($method_name =~ /^(create_|make_|build_|get_)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^(create_|make_|build_|get_)/i) 21 661 682
9829 21 $factory_info{name_pattern} = 1;
9830 }
9831  
9832 # Look for object creation patterns in the method body
9833 682 ▶ if ($method_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body) 682 0 682
9834 # Pattern 1: Returns a blessed reference
9835 682 ▶ 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 676 682
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
60676
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
9836 $method_body =~ /bless\s*\{[^}]*\},\s*['"]?(\w+(?:::\w+)*|\$\w+)['"]?.*return/s) {
9837 6 my $class_name = $1;
9838  
9839 # Handle variable class names
9840 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
9841 3 $factory_info{returns_class} = $current_package;
9842 } elsif ($class_name =~ /^\$/) {
9843 1 $factory_info{returns_class} = 'VARIABLE'; # Unknown variable
9844 } else {
9845 2 $factory_info{returns_class} = $class_name;
9846 }
9847  
9848 6 $factory_info{returns_blessed} = 1;
9849 6 $factory_info{confidence} = 'high';
9850 6 return \%factory_info;
9851 }
9852  
9853 # Pattern 2: Returns ->new() call on class or $self
9854 676 ▶ 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 643 676
Condition100%3/3
$method_body =~ /return\s+([\$\w:]+)->new\(/s or $method_body =~ /([\$\w:]+)->new\(.*return/s
l!l&&r!l&&!r
231643
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
9855 $method_body =~ /([\$\w:]+)->new\(.*return/s) {
9856 33 my $target = $1;
9857  
9858 # Determine what class is being instantiated
9859 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
9860 0 $factory_info{returns_class} = $current_package;
9861 0 $factory_info{self_new} = 1;
9862 } elsif ($target =~ /::/) {
9863 28 $factory_info{returns_class} = $target;
9864 28 $factory_info{external_class} = 1;
9865 } else {
9866 5 $factory_info{returns_class} = $target;
9867 }
9868  
9869 33 $factory_info{returns_new} = 1;
9870 33 $factory_info{confidence} = 'medium';
9871 33 return \%factory_info;
9872 }
9873  
9874 # Pattern 3: Returns an object from another factory method
9875 643 ▶ 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 643 643
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
00643
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
9876 $method_body =~ /([\$\w:]+)->(create_|make_|build_|get_).*return/si) {
9877 0 $factory_info{returns_factory_result} = 1;
9878 0 $factory_info{confidence} = 'low';
9879 0 return \%factory_info;
9880 }
9881 }
9882  
9883 # Check for return type hints in POD if available
9884 643 ▶ 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"}) 400 243 643
Condition50%3/6
$method_info and ref $method_info eq "HASH" and $method_info->{"pod"}
!ll&&!rl&&r
0243400
$method_info and ref $method_info eq "HASH"
!ll&&!rl&&r
00643
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
9885 400 my $pod = $method_info->{pod};
9886 400 ▶ 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 400 400
9887 0 $factory_info{pod_hint} = 1;
9888 0 $factory_info{confidence} = 'low';
9889 0 return \%factory_info;
9890 }
9891 }
9892  
9893 643 return undef;
9894 }
9895  
9896 # --------------------------------------------------
9897 # _detect_singleton_pattern
9898 #
9899 # Purpose: Detect singleton accessor methods
9900 # that return a shared instance rather
9901 # than creating a new object, by
9902 # checking the method name and body
9903 # for singleton patterns.
9904 #
9905 # Entry: $method_name - method name string.
9906 # $method_body - method source string.
9907 #
9908 # Exit: Returns a singleton_info hashref on
9909 # detection (always contains at least
9910 # name_pattern => 1), or undef if the
9911 # method name does not match the
9912 # singleton accessor pattern.
9913 #
9914 # Side effects: None.
9915 #
9916 # Notes: Only fires for methods named
9917 # instance, get_instance, singleton,
9918 # or shared_instance. Methods not
9919 # matching these names always return
9920 # undef regardless of body content.
9921 # --------------------------------------------------
9922 685 ▶sub _detect_singleton_pattern {
Subroutine _detect_singleton_pattern: called CC=12 SCAR=25.4
9923 685 my ($self, $method_name, $method_body) = @_;
9924  
9925 # Check method name patterns
9926 685 ▶ 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 680 5 685
9927  
9928 5 my %singleton_info = (
9929 name_pattern => 1,
9930 );
9931  
9932 # Look for singleton patterns in code
9933 5 ▶ if ($method_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body) 5 0 5
9934 # Pattern 1: Static/state variable holding instance
9935 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
9936 $method_body =~ /state\s+\$(?:instance|_instance|singleton)\b/s) {
9937 4 $singleton_info{static_variable} = 1;
9938 4 $singleton_info{confidence} = 'high';
9939 }
9940  
9941 # Pattern 2: Returns $instance if defined (with better regex)
9942 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
9943 $method_body =~ /unless\s+\$instance.*?=\s*.*?new/) {
9944 1 $singleton_info{returns_instance} = 1;
9945 1 $singleton_info{confidence} = 'high';
9946 }
9947  
9948 # Pattern 3: ||= new() pattern (with better regex)
9949 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
9950 $method_body =~ /\$instance\s*=\s*.*?new\s+unless\s+(?:defined\s+)?\$instance/) {
9951 2 $singleton_info{lazy_initialization} = 1;
9952 2 $singleton_info{confidence} = 'medium';
9953 }
9954  
9955 # Pattern 4: Direct return of $instance variable
9956 5 ▶ if ($method_body =~ /return\s+\$instance;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /return\s+\$instance;/) 3 2 5
9957 3 $singleton_info{returns_instance} = 1;
9958 3 ▶ $singleton_info{confidence} = 'high' unless $singleton_info{confidence};
Branch: 1 branch
BranchTrueFalseTotal
unless $singleton_info{"confidence"} 0 3 3
9959 }
9960 }
9961  
9962 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
9963  
9964 0 return undef;
9965 }
9966  
9967 # --------------------------------------------------
9968 # _detect_instance_method
9969 #
9970 # Purpose: Detect whether a method is an
9971 # instance method that requires a
9972 # blessed object ($self) to be called,
9973 # through multiple detection patterns
9974 # of varying confidence.
9975 #
9976 # Entry: $method_name - method name string.
9977 # $method_body - method source string.
9978 #
9979 # Exit: Returns an instance_info hashref if
9980 # any instance method signal is found.
9981 # Returns undef if no signals are
9982 # detected.
9983 # The hashref may contain: explicit_self,
9984 # shift_self, uses_self,
9985 # accesses_object_data,
9986 # calls_instance_methods,
9987 # private_method, and confidence.
9988 #
9989 # Side effects: None.
9990 # --------------------------------------------------
9991 684 ▶sub _detect_instance_method {
Subroutine _detect_instance_method: called CC=14 SCAR=26.4
9992 684 my ($self, $method_name, $method_body) = @_;
9993  
9994 684 my %instance_info;
9995  
9996 # Pattern 1: my ($self, ...) = @_;
9997 684 ▶ if ($method_body =~ /my\s*\(\s*\$self\s*[,)]/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($method_body =~ /my\s*\(\s*\$self\s*[,)]/) { } 293 391 684
elsif ($method_body =~ /my\s+\$self\s*=\s*\$_\[0\]/) { } 55 336 391
elsif ($method_body =~ /my\s+\$self\s*=\s*shift/) { } 30 306 336
elsif ($method_body =~ /\$self\s*->\s*(\w+|[\{\[])/) { } 2 304 306
9998 293 $instance_info{explicit_self} = 1;
9999 293 $instance_info{confidence} = 'high';
10000 }
10001  
10002 # Pattern 1b: my $self = $_[0]; (direct-index style)
10003 elsif ($method_body =~ /my\s+\$self\s*=\s*\$_\[0\]/) {
10004 55 $instance_info{explicit_self} = 1;
10005 55 $instance_info{confidence} = 'high';
10006 }
10007  
10008 # Pattern 2: my $self = shift;
10009 elsif ($method_body =~ /my\s+\$self\s*=\s*shift/) {
10010 30 $instance_info{shift_self} = 1;
10011 30 $instance_info{confidence} = 'high';
10012 }
10013  
10014 # Pattern 3: Uses $self->something (including hash/array access)
10015 # This catches $self->{value} and $self->[0] as well as $self->method()
10016 elsif ($method_body =~ /\$self\s*->\s*(\w+|[\{\[])/) {
10017 2 $instance_info{uses_self} = 1;
10018 2 $instance_info{confidence} = 'medium';
10019 }
10020  
10021 # Pattern 4: Accesses object data: $self->{...}, $self->[...]
10022 684 ▶ if ($method_body =~ /\$self\s*->\s*[\{\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$self\s*->\s*[\{\[]/) 162 522 684
10023 162 $instance_info{accesses_object_data} = 1;
10024 162 ▶ $instance_info{confidence} = 'high' unless $instance_info{confidence} eq 'high';
Branch: 1 branch
BranchTrueFalseTotal
unless $instance_info{"confidence"} eq "high" 2 160 162
10025 }
10026  
10027 # Pattern 5: Calls other instance methods on $self
10028 684 ▶ if ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/s) 44 640 684
10029 44 $instance_info{calls_instance_methods} = [];
10030 44 ▶ while ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$self\s*->\s*(\w+)\s*\(/g) 125 44 169
10031 125 push @{$instance_info{calls_instance_methods}}, $1;
10032 }
10033 44 ▶ $instance_info{confidence} = 'high' if @{$instance_info{calls_instance_methods}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$instance_info{"calls_instance_methods"};} 44 0 44
10034 }
10035  
10036 # Pattern 6: Method name suggests instance method (not perfect but helpful)
10037 684 ▶ if ($method_name =~ /^_/ && $method_name !~ /^_new/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^_/ and not $method_name =~ /^_new/) 17 667 684
Condition100%3/3
$method_name =~ /^_/ and not $method_name =~ /^_new/
!ll&&!rl&&r
664317
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
10038 # Private methods are usually instance methods
10039 17 $instance_info{private_method} = 1;
10040 17 ▶ $instance_info{confidence} = 'low' unless exists $instance_info{confidence};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $instance_info{"confidence"} 11 6 17
10041 }
10042  
10043 684 ▶ return \%instance_info if keys %instance_info;
Branch: 1 branch
BranchTrueFalseTotal
if keys %instance_info 391 293 684
10044 293 return undef;
10045 }
10046  
10047 # --------------------------------------------------
10048 # _check_inheritance_for_constructor
10049 #
10050 # Purpose: Determine whether the current package
10051 # uses an inherited constructor from a
10052 # parent class, by examining use parent,
10053 # use base, and @ISA declarations.
10054 #
10055 # Entry: $current_package - current package
10056 # name string.
10057 # $method_body - method source string
10058 # (checked for SUPER::
10059 # calls).
10060 #
10061 # Exit: Returns an inheritance_info hashref
10062 # if any inheritance information is
10063 # found, or undef otherwise.
10064 # The hashref may contain:
10065 # parent_statements, isa_array,
10066 # uses_super, calls_super_new,
10067 # has_own_constructor,
10068 # use_parent_constructor, parent_class.
10069 #
10070 # Side effects: None.
10071 # --------------------------------------------------
10072 379 ▶sub _check_inheritance_for_constructor {
Subroutine _check_inheritance_for_constructor: called CC=18 SCAR=29.8
10073 379 my ($self, $current_package, $method_body) = @_;
10074  
10075 379 my $doc = $self->{_document};
10076 379 ▶ return undef unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 1 378 379
10077  
10078 378 my %inheritance_info;
10079  
10080 # 1. Look for parent/base statements
10081 my @parent_classes;
10082  
10083 # Find all 'use parent' or 'use base' statements
10084 378 ▶ my $includes = $doc->find('PPI::Statement::Include') || [];
Condition100%2/2
$doc->find('PPI::Statement::Include') || []
l!l
31662
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
10085 378 foreach my $inc (@$includes) {
10086 1502 my $content = $inc->content;
10087 1502 ▶ if ($content =~ /use\s+(parent|base)\s+['"]?([\w:]+)['"]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /use\s+(parent|base)\s+['"]?([\w:]+)['"]?/) 28 1474 1502
10088 28 push @parent_classes, $2;
10089 28 $inheritance_info{parent_statements} = \@parent_classes;
10090 }
10091 # Also check for multiple parents: use parent qw(Class1 Class2)
10092 1502 ▶ if ($content =~ /use\s+(parent|base)\s+qw?[\(\[]?(.+?)[\)\]]?;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /use\s+(parent|base)\s+qw?[\(\[]?(.+?)[\)\]]?;/) 0 1502 1502
10093 0 my $parents = $2;
10094 0 my @multi_parents = split /\s+/, $parents;
10095 0 push @parent_classes, @multi_parents;
10096 0 $inheritance_info{parent_statements} = \@parent_classes;
10097 }
10098 }
10099  
10100 # 2. Look for @ISA assignments (with or without 'our')
10101 378 ▶ my $isas = $doc->find('PPI::Statement::Variable') || [];
Condition100%2/2
$doc->find('PPI::Statement::Variable') || []
l!l
3762
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
10102 378 foreach my $isa (@$isas) {
10103 12461 my $content = $isa->content();
10104 # Match both "our @ISA = qw(...)" and "@ISA = qw(...)"
10105 12461 ▶ if ($content =~ /(?:our\s+)?\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /(?:our\s+)?\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) 0 12461 12461
10106 0 my $parents = $1;
10107 0 my @isa_parents = split(/\s+/, $parents);
10108 0 push @parent_classes, @isa_parents;
10109 0 $inheritance_info{isa_array} = \@isa_parents;
10110 }
10111 }
10112  
10113 # Also look for @ISA in regular statements
10114 378 ▶ my $statements = $doc->find('PPI::Statement') || [];
Condition50%1/2
$doc->find('PPI::Statement') || []
l!l
3780
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
10115 378 foreach my $stmt (@$statements) {
10116 107600 my $content = $stmt->content;
10117 107600 ▶ if ($content =~ /\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\@ISA\s*=\s*qw?[\(\[]?(.+?)[\)\]]?/) 19 107581 107600
10118 19 my $parents = $1;
10119 19 my @isa_parents = split(/\s+/, $parents);
10120 19 push @parent_classes, @isa_parents;
10121 19 $inheritance_info{isa_array} = \@isa_parents;
10122 }
10123 }
10124  
10125 # 3. Check if method uses SUPER:: calls
10126 378 ▶ if ($method_body && $method_body =~ /SUPER::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body and $method_body =~ /SUPER::/) 2 376 378
Condition100%3/3
$method_body and $method_body =~ /SUPER::/
!ll&&!rl&&r
23742
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
10127 2 $inheritance_info{uses_super} = 1;
10128 2 ▶ if ($method_body =~ /SUPER::new/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /SUPER::new/) 0 2 2
10129 0 $inheritance_info{calls_super_new} = 1;
10130 }
10131 }
10132  
10133 # 4. Check if current package has its own new method
10134 1058683 ▶ my $has_own_new = $doc->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
10135 1058683 ▶ $_[1]->isa('PPI::Statement::Sub') &&
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Sub") and $_[1]->name eq "new" 3576 1055107 1058683
10136 $_[1]->name eq 'new'
10137 378 });
10138  
10139 378 ▶ if ($has_own_new) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_own_new) { } 199 179 378
elsif (@parent_classes) { } 25 154 179
10140 199 $inheritance_info{has_own_constructor} = 1;
10141 } elsif (@parent_classes) {
10142 # No own constructor, but has parents - might need parent constructor
10143 25 $inheritance_info{use_parent_constructor} = 1;
10144 25 $inheritance_info{parent_class} = $parent_classes[0]; # Use first parent
10145 }
10146  
10147 378 ▶ return \%inheritance_info if keys %inheritance_info;
Branch: 1 branch
BranchTrueFalseTotal
if keys %inheritance_info 224 154 378
10148 154 return undef;
10149 }
10150  
10151 # --------------------------------------------------
10152 # _detect_constructor_requirements
10153 #
10154 # Purpose: Analyse the new() method of the
10155 # current or target package to determine
10156 # what parameters the constructor
10157 # requires, including required and
10158 # optional parameters and their defaults.
10159 #
10160 # Entry: $current_package - the package being
10161 # analysed.
10162 # $target_package - the package whose
10163 # constructor will
10164 # be called (may
10165 # differ from current
10166 # for inherited
10167 # constructors).
10168 #
10169 # Exit: Returns a requirements hashref on
10170 # success, or undef if no new() method
10171 # is found. For external classes,
10172 # returns a minimal hashref with
10173 # external_class => 1.
10174 #
10175 # Side effects: None.
10176 # --------------------------------------------------
10177 380 ▶sub _detect_constructor_requirements {
Subroutine _detect_constructor_requirements: called CC=16 SCAR=27.8
10178 380 my ($self, $current_package, $target_package) = @_;
10179  
10180 380 my $doc = $self->{_document};
10181 380 ▶ return undef unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 380 380
10182  
10183 # If target is different from current, we can't analyze it
10184 # (external class, parent class in different file)
10185 380 ▶ if ($target_package ne $current_package) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_package ne $current_package) 25 355 380
10186 return {
10187 25 external_class => 1,
10188 package => $target_package,
10189 note => "Constructor for external class $target_package - parameters unknown"
10190 };
10191 }
10192  
10193 # Find the new method in current package
10194 83567 ▶ my $new_method = $doc->find_first(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
10195 83567 ▶ $_[1]->isa('PPI::Statement::Sub') &&
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Sub") and $_[1]->name eq "new" 928 82639 83567
10196 $_[1]->name eq 'new'
10197 355 });
10198  
10199 355 ▶ return undef unless $new_method;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_method 155 200 355
10200  
10201 200 my %requirements;
10202  
10203 # Get method body
10204 200 my $body = $new_method->content;
10205  
10206 # Look for parameter extraction patterns - handle both $self and $class
10207 200 ▶ 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) 127 73 200
10208 127 my $params = $2;
10209 127 my @param_names = $params =~ /\$(\w+)/g;
10210  
10211 127 ▶ if (@param_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param_names) 3 124 127
10212 3 $requirements{parameters} = \@param_names;
10213 3 $requirements{parameter_count} = scalar @param_names;
10214 }
10215 }
10216  
10217 # Look for shift patterns
10218 200 my @shift_params;
10219 200 ▶ while ($body =~ /my\s+\$(\w+)\s*=\s*shift/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /my\s+\$(\w+)\s*=\s*shift/g) 9 200 209
10220 9 push @shift_params, $1;
10221 }
10222 # Remove $self or $class if present
10223 200 @shift_params = grep { $_ !~ /^(self|class|pkg|proto|klass)$/i } @shift_params;
10224  
10225 200 ▶ if (@shift_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@shift_params) 0 200 200
10226 0 $requirements{parameters} = \@shift_params;
10227 0 $requirements{parameter_count} = scalar @shift_params;
10228 0 $requirements{shift_pattern} = 1;
10229 }
10230  
10231 # Look for validation of parameters (more flexible pattern)
10232 200 my @required_params;
10233 200 ▶ if ($body =~ /croak.*unless.*(?:defined\s+)?\$(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /croak.*unless.*(?:defined\s+)?\$(\w+)/g) 86 114 200
10234 86 push @required_params, $1;
10235 }
10236 200 ▶ if ($body =~ /die.*unless.*(?:defined\s+)?\$(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /die.*unless.*(?:defined\s+)?\$(\w+)/g) 1 199 200
10237 1 push @required_params, $1;
10238 }
10239  
10240 200 ▶ if (@required_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@required_params) 87 113 200
10241 87 $requirements{required_parameters} = \@required_params;
10242 }
10243  
10244 # Look for default values (optional parameters)
10245 200 my @optional_params;
10246 my %default_values;
10247  
10248 # Use the new _extract_default_value method
10249 # Check for each parameter in the constructor body
10250 200 ▶ if ($requirements{parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requirements{"parameters"}) 3 197 200
10251 3 foreach my $param (@{$requirements{parameters}}) {
10252 5 my $default = $self->_extract_default_value($param, $body);
10253 5 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 2 3 5
10254 2 push @optional_params, $param;
10255 2 $default_values{$param} = $default;
10256 }
10257 }
10258 }
10259  
10260 200 ▶ if (@optional_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@optional_params) 2 198 200
10261 2 $requirements{optional_parameters} = \@optional_params;
10262 2 $requirements{default_values} = \%default_values;
10263 }
10264  
10265 200 ▶ return \%requirements if keys %requirements;
Branch: 1 branch
BranchTrueFalseTotal
if keys %requirements 87 113 200
10266 113 return undef;
10267 }
10268  
10269  
10270 # --------------------------------------------------
10271 # _detect_external_object_dependency
10272 #
10273 # Purpose: Detect whether a method creates or
10274 # depends on objects from classes other
10275 # than the current package, by scanning
10276 # for ->new() calls on named classes
10277 # and method calls on typed variables.
10278 #
10279 # Entry: $method_body - method source string.
10280 # May be undef.
10281 #
10282 # Exit: Returns a dependency_info hashref if
10283 # external object usage is found, or
10284 # undef otherwise.
10285 # The hashref may contain:
10286 # creates_objects (arrayref of class
10287 # names), uses_objects (arrayref of
10288 # class names), and package (the primary
10289 # dependency class).
10290 #
10291 # Side effects: None.
10292 # --------------------------------------------------
10293 309 ▶sub _detect_external_object_dependency {
Subroutine _detect_external_object_dependency: called CC=15 SCAR=27.1
10294 309 my ($self, $method_body) = @_;
10295  
10296 309 ▶ return undef unless $method_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $method_body 1 308 309
10297  
10298 308 my %dependency_info;
10299  
10300 # Pattern 1: Creates objects of other classes with ->new() or ->create()
10301 # Reset pos for global match
10302 308 pos($method_body) = 0;
10303 308 ▶ while ($method_body =~ /(\w+(?:::\w+)*)->(?:new|create)\(/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /(\w+(?:::\w+)*)->(?:new|create)\(/g) 14 308 322
10304 14 my $class = $1;
10305 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
10306 13 push @{$dependency_info{creates_objects}}, $class;
10307 }
10308  
10309 308 ▶ if ($dependency_info{creates_objects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dependency_info{"creates_objects"}) 9 299 308
10310 # Remove duplicates
10311 9 my %seen;
10312 9 $dependency_info{creates_objects} = [grep { !$seen{$_}++ } @{$dependency_info{creates_objects}}];
10313 9 $dependency_info{package} = $dependency_info{creates_objects}[0];
10314 }
10315  
10316 # Pattern 2: Calls methods on objects from other classes
10317 308 ▶ if ($method_body =~ /\$(\w+)->\w+\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$(\w+)->\w+\(/) 12 296 308
10318 12 my %object_vars;
10319 # Reset pos for global match — the if check above used a
10320 # non-/g match so it cannot have advanced pos, but the while
10321 # loop's own /g matches still need to start from the beginning.
10322 12 pos($method_body) = 0;
10323 12 ▶ while ($method_body =~ /\$(\w+)->\w+\(/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_body =~ /\$(\w+)->\w+\(/g) 25 12 37
10324 25 $object_vars{$1}++;
10325 }
10326  
10327 # Try to determine type of object variables
10328 12 my @object_classes;
10329 12 foreach my $var (keys %object_vars) {
10330 # Look for type declarations or assignments
10331 22 ▶ 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 13 22
elsif ($method_body =~ /my\s+\$$var\s*=\s*(\w+(?:::\w+)+)->/) { } 0 13 13
10332 9 push @object_classes, $1;
10333 } elsif ($method_body =~ /my\s+\$$var\s*=\s*(\w+(?:::\w+)+)->/) {
10334 0 push @object_classes, $1;
10335 }
10336 }
10337  
10338 12 ▶ if (@object_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@object_classes) 9 3 12
10339 9 $dependency_info{uses_objects} = \@object_classes;
10340 9 ▶ $dependency_info{package} = $object_classes[0] unless $dependency_info{package};
Branch: 1 branch
BranchTrueFalseTotal
unless $dependency_info{"package"} 1 8 9
10341 }
10342 }
10343  
10344 # Pattern 3: Receives objects as parameters (type hints in comments/POD)
10345 # This would need integration with parameter analysis
10346  
10347 308 ▶ return \%dependency_info if keys %dependency_info;
Branch: 1 branch
BranchTrueFalseTotal
if keys %dependency_info 10 298 308
10348 298 return undef;
10349 }
10350  
10351 # --------------------------------------------------
10352 # _get_parent_class
10353 #
10354 # Purpose: Find the first parent class of the
10355 # current package by searching the
10356 # PPI document for use parent, use base,
10357 # or our @ISA declarations.
10358 #
10359 # Entry: None (operates on $self->{_document}).
10360 #
10361 # Exit: Returns the parent class name string,
10362 # or undef if no parent is found.
10363 #
10364 # Side effects: None.
10365 # --------------------------------------------------
10366 0 ▶sub _get_parent_class {
Subroutine _get_parent_class: not called CC=5 SCAR=34.0
10367 0 my $self = $_[0];
10368  
10369 0 my $doc = $self->{_document};
10370 0 ▶ return unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 0 0
10371  
10372 # Look for use parent statements
10373 0 ▶ my $parent_stmt = $doc->find_first(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
10374 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
10375 $_[1]->type eq 'use' &&
10376 $_[1]->module =~ /^(parent|base)$/ &&
10377 $_[1]->arguments =~ /['"](\w+(?:::\w+)*)['"]/
10378 0 });
10379 0 ▶ if ($parent_stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_stmt) 0 0 0
10380 0 my $parent = $1;
10381 0 return $parent;
10382 }
10383  
10384 # Look for @ISA assignment
10385 0 ▶ my $isa_stmt = $doc->find_first(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
10386 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
10387 $_[1]->content =~ /our\s+\@ISA\s*=\s*\(\s*['"](\w+(?:::\w+)*)['"]\s*\)/
10388 0 });
10389 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
10390 0 return $1;
10391 }
10392  
10393 0 return;
10394 }
10395  
10396 # --------------------------------------------------
10397 # _get_class_for_instance_method
10398 #
10399 # Purpose: Determine which class should be used
10400 # for object instantiation when testing
10401 # an instance method, preferring the
10402 # current package if it has a new()
10403 # method, falling back to the parent
10404 # class otherwise.
10405 #
10406 # Entry: None (operates on $self->{_document}).
10407 #
10408 # Exit: Returns the package name string to
10409 # use for instantiation. Returns
10410 # 'UNKNOWN_PACKAGE' if no package
10411 # statement is found.
10412 #
10413 # Side effects: Stores the package name in
10414 # $self->{_package_name} if not
10415 # already set.
10416 # --------------------------------------------------
10417 0 ▶sub _get_class_for_instance_method {
Subroutine _get_class_for_instance_method: not called CC=5 SCAR=34.0
10418 0 my $self = $_[0];
10419  
10420 # Get the current package
10421 0 my $doc = $self->{_document};
10422 0 my $package_stmt = $doc->find_first('PPI::Statement::Package');
10423 0 ▶ return 'UNKNOWN_PACKAGE' unless $package_stmt;
Branch: 1 branch
BranchTrueFalseTotal
unless $package_stmt 0 0 0
10424 0 my $package_name = $package_stmt->namespace;
10425 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
10426  
10427 # Check if the current package has a 'new' method
10428 0 ▶ my $has_new = $doc->find(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
10429 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
10430 0 });
10431  
10432 0 ▶ if ($has_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_new) 0 0 0
10433 0 return $package_name;
10434 }
10435  
10436 # Otherwise, try to get the parent class
10437 0 my $parent = $self->_get_parent_class();
10438 0 ▶ return $parent if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 0 0 0
10439  
10440 # Fallback to current package
10441 0 return $package_name;
10442 }
10443  
10444 # --------------------------------------------------
10445 # _extract_default_value
10446 #
10447 # Purpose: Extract a default value for a named
10448 # parameter from a method body by
10449 # matching multiple common Perl default
10450 # assignment idioms.
10451 #
10452 # Entry: $param - parameter name string.
10453 # $code - method body source string.
10454 #
10455 # Exit: Returns the cleaned default value
10456 # scalar on success, or undef if no
10457 # default assignment pattern is found.
10458 #
10459 # Side effects: None.
10460 #
10461 # Notes: Eight patterns are tried in order:
10462 # ||, //=, defined ternary, unless
10463 # defined, ||=, //, multi-line if
10464 # !defined, unless defined block.
10465 # Comment lines are stripped from the
10466 # code before matching to avoid false
10467 # positives. Delegates to
10468 # _clean_default_value for value
10469 # normalisation.
10470 # --------------------------------------------------
10471 533 ▶sub _extract_default_value {
Subroutine _extract_default_value: called CC=19 SCAR=29.5
10472 533 my ($self, $param, $code) = @_;
10473  
10474 533 ▶ return undef unless $param && $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $param and $code 3 530 533
Condition100%3/3
$param and $code
!ll&&!rl&&r
12530
MC/DC100%2/2
$param and $code
$param $code
Truth table100%3/3
A and B
A= $paramB= $code
ABresult
0X0
100
111
10475  
10476 # Clean up the code for easier pattern matching
10477 # Remove comments to avoid false positives
10478 530 my $clean_code = $code;
10479 530 $clean_code =~ s/#.*$//gm;
10480 530 $clean_code =~ s/^\s+|\s+$//g;
10481  
10482 # Pattern 1: $param = $param || 'default_value'
10483 # Also handles: $param = $arg || 'default'
10484 530 ▶ 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 518 530
10485 12 my $default = $1;
10486 12 $default =~ s/\s*;\s*$//;
10487 12 $default = $self->_clean_default_value($default);
10488 12 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 12 0 12
10489 }
10490  
10491 # Pattern 2: $param //= 'default_value'
10492 518 ▶ if ($clean_code =~ /\$$param\s*\/\/=\s*([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ m[\$$param\s*//=\s*([^;]+)]) 10 508 518
10493 10 my $default = $1;
10494 10 $default =~ s/\s*;\s*$//;
10495 10 $default = $self->_clean_default_value($default);
10496 10 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 6 4 10
10497 }
10498  
10499 # Pattern 3: $param = defined $param ? $param : 'default'
10500 # Also handles: $param = defined $arg ? $arg : 'default'
10501 512 ▶ 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 506 512
10502 6 my $default = $1;
10503 6 $default =~ s/\s*;\s*$//;
10504 6 $default = $self->_clean_default_value($default);
10505 6 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 6 0 6
10506 }
10507  
10508 # Pattern 4: $param = 'default' unless defined $param;
10509 506 ▶ 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 503 506
10510 3 my $default = $1;
10511 3 $default = $self->_clean_default_value($default);
10512 3 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 3 0 3
10513 }
10514  
10515 # Pattern 5: $param ||= 'default'
10516 503 ▶ if ($clean_code =~ /\$$param\s*\|\|=\s*([^;]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_code =~ /\$$param\s*\|\|=\s*([^;]+)/) 16 487 503
10517 16 my $default = $1;
10518 16 $default =~ s/\s*;\s*$//;
10519 16 $default = $self->_clean_default_value($default);
10520 16 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 16 0 16
10521 }
10522  
10523 # Pattern 6: $param = $arg // 'default'
10524 487 ▶ 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 485 487
10525 2 my $default = $1;
10526 2 $default =~ s/\s*;\s*$//;
10527 2 $default = $self->_clean_default_value($default);
10528 2 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 1 1 2
10529 }
10530  
10531 # Pattern 7: Multi-line: if (!defined $param) { $param = 'default'; }
10532 486 ▶ 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 485 486
10533 1 my $default = $1;
10534 1 $default =~ s/\s*;\s*$//;
10535 1 $default = $self->_clean_default_value($default);
10536 1 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 1 0 1
10537 }
10538  
10539 # Pattern 8: unless (defined $param) { $param = 'default'; }
10540 485 ▶ 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 484 485
10541 1 my $default = $1;
10542 1 $default =~ s/\s*;\s*$//;
10543 1 $default = $self->_clean_default_value($default);
10544 1 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 1 0 1
10545 }
10546  
10547 484 return undef;
10548 }
10549  
10550 # --------------------------------------------------
10551 # _extract_test_hints
10552 #
10553 # Purpose: Extract structured test hints from
10554 # a method's code and schema, including
10555 # boundary values, invalid inputs, and
10556 # valid input examples from POD.
10557 #
10558 # Entry: $method - method hashref.
10559 # $schema - schema hashref as built so
10560 # far by _analyze_method.
10561 #
10562 # Exit: Returns a hints hashref with keys:
10563 # boundary_values, invalid_inputs,
10564 # equivalence_classes, valid_inputs.
10565 # Keys with empty arrays are deleted
10566 # before returning.
10567 #
10568 # Side effects: None.
10569 # --------------------------------------------------
10570 769 ▶sub _extract_test_hints {
Subroutine _extract_test_hints: called CC=4 SCAR=13.9
10571 769 my ($self, $method, $schema) = @_;
10572  
10573 769 my %hints = (
10574 boundary_values => [],
10575 invalid_inputs => [],
10576 equivalence_classes => [],
10577 valid_inputs => [],
10578 );
10579  
10580 769 my $code = $method->{body};
10581 769 ▶ return {} unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 769 769
10582  
10583 769 $self->_extract_invalid_input_hints($code, \%hints);
10584 769 $self->_extract_boundary_value_hints($code, \%hints);
10585  
10586 # prune empties
10587 769 for my $k (keys %hints) {
10588 3076 ▶ delete $hints{$k} unless @{$hints{$k}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$hints{$k};} 2949 127 3076
10589 }
10590  
10591 769 return \%hints;
10592 }
10593  
10594 # --------------------------------------------------
10595 # _extract_invalid_input_hints
10596 #
10597 # Purpose: Detect likely invalid input values
10598 # from a method body by looking for
10599 # defined checks, empty string checks,
10600 # and negative number checks.
10601 #
10602 # Entry: $code - method body source string.
10603 # $hints - hints hashref (modified in
10604 # place via invalid_inputs key).
10605 #
10606 # Exit: Returns nothing. Appends to
10607 # $hints->{invalid_inputs}.
10608 #
10609 # Side effects: None.
10610 # --------------------------------------------------
10611 773 ▶sub _extract_invalid_input_hints {
Subroutine _extract_invalid_input_hints: called CC=5 SCAR=16.1
10612 773 my ($self, $code, $hints) = @_;
10613  
10614 # undef invalid
10615 773 ▶ if ($code =~ /defined\s*\(\s*\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /defined\s*\(\s*\$/) 63 710 773
10616 63 push @{ $hints->{invalid_inputs} }, 'undef';
10617 }
10618  
10619 # empty string invalid
10620 773 ▶ if ($code =~ /\beq\s*''/ || $code =~ /\blength\s*\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\beq\s*''/ or $code =~ /\blength\s*\(/) 41 732 773
Condition100%3/3
$code =~ /\beq\s*''/ or $code =~ /\blength\s*\(/
l!l&&r!l&&!r
437732
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
10621 41 push @{ $hints->{invalid_inputs} }, '';
10622 }
10623  
10624 # negative number invalid
10625 773 ▶ if ($code =~ /\$\w+\s*<\s*0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*<\s*0/) 10 763 773
10626 10 push @{ $hints->{invalid_inputs} }, -1;
10627 }
10628 }
10629  
10630 # --------------------------------------------------
10631 # _extract_boundary_value_hints
10632 #
10633 # Purpose: Extract numeric boundary values from
10634 # comparison operators in a method body,
10635 # adding both the boundary value and
10636 # the value one step either side.
10637 #
10638 # Entry: $code - method body source string.
10639 # $hints - hints hashref (modified in
10640 # place via boundary_values key).
10641 #
10642 # Exit: Returns nothing. Appends to and
10643 # deduplicates $hints->{boundary_values}.
10644 #
10645 # Side effects: None.
10646 # --------------------------------------------------
10647 772 ▶sub _extract_boundary_value_hints {
Subroutine _extract_boundary_value_hints: called CC=6 SCAR=17.9
10648 772 my ($self, $code, $hints) = @_;
10649  
10650 772 ▶ while ($code =~ /\$\w+\s*(<=|<|>=|>)\s*(\d+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\$\w+\s*(<=|<|>=|>)\s*(\d+)/g) 49 772 821
10651 49 my ($op, $n) = ($1, $2);
10652  
10653 49 ▶ if ($op eq '<') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '<') { } 15 34 49
elsif ($op eq '<=') { } 3 31 34
elsif ($op eq '>') { } 28 3 31
elsif ($op eq '>=') { } 3 0 3
10654 15 push @{ $hints->{boundary_values} }, $n, $n+1;
10655 } elsif ($op eq '<=') {
10656 3 push @{ $hints->{boundary_values} }, $n, $n+1;
10657 } elsif ($op eq '>') {
10658 28 push @{ $hints->{boundary_values} }, $n, $n-1;
10659 } elsif ($op eq '>=') {
10660 3 push @{ $hints->{boundary_values} }, $n, $n-1;
10661 }
10662 }
10663  
10664 # Remove duplicates
10665 772 my %seen;
10666 772 $hints->{boundary_values} = [ grep { !$seen{$_}++ } @{ $hints->{boundary_values} } ];
10667 }
10668  
10669 # --------------------------------------------------
10670 # _extract_pod_examples
10671 #
10672 # Purpose: Extract example method call patterns from a method's
10673 # SYNOPSIS section (=head1 or =head2) and from any
10674 # =for example begin/end blocks, and add them as
10675 # valid_inputs hints for fuzzing.
10676 #
10677 # Entry: $pod - POD string for the method. May be undef.
10678 # $hints - hints hashref (modified in place via the
10679 # valid_inputs key).
10680 #
10681 # Exit: Returns $hints. Appends to $hints->{valid_inputs}.
10682 #
10683 # Side effects: Logs the number of examples found to stdout when
10684 # verbose is set.
10685 #
10686 # Notes: For standalone runnable round-trip tests (not just
10687 # fuzzing hints) use App::Test::Generator::PodExampleExtractor
10688 # and bin/pod-example-tester instead.
10689 # --------------------------------------------------
10690 769 ▶sub _extract_pod_examples {
Subroutine _extract_pod_examples: called CC=17 SCAR=28.5
10691 769 my ($self, $pod, $hints) = @_;
10692  
10693 769 ▶ return $hints unless $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $pod 252 517 769
10694  
10695 517 my @examples;
10696  
10697 # Accept both =head1 SYNOPSIS (module-level) and =head2 SYNOPSIS (method-level)
10698 517 my $synopsis = '';
10699 517 ▶ 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 514 517
10700 3 $synopsis = $1;
10701 }
10702  
10703 # Also collect =for example begin ... =for example end blocks
10704 517 my @for_blocks;
10705 517 ▶ 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 517 517
10706 0 push @for_blocks, $1;
10707 }
10708 517 $synopsis .= join('', @for_blocks);
10709  
10710 517 ▶ return $hints unless length($synopsis);
Branch: 1 branch
BranchTrueFalseTotal
unless length $synopsis 514 3 517
10711  
10712 # Constructor examples: ->wilma(foo => 'bar', count => 5)
10713 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
10714 3 my ($method, $args) = ($1, $2);
10715 3 my %kv;
10716  
10717 3 ▶ while ($args =~ /(\w+)\s*=>\s*(?:'([^']*)'|"([^"]*)"|(\d+))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /(\w+)\s*=>\s*(?:'([^']*)'|"([^"]*)"|(\d+))/g) 5 3 8
10718 5 my $key = $1;
10719 5 ▶ my $val = defined $2 ? $2 : defined $3 ? $3 : $4;
Branch: 2 branches
BranchTrueFalseTotal
defined $2 ? : 2 3 5
defined $3 ? : 0 3 3
10720 5 $kv{$key} = $val;
10721 }
10722  
10723 3 ▶ push @examples, {
Branch: 1 branch
BranchTrueFalseTotal
if %kv 2 1 3
10724 style => 'named',
10725 source => 'pod',
10726 args => \%kv,
10727 function => $method, # TODO: add a sanity check this is what we expect
10728 } if %kv;
10729 }
10730  
10731 3 ▶ unless(scalar(@examples)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @examples) 1 2 3
10732 # Positional calls: func($a, $b)
10733 1 ▶ while ($synopsis =~ /\b(\w+)\s*\(\s*(.*?)\s*\)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($synopsis =~ /\b(\w+)\s*\(\s*(.*?)\s*\)/gs) 1 1 2
10734 1 my ($func, $argstr) = ($1, $2);
10735  
10736 # next if $func eq 'new'; # already handled
10737  
10738 1 my @args = map { s/^\s+|\s+$//gr } split /\s*,\s*/, $argstr;
10739  
10740 1 ▶ next unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 1 1
10741  
10742 1 push @examples, {
10743 style => 'positional',
10744 source => 'pod',
10745 function => $func,
10746 args => \@args,
10747 };
10748 }
10749 }
10750  
10751 3 ▶ if (scalar(@examples)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @examples) 3 0 3
10752 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
10753 3 push @{ $hints->{valid_inputs} }, @examples;
10754  
10755 3 $self->_log(" POD: extracted " . scalar(@examples) . " example call(s)");
10756 }
10757  
10758 3 for my $k (qw(boundary_values invalid_inputs valid_inputs equivalence_classes)) {
10759 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
10760 }
10761  
10762 3 return $hints;
10763 }
10764  
10765 # --------------------------------------------------
10766 # _clean_default_value
10767 #
10768 # Purpose: Normalise a raw default value string
10769 # extracted from code or POD into a
10770 # clean Perl scalar, handling quoted
10771 # strings, numeric literals, boolean
10772 # keywords, empty containers, and
10773 # undef.
10774 #
10775 # Entry: $value - raw value string.
10776 # May be undef.
10777 # $from_code - true if the value was
10778 # extracted from source
10779 # code (affects escape
10780 # sequence handling).
10781 #
10782 # Exit: Returns the cleaned value:
10783 # undef for undef or unparseable
10784 # {} for empty hashrefs
10785 # [] for empty arrayrefs
10786 # integer for whole numbers
10787 # float for decimal numbers
10788 # 1 or 0 for boolean keywords
10789 # string for everything else
10790 #
10791 # Side effects: None.
10792 # --------------------------------------------------
10793 252 ▶sub _clean_default_value {
Subroutine _clean_default_value: called CC=23 SCAR=31.5
10794 252 my ($self, $value, $from_code) = @_;
10795  
10796 252 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 251 252
10797  
10798 # Remove leading/trailing whitespace
10799 251 $value =~ s/^\s+|\s+$//g;
10800  
10801 # Remove parenthetical notes like "(no password)" only if there's content before them
10802 251 $value =~ s/(\S+)\s*\([^)]+\)\s*$/$1/;
10803 251 $value =~ s/^\s+|\s+$//g;
10804  
10805 # Handle chained || or // operators - extract the rightmost value
10806 251 ▶ if ($value =~ /\|\||\/{2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[\|\||/{2}]) 6 245 251
10807 6 my @parts = split(/\s*(?:\|\||\/{2})\s*/, $value);
10808 6 $value = $parts[-1];
10809 6 $value =~ s/^\s+|\s+$//g;
10810 }
10811  
10812 # Remove trailing semicolon if present
10813 251 $value =~ s/;\s*$//;
10814  
10815 # Handle q{}, qq{}, qw{} quotes
10816 251 ▶ if ($value =~ /^qq?\{(.*?)\}$/s) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ /^qq?\{(.*?)\}$/s) { } 2 249 251
elsif ($value =~ /^qw\{(.*?)\}$/s) { } 0 249 249
elsif ($value =~ /^q[qwx]?\s*([^a-zA-Z0-9\{\[])(.*?)\1$/s) { } 0 249 249
10817 2 $value = $1;
10818 } elsif ($value =~ /^qw\{(.*?)\}$/s) {
10819 0 $value = $1;
10820 } elsif ($value =~ /^q[qwx]?\s*([^a-zA-Z0-9\{\[])(.*?)\1$/s) {
10821 0 $value = $2;
10822 }
10823  
10824 # Handle quoted strings
10825 251 ▶ if ($value =~ /^(['"])(.*)\1$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(['"])(.*)\1$/s) 52 199 251
10826 52 $value = $2;
10827  
10828 52 ▶ if ($from_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_code) 16 36 52
10829 # In regex captures from source code, escape sequences are doubled
10830 # \\n in capture needs to become \n for the test
10831 16 $value =~ s/\\\\/\\/g;
10832 }
10833  
10834 # Only unescape the quote characters themselves
10835 52 $value =~ s/\\"/"/g;
10836 52 $value =~ s/\\'/'/g;
10837  
10838 # If NOT from code (i.e., from POD), interpret escape sequences
10839 52 ▶ unless ($from_code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from_code) 36 16 52
10840 36 $value =~ s/\\n/\n/g;
10841 36 $value =~ s/\\r/\r/g;
10842 36 $value =~ s/\\t/\t/g;
10843 36 $value =~ s/\\\\/\\/g;
10844 }
10845 }
10846  
10847 # Sometimes trailing ) is left on
10848 251 ▶ if($value !~ /^\(/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\(/) 249 2 251
10849 249 $value =~ s/\)$//;
10850 }
10851  
10852 # Handle Perl empty hash (must be before numeric/boolean checks)
10853 251 ▶ if ($value =~ /^\{\s*\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\{\s*\}$/) 13 238 251
10854 13 return {};
10855 }
10856  
10857 # Handle Perl empty list/array
10858 238 ▶ if ($value =~ /^\[\s*\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\[\s*\]$/) 7 231 238
10859 7 return [];
10860 }
10861  
10862 # Handle numeric values
10863 231 ▶ if ($value =~ /^-?\d+(?:\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-?\d+(?:\.\d+)?$/) 66 165 231
10864 66 ▶ if ($value =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\./) { } 13 53 66
10865 13 return $value + 0;
10866 } else {
10867 53 return int($value);
10868 }
10869 }
10870  
10871 # Handle boolean keywords
10872 165 ▶ if ($value =~ /^(true|false)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|false)$/i) 7 158 165
10873 7 ▶ return lc($1) eq 'true' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc $1 eq 'true' ? : 3 4 7
10874 }
10875  
10876 # Handle Perl boolean constants
10877 158 ▶ if ($value eq '1') {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq '1') { } 0 158 158
elsif ($value eq '0') { } 0 158 158
10878 0 return 1;
10879 } elsif ($value eq '0') {
10880 0 return 0;
10881 }
10882  
10883 # Handle undef
10884 158 ▶ if ($value eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "undef") 7 151 158
10885 7 return undef;
10886 }
10887  
10888 # Handle __PACKAGE__ and similar constants
10889 151 ▶ if ($value eq '__PACKAGE__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "__PACKAGE__") 1 150 151
10890 1 return '__PACKAGE__';
10891 }
10892  
10893 # Remove surrounding parentheses
10894 150 $value =~ s/^\((.+)\)$/$1/;
10895  
10896 # Handle expressions we can't evaluate
10897 150 ▶ if ($value =~ /^\$[a-zA-Z_]/ || $value =~ /\(.*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\$[a-zA-Z_]/ or $value =~ /\(.*\)/) 46 104 150
Condition100%3/3
$value =~ /^\$[a-zA-Z_]/ or $value =~ /\(.*\)/
l!l&&r!l&&!r
406104
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
10898 46 ▶ return if($value =~ /^\$|\@|\%/); # The default is a value, so who knows its type?
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\$|\@|\%/ 40 6 46
10899 # return $value;
10900 }
10901  
10902 110 return $value;
10903 }
10904  
10905 # --------------------------------------------------
10906 # _validate_pod_code_agreement
10907 #
10908 # Purpose: Compare POD parameter documentation
10909 # against code-inferred parameters and
10910 # return a list of disagreements when
10911 # strict_pod mode is enabled.
10912 #
10913 # Entry: $pod_params - hashref of parameters
10914 # from POD analysis.
10915 # $code_params - hashref of parameters
10916 # from code analysis.
10917 # $method_name - method name string,
10918 # used for context in
10919 # error messages.
10920 #
10921 # Exit: Returns a list of disagreement
10922 # strings. Returns an empty list if
10923 # all parameters agree.
10924 #
10925 # Side effects: None.
10926 #
10927 # Notes: Type mismatches are classified as
10928 # either 'compatible' (e.g. integer vs
10929 # number) or 'incompatible' via
10930 # _types_are_compatible. $self and
10931 # $class are excluded from undocumented
10932 # parameter warnings in appropriate
10933 # context.
10934 # --------------------------------------------------
10935 379 ▶sub _validate_pod_code_agreement {
Subroutine _validate_pod_code_agreement: called CC=36 SCAR=36.3
10936 379 my ($self, $pod_params, $code_params, $method_name) = @_;
10937  
10938 379 my @errors;
10939  
10940 # Get all parameter names from both sources
10941 379 my %all_params = map { $_ => 1 } (keys %$pod_params, keys %$code_params);
10942  
10943 379 foreach my $param (sort keys %all_params) {
10944 411 ▶ my $pod = $pod_params->{$param} || {};
Condition100%2/2
$pod_params->{$param} || {}
l!l
39021
MC/DC100%1/1
$pod_params->{$param} || {}
$pod_params->{$param}
Truth table100%2/2
A
A= $pod_params->{$param}
Aresult
11
00
10945 411 ▶ my $code = $code_params->{$param} || {};
Condition100%2/2
$code_params->{$param} || {}
l!l
253158
MC/DC100%1/1
$code_params->{$param} || {}
$code_params->{$param}
Truth table100%2/2
A
A= $code_params->{$param}
Aresult
11
00
10946  
10947 # A =head3|4 Input formal spec is authoritative for the parameter's
10948 # existence, type, optionality and pattern: heuristic code analysis may
10949 # legitimately disagree. Its bounds are still checked below, because a
10950 # formal minimum and a code maximum that do not overlap leave the merged
10951 # schema with no valid value.
10952 411 ▶ unless(($pod->{_source} // '') eq SOURCE_INPUT_SPEC) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($pod->{"_source"} // "") eq "input_spec") 69 342 411
Condition100%2/2
$pod->{"_source"} // ""
l!l
38526
MC/DC100%1/1
$pod->{"_source"} // ""
$pod->{"_source"}
Truth table100%2/2
A
A= $pod->{"_source"}
Aresult
11
00
10953 # Check if parameter exists in both
10954 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
10955 4 push @errors, "Parameter '\$$param' documented in POD but not found in code signature";
10956 4 next;
10957 }
10958  
10959 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
10960 21 ▶ if($param =~ /^(class|pkg|proto|klass)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(class|pkg|proto|klass)$/i) 0 21 21
10961 # class invocant, not a user-facing parameter
10962 0 next;
10963 }
10964 21 ▶ if($param eq 'self') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "self") 0 21 21
10965 # instance invocant, not a user-facing parameter
10966 0 next;
10967 }
10968 21 push @errors, "Parameter '\$$param' found in code but not documented in POD";
10969 21 next;
10970 }
10971  
10972 # Compare types if both exist
10973 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
10974 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
10975 5 push @errors, "Type mismatch for '\$$param': POD says '$pod->{type}', code suggests '$code->{type}' (incompatible)";
10976 } else {
10977 4 push @errors, "Type difference for '\$$param': POD says '$pod->{type}', code suggests '$code->{type}' (compatible)";
10978 }
10979 }
10980  
10981 # Compare optional status if both exist
10982 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
10983 $pod->{optional} != $code->{optional}) {
10984 3 ▶ my $pod_status = $pod->{optional} ? 'optional' : 'required';
Branch: 1 branch
BranchTrueFalseTotal
$pod->{'optional'} ? : 1 2 3
10985 3 ▶ my $code_status = $code->{optional} ? 'optional' : 'required';
Branch: 1 branch
BranchTrueFalseTotal
$code->{'optional'} ? : 2 1 3
10986 3 push @errors, "Optional status mismatch for '\$$param': POD says '$pod_status', code suggests '$code_status'";
10987 }
10988  
10989 # Check regex patterns
10990 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
10991 0 push @errors, "Pattern mismatch for '\$$param': POD says '$pod->{matches}', code suggests '$code->{matches}'";
10992 }
10993 }
10994  
10995 # Check constraints (min/max), including a formal spec's bounds.
10996 386 ▶ 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 384 386
Condition100%6/6
defined $pod->{"min"} and defined $code->{"min"} and $pod->{"min"} != $code->{"min"}
!ll&&!rl&&r
37592
defined $pod->{"min"} and defined $code->{"min"}
!ll&&!rl&&r
3423311
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
10997 2 push @errors, "Min constraint mismatch for '\$$param': POD says '$pod->{min}', code suggests '$code->{min}'";
10998 }
10999  
11000 386 ▶ 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 385 386
Condition100%6/6
defined $pod->{"max"} and defined $code->{"max"} and $pod->{"max"} != $code->{"max"}
!ll&&!rl&&r
37691
defined $pod->{"max"} and defined $code->{"max"}
!ll&&!rl&&r
3581810
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
11001 1 push @errors, "Max constraint mismatch for '\$$param': POD says '$pod->{max}', code suggests '$code->{max}'";
11002 }
11003  
11004 386 ▶ 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 384 386
Condition100%6/6
defined $pod->{"min"} and defined $code->{"max"} and $pod->{"min"} > $code->{"max"}
!ll&&!rl&&r
37772
defined $pod->{"min"} and defined $code->{"max"}
!ll&&!rl&&r
342359
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
11005 2 push @errors, "Constraint contradiction for '\$$param': POD says min '$pod->{min}', code suggests max '$code->{max}' (no valid value)";
11006 }
11007  
11008 386 ▶ 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 386 386
Condition83%5/6
defined $code->{"min"} and defined $pod->{"max"} and $code->{"min"} > $pod->{"max"}
!ll&&!rl&&r
37880
defined $code->{"min"} and defined $pod->{"max"}
!ll&&!rl&&r
37358
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
11009 0 push @errors, "Constraint contradiction for '\$$param': code suggests min '$code->{min}', POD says max '$pod->{max}' (no valid value)";
11010 }
11011 }
11012  
11013 # Return errors (empty array if no errors)
11014 379 return @errors;
11015 }
11016  
11017 # --------------------------------------------------
11018 # _validate_strictness_level
11019 #
11020 # Purpose: Validate and normalise the strict_pod
11021 # option value accepted by new() into
11022 # an integer level: 0 (off), 1 (warn),
11023 # or 2 (fatal).
11024 #
11025 # Entry: $val - the raw value passed to
11026 # strict_pod in new(). May be
11027 # undef, a number, or a string.
11028 #
11029 # Exit: Returns 0, 1, or 2.
11030 # Croaks if the value is not recognised.
11031 #
11032 # Side effects: None.
11033 # --------------------------------------------------
11034 1155 ▶sub _validate_strictness_level {
Subroutine _validate_strictness_level: called CC=5 SCAR=16.1
11035 1155 my $val = $_[0];
11036  
11037 1155 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 950 205 1155
11038  
11039 # Numeric
11040 205 ▶ return 0 if $val =~ /^(0|off|none)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(0|off|none)$/i 10 195 205
11041 195 ▶ return 1 if $val =~ /^(1|warn|warning)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(1|warn|warning)$/i 146 49 195
11042 49 ▶ return 2 if $val =~ /^(2|fatal|die|error)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(2|fatal|die|error)$/i 47 2 49
11043  
11044 2 croak("Invalid value for --strict-pod: '$val' (use off|warn|fatal)");
11045 }
11046  
11047 # --------------------------------------------------
11048 # _types_are_compatible
11049 #
11050 # Purpose: Determine whether two type strings
11051 # are compatible for POD/code agreement
11052 # checking, allowing semantically
11053 # equivalent types (e.g. 'integer' and
11054 # 'number') to coexist without
11055 # triggering a strict POD warning.
11056 #
11057 # Entry: $pod_type - type string from POD.
11058 # $code_type - type string from code.
11059 #
11060 # Exit: Returns 1 if compatible, 0 otherwise.
11061 #
11062 # Side effects: None.
11063 # --------------------------------------------------
11064 21 ▶sub _types_are_compatible {
Subroutine _types_are_compatible: called CC=4 SCAR=13.9
11065 21 my ($self, $pod_type, $code_type) = @_;
11066  
11067 # Exact match is always compatible
11068 21 ▶ return 1 if $pod_type eq $code_type;
Branch: 1 branch
BranchTrueFalseTotal
if $pod_type eq $code_type 3 18 21
11069  
11070 # Define compatibility matrix
11071 18 my %compatible_types = (
11072 'integer' => ['number', 'scalar'],
11073 'number' => ['scalar'],
11074 'string' => ['scalar'],
11075 'scalar' => ['string', 'integer', 'number'],
11076 'arrayref' => ['array'],
11077 'hashref' => ['hash'],
11078 );
11079  
11080 # Check if code_type is compatible with pod_type
11081 18 ▶ if (my $allowed = $compatible_types{$pod_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed = $compatible_types{$pod_type}) 17 1 18
11082 17 return grep { $_ eq $code_type } @$allowed;
11083 }
11084  
11085 # Check if pod_type is compatible with code_type
11086 1 ▶ if (my $allowed = $compatible_types{$code_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed = $compatible_types{$code_type}) 1 0 1
11087 1 return grep { $_ eq $pod_type } @$allowed;
11088 }
11089  
11090 0 return 0; # Not compatible
11091 }
11092  
11093 =head2 generate_pod_validation_report
11094  
11095 Generate a human-readable report of all POD/code disagreements found
11096 across a set of extracted schemas.
11097  
11098 my $schemas = $extractor->extract_all(no_write => 1);
11099 my $report = $extractor->generate_pod_validation_report($schemas);
11100 print $report;
11101  
11102 =head3 Arguments
11103  
11104 =over 4
11105  
11106 =item * C<$schemas>
11107  
11108 A hashref of method name to schema hashref as returned by
11109 C<extract_all>. Required.
11110  
11111 =back
11112  
11113 =head3 Returns
11114  
11115 A string containing the full validation report, or a single line
11116 confirming all methods passed if no disagreements were found.
11117  
11118 =head3 Side effects
11119  
11120 None.
11121  
11122 =head3 Notes
11123  
11124 Only methods whose schemas contain a C<_pod_validation_errors> key
11125 (populated when C<strict_pod> is 1 or 2) appear in the report. If
11126 C<strict_pod> was 0 when C<extract_all> was called, this method will
11127 always return the all-passed message.
11128  
11129 =head3 API specification
11130  
11131 =head4 input
11132  
11133 {
11134 self => { type => OBJECT, isa => 'App::Test::Generator::SchemaExtractor' },
11135 schemas => { type => HASHREF },
11136 }
11137  
11138 =head4 output
11139  
11140 { type => SCALAR }
11141  
11142 =cut
11143  
11144 17 ▶sub generate_pod_validation_report {
Subroutine generate_pod_validation_report: called CC=5 SCAR=16.1
Pod documented
11145 17 my ($self, $schemas) = @_;
11146  
11147 17 my @reports;
11148 17 foreach my $method_name (sort keys %$schemas) {
11149 26 my $schema = $schemas->{$method_name};
11150  
11151 26 ▶ if (my $errors = $schema->{_pod_validation_errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $schema->{"_pod_validation_errors"}) 16 10 26
11152 16 push @reports, "Method: $method_name";
11153 16 ▶ push @reports, " Severity: " . ($schema->{_pod_disagreement} ? 'warning' : 'fatal');
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'_pod_disagreement'} ? : 13 3 16
11154 16 push @reports, " Errors:";
11155 16 push @reports, map { " - $_" } @$errors;
11156 16 push @reports, '';
11157 }
11158 }
11159  
11160 17 ▶ if (@reports) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reports) { } 11 6 17
11161 11 return join("\n", "POD/Code Validation Report:", '=' x 40, '', @reports);
11162 } else {
11163 6 return 'POD/Code Validation: All methods passed consistency checks.';
11164 }
11165 }
11166  
11167 =head2 _log
11168  
11169 Log a message if verbose mode is on.
11170  
11171 =cut
11172  
11173 13032 ▶sub _log {
Subroutine _log: called CC=2 SCAR=6.9
11174 13032 my($self, $msg) = @_;
11175  
11176 13032 ▶ print "$msg\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 38 12994 13032
11177 }
11178  
11179 =head1 NOTES
11180  
11181 C<SchemaExtractor> uses heuristic analysis of Perl source and POD to infer
11182 parameter types and constraints. Inference accuracy improves with
11183 well-documented modules; C<=head3 Input> / C<=head4 Input> formal specs are
11184 parsed at highest priority and override all heuristics.
11185  
11186 A lone name in a C<=head4 input> block is treated as the first positional
11187 argument when the body reads a positional argument (C<< $_[0] >>, a list
11188 assignment from C<@_>, or a C<shift>) and does not read an argument hash
11189 (C<my %args = @_>, a hashref dereference, or C<< $_[0]{...} >>); the
11190 documented name is then a label for that argument, as C<name> is for
11191 C<$_[0]> in L<Lingua::EN::NameCase/nc>. Two or more names, and a body that
11192 only forwards C<@_>, remain named arguments.
11193  
11194 The output is always a best-effort schema suitable as a starting template;
11195 review and augment the generated YAML before using it as a definitive
11196 specification. Pass it to L<App::Test::Generator> to generate fuzz harnesses.
11197  
11198 =head1 TODO
11199  
11200 Extend C<=head4 Input> parsing to cover the C<enum>/C<memberof> constraint
11201 synonym (union types, e.g. C<scalar | scalarref>, are already handled by
11202 C<_map_formal_input_type>).
11203  
11204 =head1 SEE ALSO
11205  
11206 =over 4
11207  
11208 =item * L<App::Test::Generator> - Generate fuzz and corpus-driven test harnesses
11209  
11210 Output from this module serves as input to that module.
11211 So with well-documented code, you can automatically create your tests.
11212  
11213 =item * L<App::Test::Generator::Template> - Template of the file of tests created by C<App::Test::Generator>
11214  
11215 =back
11216  
11217 =head1 AUTHOR
11218  
11219 Nigel Horne, C<< <njh at nigelhorne.com> >>
11220  
11221 =head1 LICENCE AND COPYRIGHT
11222  
11223 Copyright 2025-2026 Nigel Horne.
11224  
11225 Usage is subject to GPL2 licence terms.
11226 If you use it,
11227 please let me know.
11228  
11229 =cut
11230  
11231 157 1;
« bin/fuzz-harness-generator ↑ Summary lib/App/Test/Generator/TestStrategy.pm »