Skip to content

Instantly share code, notes, and snippets.

@puiutucutu
Last active September 29, 2015 13:12
Show Gist options
  • Save puiutucutu/06f6b2361fddeb3e396a to your computer and use it in GitHub Desktop.
Save puiutucutu/06f6b2361fddeb3e396a to your computer and use it in GitHub Desktop.
<?php
/**
* Checks for a partial match in the geocode response
*
* @return boolean
*/
public function isPartialResponse() {
if (isset($this->geocodeResponse['results'][0]['partial_match']) && $this->geocodeResponse['results'][0]['partial_match'] == 'true')
return true;
return false;
}
@puiutucutu
Copy link
Author

Decision / Solution

Initially I went with option 1, opting to use two independent if structures which also allowed for early escaping. The one problem I have with this is the alternating false, true, and default false return pathway.

/**
 * Checks if the geocode json response also returned a field for partial match. 
 * 
 * Need to refactor at one point, do not like the alternating false, true, false structure.
 * One solution is to nest the if statements.
 *
 * @return boolean
 */
public function isPartialResponse()
{   
    if ( ! isset($this->geocodeResponse['results'][0]['partial_match']))
        return false;

    if ($this->geocodeResponse['results'][0]['partial_match'] === true)
        return true;

    return true;
}

I could instead change the second if condition to say...

if ($this->geocodeResponse['results'][0]['partial_match'] !== true)
    return false;

instead of...

if ($this->geocodeResponse['results'][0]['partial_match'] === true)
    return true;

and then the return structure would be false, false, default to true. Also worth noting here is that PHP interprets [partial_match] = 1 as boolean true.

However...

I have decided to use the following code. It still allows for escaping early on the first conditional if and also reduces the return pathways to two, at the expense of having a nested if structure.

    /**
     * Checks if the geocode json response also returned a field for partial match.
     * 
     * @return boolean
     */
    public function isPartialResponse()
    {
        // avoid an undefined index notice error
        if (isset($this->geocodeResponse['results'][0]['partial_match']))

            // php interpets this json array key as a boolean, therefore the conditional must test for boolean type
            if ($this->geocodeResponse['results'][0]['partial_match'] === true)
                return true;

        return false;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment