Passed
Push — master ( 3f6a63...eae294 )
by Andreas
128:40 queued 107:25
created

midcom_helper_reflector   F

Complexity

Total Complexity 100

Size/Duplication

Total Lines 605
Duplicated Lines 0 %

Test Coverage

Coverage 94.1%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 282
c 6
b 0
f 0
dl 0
loc 605
ccs 255
cts 271
cp 0.941
rs 2
wmc 100

20 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 3
A __construct() 0 18 3
A class_rewrite() 0 15 5
A _get_icon_map() 0 16 4
B get_object_label() 0 30 8
A property_exists() 0 3 1
A get_class_label() 0 32 5
B get_object_icon() 0 46 11
B get_link_properties() 0 67 11
B get_search_properties() 0 55 11
B get_create_icon() 0 35 8
A get_object_fieldnames() 0 17 4
A is_same_class() 0 5 1
A get_component_l10n() 0 12 3
A get_label_property() 0 14 4
A resolve_baseclass() 0 31 6
A get_object_title() 0 11 3
A get_title_property() 0 3 1
B get_property() 0 27 7
A get_name_property() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like midcom_helper_reflector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use midcom_helper_reflector, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package midcom.helper.reflector
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midgard\portable\storage\connection;
10
use midgard\portable\api\mgdobject;
11
12
/**
13
 * The Grand Unified Reflector
14
 *
15
 * @package midcom.helper.reflector
16
 */
