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.
Completed
Pull Request — master (#199)
by joseph
27:34
created

OverridesUpdateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\FileOverrider;
6
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class OverridesUpdateCommand extends AbstractCommand
14
{
15
    public const OPT_OVERRIDE_ACTION       = 'action';
16
    public const OPT_OVERRIDE_ACTION_SHORT = 'a';
17
18
    public const ACTION_TO_PROJECT   = 'toProject';
19
    public const ACTION_FROM_PROJECT = 'fromProject';
20
21
    /**
22
     * @var FileOverrider
23
     */
24
    protected $fileOverrider;
25
26
    public function __construct(FileOverrider $fileOverrider, ?string $name = null)
27
    {
28
        parent::__construct($name);
29
        $this->fileOverrider = $fileOverrider;
30
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output): void
33
    {
34
        $symfonyStyle = new SymfonyStyle($input, $output);
35
        $this->checkOptions($input);
36
        $output->writeln(
37
            '<comment>Updating overrides ' . $input->getOption(self::OPT_OVERRIDE_ACTION) . '</comment>'
38
        );
39
        $this->checkOptions($input);
40
        $this->fileOverrider->setPathToProjectRoot($input->getOption(self::OPT_PROJECT_ROOT_PATH));
41
        switch ($input->getOption(self::OPT_OVERRIDE_ACTION)) {
42
            case self::ACTION_TO_PROJECT:
43
                $invalidOverrides = $this->fileOverrider->getInvalidOverrides();
44
                if ([] !== $invalidOverrides) {
45
                    $symfonyStyle->error('Some Overrides are Invalid');
46
                    $fixed = $this->renderInvalidOverrides($invalidOverrides, $output, $symfonyStyle);
47
                    if (false === $fixed) {
48
                        throw new \RuntimeException('Errors in applying overrides');
49
                    }
50
                }
51
                $this->renderTableOfUpdatedFiles($this->fileOverrider->applyOverrides(), $output);
52
                $output->writeln('<info>Overrides have been applied to project</info>');
53
54
                return;
55
            case self::ACTION_FROM_PROJECT:
56
                $this->renderTableOfUpdatedFiles($this->fileOverrider->updateOverrideFiles(), $output);
57
                $output->writeln('<info>Overrides have been updated from the project</info>');
58
59
                return;
60
            default:
61
                throw new \InvalidArgumentException(
62
                    ' Invalid action ' . $input->getOption(self::OPT_OVERRIDE_ACTION)
63
                );
64
        }
65
    }
66
67
    private function renderInvalidOverrides(
68
        array $invalidOverrides,
69
        OutputInterface $output,
70
        SymfonyStyle $symfonyStyle
71
    ): bool {
72
        $return = false;
73
        foreach ($invalidOverrides as $pathToFileInOverrides => $details) {
74
            $return = $this->processInvalidOverride($pathToFileInOverrides, $details, $output, $symfonyStyle);
75
        }
76
77
        return $return;
78
    }
79
80
    private function processInvalidOverride(
81
        string $relativePathToFileInOverrides,
82
        array $details,
83
        OutputInterface $output,
84
        SymfonyStyle $symfonyStyle
85
    ): bool {
86
        $symfonyStyle->title('Working on ' . basename($relativePathToFileInOverrides));
87
        $output->writeln('<comment>' . $relativePathToFileInOverrides . '</comment>');
88
        $symfonyStyle->section('Details');
89
        $table = new Table($output);
90
        $table->setHeaders(['Key', 'Value']);
91
        $table->addRows(
92
            [
93
                ['Project File', $details['projectPath']],
94
                ['Override File', $details['overridePath']],
95
                ['New MD5', $details['new md5']],
96
                ['Diff Size', substr_count($details['diff'], "\n")],
97
            ]
98
        );
99
        $table->render();
100
        $output->writeln('<info>Diff:</info>');
101
        $output->write($details['diff']);
102
        $output->writeln("\n\n");
103
        $output->writeln('<info>Fixing this</info>');
104
        $output->writeln(<<<TEXT
105
        
106
The suggested fix in this situation is:
107
108
 * Rename the current override
109
 * Make a new override from the newly generated file 
110
 * Reapply your custom code to the new override
111
 * Finally delete the old override.
112
 
113
TEXT
114
        );
115
        if (!$symfonyStyle->ask('Would you like to move the current override and make a new one and then diff this?',
116
                                true)) {
117
            $output->writeln('<commment>Skipping ' . $relativePathToFileInOverrides . '</commment>');
118
119
            return false;
120
        }
121
122
        $symfonyStyle->section('Recreating Override');
123
        list($old,) = $this->fileOverrider->recreateOverride($relativePathToFileInOverrides);
124
125
        $table = new Table($output);
126
        $table->addRow(['project file', $details['projectPath']]);
127
        $table->render();
128
129
        $table = new Table($output);
130
        $table->addRow(['old override', $old]);
131
        $table->render();
132
133
        $output->writeln(<<<TEXT
134
        
135
Now we have created a new override from your freshly generated file, 
136
you need to manually copy across all the changes from the old override into your project file.
137
138
* Open the project file
139
 
140
* In PHPStorm, find the old file, right click it and select "compare with editor"
141
 
142
TEXT
143
        );
144
        $symfonyStyle->caution('You must do this bit really carefully and exactly as instructed!!');
145
146
        while (
147
            false === $symfonyStyle->confirm(
148
                'Confirm you have now copied all required changes from the old override to the new one?',
149
                false
150
            )
151
        ) {
152
            $symfonyStyle->warning('You must now copy all required changes from the old override to the new one');
153
        }
154
        $symfonyStyle->section('Now updating override');
155
        $this->fileOverrider->updateOverrideFiles();
156
        $symfonyStyle->success("\n\nCompleted override update for $relativePathToFileInOverrides\n\n");
157
158
        return true;
159
    }
160
161
    private function renderTableOfUpdatedFiles(array $files, OutputInterface $output): void
162
    {
163
        list($updated, $same) = $files;
164
        if ([] !== $updated) {
165
            $output->writeln('Files Updated:');
166
            $table = new Table($output);
167
            foreach ($updated as $file) {
168
                $table->addRow([$file]);
169
            }
170
            $table->render();
171
        }
172
        if ([] !== $same) {
173
            $output->writeln('Files Same:');
174
            $table = new Table($output);
175
            foreach ($same as $file) {
176
                $table->addRow([$file]);
177
            }
178
            $table->render();
179
        }
180
    }
181
182
    /**
183
     * @throws DoctrineStaticMetaException
184
     */
185
    protected function configure(): void
186
    {
187
        try {
188
            $this
189
                ->setName(AbstractCommand::COMMAND_PREFIX . 'overrides:update')
190
                ->setDefinition(
191
                    [
192
                        new InputOption(
193
                            self::OPT_OVERRIDE_ACTION,
194
                            self::OPT_OVERRIDE_ACTION_SHORT,
195
                            InputOption::VALUE_REQUIRED,
196
                            'One of [ fromProject,  toProject ]'
197
                        ),
198
                        $this->getProjectRootPathOption(),
199
                    ]
200
                )->setDescription(
201
                    'Update project overrides'
202
                );
203
        } catch (\Exception $e) {
204
            throw new DoctrineStaticMetaException(
205
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
206
                $e->getCode(),
207
                $e
208
            );
209
        }
210
    }
211
}
212