Passed
Push — master ( 157485...b7615a )
by Nikolaos
09:23
created

AbstractFilter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 1
b 0
f 0
dl 0
loc 65
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A upper() 0 7 2
A len() 0 7 2
A lower() 0 7 2
A substr() 0 14 2
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Filter\Sanitize;
15
16
use function function_exists;
17
use function mb_strlen;
18
use function strlen;
19
use function strtolower;
20
use function strtoupper;
21
use function utf8_decode;
22
23
use const MB_CASE_UPPER;
24
25
/**
26
 * Phalcon\Filter\Sanitize\AbsInt
27
 *
28
 * Sanitizes a value to absolute integer
29
 */
30
abstract class AbstractFilter
31
{
32
    /**
33
     * @param string $input
34
     *
35
     * @return int
36
     */
37
    protected function len(string $input): int
38
    {
39
        if (true === function_exists("mb_strlen")) {
40
            return mb_strlen($input, "UTF-8");
41
        }
42
43
        return strlen(utf8_decode($input));
44
    }
45
46
    /**
47
     * @param string $input
48
     *
49
     * @return string
50
     */
51
    protected function lower(string $input): string
52
    {
53
        if (true === function_exists("mb_convert_case")) {
54
            return mb_convert_case($input, MB_CASE_LOWER, "UTF-8");
55
        }
56
57
        return strtolower(utf8_decode($input));
58
    }
59
60
    /**
61
     * @param string   $input
62
     * @param int      $start
63
     * @param int|null $length
64
     *
65
     * @return string
66
     */
67
    protected function substr(string $input, int $start, int $length = null)
68
    {
69
        if (true === function_exists("mb_substr")) {
70
            return mb_substr($input, $start, $length, 'UTF-8');
71
        }
72
73
        $split = preg_split(
74
            "//u",
75
            $input,
76
            -1,
77
            PREG_SPLIT_NO_EMPTY
78
        );
79
80
        return implode('', array_slice($split, $start, $length));
0 ignored issues
show
Bug introduced by
It seems like $split can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        return implode('', array_slice(/** @scrutinizer ignore-type */ $split, $start, $length));
Loading history...
81
    }
82
83
    /**
84
     * @param string $input
85
     *
86
     * @return string
87
     */
88
    protected function upper(string $input): string
89
    {
90
        if (true === function_exists("mb_convert_case")) {
91
            return mb_convert_case($input, MB_CASE_UPPER, "UTF-8");
92
        }
93
94
        return strtoupper(utf8_decode($input));
95
    }
96
}
97