Completed
Push — master ( 56096b...601ee3 )
by Sergey
12s
created

Regex::fetchQuantifier()   B

Complexity

Conditions 15
Paths 14

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 16
nc 14
nop 3
dl 0
loc 20
rs 7.3427
c 0
b 0
f 0

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
namespace Larapulse\Support\Helpers;
4
5
class Regex
6
{
7
    public static function quantifyGroup(string $str, $min = null, $max = INF, string $tag = null) : string
8
    {
9
        $tag = $tag ? "?<{$tag}>" : '';
10
11
        return "({$tag}{$str})".self::fetchQuantifier($min, $max);
12
    }
13
14
    /**
15
     * Build quantifier from parameters
16
     *
17
     * @param int|null  $min
18
     * @param float|int $max
19
     * @param bool      $lazyLoad
20
     *
21
     * @return string
22
     */
23
    public static function fetchQuantifier($min = null, $max = INF, bool $lazyLoad = false) : string
24
    {
25
        $lazy = $lazyLoad ? '?' : '';
26
27
        switch (true) {
28
            case ($min === 0 && $max === 1):
29
                return '?'.$lazy;
30
            case ($min === 0 && $max === INF):
31
                return '*'.$lazy;
32
            case ($min === 1 && $max === INF):
33
                return '+'.$lazy;
34
            case (is_int($min) && $min >= 1 && $max === null):
35
                return '{'.$min.'}'.$lazy;
36
            case (is_int($min) && (is_int($max) || $max === INF)):
37
                $max = is_int($max) ? $max : '';
38
                return '{'.$min.','.$max.'}'.$lazy;
39
            default:
40
                return '';
41
        }
42
    }
43
}
44