InstallIdeHelper::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 65
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 45
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 65
rs 9.2

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\CommandRunner;
8
use Scriptura\QuickStart\ProjectFilesystem;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class InstallIdeHelper
13
{
14
    public function __construct()
15
    {
16
    }
17
18
    public function __invoke(string $name, InputInterface $input, OutputInterface $output)
19
    {
20
        $output->writeln('<info># Installing Ide Helper...</info>');
21
22
        $directory = getcwd() . '/' . $name;
23
24
        $composer = $this->findComposer();
25
26
        $commands = [
27
            $composer . ' require --dev barryvdh/laravel-ide-helper doctrine/dbal',
28
            'php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config',
29
        ];
30
31
        $runner = new CommandRunner($output, $directory);
32
        $runner->run($commands);
33
34
        $filesystem = new ProjectFilesystem(getcwd() . '/' . $name);
35
36
        $output->writeln('<info>Update file: composer.json</info>');
37
        $filesystem->updateFile('composer.json', function (string $content) {
38
            $refreshScript = <<<STRING
39
        "refresh": [
40
            "@php artisan migrate:fresh --ansi",
41
            "@php artisan db:seed --ansi",
42
            "@php artisan ide-helper:models -W -R --ansi"
43
        ],
44
        "post-update-cmd": [
45
            "Illuminate\\\\\\\\Foundation\\\\\\\\ComposerScripts::postUpdate",
46
            "php artisan ide-helper:generate",
47
            "php artisan ide-helper:meta"
48
        ],
49
50
STRING;
51
            $patterns = [
52
                '/("scripts": {\n)/',
53
            ];
54
            $replace = [
55
                '${1}' . $refreshScript,
56
            ];
57
58
            return preg_replace($patterns, $replace, $content);
59
        });
60
61
        $output->writeln('<info>Update file: config/ide-helper.php</info>');
62
        $filesystem->updateFile('config/ide-helper.php', function (string $content) {
63
            $patterns = [
64
                '/\'include_fluent\' => false,/',
65
                '/\'include_factory_builders\' => false,/',
66
                '/\'write_model_magic_where\' => true,/',
67
                '/\'write_eloquent_model_mixins\' => false,/',
68
            ];
69
            $replace = [
70
                '\'include_fluent\' => true,',
71
                '\'include_factory_builders\' => true,',
72
                '\'write_model_magic_where\' => false,',
73
                '\'write_eloquent_model_mixins\' => true,',
74
            ];
75
76
            return preg_replace($patterns, $replace, $content);
77
        });
78
79
        $isSuccessful = $runner->run([$composer . ' update']);
80
81
        if ($isSuccessful) {
82
            $output->writeln('<comment>Ide Helper installed.</comment>');
83
        }
84
    }
85
86
    /**
87
     * Get the composer command for the environment.
88
     *
89
     * @return string
90
     */
91
    protected function findComposer(): string
92
    {
93
        $composerPath = getcwd() . '/composer.phar';
94
95
        if (file_exists($composerPath)) {
96
            return '"' . PHP_BINARY . '" ' . $composerPath;
97
        }
98
99
        return 'composer';
100
    }
101
}
102