RequestObfuscator::dataProviderObfuscate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 9.0675
c 0
b 0
f 0
cc 1
eloc 46
nc 1
nop 0

How to fix   Long Method   

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 Rezzza\SecurityBundle\Tests\Units\Request\Obfuscator;
4
5
use mageekguy\atoum;
6
use Rezzza\SecurityBundle\Request\Obfuscator\RequestObfuscator as TestedClass;
7
8
/**
9
 * RequestObfuscator
10
 *
11
 * @uses atoum\test
12
 * @author Stephane PY <[email protected]>
13
 */
14
class RequestObfuscator extends atoum\test
15
{
16
    public function dataProviderObfuscate()
17
    {
18
        return array(
19
            // string on data.
20
            //@ObfuscateRequest(content="*")
21
            array(
22
                array('content' => 'foo'),
23
                array('content' => '*'),
24
                array('content' => 'XXX')
25
            ),
26
            //@ObfuscateRequest(content=".")
27
            array(
28
                array('content' => 'foobar'),
29
                array('content' => '.'),
30
                array('content' => 'XXXXXX')
31
            ),
32
            //@ObfuscateRequest()
33
            array(
34
                array('content' => 'foobar'),
35
                array(),
36
                array('content' => 'foobar')
37
            ),
38
            //@ObfuscateRequest(otherkey="*")
39
            array(
40
                array('content' => 'foobar'),
41
                array('otherkey' => '*'),
42
                array('content' => 'foobar')
43
            ),
44
            // array on data, simple patterns
45
            //@ObfuscateRequest(content="*")
46
            array(
47
                array('content' => array('key1' => 'foo', 'key2' => 'bar')),
48
                array('content' => '*'),
49
                array('content' => 'X')
50
            ),
51
            //@ObfuscateRequest(content="key1")
52
            array(
53
                array('content' => array('key1' => 'foo', 'key2' => 'bar')),
54
                array('content' => 'key1'),
55
                array('content' => array('key1' => 'XXX', 'key2' => 'bar'))
56
            ),
57
            //@ObfuscateRequest(content={"key1"})
58
            array(
59
                array('content' => array('key1' => array('foo' => 'pouet'), 'key2' => 'bar')),
60
                array('content' => array('key1')),
61
                array('content' => array('key1' => 'X', 'key2' => 'bar'))
62
            ),
63
            //@ObfuscateRequest(content={"key1", "key2"})
64
            array(
65
                array('content' => array('key1' => array('foo' => 'pouet'), 'key2' => 'bar')),
66
                array('content' => array('key1', 'key2')),
67
                array('content' => array('key1' => 'X', 'key2' => 'XXX'))
68
            ),
69
            //@ObfuscateRequest(content={"key1[key2]"})
70
            array(
71
                array('content' => array('key1' => array('key2' => 'pouet'), 'key2' => 'bar')),
72
                array('content' => array('key1[key2]')),
73
                array('content' => array('key1' => array('key2' => 'XXXXX'), 'key2' => 'bar'))
74
            ),
75
            //@ObfuscateRequest(content={"key1[key2][key3]"})
76
            array(
77
                array('content' => array('key1' => array('key2' => array('key3' => 'foo')))),
78
                array('content' => array('key1[key2][key3]')),
79
                array('content' => array('key1' => array('key2' => array('key3' => 'XXX')))),
80
            ),
81
            //@ObfuscateRequest(content={"key1[*]"})
82
            array(
83
                array('content' => array('key1' => array('key2' => array('key3' => 'foo')))),
84
                array('content' => array('key1[*]')),
85
                array('content' => array('key1' => 'X')),
86
            ),
87
        );
88
    }
89
90
    /**
91
     * @dataProvider dataProviderObfuscate
92
     */
93
    public function testObfuscate(array $data, array $patterns, array $expectedData)
94
    {
95
        $this->if($obfuscator = new TestedClass())
0 ignored issues
show
Documentation Bug introduced by
The method if does not exist on object<Rezzza\SecurityBu...ator\RequestObfuscator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
96
            ->array($obfuscator->obfuscate($data, $patterns))
97
            ->isIdenticalTo($expectedData);
98
    }
99
}
100