Completed
Push — master ( 0c3733...9b1319 )
by Vladimir
02:41
created

JailObject   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
dl 8
loc 76
ccs 23
cts 26
cp 0.8846
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
B __call() 8 29 6
A __get() 0 9 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
/**
4
 * @copyright 2016 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Object;
9
10
/**
11
 * Class JailObject
12
 *
13
 * A wrapper object to only allow certain functions on the white list to be called. This is used in order to limit which
14
 * functions a user can call from Twig templates to prevent unexpected behavior.
15
 *
16
 * @package allejo\stakx\Object
17
 */
18
class JailObject
19
{
20
    /**
21
     * @var string[]
22
     */
23
    private $whiteListFunctions;
24
25
    /**
26
     * @var string[]
27
     */
28
    private $jailedFunctions;
29
30
    /**
31
     * @var Jailable
32
     */
33
    private $object;
34
35
    /**
36
     * JailObject constructor.
37
     *
38
     * @param Jailable $object             The object that will be jailed
39
     * @param array    $whiteListFunctions A list of function names that can be called
40
     * @param array    $jailedFunctions
41
     */
42 4
    public function __construct (&$object, array $whiteListFunctions, array $jailedFunctions = array())
43
    {
44 4
        if (!($object instanceof Jailable))
45 4
        {
46
            throw new \InvalidArgumentException('Must implement Jailable interface');
47
        }
48
49 4
        $this->object = &$object;
50 4
        $this->whiteListFunctions = $whiteListFunctions;
51 4
        $this->jailedFunctions = $jailedFunctions;
52 4
    }
53
54 3
    public function __call ($name, $arguments)
55
    {
56
        // White listed functions will always be getter functions, so somehow get the name of a possible getter function
57
        // name.
58 3
        $lcName = lcfirst($name);
59 3
        $getFxnCall = ($lcName[0] === 'g' && strpos($lcName, 'get') === 0) ? $lcName : sprintf('get%s', ucfirst($name));
60
61
        // Check if our function call is a jailed call, meaning the function should be mapped to special "jailed"
62
        // jailed version of the function call.
63 3 View Code Duplication
        if (in_array($getFxnCall, $this->jailedFunctions))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64 3
        {
65
            return call_user_func_array(array($this->object, $this->jailedFunctions[$getFxnCall]), $arguments);
66
        }
67
68
        // Otherwise, test to see if the function call is in our white list and call it
69 3 View Code Duplication
        if (in_array($getFxnCall, $this->whiteListFunctions))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70 3
        {
71 2
            return call_user_func_array(array($this->object, $getFxnCall), $arguments);
72
        }
73
74
        // Functions should take precedence over __get(), so if it's not a function we'll search to see if the __get()
75
        // handles it
76 2
        if ($this->object->isMagicGet($name))
77 2
        {
78 1
            return $this->object->$name;
79
        }
80
81 1
        throw new \BadMethodCallException();
82
    }
83
84 1
    public function __get($name)
85
    {
86 1
        if ($this->object->isMagicGet($name))
87 1
        {
88 1
            return $this->object->$name;
89
        }
90
91
        return NULL;
92
    }
93
}