DefaultFormCreator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
A create() 0 7 1
A extractPropertyName() 0 4 1
A hasRelatedSetter() 0 4 1
A __construct() 0 6 2
C preSetData() 0 28 7
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Knp\RadBundle\Form;
4
5
use Symfony\Component\Form\FormFactoryInterface;
6
use Symfony\Component\Form\FormEvent;;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\Form\FormEvents;
9
use Knp\RadBundle\Reflection\ClassMetadataFetcher;
10
use Doctrine\Common\Inflector\Inflector;
11
12
class DefaultFormCreator implements FormCreatorInterface, EventSubscriberInterface
13
{
14
    private $fetcher;
15
    private $factory;
16
    private $dataTypeGuesser;
17
18
    public function __construct(ClassMetadataFetcher $fetcher = null, FormFactoryInterface $factory, DataTypeGuesser $dataTypeGuesser)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
19
    {
20
        $this->fetcher = $fetcher ?: new ClassMetadataFetcher;
21
        $this->factory = $factory;
22
        $this->dataTypeGuesser = $dataTypeGuesser;
23
    }
24
25
    public static function getSubscribedEvents()
26
    {
27
        return array(
28
            FormEvents::PRE_SET_DATA => 'preSetData'
29
        );
30
    }
31
32
    public function create($object, $purpose = null, array $options = array())
33
    {
34
        $builder = $this->factory->createBuilder('form', $object, $options);
35
        $builder->addEventSubscriber($this);
36
37
        return $builder->getForm();
38
    }
39
40
    public function preSetData(FormEvent $event)
41
    {
42
        $object = $event->getData();
43
        $form = $event->getForm();
44
45
        if (!is_object($object)) {
46
            return;
47
        }
48
49
        $this->dataTypeGuesser->setData($object);
50
51
        foreach ($this->fetcher->getMethods($object) as $method) {
52
            if (0 === strpos($method, 'get') || 0 === strpos($method, 'is')) {
53
                $propertyName = $this->extractPropertyName($method);
54
                if ($this->hasRelatedSetter($object, $propertyName)) {
55
                    $form->add($this->factory->createForProperty(get_class($object), $propertyName, null, array(
56
                        'auto_initialize' => false,
57
                    )));
58
                }
59
            }
60
        }
61
62
        foreach ($this->fetcher->getProperties($object) as $property) {
63
            $form->add($this->factory->createForProperty(get_class($object), $property, null, array(
64
                'auto_initialize' => false,
65
            )));
66
        }
67
    }
68
69
    private function extractPropertyName($methodName)
70
    {
71
        return lcfirst(preg_replace('#is|get#', '', $methodName));
72
    }
73
74
    private function hasRelatedSetter($object, $propertyName)
75
    {
76
        return $this->fetcher->hasMethod($object, 'set'.Inflector::classify($propertyName));
77
    }
78
}
79