InputSanitizer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.13%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 81
ccs 25
cts 32
cp 0.7813
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getInputSanitizer() 0 4 1
A instance() 0 8 2
A __callStatic() 0 17 4
1
<?php
2
3
/**
4
 * Part of the InputSanitizer package.
5
 *
6
 * @package        InputSanitizer
7
 * @version        1.0.2
8
 * @author         Arthur Lorent <[email protected]>, Daniel Lucas <[email protected]>
9
 * @license        MIT
10
 * @copyright  (c) 2006-2017, ACID-Solutions SARL
11
 * @link           https://acid.fr
12
 */
13
14
namespace AcidSolutions\InputSanitizer\Native\Facades;
15
16
use AcidSolutions\InputSanitizer\Native\InputSanitizerBootstrapper;
17
18
class InputSanitizer
19
{
20
    /**
21
     * The InputSanitizer instance.
22
     *
23
     * @var \AcidSolutions\InputSanitizer\InputSanitizer
24
     */
25
    protected $inputSanitizer;
26
    /**
27
     * The Native BootStrapper instance.
28
     *
29
     * @var InputSanitizerBootstrapper
30
     */
31
    protected static $instance;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param InputSanitizerBootstrapper $bootstrapper
37
     */
38 8
    public function __construct(InputSanitizerBootstrapper $bootstrapper = null)
39
    {
40 8
        if ($bootstrapper === null) {
41 6
            $bootstrapper = new InputSanitizerBootstrapper;
42 6
        }
43
44 8
        $this->inputSanitizer = $bootstrapper->createInputSanitizer();
45 8
    }
46
47
    /**
48
     * Returns the InputSanitizer instance.
49
     *
50
     * @return \AcidSolutions\InputSanitizer\InputSanitizer
51
     */
52 6
    public function getInputSanitizer()
53
    {
54 6
        return $this->inputSanitizer;
55
    }
56
57
    /**
58
     * Creates a new Native Bootstrapper instance.
59
     *
60
     * @param InputSanitizerBootstrapper $bootstrapper
61
     *
62
     * @return void
63
     */
64 4
    public static function instance(InputSanitizerBootstrapper $bootstrapper = null)
65
    {
66 4
        if (static::$instance === null) {
67 2
            static::$instance = new static($bootstrapper);
0 ignored issues
show
Documentation Bug introduced by
It seems like new static($bootstrapper) of type this<AcidSolutions\Input...Facades\InputSanitizer> is incompatible with the declared type object<AcidSolutions\Inp...tSanitizerBootstrapper> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68 2
        }
69
70 4
        return static::$instance;
71
    }
72
73
    /**
74
     * Handle dynamic, static calls to the object.
75
     *
76
     * @param string $method
77
     * @param array  $args
78
     *
79
     * @return mixed
80
     */
81 2
    public static function __callStatic($method, $args)
82
    {
83 2
        $instance = static::instance()->getInputSanitizer();
0 ignored issues
show
Bug introduced by
The method getInputSanitizer does only exist in AcidSolutions\InputSanit...\Facades\InputSanitizer, but not in AcidSolutions\InputSanit...utSanitizerBootstrapper.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84 2
        switch (count($args)) {
85 2
            case 1:
86 2
                return $instance->{$method}($args[0]);
87
88 2
            case 2:
89 2
                return $instance->{$method}($args[0], $args[1]);
90
91 2
            case 3:
92 2
                return $instance->{$method}($args[0], $args[1], $args[2]);
93
94 2
            default:
95 2
                return call_user_func_array([$instance, $method], $args);
96 2
        }
97
    }
98
}
99