Completed
Push — internationalization ( 50fb1f )
by Arnaud
02:01
created

FileStr::sub()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Collection\Page;
10
11
/**
12
 * Class FileStr.
13
 */
14
class FileStr
0 ignored issues
show
Coding Style introduced by
FileStr does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
15
{
16
    // https://regex101.com/r/tJWUrd/6
17
    // ie: "blog/2017-10-19-post-1.md" prefix is "2017-10-19"
18
    // ie: "projet/1-projet-a.md" prefix is "1"
19
    const PREFIX_PATTERN = '^(|.*\/)(([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])|[0-9]+)(-|_|\.)(.*)$';
20
    // https://regex101.com/r/GlgBdT/7
21
    // ie: "blog/2017-10-19-post-1.en.md" suffix is "en"
22
    // ie: "projet/1-projet-a.fr-FR.md" suffix is "fr-FR"
23
    const SUFFIX_PATTERN = '^(.*)\.([a-z]{2}(-[A-Z]{2})?)$';
24
25
    /**
26
     * Return true if the string contains a prefix or a suffix.
27
     *
28
     * @param string $string
29
     * @param string $type
30
     *
31
     * @return bool
32
     */
33
    protected static function has(string $string, string $type): bool
34
    {
35
        if (preg_match('/'.self::getPattern($type).'/', $string)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) preg_match...$type) . '/', $string);.
Loading history...
36
            return true;
37
        }
38
39
        return false;
40
    }
41
42
    /**
43
     * Return true if the string contains a prefix.
44
     *
45
     * @param string $string
46
     *
47
     * @return bool
48
     */
49
    public static function hasPrefix(string $string): bool
50
    {
51
        return self::has($string, 'prefix');
52
    }
53
54
    /**
55
     * Return true if the string contains a suffix.
56
     *
57
     * @param string $string
58
     *
59
     * @return bool
60
     */
61
    public static function hasSuffix(string $string): bool
62
    {
63
        return self::has($string, 'suffix');
64
    }
65
66
    /**
67
     * Return the prefix or the suffix if exists.
68
     *
69
     * @param string $string
70
     * @param string $type
71
     *
72
     * @return string[]|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
73
     */
74
    protected static function get(string $string, string $type): ?string
75
    {
76
        if (self::has($string, $type)) {
77
            preg_match('/'.self::getPattern($type).'/', $string, $matches);
78
            switch ($type) {
79
                case 'prefix':
80
                    return $matches[2];
81
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
82
                case 'suffix':
83
                    return $matches[2];
84
            }
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * Return the prefix if exists.
92
     *
93
     * @param string $string
94
     *
95
     * @return string[]|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    public static function getPrefix(string $string): ?string
98
    {
99
        return self::get($string, 'prefix');
100
    }
101
102
    /**
103
     * Return the suffix if exists.
104
     *
105
     * @param string $string
106
     *
107
     * @return string[]|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
108
     */
109
    public static function getSuffix(string $string): ?string
110
    {
111
        return self::get($string, 'suffix');
112
    }
113
114
    /**
115
     * Return string without the prefix and the suffix (if exists).
116
     *
117
     * @param string $string
118
     *
119
     * @return string
120
     */
121
    public static function sub(string $string): string
122
    {
123
        if (self::hasPrefix($string)) {
124
            preg_match('/'.self::getPattern('prefix').'/', $string, $matches);
125
126
            $string = $matches[1].$matches[7];
127
        }
128
        if (self::hasSuffix($string)) {
129
            preg_match('/'.self::getPattern('suffix').'/', $string, $matches);
130
131
            $string = $matches[1];
132
        }
133
134
        return $string;
135
    }
136
137
    /**
138
     * Return string without the prefix (if exists).
139
     *
140
     * @param string $string
141
     *
142
     * @return string
143
     */
144
    public static function subPrefix(string $string): string
145
    {
146
        if (self::hasPrefix($string)) {
147
            preg_match('/'.self::getPattern('prefix').'/', $string, $matches);
148
149
            return $matches[1].$matches[7];
150
        }
151
152
        return $string;
153
    }
154
155
    /**
156
     * Return expreg pattern by $type.
157
     *
158
     * @param string $type
159
     *
160
     * @return string
161
     */
162
    protected static function getPattern(string $type): string
163
    {
164
        switch ($type) {
165
            case 'prefix':
166
                return self::PREFIX_PATTERN;
167
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
168
            case 'suffix':
169
                return self::SUFFIX_PATTERN;
170
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
171
            default:
172
                throw new Exception(\sprintf('%s must be "prefix" or "suffix"', $type));
173
        }
174
    }
175
}
176