17
class midcom_helper_reflector extends midcom_baseclasses_components_purecode
18
{
19
    public $mgdschema_class = '';
20
21
    /**
22
     * @var midgard_reflection_property
23
     */
24
    protected $_mgd_reflector;
25
26
    protected $_dummy_object;
27
28
    private static $_cache = [
29
        'l10n' => [],
30
        'instance' => [],
31
        'title' => [],
32
        'name' => [],
33
        'fieldnames' => [],
34
        'object_icon_map' => null,
35
        'create_icon_map' => null
36
    ];
37
38
    /**
39
     * Constructor, takes classname or object, resolved MgdSchema root class automagically
40
     *
41
     * @param string|mgdobject $src classname or object
42
     */
43 191
    public function __construct($src)
44
    {
45 191
        parent::__construct();
46
47
        // Resolve root class name
48 191
        $this->mgdschema_class = self::resolve_baseclass($src);
49
        // Could not resolve root class name
50 191
        if (empty($this->mgdschema_class)) {
51
            // Handle object vs string
52
            $original_class = (is_object($src)) ? get_class($src) : $src;
53
            throw new midcom_error("Could not determine MgdSchema baseclass for '{$original_class}'");
54
        }
55
56
        // Instantiate midgard reflector
57 191
        $this->_mgd_reflector = new midgard_reflection_property($this->mgdschema_class);
58
59
        // Instantiate dummy object
60 191
        $this->_dummy_object = new $this->mgdschema_class;
61 191
    }
62
63
    /**
64
     * Get cached reflector instance
65
     *
66
     * @param mixed $src Object or classname
67
     * @return static
68
     */
69 395
    public static function &get($src)
70
    {
71 395
        $identifier = get_called_class() . (is_object($src) ? get_class($src) : $src);
72
73 395
        if (!isset(self::$_cache['instance'][$identifier])) {
74 36
            self::$_cache['instance'][$identifier] = new static($src);
75
        }
76 395
        return self::$_cache['instance'][$identifier];
77
    }
78
79
    /**
80
     * Get object's (mgdschema) fieldnames.
81
     *
82
     * @param object $object Object The object to query
83
     */
84 111
    public static function get_object_fieldnames(object $object) : array
85
    {
86 111
        $classname = get_class($object);
87 111
        $metadata = false;
88
89 111
        if (midcom::get()->dbclassloader->is_midcom_db_object($object)) {
90 101
            $classname = $object->__mgdschema_class_name__;
91 88
        } elseif ($object instanceof midcom_helper_metadata) {
92 78
            $metadata = true;
93 78
            $classname = $object->object->__mgdschema_class_name__;
94
        }
95
96 111
        if (is_subclass_of($classname, mgdobject::class)) {
97 111
            $cm = connection::get_em()->getClassMetadata($classname);
98 111
            return $cm->get_schema_properties($metadata);
0 ignored issues
show
introduced by
The method get_schema_properties() does not exist on Doctrine\ORM\Mapping\ClassMetadata. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            return $cm->/** @scrutinizer ignore-call */ get_schema_properties($metadata);
Loading history...
99
        }
100 5
        return array_keys(get_object_vars($object));
101
    }
102
103 150
    public function property_exists(string $property, bool $metadata = false) : bool
104
    {
105 150
        return $this->_mgd_reflector->property_exists($property, $metadata);
106
    }
107
108
    /**
109
     * Gets a midcom_helper_l10n instance for component governing the type
110
     */
111 67
    public function get_component_l10n() : midcom_services_i18n_l10n
112
    {
113 67
        if (!isset(self::$_cache['l10n'][$this->mgdschema_class])) {
114 11
            if ($component = midcom::get()->dbclassloader->get_component_for_class($this->mgdschema_class)) {
0 ignored issues
show
Bug introduced by
It seems like $this->mgdschema_class can also be of type null; however, parameter $classname of midcom_services_dbclassl...t_component_for_class() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            if ($component = midcom::get()->dbclassloader->get_component_for_class(/** @scrutinizer ignore-type */ $this->mgdschema_class)) {
Loading history...
115 10
                self::$_cache['l10n'][$this->mgdschema_class] = $this->_i18n->get_l10n($component);
116
            } else {
117 1
                debug_add("Could not resolve component for class {$this->mgdschema_class}, using our own l10n", MIDCOM_LOG_INFO);
118 1
                self::$_cache['l10n'][$this->mgdschema_class] = $this->_l10n;
119
            }
120
        }
121
122 67
        return self::$_cache['l10n'][$this->mgdschema_class];
123
    }
124
125
    /**
126
     * Get the localized label of the class
127
     *
128
     * @todo remove any hardcoded class names/prefixes
129
     */
130 67
    public function get_class_label() : string
131
    {
132 67
        $component_l10n = $this->get_component_l10n();
133 67
        $use_classname = $this->mgdschema_class;
134
135 67
        $midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($use_classname);
136
137 67
        if (!empty($midcom_dba_classname)) {
138 67
            $use_classname = $midcom_dba_classname;
139
        }
140
141 67
        $use_classname = preg_replace('/_(db|dba)$/', '', $use_classname);
142
143 67
        $label = $component_l10n->get($use_classname);
144 67
        if ($label == $use_classname) {
145
            // Class string not localized, try Bergie's way to pretty-print
146 67
            $classname_parts = explode('_', $use_classname);
147 67
            if (count($classname_parts) >= 3) {
148
                // Drop first two parts of class name
149 67
                array_shift($classname_parts);
150 67
                array_shift($classname_parts);
151
            }
152
            // FIXME: Remove hardcoded class prefixes
153 67
            $use_label = preg_replace('/(openpsa|notifications)_/', '', implode('_', $classname_parts));
154
155 67
            $use_label = str_replace('_', ' ', $use_label);
156 67
            $label = $component_l10n->get($use_label);
157 67
            if ($use_label == $label) {
158 62
                $label = ucwords($use_label);
159
            }
160
        }
161 67
        return $label;
162
    }
163
164
    /**
165
     * Get property name to use as label
166
     *
167
     * @return string name of property to use as label (or false on failure)
168
     */
169 25
    public function get_label_property()
170
    {
171 25
        $midcom_class = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($this->mgdschema_class);
172 25
        $obj = ($midcom_class) ? new $midcom_class : new $this->mgdschema_class;
173
174 25
        if (method_exists($obj, 'get_label_property')) {
175 2
            return $obj->get_label_property();
176
        }
177 23
        if (midcom::get()->dbfactory->is_a($obj, midcom_db_person::class)) {
178 1
            return ['rname', 'id'];
179
        }
180 22
        return $this->get_property('title', $obj) ??
181 6
            $this->get_property('name', $obj) ??
182 22
            'guid';
183
    }
184
185
    /**
186
     * Get the object label property value
187
     */
188 55
    public function get_object_label(object $object) : ?string
189
    {
190 55
        if ($object instanceof mgdobject) {
191
            try {
192 2
                $obj = midcom::get()->dbfactory->convert_midgard_to_midcom($object);
193
            } catch (midcom_error $e) {
194 2
                return null;
195
            }
196
        } else {
197 53
            $obj = $object;
198
        }
199 55
        if (method_exists($obj, 'get_label')) {
200 43
            return $obj->get_label();
201
        }
202
203 14
        $properties = array_flip($obj->get_properties());
204 14
        if (empty($properties)) {
205
            debug_add("Could not list object properties, aborting", MIDCOM_LOG_ERROR);
206
            return null;
207
        }
208 14
        if (isset($properties['title'])) {
209 12
            return $obj->title;
210
        }
211 3
        if (isset($properties['name'])) {
212 2
            return $obj->name;
213
        }
214 1
        if ($obj->id > 0) {
215
            return $this->get_class_label() . ' #' . $obj->id;
216
        }
217 1
        return '';
218
    }
219
220
    /**
221
     * Get the name of the create icon image
222
     */
223 28
    public static function get_create_icon(string $type) : string
224
    {
225 28
        if (null === self::$_cache['create_icon_map']) {
226 1
            self::$_cache['create_icon_map'] = self::_get_icon_map('create_type_magic', 'file-o');
227
        }
228
229 28
        $icon_callback = [$type, 'get_create_icon'];
230
        switch (true) {
231
            // class has static method to tell us the answer ? great !
232 28
            case (is_callable($icon_callback)):
233
                $icon = call_user_func($icon_callback);
234
                break;
235
            // configuration icon
236 28
            case (isset(self::$_cache['create_icon_map'][$type])):
237 16
                $icon = self::$_cache['create_icon_map'][$type];
238 16
                break;
239
240
            // heuristics magic (instead of adding something here, take a look at config key "create_type_magic")
241 25
            case (str_contains($type, 'member')):
242 25
            case (str_contains($type, 'organization')):
243 3
                $icon = 'users';
244 3
                break;
245 24
            case (str_contains($type, 'person')):
246 1
                $icon = 'user-o';
247 1
                break;
248 23
            case (str_contains($type, 'event')):
249 1
                $icon = 'calendar-o';
250 1
                break;
251
252
            // Fallback default value
253
            default:
254 22
                $icon = self::$_cache['create_icon_map']['__default__'];
255 22
                break;
256
        }
257 28
        return $icon;
258
    }
259
260
    /**
261
     * Get the object icon
262
     */
263 12
    public static function get_object_icon(object $obj) : string
264
    {
265 12
        if (null === self::$_cache['object_icon_map']) {
266 1
            self::$_cache['object_icon_map'] = self::_get_icon_map('object_icon_magic', 'file');
267
        }
268
269 12
        $object_class = get_class($obj);
270 12
        $object_baseclass = self::resolve_baseclass($obj);
271
272
        switch (true) {
273
            // object knows it's icon, how handy!
274 12
            case (method_exists($obj, 'get_icon')):
275 4
                $icon = $obj->get_icon();
276 4
                break;
277
278
            // configuration icon
279 9
            case (isset(self::$_cache['object_icon_map'][$object_class])):
280 2
                $icon = self::$_cache['object_icon_map'][$object_class];
281 2
                break;
282 8
            case (isset(self::$_cache['object_icon_map'][$object_baseclass])):
283 2
                $icon = self::$_cache['object_icon_map'][$object_baseclass];
284 2
                break;
285
286
            // heuristics magic (instead of adding something here, take a look at config key "object_icon_magic")
287 7
            case (str_contains($object_class, 'person')):
288 3
                $icon = 'user';
289 3
                break;
290 6
            case (str_contains($object_class, 'event')):
291 1
                $icon = 'calendar-o';
292 1
                break;
293 5
            case (str_contains($object_class, 'member')):
294 5
            case (str_contains($object_class, 'organization')):
295 5
            case (str_contains($object_class, 'group')):
296 3
                $icon = 'users';
297 3
                break;
298 4
            case (str_contains($object_class, 'element')):
299 1
                $icon = 'file-code-o';
300 1
                break;
301
302
            // Fallback default value
303
            default:
304 3
                $icon = self::$_cache['object_icon_map']['__default__'];
305 3
                break;
306
        }
307
308 12
        return '<i class="fa fa-' . $icon . '"></i>';
309
    }
310
311 2
    private static function _get_icon_map(string $config_key, string $fallback) : array
312
    {
313 2
        $config = midcom_baseclasses_components_configuration::get('midcom.helper.reflector', 'config');
314 2
        $icons2classes = $config->get($config_key);
315 2
        $icon_map = [];
316
        //sanity
317 2
        if (!is_array($icons2classes)) {
318
            throw new midcom_error('Config key "' . $config_key . '" is not an array');
319
        }
320 2
        foreach ($icons2classes as $icon => $classes) {
321 2
            $icon_map = array_merge($icon_map, array_fill_keys($classes, $icon));
322
        }
323 2
        if (!isset($icon_map['__default__'])) {
324
            $icon_map['__default__'] = $fallback;
325
        }
326 2
        return $icon_map;
327
    }
328
329
    /**
330
     * Get class properties to use as search fields in choosers or other direct DB searches
331
     */
332 15
    public function get_search_properties() : array
333
    {
334
        // Return cached results if we have them
335 15
        static $cache = [];
336 15
        if (isset($cache[$this->mgdschema_class])) {
337 9
            return $cache[$this->mgdschema_class];
338
        }
339 7
        debug_add("Starting analysis for class {$this->mgdschema_class}");
340
341 7
        $properties = self::get_object_fieldnames($this->_dummy_object);
342
343
        $default_properties = [
344 7
            'title' => true,
345
            'tag' => true,
346
            'firstname' => true,
347
            'lastname' => true,
348
            'official' => true,
349
            'username' => true,
350
        ];
351
352 7
        $search_properties = array_intersect_key($default_properties, array_flip($properties));
353
354 7
        foreach ($properties as $property) {
355 7
            if (str_contains($property, 'name')) {
356 6
                $search_properties[$property] = true;
357
            }
358
            // TODO: More per property heuristics
359
        }
360
        // TODO: parent and up heuristics
361
362 7
        $label_prop = $this->get_label_property();
363
364 7
        if (    is_string($label_prop)
365 7
             && $label_prop != 'guid'
366 7
             && $this->_mgd_reflector->property_exists($label_prop)) {
367 6
            $search_properties[$label_prop] = true;
368
        }
369
370
        // Exceptions - always search these fields
371 7
        $always_search_all = $this->_config->get('always_search_fields') ?: [];
372 7
        if (!empty($always_search_all[$this->mgdschema_class])) {
373 1
            $fields = array_intersect($always_search_all[$this->mgdschema_class], $properties);
374 1
            $search_properties += array_flip($fields);
375
        }
376
377
        // Exceptions - never search these fields
378 7
        $never_search_all = $this->_config->get('never_search_fields') ?: [];
379 7
        if (!empty($never_search_all[$this->mgdschema_class])) {
380
            $search_properties = array_diff_key($search_properties, array_flip($never_search_all[$this->mgdschema_class]));
381
        }
382
383 7
        $search_properties = array_keys($search_properties);
384 7
        debug_print_r("Search properties for {$this->mgdschema_class}: ", $search_properties);
385 7
        $cache[$this->mgdschema_class] = $search_properties;
386 7
        return $search_properties;
387
    }
388
389
    /**
390
     * Gets a list of link properties and the links target info
391
     *
392
     * Link info key specification
393
     *     'class' string link target class name
394
     *     'target' string link target property (of target class)
395
     *     'parent' boolean link is link to "parent" in object tree
396
     *     'up' boolean link is link to "up" in object tree
397
     *
398
     * @return array multidimensional array keyed by property, values are arrays with link info (or false in case of failure)
399
     */
400 4
    public function get_link_properties() : array
401
    {
402
        // Return cached results if we have them
403 4
        static $cache = [];
404 4
        if (isset($cache[$this->mgdschema_class])) {
405 1
            return $cache[$this->mgdschema_class];
406
        }
407 3
        debug_add("Starting analysis for class {$this->mgdschema_class}");
408
409
        // Shorthands
410 3
        $ref = $this->_mgd_reflector;
411 3
        $obj = $this->_dummy_object;
412
413
        // Get property list and start checking (or abort on error)
414 3
        $properties = self::get_object_fieldnames($obj);
415
416 3
        $links = [];
417 3
        $parent_property = midgard_object_class::get_property_parent($this->mgdschema_class);
0 ignored issues
show
Bug introduced by
It seems like $this->mgdschema_class can also be of type null; however, parameter $classname of midgard_object_class::get_property_parent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

417
        $parent_property = midgard_object_class::get_property_parent(/** @scrutinizer ignore-type */ $this->mgdschema_class);
Loading history...
418 3
        $up_property = midgard_object_class::get_property_up($this->mgdschema_class);
0 ignored issues
show
Bug introduced by
It seems like $this->mgdschema_class can also be of type null; however, parameter $classname of midgard_object_class::get_property_up() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

418
        $up_property = midgard_object_class::get_property_up(/** @scrutinizer ignore-type */ $this->mgdschema_class);
Loading history...
419 3
        foreach ($properties as $property) {
420 3
            if ($property == 'guid') {
421
                // GUID, even though of type MGD_TYPE_GUID, is never a link
422 3
                continue;
423
            }
424
425 3
            if (   !$ref->is_link($property)
426 3
                && $ref->get_midgard_type($property) != MGD_TYPE_GUID) {
427 3
                continue;
428
            }
429 3
            debug_add("Processing property '{$property}'");
430
            $linkinfo = [
431 3
                'class' => null,
432
                'target' => null,
433
                'parent' => false,
434
                'up' => false,
435 3
                'type' => $ref->get_midgard_type($property),
436
            ];
437 3
            if ($parent_property === $property) {
438 2
                debug_add("Is 'parent' property");
439 2
                $linkinfo['parent'] = true;
440
            }
441 3
            if ($up_property === $property) {
442 1
                debug_add("Is 'up' property");
443 1
                $linkinfo['up'] = true;
444
            }
445
446 3
            $type = $ref->get_link_name($property);
447 3
            debug_add("get_link_name returned '{$type}'");
448 3
            if (!empty($type)) {
449 2
                $linkinfo['class'] = $type;
450
            }
451
452 3
            $target = $ref->get_link_target($property);
453
454 3
            debug_add("get_link_target returned '{$target}'");
455 3
            if (!empty($target)) {
456 2
                $linkinfo['target'] = $target;
457 1
            } elseif ($linkinfo['type'] == MGD_TYPE_GUID) {
458 1
                $linkinfo['target'] = 'guid';
459
            }
460
461 3
            $links[$property] = $linkinfo;
462
        }
463
464 3
        debug_print_r("Links for {$this->mgdschema_class}: ", $links);
465 3
        $cache[$this->mgdschema_class] = $links;
466 3
        return $links;
467
    }
468
469
    /**
470
     * Map extended classes
471
     *
472
     * For example org.openpsa.* components often expand core objects,
473
     * in config we specify which classes we wish to substitute with which
474
     *
475
     * @param string $schema_type classname to check rewriting for
476
     * @return string new classname (or original in case no rewriting is to be done)
477
     */
478 38
    public static function class_rewrite(string $schema_type) : string
479
    {
480 38
        static $extends = false;
481 38
        if ($extends === false) {
482
            $extends = midcom_baseclasses_components_configuration::get('midcom.helper.reflector', 'config')->get('class_extends');
483
            // Safety against misconfiguration
484
            if (!is_array($extends)) {
485
                throw new midcom_error("config->get('class_extends') did not return array, invalid configuration ??");
486
            }
487
        }
488 38
        if (   isset($extends[$schema_type])
489 38
            && class_exists($extends[$schema_type])) {
490 2
            return $extends[$schema_type];
491
        }
492 37
        return $schema_type;
493
    }
494
495
    /**
496
     * See if two MgdSchema classes are the same
497
     *
498
     * NOTE: also takes into account the various extended class scenarios
499
     *
500
     * @param string $class_one first class to compare
501
     * @param string $class_two second class to compare
502
     */
503 11
    public static function is_same_class($class_one, $class_two) : bool
504
    {
505 11
        $one = self::resolve_baseclass($class_one);
506 11
        $two = self::resolve_baseclass($class_two);
507 11
        return $one == $two;
508
    }
509
510
    /**
511
     * Get the MgdSchema classname for given class
512
     *
513
     * @param mixed $classname either string (class name) or object
514
     * @return string the base class name
515
     */
516 227
    public static function resolve_baseclass($classname) : ?string
517
    {
518 227
        static $cached = [];
519
520 227
        if (is_object($classname)) {
521 182
            $class_instance = $classname;
522 182
            $classname = get_class($classname);
523
        }
524
525 227
        if (empty($classname)) {
526
            return null;
527
        }
528
529 227
        if (isset($cached[$classname])) {
530 207
            return $cached[$classname];
531
        }
532
533 36
        if (!isset($class_instance)) {
534 10
            $class_instance = new $classname();
535
        }
536
537
        // Check for decorators first
538 36
        if (!empty($class_instance->__mgdschema_class_name__)) {
539 29
            $parent_class = $class_instance->__mgdschema_class_name__;
540
        } else {
541 7
            $parent_class = $classname;
542
        }
543
544 36
        $cached[$classname] = self::class_rewrite($parent_class);
545
546 36
        return $cached[$classname];
547
    }
548
549 389
    private function get_property(string $type, object $object) : ?string
550
    {
551
        // Cache results per class within request
552 389
        $key = get_class($object);
553 389
        if (array_key_exists($key, self::$_cache[$type])) {
554 374
            return self::$_cache[$type][$key];
555
        }
556 52
        self::$_cache[$type][$key] = null;
557
558
        // Configured properties
559 52
        $exceptions = $this->_config->get($type . '_exceptions');
560 52
        foreach ($exceptions as $class => $property) {
561 52
            if (midcom::get()->dbfactory->is_a($object, $class)) {
562 8
                if (   $property !== false
563 8
                    && !$this->_mgd_reflector->property_exists($property)) {
564
                    debug_add("Matched class '{$key}' to '{$class}' via is_a but property '{$property}' does not exist", MIDCOM_LOG_ERROR);
565
                } else {
566 8
                    self::$_cache[$type][$key] = $property;
567
                }
568 8
                return self::$_cache[$type][$key];
569
            }
570
        }
571
        // The simple heuristic
572 47
        if ($this->_mgd_reflector->property_exists($type)) {
573 22
            self::$_cache[$type][$key] = $type;
574
        }
575 47
        return self::$_cache[$type][$key];
576
    }
577
578
    /**
579
     * Resolve the "name" property of given object
580
     *
581
     * @param object $object the object to get the name property for
582
     */
583 304
    public static function get_name_property(object $object) : ?string
584
    {
585 304
        return self::get($object)->get_property('name', $object);
586
    }
587
588
    /**
589
     * Resolve the "title" of given object
590
     *
591
     * NOTE: This is distinctly different from get_object_label, which will always return something
592
     * even if it's just the class name and GUID, also it will for some classes include extra info (like datetimes)
593
     * which we do not want here.
594
     *
595
     * @param object $object the object to get the name property for
596
     * @param string $title_property property to use as "name", if left to default (null), will be reflected
597
     */
598 184
    public static function get_object_title(object $object, ?string $title_property = null) : ?string
599
    {
600 184
        if ($title_property === null) {
601 184
            $title_property = self::get_title_property($object);
602
        }
603 184
        if (empty($title_property)) {
604
            // Could not resolve valid property
605 3
            return null;
606
        }
607
608 181
        return (string) $object->{$title_property};
609
    }
610
611
    /**
612
     * Resolve the "title" property of given object
613
     *
614
     * NOTE: This is distinctly different from get_label_property, which will always return something
615
     * even if it's just the guid
616
     *
617
     * @param object $object The object to get the title property for
618
     */
619 206
    public static function get_title_property(object $object) : ?string
620
    {
621 206
        return self::get($object)->get_property('title', $object);
622
    }
623
}
624