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.

ComposerJson   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.89%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 84
ccs 34
cts 37
cp 0.9189
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A operate() 0 41 4
A removeItemFromIndex() 0 12 3
1
<?php declare(strict_types = 1);
2
3
namespace ApiClients\Tools\Installer\Operation;
4
5
use ApiClients\Tools\Installer\OperationInterface;
6
use Composer\Factory;
7
use Composer\Json\JsonFile;
8
use Symfony\Component\Console\Style\SymfonyStyle;
9
10
final class ComposerJson implements OperationInterface
11
{
12
    /**
13
     * @var JsonFile
14
     */
15
    private $jsonFile;
16
17
    /**
18
     * @internal
19
     * @param JsonFile $jsonFile
20
     */
21 3
    public function __construct(JsonFile $jsonFile)
22
    {
23 3
        $this->jsonFile = $jsonFile;
24 3
    }
25
26
    /**
27
     * @return OperationInterface
28
     */
29
    public static function create(): OperationInterface
30
    {
31
        return new self(new JsonFile(Factory::getComposerFile()));
32
    }
33
34
    /**
35
     * @param array        $replacements
36
     * @param array        $configuration
37
     * @param SymfonyStyle $style
38
     */
39 3
    public function operate(array $replacements, array $configuration, SymfonyStyle $style)
40
    {
41 3
        $style->section('Updating composer.json');
42 3
        $style->text('Reading composer.json');
43 3
        $composerJson = $this->jsonFile->read();
44
45 3
        $style->text('Replacing package name');
46 3
        $composerJson['name'] = $replacements['package_name'];
47
48 3
        $style->text('Adding authors');
49 3
        $composerJson['authors'] = [
50
            [
51 3
                'name'  => $replacements['author_name'],
52 3
                'email' => $replacements['author_email'],
53
            ],
54
        ];
55
56 3
        $style->text('Updating autoload');
57 3
        $composerJson['autoload']['psr-4'] = [
58 3
            $replacements['ns_vendor'] . '\\' . $replacements['ns_project'] . '\\' => 'src/',
59
        ];
60 3
        $composerJson['autoload-dev']['psr-4'] = [
61 3
            $replacements['ns_tests_vendor'] . '\\' . $replacements['ns_project'] . '\\' => 'tests/',
62
        ];
63
64 3
        $style->text('Removing package needed for installation and post create script');
65
66 3
        foreach (['require', 'require-dev', 'scripts'] as $index) {
67 3
            if (!isset($configuration[$index])) {
68 3
                continue;
69
            }
70
71 2
            $itemCount = count($configuration[$index]);
72 2
            $style->text('Removing ' . $itemCount . ' item' . ($itemCount > 1 ? 's' : '') . ' from ' . $index);
73 2
            $composerJson = $this->removeItemFromIndex($composerJson, $configuration, $index);
74
        }
75
76 3
        $style->text('Writing updated composer.json');
77 3
        $this->jsonFile->write($composerJson);
78 3
        $style->success('Updated composer.json');
79 3
    }
80
81 2
    private function removeItemFromIndex(array $composerJson, array $configuration, string $index): array
82
    {
83 2
        foreach ($configuration[$index] as $package) {
84 2
            if (!isset($composerJson[$index][$package])) {
85
                continue;
86
            }
87
88 2
            unset($composerJson[$index][$package]);
89
        }
90
91 2
        return $composerJson;
92
    }
93
}
94