• 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

97.92
/src/Package.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
class Package
20
{
21
    public const MAIN         = 'main';
22
    public const DIRECT       = 'direct';
23
    public const PHP          = 'php';
24
    public const EXT          = 'ext';
25
    public const REQUIRED     = 'require';
26
    public const REQUIRED_DEV = 'require-dev';
27
    public const SUGGEST      = 'suggest';
28
    public const HAS_META     = 'has-meta';
29
    public const INSTALLED    = 'installed';
30

31
    private string $name;
32

33
    private string $version = '*';
34

35
    private array $required = [];
36

37
    private array $requiredDev = [];
38

39
    private array $suggests = [];
40

41
    private array $tags = [];
42

43
    public function __construct(string $name, ?string $vendorDir = null)
44
    {
45
        $this->name = \strtolower($name);
96✔
46

47
        if (
48
            !\str_contains($this->name, '/')
96✔
49
            && (
50
                \preg_match('#^ext-[a-z\\d]*#', $this->name) > 0
96✔
51
                || \preg_match('#^lib-[a-z\\d]*#', $this->name) > 0
96✔
52
            )
53
        ) {
54
            $this->addTags([self::EXT, self::HAS_META]);
88✔
55

56
            if (
57
                \extension_loaded($this->name)
88✔
58
                || \extension_loaded(\str_replace(['ext-', 'lib-'], '', $this->name))
88✔
59
            ) {
60
                $this->addTags([self::INSTALLED]);
88✔
61
            }
62
        }
63

64
        if (
65
            $vendorDir !== null
96✔
66
            && $vendorDir !== ''
96✔
67
            && (\is_dir("{$vendorDir}/{$this->name}") || \is_dir("{$vendorDir}/{$name}"))
96✔
68
        ) {
69
            $this->addTags([self::INSTALLED]);
×
70
        }
71
    }
72

73
    public function setVersion(string $version): self
74
    {
75
        if ($version !== '') {
96✔
76
            $this->version = \strtolower($version);
92✔
77
        }
78

79
        return $this;
96✔
80
    }
81

82
    public function addRequire(array $required): self
83
    {
84
        $this->required = \array_merge($this->required, $required);
96✔
85

86
        return $this;
96✔
87
    }
88

89
    public function addRequireDev(array $requiredDev): self
90
    {
91
        $this->requiredDev = \array_merge($this->requiredDev, $requiredDev);
96✔
92

93
        return $this;
96✔
94
    }
95

96
    public function addSuggest(array $suggest): self
97
    {
98
        $this->suggests = \array_merge($this->suggests, $suggest);
96✔
99

100
        return $this;
96✔
101
    }
102

103
    public function addTags(array $tags): self
104
    {
105
        $this->tags = \array_unique(\array_merge($this->tags, $tags));
96✔
106

107
        return $this;
96✔
108
    }
109

110
    public function isTag(string $tag): bool
111
    {
112
        return \in_array($tag, $this->tags, true);
96✔
113
    }
114

115
    public function isDirectPackage(): bool
116
    {
117
        return $this->isTag(self::DIRECT) && $this->isTag(self::REQUIRED);
92✔
118
    }
119

120
    public function isDirectPackageDev(): bool
121
    {
122
        return $this->isTag(self::DIRECT) && $this->isTag(self::REQUIRED_DEV);
48✔
123
    }
124

125
    public function isPlatform(): bool
126
    {
127
        return !$this->isMain() && ($this->isTag(self::PHP) || $this->isTag(self::EXT));
92✔
128
    }
129

130
    public function isPhp(): bool
131
    {
132
        return $this->isTag(self::PHP);
64✔
133
    }
134

135
    public function isPhpExt(): bool
136
    {
137
        return $this->isTag(self::EXT);
64✔
138
    }
139

140
    public function isMain(): bool
141
    {
142
        return $this->isTag(self::MAIN);
96✔
143
    }
144

145
    public function getName(bool $addVersion = true): string
146
    {
147
        $name = \strtolower(\trim($this->name));
96✔
148

149
        if ($name === 'php') {
96✔
150
            $name = 'PHP';
24✔
151
        }
152

153
        $prefixNoMeta = '';
96✔
154
        if (!$this->isTag(self::HAS_META)) {
96✔
155
            $prefixNoMeta = '* ';
36✔
156
        }
157

158
        if (!$addVersion) {
96✔
159
            $result = $name;
96✔
160
        } else {
161
            $result = $this->version !== '' && $this->version !== '*'
96✔
162
                ? "{$name}@{$this->version}"
32✔
163
                : $name;
96✔
164
        }
165

166
        return $prefixNoMeta . $result;
96✔
167
    }
168

169
    public function getRequired(): array
170
    {
171
        return $this->required;
96✔
172
    }
173

174
    public function getRequiredDev(): array
175
    {
176
        return $this->requiredDev;
48✔
177
    }
178

179
    public function getSuggested(): array
180
    {
181
        return $this->suggests;
48✔
182
    }
183

184
    public function getId(): string
185
    {
186
        return self::alias($this->getName(false));
96✔
187
    }
188

189
    public static function alias(string $string): string
190
    {
191
        $string = \strip_tags($string);
96✔
192

193
        return \str_replace(['/', '-', 'graph', '(', ')', ' ', '*'], ['__', '_', 'g_raph', '', '', '', ''], $string);
96✔
194
    }
195
}
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

© 2026 Coveralls, Inc