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

eliashaeussler / composer-package-url-generator / 17540936684

08 Sep 2025 12:46AM UTC coverage: 92.308%. Remained the same
17540936684

push

github

web-flow
[TASK] Update all dependencies

132 of 143 relevant lines covered (92.31%)

3.01 hits per line

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

85.71
/src/Helper/UrlHelper.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\Helper;
25

26
use EliasHaeussler\ComposerPackageUrlGenerator\Exception;
27
use GuzzleHttp\Psr7\Uri;
28
use Psr\Http\Message;
29

30
use function parse_url;
31
use function preg_replace;
32
use function str_contains;
33

34
/**
35
 * UriHelper.
36
 *
37
 * @author Elias Häußler <elias@haeussler.dev>
38
 * @license GPL-3.0-or-later
39
 */
40
final class UrlHelper
41
{
42
    /**
43
     * @throws Exception\UrlIsMalformed
44
     */
45
    public static function normalizeUrl(string $url): Message\UriInterface
6✔
46
    {
47
        $normalizedUrl = preg_replace('/\.git\/?$/', '', $url);
6✔
48

49
        if (null === $normalizedUrl) {
6✔
50
            throw new Exception\UrlIsMalformed($url);
×
51
        }
52

53
        // Convert Git-style syntax to HTTPS syntax
54
        // Example: git@gitlab.example.com:foo/bar => https://gitlab.example.com/foo/bar
55
        if (!str_contains($normalizedUrl, '://')) {
6✔
56
            $normalizedUrl = preg_replace('/^.+@([^:]+):/', 'https://$1/', $normalizedUrl);
2✔
57
        }
58

59
        if (null === $normalizedUrl) {
6✔
60
            throw new Exception\UrlIsMalformed($url);
×
61
        }
62

63
        if (false === parse_url($normalizedUrl)) {
6✔
64
            throw new Exception\UrlIsMalformed($url);
1✔
65
        }
66

67
        $uri = new Uri($normalizedUrl);
5✔
68

69
        // Enforce HTTPS scheme for SSH urls and missing url schemes
70
        if ('' === $uri->getScheme() || 'ssh' === $uri->getScheme()) {
5✔
71
            $uri = $uri->withScheme('https');
3✔
72
        }
73

74
        // Drop user info and port
75
        return $uri->withUserInfo('')->withPort(null);
5✔
76
    }
77
}
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