You could make your code "more dynamic" by creating a whitelist of "actionable keys" and the associated method name to be called, but that seems like unnecessary convolution.
Your current method is direct and literal and probably makes sense to those on your development team. I only have a few minor suggestions, but overall you should keep your basic structure.
It is not clear where $pump
and $pumpFactory
come from in your posted method, so I cannot offer advice on those variables. I also don't know what processes are actioned by the subsequent method calls, nor do I know the value type (boolean?, array?, string?, etc) or value of the return. A DocBlock would be a wise choice.
class AssemblyFactory
{
/**
* Generates assembly data based on model number or product id.
*
* @param array $params An array of pump details expected
* to contain model number or product id.
*
* @throws InvalidArgumentException if $params does not contain a
* model number or product id.
*
* @return string Pump assembly name
*/
public function makeAssembly(array $params)
{
if (array_key_exists('modelNumber', $params)) {
return $pumpFactory->fromModelNumber($params['modelNumber']);
}
if (array_key_exists('productId', $params)) {
return $pumpFactory->fromProductId($params['productId']);
}
throw new \InvalidArgumentException("FAIL");
}
}
When seemingly simple code doesn't feel clear enough (or if you really love documentation blocks), then use DocBlocks to explain your script and avoid misinterpretation or lost time in the future.
PSR-5: PHPDoc
Here's an example that is relevant to your code: https://docs.phpdoc.org/references/phpdoc/tags/throws.html
Beyond your provided code, I feel that you should determine which lookup method is more efficient within your application (If this is a database lookup, is one column a Primary Key and another one not?). Make the more efficient process the default one.
Alternatively, if you have two separate collections of data containing redundant data, consider merging the data storage so that only one lookup is necessary.
$pumpFactory
seems to fly in from nowhere. \$\endgroup\$$params
can have more than one param. It can have other keys and values. It may even have bothproductId
andmodelNumber
(doesn't matter which one takes precedence, although if requiredproductId
can take precedence overmodelNumber
). I was hoping for a way to better use PHP facilities, to where any errors with input or any other errors are shown early, so that I can fix them. Or there is more clarity in the code. With the array, it is not immediately clear what are the actual expected parameters, so future programmers will have harder time deciphering and understanding code. \$\endgroup\$$params
was hiding the parameters. i.e. not clear how many parameters there are, or if valid parameters are being passed. \$\endgroup\$