LineFixer::detectOperator()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
rs 9.2222
cc 6
nc 6
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer\Fixer\OperatorWhitespace;
5
6
class LineFixer
7
{
8
    /**
9
     * @param string $line
10
     *
11
     * @return string
12
     */
13
    public function fixOperatorWhitespace(string $line): string
14
    {
15
        $operator = $this->detectOperator($line);
16
        if ($operator === null) {
17
            return $line;
18
        }
19
        $parts = explode($operator, $line);
20
        $parts[0] = rtrim($parts[0]) . ' ';
21
        $parts[1] = ' ' . ltrim($parts[1]);
22
        return rtrim(implode($operator, $parts));
23
    }
24
25
    /**
26
     * @param string $line
27
     *
28
     * @return string|null
29
     */
30
    private function detectOperator(string $line): ?string
31
    {
32
        if (strpos($line, '=<') !== false) {
33
            return '=<';
34
        }
35
        if (strpos($line, ':=') !== false) {
36
            return ':=';
37
        }
38
        if (strpos($line, '=') !== false) {
39
            return '=';
40
        }
41
        if (strpos($line, '<') !== false) {
42
            return '<';
43
        }
44
        if (strpos($line, '>') !== false) {
45
            return '>';
46
        }
47
        return null;
48
    }
49
}
50