Issues (224)

src/RequirementChecker/RequirementsBuilder.php (4 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\RequirementChecker;
16
17
use KevinGH\Box\Composer\Package\Extension;
18
use function array_diff_key;
19
use function array_unique;
20
use function natsort;
21
use function strnatcmp;
22
use function uksort;
23
24
final class RequirementsBuilder
25
{
26
    private array $predefinedRequirements = [];
27
    private array $requiredExtensions = [];
28
    private array $providedExtensions = [];
29
    private array $conflictingExtensions = [];
30
31
    public function addRequirement(Requirement $requirement): void
32
    {
33
        $this->predefinedRequirements[] = $requirement;
34
    }
35
36
    public function addRequiredExtension(Extension $extension, ?string $source): void
37
    {
38
        $this->requiredExtensions[$extension->name][] = $source;
39
    }
40
41
    public function addProvidedExtension(Extension $extension, ?string $source): void
42
    {
43
        $this->providedExtensions[$extension->name][] = $source;
44
    }
45
46
    public function addConflictingExtension(Extension $extension, ?string $source): void
47
    {
48
        $this->conflictingExtensions[$extension->name][] = $source;
49
    }
50
51
    public function build(): Requirements
52
    {
53
        $requirements = $this->predefinedRequirements;
54
55
        foreach ($this->getSortedRequiredExtensions() as $extensionName => $sources) {
56
            $sortedDistinctSources = self::createSortedDistinctList($sources);
57
58
            foreach ($sortedDistinctSources as $source) {
59
                $requirements[] = Requirement::forRequiredExtension(
60
                    $extensionName,
61
                    $source,
62
                );
63
            }
64
        }
65
66
        foreach ($this->getSortedConflictedExtensions() as $extensionName => $sources) {
67
            $sortedDistinctSources = self::createSortedDistinctList($sources);
68
69
            foreach ($sortedDistinctSources as $source) {
70
                $requirements[] = Requirement::forConflictingExtension(
71
                    $extensionName,
72
                    $source,
73
                );
74
            }
75
        }
76
77
        return new Requirements($requirements);
78
    }
79
80
    /**
81
     * @return array<string, list<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, list<string>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
82
     */
83
    private function getSortedRequiredExtensions(): array
84
    {
85
        $extensions = array_diff_key(
86
            $this->requiredExtensions,
87
            $this->providedExtensions,
88
        );
89
90
        uksort($extensions, strnatcmp(...));
0 ignored issues
show
The type strnatcmp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
92
        return $extensions;
93
    }
94
95
    /**
96
     * @return array<string, list<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, list<string>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
97
     */
98
    private function getSortedConflictedExtensions(): array
99
    {
100
        $extensions = $this->conflictingExtensions;
101
102
        uksort($extensions, strnatcmp(...));
103
104
        return $extensions;
105
    }
106
107
    /**
108
     * @param array<string|null> $sources
109
     *
110
     * @return list<string|null>
0 ignored issues
show
The type KevinGH\Box\RequirementChecker\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
     */
112
    private static function createSortedDistinctList(array $sources): array
113
    {
114
        $uniqueSources = array_unique($sources);
115
116
        natsort($uniqueSources);
117
118
        return $uniqueSources;
119
    }
120
}
121