Type::determine()   B
last analyzed

Complexity

Conditions 10
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
c 2
b 1
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 7.6666
cc 10
nc 6
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * Pickle
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2015-2015, Pickle community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
namespace Pickle\Package\Convey\Command;
38
39
use Composer\Package\Version\VersionParser;
40
41
class Type
42
{
43
    public const PICKLE = 'pickle';
44
45
    public const PECL = 'pecl';
46
47
    public const GIT = 'git';
48
49
    public const TGZ = 'tgz';
50
51
    public const SRC_DIR = 'srcdir';
52
53
    public const ANY = 'any';
54
55
    public static function match($regs, $arg, &$matches)
56
    {
57 1
        foreach ($regs as $reg) {
58 1
            $ret = preg_match($reg, $arg, $matches);
59 1
            if ($ret > 0) {
60 1
                return $ret;
61
            }
62
        }
63
64 1
        return 0;
65
    }
66
67
    public static function determinePickle($arg, &$matches)
68
    {
69 1
        $versionParser = new VersionParser();
70 1
        $res = $versionParser->parseNameVersionPairs([$arg]);
71 1
        $argPrefix = substr($arg, 0, 1);
72 1
        if ($argPrefix == '/' || $argPrefix == '.') {
73 1
            return 0;
74
        }
75
        $matches = [
76 1
            'package' => $res[0]['name'],
77 1
            'version' => $res[0]['version'] ?? '',
78
        ];
79
80 1
        return 1;
81
    }
82
83
    public static function determinePecl($arg, &$matches)
84
    {
85 1
        $reg0 = '#^
86
            (?:pecl/)?
87
            (?<package>\w+)
88
            (?:
89
                \-(?<stability>beta|stable|alpha)
90
            )?
91
        $#x';
92
93 1
        $reg1 = '#^
94
            (?:pecl/)?
95
            (?<package>\w+)
96
            (?:
97
                (\-|@)(?<version>(?:\d+(?:\.\d+){1,2})|(?:[1-2]\d{3}[0-1]\d[0-3]\d{1}))
98
            )?
99
        $#x';
100
101 1
        return self::match([$reg0, $reg1], $arg, $matches);
102
    }
103
104
    public static function determineGit($arg, &$matches)
105
    {
106 1
        $reg0 = '#^
107
            (?:git|https|http|ssh|rsync|file?)(://|@).*?(/|\:)
108
            (?P<package>[a-zA-Z0-9\-_]+)
109
            (?:
110
                (?:\.git|)
111
                (?:\#(?P<reference>.*?)|)
112
            )?
113
        $#x';
114
115 1
        return self::match([$reg0], $arg, $matches);
116
    }
117
118
    public static function determine($path, $remote)
119
    {
120 1
        if (substr($path, -4) == '.tgz' || substr($path, -7) == '.tar.gz') {
121 1
            return self::TGZ;
122
        }
123 1
        $matches = null;
124 1
        if ($remote && self::determinePecl($path, $matches) > 0) {
125 1
            return self::PECL;
126
        }
127 1
        if ($remote && self::determineGit($path, $matches) > 0) {
128 1
            return self::GIT;
129
        }
130 1
        if (!$remote && is_dir($path)) {
131 1
            return self::SRC_DIR;
132
        }
133 1
        if (self::determinePickle($path, $matches) > 0) {
134 1
            return self::PICKLE;
135
        }
136
137 1
        return self::ANY;
138
    }
139
}
140
141
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
142