BacktraceAccessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 39
rs 10
c 1
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A getNthObjectOfType() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\StyleImporter\Util;
6
7
class BacktraceAccessor
8
{
9
    private const KEY_CLASS = 'class';
10
    private const KEY_OBJECT = 'object';
11
    private const NO_CLASS = '**NO_CLASS**';
12
    private const NO_OBJECT = '**NO_OBJECT**';
13
14
    /**
15
     * @codeCoverageIgnore
16
     *
17
     * @param int    $position Position (n)
18
     * @param string $type     Object
19
     *
20
     * @return null|mixed
21
     */
22
    public function getNthObjectOfType(int $position, string $type)
23
    {
24
        $backtrace = debug_backtrace();
25
        $lastFoundObjectOfType = null;
26
        $currentPosition = 0;
27
28
        foreach ($backtrace as $item) {
29
            $itemClass = $item[self::KEY_CLASS] ?? self::NO_CLASS;
30
            $itemObject = $item[self::KEY_OBJECT] ?? self::NO_OBJECT;
31
32
            if ($itemClass !== $type
33
                || (!empty($lastFoundObjectOfType) && $lastFoundObjectOfType === $itemObject)) {
34
                continue;
35
            }
36
37
            $lastFoundObjectOfType = $itemObject;
38
            ++$currentPosition;
39
40
            if ($currentPosition === $position) {
41
                return $itemObject;
42
            }
43
        }
44
45
        return null;
46
    }
47
}
48