CurlyBraces   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 4 1
B removeGenericDocBlocks() 0 23 7
B writePublic() 0 21 6
A openCurly() 0 17 6
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Checks\PSR12;
4
5
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor;
6
7
class CurlyBraces
8
{
9
    public static $command;
10
11
    public static function check($tokens, $absFilePath, $classFilePath, $psr4Path, $psr4Namespace)
0 ignored issues
show
Unused Code introduced by
The parameter $absFilePath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $psr4Path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $psr4Namespace is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
12
    {
13
        self::removeGenericDocBlocks($tokens, $classFilePath->getRealpath());
14
    }
15
16
    private static function removeGenericDocBlocks($tokens, $classFilePath)
17
    {
18
        $level = 0;
19
        $isInSideClass = false;
20
        $ct = \count($tokens);
21
        $i = 0;
22
        while ($i < $ct - 1) {
23
            $i++;
24
            $token = $tokens[$i];
25
            \in_array($token[0], [T_CURLY_OPEN, '{']) && $level++;
26
            ($token[0] == '}') && $level--;
27
            if ($level == 0) {
28
                if (\in_array($token[0], [T_CLASS, T_TRAIT, T_INTERFACE])) {
29
                    if ($tokens[$i - 1] != T_DOUBLE_COLON) {
30
                        $isInSideClass = true;
31
                    }
32
                }
33
            }
34
            self::openCurly($token, $level, $tokens, $i, $classFilePath);
35
36
            [$tokens, $i] = self::writePublic($level, $token, $isInSideClass, $i, $tokens, $classFilePath);
37
        }
38
    }
39
40
    private static function openCurly($token, $level, $tokens, $i, $classFilePath)
41
    {
42
        if ($token == '{' && ! \in_array($tokens[$i - 1][0], [T_DOUBLE_COLON, T_OBJECT_OPERATOR])) {
43
            $sp = str_repeat('    ', $level);
44
            if ($tokens[$i + 1][0] == T_WHITESPACE) {
45
                if ($tokens[$i + 1][1] != PHP_EOL.$sp && $tokens[$i + 1][1] != "\n".$sp) {
46
                    $tokens[$i + 1][1] = PHP_EOL.$sp;
47
                    Refactor::saveTokens($classFilePath, $tokens);
48
                } else {
49
                    ///
50
                }
51
            } else {
52
                array_splice($tokens, $i + 1, 0, [[T_WHITESPACE, PHP_EOL.$sp]]);
53
                Refactor::saveTokens($classFilePath, $tokens);
54
            }
55
        }
56
    }
57
58
    private static function writePublic($level, $token, $isInClass, $i, $tokens, $absolutePath)
59
    {
60
        if (($level != 1) || ($token[0] != T_FUNCTION) || ! $isInClass) {
61
            return [$tokens, $i];
62
        }
63
64
        $t = $i;
65
        if (\in_array($tokens[$t - 2][0], [T_STATIC])) {
66
            $t = $t - 2;
67
        }
68
69
        if (! \in_array($tokens[$t - 2][0], [T_PUBLIC, T_PROTECTED, T_PRIVATE])) {
70
            array_splice($tokens, $t, 0, [[T_WHITESPACE, ' ']]);
71
            array_splice($tokens, $t, 0, [[T_PUBLIC, 'public']]);
72
            $i++;
73
            $i++;
74
            Refactor::saveTokens($absolutePath, $tokens);
75
        }
76
77
        return [$tokens, $i];
78
    }
79
}
80