PHP wrong way - Što mislite?

Zna se, samo mislim da tebi nije jasno, kako radi i kako funkcionira.

U posljednje vrijeme pisšem ovakav kod i to vrijedi za većinu metoda. Mislim da je poprilično robustan, lako održavan i vrlo čitljiv. Bukvalno, čitam svoje metode od kraja prema početku. :upside_down_face:

/**
 * Checks for something me like and returns appropriate boolean condition
 * 
 * @param string|null $somethingMeLike
 * @return bool
 * @throws Exception
 */
public function sampleMethod(?string $somethingMeLike): bool
try {
    /** some code here */
    if (!$somethingMeLike) {
        throw new Exception('Not valid something.');
    }
    /** some more code here */
    $return = true; // anything that's declared in return type
} catch (ConcreteException | Exception $e) {
    /** various action and exception handling */
    $return = false;
} finally {
    return $return
}

Evo konkretno što sam neki dan radio na Shopify aplikaciji

/**
 * Retrieves all price rules
 *
 * @return string
 * @throws GuzzleException
 */
public function getPriceRules(): string
{
    try {
        $request = $this->guzzle->request('GET', $this->config['ShopUrl']
                . '/admin/api/2019-04/price_rules.json');
        $return = $request->getBody()->getContents();
    } catch (ClientException | Exception $e) {
        $return = $e->getMessage();
    } finally {
        return $return;
    }
}

Gledam da se egzekucija sprovede do kraja a lako u final bloku bacim izuzetak ako mi baš treba u prethodnoj metodi u lancu.

1 Like