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 |
||
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 ") { |
||
64 |