aboutsummaryrefslogtreecommitdiff
path: root/lib/classes
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2026-01-26 08:58:07 +0100
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2026-01-26 08:58:07 +0100
commitbbbb7ca8e95258c239b1bbfceea252f20730aceb (patch)
tree5c8b44ac7a0b7800999af3250c3a1eac767f630e /lib/classes
parente173374994e6811c70bf21e513355920d6f85540 (diff)
make RangeConfig always return typed classes, fixes #6191
Closes #6191 Merge request studip/studip!4689
Diffstat (limited to 'lib/classes')
-rw-r--r--lib/classes/RangeConfig.php39
1 files changed, 33 insertions, 6 deletions
diff --git a/lib/classes/RangeConfig.php b/lib/classes/RangeConfig.php
index 2e90c86..04b747f 100644
--- a/lib/classes/RangeConfig.php
+++ b/lib/classes/RangeConfig.php
@@ -15,8 +15,15 @@
* @category Stud.IP
*/
-class RangeConfig extends Config
+abstract class RangeConfig extends Config
{
+ private static array $range_mapping = [
+ 'sem' => CourseConfig::class,
+ 'user' => UserConfig::class,
+ 'fak' => InstituteConfig::class,
+ 'inst' => InstituteConfig::class,
+ ];
+
/**
* range type
*/
@@ -24,9 +31,9 @@ class RangeConfig extends Config
/**
* cache of created RangeConfig instances
- * @var array
+ * @var array<string, static>
*/
- protected static $instances = [];
+ private static array $instances = [];
/**
* range_id
@@ -42,7 +49,7 @@ class RangeConfig extends Config
* need this just for compatibility with
* Config::get())
*/
- public static function get(string|Range|null $range_id = null): static
+ public static function get(string|Range|null $range_id = null): RangeConfig
{
if ($range_id === null) {
throw new InvalidArgumentException('No range_id given');
@@ -52,8 +59,28 @@ class RangeConfig extends Config
$range_id = $range_id->getRangeId();
}
- return static::$instances[$range_id]
- ??= new static($range_id);
+ $type = get_object_type($range_id, array_keys(self::$range_mapping));
+ if (self::class === static::class) {
+ // Detect correct class for range
+ if (!$type) {
+ throw new InvalidArgumentException('Invalid range_id given');
+ }
+
+ $class_name = self::$range_mapping[$type];
+ } else {
+ // Prevent type mismatches (allow non existing ids)
+ if (
+ $type
+ && !in_array($type, array_keys(self::$range_mapping, static::class))
+ ) {
+ throw new InvalidArgumentException('Invalid range_id given');
+ }
+
+ $class_name = static::class;
+ }
+
+ return self::$instances[$range_id]
+ ??= new $class_name($range_id);
}
/**