GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ExportCommand   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 33.96 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 36
loc 106
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A handle() 0 16 2
A runBeforeHooks() 18 19 4
A runAfterHooks() 18 19 4
A runHooks() 0 18 4

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 Spatie\Export\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Spatie\Export\Exporter;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Process\Process;
10
11
class ExportCommand extends Command
12
{
13
    protected $name = 'export';
14
15
    protected $description = 'Export the entire app to a static site';
16
17
    public function __construct()
18
    {
19
        parent::__construct();
20
21
        collect()
22
            ->merge(config('export.before', []))
23
            ->merge(config('export.after', []))
24
            ->keys()
25
            ->unique()
26
            ->sort()
27
            ->each(function (string $name) {
28
                $this->addOption(
29
                    "skip-{$name}",
30
                    null,
31
                    InputOption::VALUE_NONE,
32
                    "Skip the {$name} hook"
33
                );
34
            });
35
36
        $this->addOption("skip-all", null, InputOption::VALUE_NONE, 'Skip all hooks');
37
        $this->addOption("skip-before", null, InputOption::VALUE_NONE, 'Skip all before hooks');
38
        $this->addOption("skip-after", null, InputOption::VALUE_NONE, 'Skip all after hooks');
39
    }
40
41
    public function handle(Exporter $exporter)
42
    {
43
        $this->runBeforeHooks();
44
45
        $this->info('Exporting site...');
46
47
        $exporter->export();
48
49
        if (config('export.disk')) {
50
            $this->info('Files were saved to disk `'.config('export.disk').'`');
51
        } else {
52
            $this->info('Files were saved to `dist`');
53
        }
54
55
        $this->runAfterHooks();
56
    }
57
58 View Code Duplication
    protected function runBeforeHooks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        if ($this->input->getOption('skip-all') || $this->input->getOption('skip-before')) {
61
            return;
62
        }
63
64
        $beforeHooks = collect(config('export.before', []))
65
            ->reject(function (string $hook, string $name) {
66
                return $this->input->getOption("skip-{$name}");
67
            });
68
69
        if (! count($beforeHooks)) {
70
            return;
71
        }
72
73
        $this->info('Running before hooks...');
74
75
        $this->runHooks($beforeHooks);
76
    }
77
78 View Code Duplication
    protected function runAfterHooks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        if ($this->input->getOption('skip-all') || $this->input->getOption('skip-after')) {
81
            return;
82
        }
83
84
        $afterHooks = collect(config('export.after', []))
85
            ->reject(function (string $hook, string $name) {
86
                return $this->input->getOption("skip-{$name}");
87
            });
88
89
        if (! count($afterHooks)) {
90
            return;
91
        }
92
93
        $this->info('Running after hooks...');
94
95
        $this->runHooks($afterHooks);
96
    }
97
98
    protected function runHooks(Collection $hooks)
99
    {
100
        foreach ($hooks as $name => $command) {
101
            $this->comment("[{$name}]", 'v');
102
103
            if (method_exists(Process::class, 'fromShellCommandline')) {
104
                $process = Process::fromShellCommandline($command);
105
            } else {
106
                $process = new Process($command);
107
            }
108
109
            $process->mustRun();
110
111
            foreach ($process as $data) {
112
                $this->output->write($data);
113
            }
114
        }
115
    }
116
}
117