XF 2.0 Why ->getXRepo()?

CMTV

Well-known member
Hi!

I wonder why there are so many functions like the one below in XenForo's code?
PHP:
/**
* @return \XF\Repository\Trophy
*/
protected function getTrophyRepo()
{
    return $this->repository('XF:Trophy');
}

Why not simply writing something like that:
PHP:
/** @var \XF\Repository\Trophy $trophyRepo */
$trophyRepo = $this->repository('XF:Trophy');

It is shorted and cleaner...

I can understand that sometimes we need repo in multiply places within the same class but is $this->repository(...) operation is so consuming? Why not calling it twice?
 
Just essentially Don't repeat yourself principles.

It's just a generally good practice to get into and it saves having to have type hint comments all over the place.

You could argue it's slightly wasteful when the getXRepo() method is only used once, and it doesn't really save anything but of course any add-ons extending the class would be able to use it as well.

Ultimately it's as simple as:
PHP:
$trophyRepo = $this->getTrophyRepo();
Is better than typing:
PHP:
/** @var \XF\Repository\Trophy $trophyRepo */
$trophyRepo = $this->repository('XF:Trophy');
Every time.
 
I primarily use it for DRY + type hinting - I find it much better to type hint a function once in a class than having to type hint every time you declare a new variable.
 
Back
Top Bottom