Validator::__call()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace Almendra\Validators;
4
5
use Almendra\Validators\Interfaces\ValidatorInterface;
6
use Almendra\Validators\Traits\ArrayTrait;
7
8
/**
9
 * The validator.
10
 */
11
class Validator implements ValidatorInterface
12
{
13
    use ArrayTrait;
14
15
    public function getSupportedTypes()
16
    {
17
        return $this->register();
18
    }
19
20
    public function register()
21
    {
22
        return [
23
            \Almendra\Validators\Types\IntegerType::class => 'integer',
24
            \Almendra\Validators\Types\StringType::class => 'string',
25
            \Almendra\Validators\Types\FloatType::class => 'float',
26
            \Almendra\Validators\Types\DoubleType::class => 'double',
27
            \Almendra\Validators\Types\FileType::class => 'file',
28
        ];
29
    }
30
31
    public function __call($name, $values)
32
    {
33
        foreach ($this->register() as $type => $alias) {
34
            if ($alias == $name) {
35
                // call that method
36
                return $this->type = new $type(
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
                    $this->toArray($values)
38
                    );
39
            }
40
        }
41
42
        throw new \Exception('The called method is not defined or unsupported');
43
    }
44
}
45