dimanche 7 juin 2020

Joomla JDatabase: selectRowNumber strange use of class attribute or method

in Joomla 3.9.16 there is a strage use of a class attribute that is also used as a method!

In libraries/joomla/database/query.php you can find:

protected $selectRowNumber = null;

but also

public function selectRowNumber($orderBy, $orderColumnAlias)
{
  $this->validateRowNumber($orderBy, $orderColumnAlias);
  $this->select("ROW_NUMBER() OVER (ORDER BY $orderBy) AS $orderColumnAlias");

  return $this;
}

You can undestand why reading this method of the same class:

protected function validateRowNumber($orderBy, $orderColumnAlias)
{
  if ($this->selectRowNumber)
  {
    throw new RuntimeException("Method 'selectRowNumber' can be called only once per instance.");
  }

  $this->type = 'select';

  $this->selectRowNumber = array(
    'orderBy' => $orderBy,
    'orderColumnAlias' => $orderColumnAlias,
  );
}

When you call $this->selectRowNumber() as a method for the first time, it calls $this->validateRowNumber() in which $this->selectRowNumber() becomes an array! If you call again $this->selectRowNumber() it throws an exception because you can call it only once and is no more a method.

I wonder if this is a good programming practice, I think definitely NOT because is not easy to understand and mantain. Maybe you can achive the same result in another way much more clear and linear. What I ask is: am I right or this is a common practice?

Thanks

Aucun commentaire:

Enregistrer un commentaire