Completed
Pull Request — master (#128)
by Ali
01:36
created

ActionsUnDocblock   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 48
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 48
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Checks;
4
5
use Illuminate\Support\Str;
6
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor;
7
8
class ActionsUnDocblock
9
{
10
    public const LARAVEL_DOCBLOCK_CONTENT =
11
    [
12
        'Remove the specified resource from storage.',
13
        'Update the specified resource in storage.',
14
        'Store a newly created resource in storage.',
15
        'Display the specified resource.',
16
        'Display a listing of the resource.',
17
        'Show the form for creating a new resource.',
18
        'Show the form for editing the specified resource.',
19
    ];
20
21
    public static $command;
22
23
    
24
    public static function check($tokens, $absFilePath, $classFilePath, $psr4Path, $psr4Namespace)
25
    {
26
        $fullNamespace = RoutelessActions::getFullNamespace($classFilePath, $psr4Path, $psr4Namespace);
27
28
        if (RoutelessActions::isLaravelController($fullNamespace)) {
29
            self::removeGenericDocBlocks($tokens, $classFilePath->getRealpath());
30
        }
31
    }
32
33
    private static function isValidDockBlock($tokenType, $tokenContent)
34
    {
35
        return $tokenType == T_DOC_COMMENT && Str::contains($tokenContent, self::LARAVEL_DOCBLOCK_CONTENT);
36
    }
37
38
    private static function removeGenericDocBlocks($tokens, $absFilePath)
39
    {
40
        foreach ($tokens as $i => $token) {
41
            if (self::isValidDockBlock($token(0), $token(1))) {
42
                    unset($tokens[$i]);
43
                    if ($tokens[$i + 1][0] == T_WHITESPACE) {
44
                        unset($tokens[$i + 1]);
45
                    }
46
                    Refactor::saveTokens($absFilePath, $tokens);
47
                }
48
            }
49
50
            if (\in_array($token[0], [T_PUBLIC, T_PRIVATE, T_PROTECTED]) && ($tokens[$i - 1][0] == T_WHITESPACE)) {
51
                if (($tokens[$i - 2][0] == '}') && $tokens[$i - 1][1] != "\n\n    ") {
52
                    $tokens[$i - 1][1] = "\n\n    ";
53
                    Refactor::saveTokens($absFilePath, $tokens);
54
                }
55
56
                if (($tokens[$i - 2][0] == '{') && $tokens[$i - 1][1] != "\n    ") {
57
                    $tokens[$i - 1][1] = "\n    ";
58
                    Refactor::saveTokens($absFilePath, $tokens);
59
                }
60
            }
61
        }
62
    }
63
}
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
64