Reducing complexity of method I'm trying to reduce the complexity of some methods, and I'm not exactly sure what approach to take. I'm currently building a PHP wrapper for a REST API, and the main problematic class is here:

https://github.com/petrepatrasc/blizzard-starcraft-api/blob/master/Service/ApiService.php

In short, since data retrieved from the API is not always consistent, I have to do this:

$portrait = new Portrait();
$portrait-&gt;setXCoordinate(isset($apiData['portrait']['x']) ? $apiData['portrait']['x'] : null)
    -&gt;setYCoordinate(isset($apiData['portrait']['y']) ? $apiData['portrait']['y'] : null)
    -&gt;setWidth(isset($apiData['portrait']['w']) ? $apiData['portrait']['w'] : null)
    -&gt;setHeight(isset($apiData['portrait']['h']) ? $apiData['portrait']['h'] : null)
    -&gt;setOffset(isset($apiData['portrait']['offset']) ? $apiData['portrait']['offset'] : null)
    -&gt;setUrl(isset($apiData['portrait']['url']) ? $apiData['portrait']['url'] : null);
return $portrait;


What would be a better way of handling the problem, so that the method complexity goes down? I suppose that I can try and catch an "undefined index exception", but I kind of wanted to see if there's something a bit more elegant that I can use.
You could remove some duplication with a getPortraitValue function:

function getPortraitValue($apiData, $key) {
    $portrait = $apiData['portrait'];
    if (isset($portrait[$key])) {
        return $portrait[$key];
    }
    return null;
}

$portrait = new Portrait();
$portrait-&gt;setXCoordinate(getPortraitValue($apiData, 'x')
    -&gt;setYCoordinate(getPortraitValue($apiData, 'y'))
    -&gt;setWidth(getPortraitValue($apiData, 'w'))
    -&gt;setHeight(getPortraitValue($apiData, 'h'))
    -&gt;setOffset(getPortraitValue($apiData, 'offset')
    -&gt;setUrl(getPortraitValue($apiData, 'url');
return $portrait;