• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Composer-Graph / 5399088069

pending completion
5399088069

push

github

web-flow
New code style and PHP 8.1+ (#14)

86 of 86 new or added lines in 5 files covered. (100.0%)

276 of 310 relevant lines covered (89.03%)

76.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.23
/src/Collection.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Composer-Graph.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Composer-Graph
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\ComposerGraph;
18

19
use JBZoo\Data\JSON;
20

21
use function JBZoo\Data\json;
22

23
class Collection
24
{
25
    private JSON $composerFile;
26

27
    private JSON $lockFile;
28

29
    /** @var Package[] */
30
    private array $collection = [];
31

32
    private ?string $vendorDir;
33

34
    public function __construct(JSON $composerFile, JSON $lockFile, ?string $vendorDir = null)
35
    {
36
        $this->composerFile = $composerFile;
96✔
37
        $this->lockFile     = $lockFile;
96✔
38
        $this->vendorDir    = $vendorDir;
96✔
39

40
        $this->buildCollection();
96✔
41
    }
42

43
    /**
44
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
45
     * @SuppressWarnings(PHPMD.NPathComplexity)
46
     */
47
    public function buildCollection(): void
48
    {
49
        /** @phpstan-ignore-next-line */
50
        $istTest = \defined('\IS_PHPUNIT_TEST') && IS_PHPUNIT_TEST;
96✔
51

52
        $this->add('php', [
96✔
53
            'version' => $istTest ? null : \PHP_VERSION,
96✔
54
            'tags'    => [Package::PHP, Package::HAS_META],
48✔
55
        ]);
48✔
56

57
        $this->add((string)$this->composerFile->get('name'), [
96✔
58
            'version'     => $istTest ? null : Helper::getGitVersion(),
96✔
59
            'require'     => $this->composerFile->get('require'),
96✔
60
            'require-dev' => $this->composerFile->get('require-dev'),
96✔
61
            'suggest'     => $this->composerFile->get('suggest'),
96✔
62
            'tags'        => [Package::MAIN, Package::HAS_META],
48✔
63
        ]);
48✔
64

65
        $mainRequire = \array_keys((array)$this->composerFile->get('require'));
96✔
66

67
        foreach ($mainRequire as $package) {
96✔
68
            $this->add((string)$package, ['tags' => [Package::DIRECT]]);
92✔
69
        }
70

71
        $mainRequireDev = \array_keys((array)$this->composerFile->get('require-dev'));
96✔
72

73
        foreach ($mainRequireDev as $packageDev) {
96✔
74
            $this->add((string)$packageDev, ['tags' => [Package::DIRECT]]);
84✔
75
        }
76

77
        $mainSuggest = \array_keys((array)$this->composerFile->get('suggest'));
96✔
78

79
        foreach ($mainSuggest as $suggest) {
96✔
80
            $this->add((string)$suggest, ['tags' => [Package::DIRECT, Package::SUGGEST]]);
60✔
81
        }
82

83
        // Lock file
84
        $scopes = [
96✔
85
            Package::REQUIRED     => (array)$this->lockFile->get('packages'),
96✔
86
            Package::REQUIRED_DEV => (array)$this->lockFile->get('packages-dev'),
96✔
87
        ];
48✔
88

89
        foreach ($scopes as $scopeType => $scope) {
96✔
90
            foreach ($scope as $package) {
96✔
91
                $package = json($package);
92✔
92

93
                $require = (array)$package->get('require');
92✔
94
                $suggest = (array)$package->get('suggest');
92✔
95

96
                $version = $package->getString('version');
92✔
97
                $version = $package->findString("extra.branch-alias.{$version}", $version);
92✔
98

99
                $this->add((string)$package->get('name'), [
92✔
100
                    'version' => $version,
46✔
101
                    'require' => $require,
46✔
102
                    'suggest' => $suggest,
46✔
103
                    'tags'    => [$scopeType, Package::HAS_META],
46✔
104
                ]);
46✔
105

106
                foreach (\array_keys($require) as $innerRequired) {
92✔
107
                    $this->add((string)$innerRequired, ['tags' => [$scopeType]]);
88✔
108
                }
109

110
                foreach (\array_keys($suggest) as $innerSuggested) {
92✔
111
                    $this->add((string)$innerSuggested, ['tags' => [$scopeType, Package::SUGGEST]]);
84✔
112
                }
113
            }
114
        }
115
    }
116

117
    public function getMain(): Package
118
    {
119
        foreach ($this->collection as $package) {
96✔
120
            if ($package->isMain()) {
96✔
121
                return $package;
96✔
122
            }
123
        }
124

125
        throw new Exception('Main package not found');
×
126
    }
127

128
    public function getByName(string $packageName): Package
129
    {
130
        $packageAlias = Package::alias($packageName);
92✔
131
        if (\array_key_exists($packageAlias, $this->collection)) {
92✔
132
            return $this->collection[$packageAlias];
92✔
133
        }
134

135
        throw new Exception("Package \"{$packageName} ({$packageAlias})\" not found in collection");
×
136
    }
137

138
    private function add(string $packageName, array $packageMeta): void
139
    {
140
        $current      = json($packageMeta);
96✔
141
        $packageAlias = Package::alias($packageName);
96✔
142

143
        /** @var Package $package */
144
        $package = $this->collection[$packageAlias] ?? new Package($packageName, $this->vendorDir);
96✔
145

146
        $package
48✔
147
            ->setVersion((string)$current->get('version'))
96✔
148
            ->addRequire((array)$current->get('require'))
96✔
149
            ->addRequireDev((array)$current->get('require-dev'))
96✔
150
            ->addSuggest((array)$current->get('suggest'))
96✔
151
            ->addTags((array)$current->get('tags'));
96✔
152

153
        $this->collection[$packageAlias] = $package;
96✔
154
    }
155
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc