Sanitizer   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 32
c 2
b 1
f 0
dl 0
loc 87
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fixupJsonDataType() 0 13 6
B sanitize() 0 40 6
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Repository\Lock;
7
8
use Vaimo\ComposerPatches\Composer\ConfigKeys as Config;
9
use Vaimo\ComposerPatches\Config as PluginConfig;
10
use Vaimo\ComposerPatches\Composer\Constraint;
11
12
class Sanitizer
13
{
14
    /**
15
     * @var \Vaimo\ComposerPatches\Managers\LockerManager
16
     */
17
    private $lockerManager;
18
19
    /**
20
     * @var \Vaimo\ComposerPatches\Utils\DataUtils
21
     */
22
    private $dataUtils;
23
24
    /**
25
     * @param \Composer\IO\IOInterface $appIO
26
     */
27
    public function __construct(
28
        \Composer\IO\IOInterface $appIO
29
    ) {
30
        $this->lockerManager = new \Vaimo\ComposerPatches\Managers\LockerManager($appIO);
31
32
        $this->dataUtils = new \Vaimo\ComposerPatches\Utils\DataUtils();
33
    }
34
35
    public function sanitize()
36
    {
37
        $lockData = $this->lockerManager->readLockData();
38
39
        if (!$lockData) {
40
            return;
41
        }
42
43
        $dataChanged = false;
44
45
        $queriedPaths = array(
46
            implode('/', array(Config::PACKAGES, Constraint::ANY)),
47
            implode('/', array(Config::PACKAGES_DEV, Constraint::ANY))
48
        );
49
50
        $nodes = $this->dataUtils->getNodeReferencesByPaths($lockData, $queriedPaths);
51
52
        foreach ($nodes as &$node) {
53
            if (!isset($node[Config::CONFIG_ROOT][PluginConfig::APPLIED_FLAG])) {
54
                continue;
55
            }
56
57
            unset($node[Config::CONFIG_ROOT][PluginConfig::APPLIED_FLAG]);
58
            $dataChanged = true;
59
60
            if ($node[Config::CONFIG_ROOT]) {
61
                continue;
62
            }
63
64
            unset($node[Config::CONFIG_ROOT]);
65
        }
66
67
        unset($node);
68
69
        if (!$dataChanged) {
70
            return;
71
        }
72
73
        $lockData = $this->fixupJsonDataType($lockData);
74
        $this->lockerManager->writeLockData($lockData);
75
    }
76
77
    /**
78
     * Copy-paste from composer/composer/src/Composer/Package/Locker.php. Would prefer to use the locker directly,
79
     * but there's no alternative to src/Managers/LockerManager::writeLockData(). The method setLockData() is really
80
     * close, but it seems to expect some of the data to be objects, which we don't have in this context.
81
     *
82
     * @param mixed[] $lockData
83
     *
84
     * @return mixed[]
85
     */
86
    private function fixupJsonDataType(array $lockData)
87
    {
88
        foreach (['stability-flags', 'platform', 'platform-dev'] as $key) {
89
            if (isset($lockData[$key]) && is_array($lockData[$key]) && \count($lockData[$key]) === 0) {
90
                $lockData[$key] = new \stdClass();
91
            }
92
        }
93
94
        if (is_array($lockData['stability-flags'])) {
95
            ksort($lockData['stability-flags']);
96
        }
97
98
        return $lockData;
99
    }
100
}
101