CodeCleanup::__invoke()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 142
Code Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 96
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 142
rs 8.0872

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scriptura\QuickStart\Tasks;
6
7
use Scriptura\QuickStart\ProjectFilesystem;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class CodeCleanup
12
{
13
    public function __construct()
14
    {
15
    }
16
17
    public function __invoke(string $name, InputInterface $input, OutputInterface $output)
18
    {
19
        $output->writeln('<info># Cleaning up some code...</info>');
20
21
        $filesystem = new ProjectFilesystem(getcwd() . '/' . $name);
22
23
        $output->writeln('<info>Update file: app/User.php</info>');
24
        $filesystem->updateFile('app/User.php', function (string $content) {
25
            $uses = <<<STRING
26
use Illuminate\\\\Auth\\\\Authenticatable;
27
use Illuminate\\\\Auth\\\\MustVerifyEmail;
28
use Illuminate\\\\Database\\\\Eloquent\\\\Model;
29
use Illuminate\\\\Notifications\\\\Notifiable;
30
use Illuminate\\\\Auth\\\\Passwords\\\\CanResetPassword;
31
use Illuminate\\\\Foundation\\\\Auth\\\\Access\\\\Authorizable;
32
use Illuminate\\\\Database\\\\Eloquent\\\\Builder as EloquentBuilder;
33
use Illuminate\\\\Contracts\\\\Auth\\\\Authenticatable as AuthenticatableContract;
34
use Illuminate\\\\Contracts\\\\Auth\\\\Access\\\\Authorizable as AuthorizableContract;
35
use Illuminate\\\\Contracts\\\\Auth\\\\CanResetPassword as CanResetPasswordContract;
36
37
STRING;
38
39
            $definition = <<<STRING
40
class User extends Model implements
41
    AuthenticatableContract,
42
    AuthorizableContract,
43
    CanResetPasswordContract
44
{
45
    use Authenticatable;
46
    use Authorizable;
47
    use CanResetPassword;
48
    use MustVerifyEmail;
49
50
STRING;
51
            $patterns = [
52
                '/use Illuminate\\\\Contracts\\\\Auth\\\\MustVerifyEmail;\n/',
53
                '/use Illuminate\\\\Notifications\\\\Notifiable;\n/',
54
                '/use Illuminate\\\\Foundation\\\\Auth\\\\User as Authenticatable;/',
55
                '/class User extends Authenticatable\n{\n/',
56
            ];
57
            $replace = [
58
                '',
59
                '',
60
                $uses,
61
                $definition,
62
            ];
63
64
            return preg_replace($patterns, $replace, $content);
65
        });
66
67
        $redirectFunc = <<<STRING
68
public function redirectTo() : string
69
    {
70
        return RouteServiceProvider::HOME;
71
    }
72
STRING;
73
74
        $output->writeln('<info>Update file: app/Http/Controllers/Auth/ConfirmPasswordController.php</info>');
75
        $filesystem->updateFile('app/Http/Controllers/Auth/ConfirmPasswordController.php', function (string $content) use ($redirectFunc) {
76
            $patterns = [
77
                '/(@var)( string\s*\*\/\s*)(protected \$redirectTo = RouteServiceProvider::HOME;)/',
78
            ];
79
            $replace = [
80
                '@return${2}' . $redirectFunc,
81
            ];
82
83
            return preg_replace($patterns, $replace, $content);
84
        });
85
86
        $output->writeln('<info>Update file: app/Http/Controllers/Auth/LoginController.php</info>');
87
        $filesystem->updateFile('app/Http/Controllers/Auth/LoginController.php', function (string $content) use ($redirectFunc) {
88
            $patterns = [
89
                '/(@var)( string\s*\*\/\s*)(protected \$redirectTo = RouteServiceProvider::HOME;)/',
90
            ];
91
            $replace = [
92
                '@return${2}' . $redirectFunc,
93
            ];
94
95
            return preg_replace($patterns, $replace, $content);
96
        });
97
98
        $output->writeln('<info>Update file: app/Http/Controllers/Auth/RegisterController.php</info>');
99
        $filesystem->updateFile('app/Http/Controllers/Auth/RegisterController.php', function (string $content) use ($redirectFunc) {
100
            $patterns = [
101
                '/(@var)( string\s*\*\/\s*)(protected \$redirectTo = RouteServiceProvider::HOME;)/',
102
            ];
103
            $replace = [
104
                '@return${2}' . $redirectFunc,
105
            ];
106
107
            return preg_replace($patterns, $replace, $content);
108
        });
109
110
        $output->writeln('<info>Update file: app/Http/Controllers/Auth/ResetPasswordController.php</info>');
111
        $filesystem->updateFile('app/Http/Controllers/Auth/ResetPasswordController.php', function (string $content) use ($redirectFunc) {
112
            $patterns = [
113
                '/(@var)( string\s*\*\/\s*)(protected \$redirectTo = RouteServiceProvider::HOME;)/',
114
            ];
115
            $replace = [
116
                '@return${2}' . $redirectFunc,
117
            ];
118
119
            return preg_replace($patterns, $replace, $content);
120
        });
121
122
        $output->writeln('<info>Update file: app/Http/Controllers/Auth/VerificationController.php</info>');
123
        $filesystem->updateFile('app/Http/Controllers/Auth/VerificationController.php', function (string $content) use ($redirectFunc) {
124
            $patterns = [
125
                '/(@var)( string\s*\*\/\s*)(protected \$redirectTo = RouteServiceProvider::HOME;)/',
126
            ];
127
            $replace = [
128
                '@return${2}' . $redirectFunc,
129
            ];
130
131
            return preg_replace($patterns, $replace, $content);
132
        });
133
134
        $output->writeln('<info>Update all *.php files</info>');
135
        $filesystem->updateAllFilesOfType('php', function (string $content) {
136
            $patterns = [
137
                '/\<\?php/',
138
            ];
139
            $replace = [
140
                "<?php\n\ndeclare(strict_types=1);",
141
            ];
142
143
            return preg_replace($patterns, $replace, $content);
144
        });
145
146
        $output->writeln('<info>Update all *.stub files</info>');
147
        $filesystem->updateAllFilesOfType('stub', function (string $content) {
148
            $patterns = [
149
                '/\<\?php/',
150
            ];
151
            $replace = [
152
                "<?php\n\ndeclare(strict_types=1);",
153
            ];
154
155
            return preg_replace($patterns, $replace, $content);
156
        });
157
158
        $output->writeln('<comment>Code cleanup done.</comment>');
159
    }
160
}
161