Completed
Push — master ( b6d955...929034 )
by Marcus
02:03
created

Regex   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 27.03 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 10
loc 37
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 10 10 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
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
class Regex implements SanitizeRuleInterface
6
{
7
    /** @var string  */
8
    protected $expr;
9
10
    /** @var string */
11
    protected $replace;
12
13
    /**
14
     * @param string $expr The regular expression pattern to apply.
15
     * @param string $replace Value to replace the found pattern with
16
     */
17 6
    public function __construct(string $expr, string $replace)
18
    {
19 6
        $this->expr = $expr;
20 6
        $this->replace =$replace;
21 6
    }
22
23
    /**
24
     * Applies `preg_replace()` to the value.
25
     *
26
     * @param object $subject The subject to be filtered.
27
     * @param string $field The subject field name.
28
     *
29
     * @return bool True if the value was sanitized, false if not.
30
     */
31 6 View Code Duplication
    public function __invoke($subject, string $field): bool
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...
32
    {
33 6
        $value = $subject->$field;
34 6
        if (!is_scalar($value)) {
35 3
            return false;
36
        }
37 3
        $subject->$field = preg_replace($this->expr, $this->replace, $value);
38
39 3
        return true;
40
    }
41
}
42