AdapterUtility   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 25
c 1
b 0
f 0
dl 0
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findFirstNestedAppearanceOfObjectPath() 0 18 6
A findEndLineOfNestedStatement() 0 26 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer\Adapter;
5
6
use Helmich\TypoScriptParser\Tokenizer\LineGrouper;
7
use Helmich\TypoScriptParser\Tokenizer\TokenInterface;
8
9
final class AdapterUtility
10
{
11
    /**
12
     * @param int   $startLine
13
     * @param array $tokens
14
     *
15
     * @return int
16
     */
17
    public function findEndLineOfNestedStatement(int $startLine, array $tokens): int
18
    {
19
        $endLine = 0;
20
        $tokenLines = new LineGrouper($tokens);
21
        $lines = $tokenLines->getLines();
22
23
        $lines = array_slice($lines, $startLine - 1);
24
25
        $openedBraces = 0;
26
        /** @var TokenInterface[] $line */
27
        foreach ($lines as $line) {
28
            foreach ($line as $token) {
29
                if ($token->getType() === TokenInterface::TYPE_BRACE_OPEN) {
30
                    $openedBraces++;
31
                }
32
                if ($token->getType() === TokenInterface::TYPE_BRACE_CLOSE) {
33
                    $openedBraces--;
34
                }
35
            }
36
            if ($openedBraces === 0) {
37
                $endLine = $line[0]->getLine();
38
                break;
39
            }
40
        }
41
42
        return $endLine;
43
    }
44
45
    /**
46
     * @param int    $startLine
47
     * @param string $objectPath
48
     * @param array  $tokens
49
     *
50
     * @return int
51
     */
52
    public function findFirstNestedAppearanceOfObjectPath(int $startLine, string $objectPath, array $tokens): int
53
    {
54
        $tokenLines = new LineGrouper($tokens);
55
        $lines = $tokenLines->getLines();
56
57
        /** @var TokenInterface[] $line */
58
        foreach ($lines as $line) {
59
            $value = $line[0]->getValue();
60
            if (preg_match('/^' . $objectPath . '($|\..*)/', $value) && $line[0]->getLine() !== $startLine) {
61
                foreach ($line as $token) {
62
                    if ($token->getType() === TokenInterface::TYPE_BRACE_OPEN) {
63
                        return $line[0]->getLine();
64
                    }
65
                }
66
            }
67
        }
68
69
        return 0;
70
    }
71
}
72