PurifierValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 20
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A purifierMaxLength() 10 10 2
A purifierMinLength() 10 10 2
A _purifyEmpty() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace App\Model\Validation;
3
4
use Cake\Validation\Validator;
5
use HTMLPurifier;
6
use HTMLPurifier_Config;
7
8
class PurifierValidator extends Validator
9
{
10
11
    /**
12
     * The default configuration for the HTMLPurifier_Config.
13
     *
14
     * @var array
15
     */
16
    protected static $_purifierEmptyConfig = [
17
        'Core.Encoding' => 'UTF-8',
18
        'HTML.Allowed' => ''
19
    ];
20
21
    /**
22
     * Check if the length of a HTML string is smaller or equal to a maximal length.
23
     *
24
     * @param string $check The value to check.
25
     * @param int    $max   The maximal string length.
26
     *
27
     * @return bool
28
     */
29 View Code Duplication
    public static function purifierMaxLength($check, $max)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $check = static::_purifyEmpty($check);
32
33
        if ($check === false) {
34
            return false;
35
        }
36
37
        return mb_strlen($check) <= $max;
38
    }
39
40
    /**
41
     * Check if the length of a HTML string is greater or equal to a minimal length.
42
     *
43
     * @param string $check The value to check.
44
     * @param int    $min   The minimal string length.
45
     *
46
     * @return bool
47
     */
48 View Code Duplication
    public static function purifierMinLength($check, $min)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $check = static::_purifyEmpty($check);
51
52
        if ($check === false) {
53
            return false;
54
        }
55
56
        return mb_strlen($check) >= $min;
57
    }
58
59
    /**
60
     * Parse a HTML string with HTMLPurifier to remove all/special HTML tags.
61
     *
62
     * @param bool|string $text The string to be parsed.
63
     *
64
     * @return bool|string
65
     */
66
    protected static function _purifyEmpty($text = false)
67
    {
68
        if ($text === false) {
69
            return false;
70
        }
71
72
        $config = HTMLPurifier_Config::createDefault();
73
        $config->loadArray(static::$_purifierEmptyConfig);
74
75
        return (new HTMLPurifier($config))->purify(trim($text));
76
    }
77
}
78