Passed
Push — master ( f2e35b...cd03c9 )
by Allan
04:05 queued 15s
created

ConstraintsComponent   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B process() 0 38 6
B resolveComparisonResults() 0 51 8
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Patch\DefinitionList\LoaderComponents;
7
8
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
9
10
class ConstraintsComponent implements \Vaimo\ComposerPatches\Interfaces\DefinitionListLoaderComponentInterface
11
{
12
    /**
13
     * @var \Vaimo\ComposerPatches\Interfaces\PackageConfigExtractorInterface
14
     */
15
    private $configExtractor;
16
    
17
    /**
18
     * @var \Composer\Package\Version\VersionParser
19
     */
20
    private $versionParser;
21
22
    /**
23
     * @var \Vaimo\ComposerPatches\Utils\ConstraintUtils
24
     */
25
    private $constraintUtils;
26
27
    /**
28
     * @param \Vaimo\ComposerPatches\Interfaces\PackageConfigExtractorInterface $configExtractor
29
     */
30
    public function __construct(
31
        \Vaimo\ComposerPatches\Interfaces\PackageConfigExtractorInterface $configExtractor
32
    ) {
33
        $this->configExtractor = $configExtractor;
34
        $this->versionParser = new \Composer\Package\Version\VersionParser();
35
        $this->constraintUtils = new \Vaimo\ComposerPatches\Utils\ConstraintUtils();
36
    }
37
38
    /**
39
     * @param array $patches
40
     * @param \Composer\Package\PackageInterface[] $packagesByName
41
     * @return array
42
     */
43
    public function process(array $patches, array $packagesByName)
44
    {
45
        $rootPackages = array_filter($packagesByName, function ($package) {
46
            return $package instanceof \Composer\Package\RootPackage;
47
        });
48
49
        /** @var \Composer\Package\CompletePackageInterface $rootPackage */
50
        $rootPackage = reset($rootPackages);
51
52
        $rootRequires = array_replace(
53
            $rootPackage->getRequires(),
54
            $rootPackage->getDevRequires()
55
        );
56
        
57
        foreach ($patches as $target => &$packagePatches) {
58
            foreach ($packagePatches as &$patchData) {
59
                if ($target !== PatchDefinition::BUNDLE_TARGET && !isset($packagesByName[$target])) {
60
                    $patchData = false;
61
                    continue;
62
                }
63
64
                $patchConstraints = $patchData[PatchDefinition::DEPENDS];
65
66
                $comparisonResults = $this->resolveComparisonResults(
67
                    $patchConstraints,
68
                    $packagesByName,
69
                    $rootRequires
70
                );
71
72
                if (count($patchConstraints) !== count(array_filter($comparisonResults))) {
73
                    $patchData = false;
74
                }
75
            }
76
77
            $packagePatches = array_filter($packagePatches);
78
        }
79
80
        return array_filter($patches);
81
    }
82
    
83
    private function resolveComparisonResults(array $patchConstraints, array $packages, array $rootRequires)
84
    {
85
        $comparisonResults = array();
86
        
87
        foreach ($patchConstraints as $constraintTarget => $version) {
88
            if (!isset($packages[$constraintTarget])) {
89
                continue;
90
            }
91
92
            $package = $packages[$constraintTarget];
93
94
            $packageVersion = $package->getVersion();
95
            $packageVersions = array($package->getVersion());
96
97
            if (isset($rootRequires[$constraintTarget])) {
98
                /** @var \Composer\Package\CompletePackageInterface $targetRootPackage */
99
                $targetRootPackage = $rootRequires[$constraintTarget];
100
101
                preg_match('/.* as (.*)$/', $targetRootPackage->getPrettyConstraint(), $matches);
0 ignored issues
show
Bug introduced by
The method getPrettyConstraint() does not exist on Composer\Package\CompletePackageInterface. Did you maybe mean getPrettyVersion()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
                preg_match('/.* as (.*)$/', $targetRootPackage->/** @scrutinizer ignore-call */ getPrettyConstraint(), $matches);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
103
                if (isset($matches[1])) {
104
                    $packageVersions[] = $matches[1];
105
                }
106
            }
107
108
            if ($this->constraintUtils->isDevConstraint($packageVersion)) {
109
                $definedVersion = $this->configExtractor->getConfig(
110
                    $package,
111
                    'version'
112
                );
113
114
                $packageVersions[] = $definedVersion;
115
            }
116
117
            $matchResult = false;
118
119
            foreach (array_filter($packageVersions) as $packageVersion) {
120
                $packageConstraint = $this->versionParser->parseConstraints($packageVersion);
121
                $patchConstraint = $this->versionParser->parseConstraints($version);
122
123
                $matchResult = $patchConstraint->matches($packageConstraint);
124
125
                if ($matchResult) {
126
                    break;
127
                }
128
            }
129
130
            $comparisonResults[] = $matchResult;
131
        }
132
        
133
        return $comparisonResults;
134
    }
135
}
136