ENH Speed up DataObject::get_by_id by checking if there is an ID at all

This commit is contained in:
Nicolaas / Sunn Side Up 2022-01-17 09:45:56 +13:00 committed by GitHub
parent d9aad6cd94
commit d1cac485b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3481,14 +3481,17 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @param int|bool $idOrCache The id of the element, or cache if called on target class * @param int|bool $idOrCache The id of the element, or cache if called on target class
* @param boolean $cache See {@link get_one()} * @param boolean $cache See {@link get_one()}
* *
* @return static The element * @return static|null The element
*/ */
public static function get_by_id($classOrID, $idOrCache = null, $cache = true) public static function get_by_id($classOrID, $idOrCache = null, $cache = true)
{ {
// Shift arguments if passing id in first or second argument // Shift arguments if passing id in first or second argument
list ($class, $id, $cached) = is_numeric($classOrID) list ($class, $id, $cached) = is_numeric($classOrID)
? [get_called_class(), $classOrID, isset($idOrCache) ? $idOrCache : $cache] ? [get_called_class(), (int) $classOrID, isset($idOrCache) ? $idOrCache : $cache]
: [$classOrID, $idOrCache, $cache]; : [$classOrID, (int) $idOrCache, $cache];
if ($id < 1) {
return null;
}
// Validate class // Validate class
if ($class === self::class) { if ($class === self::class) {