Completed
Pull Request — master (#395)
by
unknown
02:26
created

FormContractor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\AdminInterface;
15
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
16
use Sonata\AdminBundle\Builder\AbstractFormContractor;
17
18
class FormContractor extends AbstractFormContractor
19
{
20
    /**
21
     * The method defines the correct default settings for the provided FieldDescription.
22
     *
23
     * {@inheritdoc}
24
     *
25
     * @throws \RuntimeException if the $fieldDescription does not specify a type.
26
     */
27
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
28
    {
29
        $metadata = null;
0 ignored issues
show
Unused Code introduced by
$metadata is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
31
            /** @var \Doctrine\ODM\PHPCR\Mapping\ClassMetadata $metadata */
32
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
33
34
            // set the default field mapping
35
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
36
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
37
            }
38
39
            // set the default association mapping
40
            if ($metadata->hasAssociation($fieldDescription->getName())) {
41
                $fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName()));
42
            }
43
        }
44
45
        if (!$fieldDescription->getType()) {
46
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
47
        }
48
49
        $fieldDescription->setAdmin($admin);
50
        $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
51
52
        if ($fieldDescription->describesAssociation()) {
0 ignored issues
show
Bug introduced by
The method describesAssociation() does not seem to exist on object<Sonata\AdminBundl...ldDescriptionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            $admin->attachAdminClass($fieldDescription);
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
61
    {
62
        $options = parent::getDefaultOptions($type, $fieldDescription);
63
64
        switch ($type) {
65
            case 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType':
66
            case 'doctrine_phpcr_odm_tree':
67
                $options['class'] = $fieldDescription->getTargetEntity();
68
                $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
69
                break;
70
        }
71
72
        return $options;
73
    }
74
}
75