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

eliashaeussler / composer-package-url-generator / 12718293985

10 Jan 2025 10:36PM UTC coverage: 92.857% (+0.4%) from 92.453%
12718293985

Pull #18

github

web-flow
Merge 96d630ed8 into 46e10971e
Pull Request #18: [TASK] Provide base VcsUrlGenerator

36 of 38 new or added lines in 3 files covered. (94.74%)

130 of 140 relevant lines covered (92.86%)

2.96 hits per line

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

92.86
/src/Url/Generator/VcsUrlGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/composer-package-url-generator".
7
 *
8
 * Copyright (C) 2023-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\ComposerPackageUrlGenerator\Url\Generator;
25

26
use Composer\Package;
27
use EliasHaeussler\ComposerPackageUrlGenerator\Exception;
28
use EliasHaeussler\ComposerPackageUrlGenerator\Helper;
29
use Psr\Http\Message;
30

31
use function str_contains;
32

33
/**
34
 * VcsUrlGenerator.
35
 *
36
 * @author Elias Häußler <elias@haeussler.dev>
37
 * @license GPL-3.0-or-later
38
 */
39
abstract class VcsUrlGenerator implements UrlGenerator
40
{
41
    /**
42
     * @throws Exception\NoSourceUrlAvailable
43
     * @throws Exception\UrlIsMalformed
44
     */
45
    public function generateSourceUrl(Package\PackageInterface $package): Message\UriInterface
7✔
46
    {
47
        $candidates = [];
7✔
48
        $candidates += $package->getSourceUrls();
7✔
49
        $candidates += $package->getDistUrls();
7✔
50

51
        foreach ($candidates as $candidate) {
7✔
52
            $sourceUrl = Helper\UrlHelper::normalizeUrl($candidate);
6✔
53

54
            if ($this->isApiUrl($sourceUrl)) {
6✔
55
                $sourceUrl = $this->extractSourceUrlFromApiUrl($sourceUrl);
4✔
56
            }
57

58
            if (null !== $sourceUrl) {
6✔
59
                return $sourceUrl;
4✔
60
            }
61
        }
62

63
        throw new Exception\NoSourceUrlAvailable($package->getName());
3✔
64
    }
65

66
    /**
67
     * @throws Exception\UrlIsMalformed
68
     */
69
    public function generateHomepageUrl(Package\PackageInterface $package): ?Message\UriInterface
4✔
70
    {
71
        if ($package instanceof Package\CompletePackageInterface && null !== $package->getHomepage()) {
4✔
72
            return Helper\UrlHelper::normalizeUrl($package->getHomepage());
1✔
73
        }
74

75
        try {
76
            return $this->generateSourceUrl($package);
3✔
77
        } catch (Exception\NoSourceUrlAvailable) {
1✔
78
            return null;
1✔
79
        }
80
    }
81

82
    public function supports(Package\PackageInterface $package): bool
3✔
83
    {
84
        $candidates = [];
3✔
85
        $candidates += $package->getSourceUrls();
3✔
86
        $candidates += $package->getDistUrls();
3✔
87

88
        foreach ($candidates as $candidate) {
3✔
89
            foreach ($this->getDomains() as $domain) {
2✔
90
                if (str_contains($candidate, $domain)) {
2✔
91
                    return true;
2✔
92
                }
93
            }
94
        }
95

96
        return false;
1✔
97
    }
98

NEW
99
    public static function getPriority(): int
×
100
    {
NEW
101
        return 100;
×
102
    }
103

104
    abstract protected function isApiUrl(Message\UriInterface $sourceUrl): bool;
105

106
    abstract protected function extractSourceUrlFromApiUrl(Message\UriInterface $apiUrl): ?Message\UriInterface;
107

108
    /**
109
     * @return non-empty-array<string>
110
     */
111
    abstract protected function getDomains(): array;
112
}
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