DataTypeGuesser   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 65
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
C getType() 0 23 7
A setData() 0 4 1
A guessType() 0 15 3
A guessRequired() 0 3 1
A guessPattern() 0 3 1
A guessMaxLength() 0 3 1
A guessMinLength() 0 3 1
1
<?php
2
3
namespace Knp\RadBundle\Form;
4
5
use Symfony\Component\Form\FormTypeGuesserInterface;
6
use Symfony\Component\Form\Guess\TypeGuess;
7
use Symfony\Component\Form\Guess\Guess;
8
9
class DataTypeGuesser implements FormTypeGuesserInterface
10
{
11
    private $data;
12
13
    public function setData($data = null)
14
    {
15
        $this->data = $data;
16
    }
17
18
    public function guessType($class, $property)
19
    {
20
        if ($property === '_id') {
21
            return new TypeGuess('hidden', array(), Guess::LOW_CONFIDENCE);
22
        }
23
24
        if (!isset($this->data->$property)) {
25
            return;
26
        }
27
        $data = $this->data->$property;
28
29
        $type = $this->getType($data);
30
31
        return new TypeGuess($type, array(), Guess::LOW_CONFIDENCE);
32
    }
33
34
    public function guessRequired($class, $property)
35
    {
36
    }
37
38
    public function guessPattern($class, $property)
39
    {
40
    }
41
42
    public function guessMaxLength($class, $property)
43
    {
44
    }
45
46
    public function guessMinLength($class, $property)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
    }
49
50
    private function getType($value)
51
    {
52
        if (is_object($value)) {
53
            switch (true) {
54
                case $value instanceof \DateTime:
55
                    return 'date';
56
                case $value instanceof \ArrayIterator:
57
                    return 'collection';
58
                default:
59
                    return 'text';
60
            }
61
        }
62
63
        switch (gettype($value)) {
64
            case 'boolean':
65
                return 'checkbox';
66
            case 'array':
67
                return 'collection';
68
            case 'string':
69
            default:
70
                return 'text';
71
        }
72
    }
73
}
74