4
\$\begingroup\$

I'm new to Zend Framework. Can you help me optimize this function, which gets a list of Manufactures with some conditions from a database?

private function Manufactures(
    $Condition = array(),
    $Order = array('name' => 'ASC'),
    $Strict = TRUE,
    $Limit = NULL)
{
    $Request = $this->_DbTable->select();
    $Clause  = $Strict ? '=' : 'LIKE';
    foreach ($Condition as $index => $value) {
        if (!is_array($value)) {
            $Request->where($this->_DbTable->info('name') . '.' . $index . ' '. $Clause .' ?', $value);
        } else {
            foreach ($value as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $valIndex) {
                        if (isset($valIndex['modifier']) && isset($valIndex['value'])) {
                            $Request->where($this->_DbTable->info('name') . '.' . $key . ' ' . $valIndex['modifier']. ' ?', $valIndex['value']);
                        }
                    }
                }
            }
        }
    }
    foreach ($Order as $index => $value) {
        $Request->order($index . ' ' . $value);
    }
    if($Limit) $Request->limit($Limit);
    $Data = $this->_DbTable->fetchAll($Request)
                           ->toArray();
    return isset($Data) ? $Data : NULL;
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

General Review

I apologize nobody appears to have reviewed this and more than 12 years have passed. Zend Framework may or may not still be used for this project; according to this reddit post it has been continued as Laminas. The information below likely wouldn't allow for much optimization (given there isn't much information about the database schema), though hopefully it is useful.

Suggestions

Follow common conventions for readability

While there is no de-facto rules about readability conventions and it is up to individuals/teams to decide their conventions, idiomatic PHP code often follows the PHP Standards Recommendations - e.g. PSR-12: Extended Coding Style. The code presented follows many of the recommendations though not all. On the whole the spacing around operators is inline with idiomatic PHP code, however proper-case variable and method names are un-common. Proper-case names are typically used for class names.

Combine calls to isset()

While it may not allow for much simplification, the two conditions to check if $valIndex has two specific elements can be simplified to a single call.

         if (isset($valIndex['modifier']) && isset($valIndex['value'])) {

The function accepts multiple parameters and "will return true only if all of the parameters are considered set" 1:

             if (isset($valIndex['modifier'], $valIndex['value'])) {

Variable naming could be improved

While this is a minor nit-pick it is still something I would suggest changing. The variable $Clause appears to contain an operator instead of an entire clause. A more appropriate name might be $operator.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.