Problem: Implementing fluent interface with many methods yields class complexity metric growing very fast.
How to keep low complexity for class which implements fluent interface?
Some information about specific class:
- Class already has 25 methods and will get another 15-ish more.
- All methods in class transform
$this->wrapped
object in one way or another. - Several (5-7) methods reuses already existing methods (those can be extracted to class and added via inheritance, not issue here).
Already considered options:
- Traits - I want to support PHP 5.3 and up.
- One class per method - Massive extend chain, not nice.
- 'Plugins' - helper classes somehow injected into "master class", called via magic methods and autocomplete support added via
@method
annotation.
Feedback (on design, performance, maintainability, etc.) for any options is highly anticipated.
Checked examples:
Lodash: 170-ish methods, 1 class, 8400 lines
Doctrine2 QueryBuilder: 40+40-ish methods, 2 classes, 1400+600 lines of code; separated via
ExpressionBuilder
classSymfony2 FormBuilder: 10-ish exposed methods, 1 class, 300 lines
Question can be considered language-agnostic - answers both from PHP implementation and design point of view are both equally welcome.
EDIT:
Aim is to have nice (easy to use and easy to maintain) tool for functional programming (map
, reduce
, etc.)
$this
) in methods, that return no other values, which adds 1 LOC pro method at most (and no additional cyclomatic complexity). Or do you mean another definition of fluent interface and/or complexity? @Tortuosity – Onanismreturn $this
). Complexity adds up very quickly. – Tortuosity