I have two components, both are a parser/builder pair. So component A can parse A and build A, component B can parse B and build B.
A and B both contain different entities, which are extracted by the parsers.
Example, BParser works the same:
$AParserResult = AParser::parse(A);
dump(
$AParserResult->getEntityZ(),
$AParserResult->getEntityX(),
$AParserResult->getEntityY(),
...
);
$aBuilderSpecification = ABuilderSpecification::new()
->addEntityZ(Z)
->addEntityY(Y)
->addEntityX(X);
A = ABuilder::build($aBuilderSpecification);
// now I have a "fully functionally" A again
My task is to write component C that uses AParser to parse an A, then create a BBuilderSpecification from the AParserResult and then pass that to the BBuilder and get a B from it - and the other way round, creating a B from an A.
The parsers/builders are relatively straightforward to use, however, my code in component C mostly looks like this:
// in method 'transformAToB()'
$AParserResult = AParser::parse(A);
$BBuilderSpecification = BBuilderSpecification::new();
if ($AParserResult->hasEntityZ()) {
$BBuilderSpecification->setEntityZ($AParserResult->getEntityZ());
}
if ($AParserResult->hasEntityX()) {
$BBuilderSpecification->setEntityX($AParserResult->getEntityX());
}
// sometimes some simple conditions maybe, and simple transformations
if ($AParserResult->hasEntityY() && someCondition()) {
$BBuilderSpecification->setEntityY(to_lower_case($AParserResult->getEntityY()));
}
...
// Goes on for about 10 more entities
// and very similar code in the method 'transformBToA'
This is very repetitive code - I wonder is there any way to clean that up/refactoring it - without introducing additional complexity by adding an XML configuration or so … in the end it's (almost) only boilerplate code.
Language is PHP, if that matters.