Skip to content
Snippets Groups Projects
Commit 5d2d1934 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

use static instead of get_called_class(), fixes #3976

Closes #3976

Merge request studip/studip!2826
parent 5a70856f
No related branches found
No related tags found
No related merge requests found
...@@ -246,7 +246,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -246,7 +246,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
*/ */
protected static function configure($config = []) protected static function configure($config = [])
{ {
$class = get_called_class(); $class = static::class;
if (empty($config['db_table'])) { if (empty($config['db_table'])) {
$config['db_table'] = strtolower($class); $config['db_table'] = strtolower($class);
...@@ -459,8 +459,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -459,8 +459,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
*/ */
public static function find($id) public static function find($id)
{ {
$class = get_called_class(); $ref = new ReflectionClass(static::class);
$ref = new ReflectionClass($class);
/** @var static $record */ /** @var static $record */
$record = $ref->newInstanceArgs(func_get_args()); $record = $ref->newInstanceArgs(func_get_args());
if (!$record->isNew()) { if (!$record->isNew()) {
...@@ -480,9 +479,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -480,9 +479,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
{ {
$ret = false; $ret = false;
$db_table = static::db_table(); $db_table = static::db_table();
$class = get_called_class(); $record = new static();
$record = new $class(); $record->setId(...func_get_args());
call_user_func_array([$record, 'setId'], func_get_args());
$where_query = $record->getWhereQuery(); $where_query = $record->getWhereQuery();
if ($where_query) { if ($where_query) {
$query = "SELECT 1 FROM `$db_table` WHERE " $query = "SELECT 1 FROM `$db_table` WHERE "
...@@ -521,8 +519,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -521,8 +519,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
*/ */
public static function create($data) public static function create($data)
{ {
$class = get_called_class(); $record = new static();
$record = new $class();
$record->setData($data, false); $record->setData($data, false);
if ($record->store()) { if ($record->store()) {
return $record; return $record;
...@@ -540,9 +537,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -540,9 +537,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
*/ */
public static function build($data, $is_new = true) public static function build($data, $is_new = true)
{ {
$class = get_called_class(); $record = new static();
/** @var static $record */
$record = new $class();
$record->setData($data, !$is_new); $record->setData($data, !$is_new);
$record->setNew($is_new); $record->setNew($is_new);
return $record; return $record;
...@@ -570,7 +565,6 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -570,7 +565,6 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
*/ */
public static function import($data) public static function import($data)
{ {
$class = get_called_class();
$record_data = []; $record_data = [];
$relation_data = []; $relation_data = [];
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
...@@ -581,8 +575,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -581,8 +575,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
} }
} }
$record = static::toObject($record_data); $record = static::toObject($record_data);
if (!$record instanceof $class) { if (!$record instanceof static) {
$record = new $class(); $record = new static();
$record->setData($record_data, true); $record->setData($record_data, true);
} else { } else {
$record->setData($record_data); $record->setData($record_data);
...@@ -856,8 +850,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -856,8 +850,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
*/ */
public static function toObject($id_or_object) public static function toObject($id_or_object)
{ {
$class = get_called_class(); if ($id_or_object instanceof static) {
if ($id_or_object instanceof $class) {
return $id_or_object; return $id_or_object;
} }
if (is_array($id_or_object)) { if (is_array($id_or_object)) {
...@@ -880,31 +873,31 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -880,31 +873,31 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
} else { } else {
$id = $id_or_object; $id = $id_or_object;
} }
return call_user_func([$class, 'find'], $id); return static::find($id);
} }
/** /**
* interceptor for static findByColumn / findEachByColumn / countByColumn / * interceptor for static findByColumn / findEachByColumn / countByColumn /
* deleteByColumn magic * deleteByColumn magic
*
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
* @throws BadMethodCallException * @throws BadMethodCallException
* @return int|static|static[] * @return int|static|static[]
*/ */
public static function __callStatic($name, $arguments) public static function __callStatic(string $name, array $arguments)
{ {
$db_table = static::db_table(); $db_table = static::db_table();
$alias_fields = static::alias_fields(); $alias_fields = static::alias_fields();
$db_fields = static::db_fields(); $db_fields = static::db_fields();
$name = strtolower($name); $name = strtolower($name);
$class = get_called_class();
$order = ''; $order = '';
$param_arr = []; $param_arr = [];
$where = ''; $where = '';
$where_param = is_array($arguments[0]) ? $arguments[0] : [$arguments[0]]; $where_param = is_array($arguments[0]) ? $arguments[0] : [$arguments[0]];
$prefix = strstr($name, 'by', true); $action = strstr($name, 'by', true);
$field = substr($name, strlen($prefix) + 2); $field = substr($name, strlen($action) + 2);
switch ($prefix) { switch ($action) {
case 'findone': case 'findone':
$order = $arguments[1] ?? ''; $order = $arguments[1] ?? '';
$param_arr[0] =& $where; $param_arr[0] =& $where;
...@@ -930,19 +923,19 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -930,19 +923,19 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
case 'delete': case 'delete':
$param_arr[0] =& $where; $param_arr[0] =& $where;
$param_arr[1] = [$where_param]; $param_arr[1] = [$where_param];
$method = "{$prefix}bysql"; $method = "{$action}bysql";
break; break;
default: default:
throw new BadMethodCallException("Method $class::$name not found"); throw new BadMethodCallException("Method " . static::class . "::$name not found");
} }
if (isset($alias_fields[$field])) { if (isset($alias_fields[$field])) {
$field = $alias_fields[$field]; $field = $alias_fields[$field];
} }
if (isset($db_fields[$field])) { if (isset($db_fields[$field])) {
$where = "`$db_table`.`$field` IN(?) " . $order; $where = "`$db_table`.`$field` IN(?) " . $order;
return call_user_func_array([$class, $method], $param_arr); return call_user_func_array([static::class, $method], $param_arr);
} }
throw new BadMethodCallException("Method $class::$name not found"); throw new BadMethodCallException("Method " . static::class . "::$name not found");
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment