Skip to content

Simplify Twig templates with pre-computed values#100

Merged
dereuromark merged 1 commit intomasterfrom
refactor/template-data-preparer
Mar 23, 2026
Merged

Simplify Twig templates with pre-computed values#100
dereuromark merged 1 commit intomasterfrom
refactor/template-data-preparer

Conversation

@dereuromark
Copy link
Contributor

@dereuromark dereuromark commented Mar 23, 2026

Summary

Introduces TemplateDataPreparer class that moves conditional logic from Twig templates into PHP for better maintainability and testability.

Key principle: Only extract logic that computes flags/values - no PHP code generation inside PHP.

Pre-computed values

DTO-level:

  • needsInvalidArgumentException - true if any field is required
  • needsRuntimeException - true if any field is nullable or has associative singular

Field-level flags:

  • useWithMethod - use with* methods (immutable DTOs)
  • needsOrFailMethod - generate *OrFail variants (nullable fields)
  • needsHasMethod - generate has* method
  • useImmutableCollectionMethods - withAdded/withRemoved
  • useMutableCollectionMethods - add/remove

Property declaration:

  • propertyVisibility - 'protected' or 'public readonly'
  • propertyTypeHint - computed type hint
  • propertyDefault - default value string

Templates simplified

Template Before After
dto.twig 7-line loop to detect exceptions Uses pre-computed flags
methods.twig Dense single-line conditionals Readable flag checks with comments
property.twig 300+ char line with nested conditionals Uses pre-computed values

What we intentionally kept in Twig

  • Lazy loading hydration code in method_get.twig - kept as template conditionals to avoid generating PHP inside PHP

@codecov
Copy link

codecov bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.09%. Comparing base (0476c67) to head (8f70c5d).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/Generator/TemplateDataPreparer.php 80.95% 12 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #100      +/-   ##
============================================
- Coverage     83.11%   83.09%   -0.03%     
- Complexity     1415     1447      +32     
============================================
  Files            41       42       +1     
  Lines          3477     3543      +66     
============================================
+ Hits           2890     2944      +54     
- Misses          587      599      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Introduces TemplateDataPreparer class that moves conditional logic from
Twig templates into PHP for better maintainability and testability.

Pre-computed values (no PHP code generation):
- needsInvalidArgumentException/needsRuntimeException (DTO-level)
- useWithMethod, needsOrFailMethod, needsHasMethod (field flags)
- useImmutableCollectionMethods, useMutableCollectionMethods (field flags)
- propertyVisibility, propertyTypeHint, propertyDefault (property declaration)

Templates simplified:
- dto.twig: Removed 7-line exception detection loop
- methods.twig: Replaced dense conditionals with readable flag checks
- property.twig: Simplified complex line to use pre-computed values
@dereuromark dereuromark force-pushed the refactor/template-data-preparer branch from 43dd3d8 to 8f70c5d Compare March 23, 2026 15:34
@dereuromark dereuromark merged commit d026f17 into master Mar 23, 2026
10 of 12 checks passed
@dereuromark dereuromark deleted the refactor/template-data-preparer branch March 23, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant