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

JBZoo / Composer-Diff / 6408983532

16 Aug 2023 06:22PM UTC coverage: 94.294%. Remained the same
6408983532

push

github

web-flow
Upgrade `jbzoo/cli:^7.1.1` (#26)

1 of 1 new or added line in 1 file covered. (100.0%)

314 of 333 relevant lines covered (94.29%)

49.24 hits per line

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

95.74
/src/Url.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Composer-Diff.
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-Diff
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\ComposerDiff;
18

19
final class Url
20
{
21
    public const BITBUCKET = 'bitbucket';
22
    public const GITHUB    = 'github';
23
    public const GITLAB    = 'gitlab';
24
    public const UNKNOWN   = 'unknown';
25

26
    public static function getCompareUrl(?string $sourceUrl, ?string $fromVersion, ?string $toVersion): ?string
27
    {
28
        if (
29
            $sourceUrl === null
100✔
30
            || $sourceUrl === ''
100✔
31
            || $fromVersion === null
100✔
32
            || $fromVersion === ''
100✔
33
            || $toVersion === null
100✔
34
            || $toVersion === ''
100✔
35
        ) {
36
            return null;
×
37
        }
38

39
        $service = self::detectService($sourceUrl);
100✔
40
        $url     = self::getPackageUrl($sourceUrl);
100✔
41

42
        $fromVersion = \urlencode($fromVersion);
100✔
43
        $toVersion   = \urlencode($toVersion);
100✔
44

45
        if (\in_array($service, [self::GITHUB, self::GITLAB], true)) {
100✔
46
            return "{$url}/compare/{$fromVersion}...{$toVersion}";
100✔
47
        }
48

49
        if ($service === self::BITBUCKET) {
8✔
50
            return "{$url}/branches/compare/{$fromVersion}%0D{$toVersion}";
8✔
51
        }
52

53
        return null;
8✔
54
    }
55

56
    public static function getPackageUrl(string $sourceUrl): ?string
57
    {
58
        $service = self::detectService($sourceUrl);
124✔
59

60
        if ($service === self::GITHUB) {
124✔
61
            if (!\str_contains($sourceUrl, 'http')) {
24✔
62
                $sourceUrl = (string)\preg_replace('/^git@(github\.[^:]+):/', 'https://$1/', $sourceUrl);
16✔
63
            }
64

65
            return (string)\preg_replace('/\.git$/', '', $sourceUrl);
24✔
66
        }
67

68
        if ($service === self::GITLAB) {
116✔
69
            if (!\str_contains($sourceUrl, 'http')) {
116✔
70
                $sourceUrl = (string)\preg_replace('/^git@(gitlab\.[^:]+):/', 'https://$1/', $sourceUrl);
116✔
71
            }
72

73
            return (string)\preg_replace('/\.git$/', '', $sourceUrl);
116✔
74
        }
75

76
        if ($service === self::BITBUCKET) {
16✔
77
            if (!\str_contains($sourceUrl, 'http')) {
16✔
78
                $sourceUrl = (string)\preg_replace('/^git@(bitbucket\.[^:]+):/', 'https://$1/', $sourceUrl);
16✔
79
            }
80

81
            return (string)\preg_replace('/\.git$/', '', $sourceUrl);
16✔
82
        }
83

84
        return null;
16✔
85
    }
86

87
    public static function isUrl(string $string): bool
88
    {
89
        return (bool)\filter_var($string, \FILTER_VALIDATE_URL, \FILTER_FLAG_PATH_REQUIRED);
108✔
90
    }
91

92
    private static function detectService(string $url): string
93
    {
94
        if (\preg_match('/^git@github\..+:.+\.git$/', $url) > 0) {
124✔
95
            return self::GITHUB;
16✔
96
        }
97

98
        if (\preg_match('/^git@gitlab\..+:.+\.git$/', $url) > 0) {
124✔
99
            return self::GITLAB;
116✔
100
        }
101

102
        if (\preg_match('/^git@bitbucket\..+:.+\.git$/', $url) > 0) {
24✔
103
            return self::BITBUCKET;
16✔
104
        }
105

106
        if (\stripos($url, 'http') !== 0) {
24✔
107
            return self::UNKNOWN;
×
108
        }
109

110
        $host = \strtolower((string)\parse_url($url, \PHP_URL_HOST));
24✔
111

112
        if (\str_contains($host, 'github')) {
24✔
113
            return self::GITHUB;
24✔
114
        }
115

116
        if (\str_contains($host, 'bitbucket')) {
16✔
117
            return self::BITBUCKET;
16✔
118
        }
119

120
        if (\str_contains($host, 'gitlab')) {
16✔
121
            return self::GITLAB;
16✔
122
        }
123

124
        return self::UNKNOWN;
16✔
125
    }
126
}
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