Completed
Pull Request — master (#596)
by
unknown
14:21
created

ShowBuilder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 9
Bugs 3 Features 1
Metric Value
wmc 16
c 9
b 3
f 1
lcom 1
cbo 4
dl 0
loc 108
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getBaseList() 0 4 1
A addField() 0 14 2
D fixFieldDescription() 0 42 10
A getTemplate() 0 8 2
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\DoctrineORMAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\AdminInterface;
15
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
16
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
17
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
18
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
19
20
class ShowBuilder implements ShowBuilderInterface
21
{
22
    /**
23
     * @var TypeGuesserInterface
24
     */
25
    protected $guesser;
26
27
    /**
28
     * @var string[]
29
     */
30
    protected $templates;
31
32
    /**
33
     * @param TypeGuesserInterface $guesser
34
     * @param string[]             $templates
35
     */
36
    public function __construct(TypeGuesserInterface $guesser, array $templates)
37
    {
38
        $this->guesser = $guesser;
39
        $this->templates = $templates;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getBaseList(array $options = array())
46
    {
47
        return new FieldDescriptionCollection();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
54
    {
55
        if ($type == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $type of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
56
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
57
            $fieldDescription->setType($guessType->getType());
58
        } else {
59
            $fieldDescription->setType($type);
60
        }
61
62
        $this->fixFieldDescription($admin, $fieldDescription);
63
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
64
65
        $list->add($fieldDescription);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
72
    {
73
        $fieldDescription->setAdmin($admin);
74
75
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
76
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
77
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
78
79
            // set the default field mapping
80
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
81
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
82
            }
83
84
            // set the default association mapping
85
            if (isset($metadata->associationMappings[$lastPropertyName])) {
86
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
87
            }
88
        }
89
90
        if (!$fieldDescription->getType()) {
91
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
92
        }
93
94
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
95
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
96
97
        if (!$fieldDescription->getTemplate()) {
98
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
99
100
            if (!$fieldDescription->getTemplate()) {
101
                if ($fieldDescription->describesSingleValuedAssociation()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\AdminBundle\Admin\FieldDescriptionInterface as the method describesSingleValuedAssociation() does only exist in the following implementations of said interface: Sonata\DoctrineORMAdminB...\Admin\FieldDescription.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
102
                    $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_single.html.twig');
103
                } elseif ($fieldDescription->describesCollectionValuedAssociation()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\AdminBundle\Admin\FieldDescriptionInterface as the method describesCollectionValuedAssociation() does only exist in the following implementations of said interface: Sonata\DoctrineORMAdminB...\Admin\FieldDescription.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
104
                    $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_collection.html.twig');
105
                }
106
            }
107
        }
108
109
        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...
110
            $admin->attachAdminClass($fieldDescription);
111
        }
112
    }
113
114
    /**
115
     * @param string $type
116
     *
117
     * @return string
118
     */
119
    private function getTemplate($type)
120
    {
121
        if (!isset($this->templates[$type])) {
122
            return;
123
        }
124
125
        return $this->templates[$type];
126
    }
127
}
128