FieldExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A hasType() 0 8 2
A loadTypes() 0 5 1
A getType() 0 23 3
1
<?php
2
3
namespace Psi\Component\ContentType\Form\Extension;
4
5
use Metadata\MetadataFactoryInterface;
6
use Psi\Component\ContentType\FieldRegistry;
7
use Psi\Component\ContentType\Form\Extension\Type\SurrogateType;
8
use Symfony\Component\Form\AbstractExtension;
9
10
/**
11
 * Form type extension to provide form types for user content-type-managed
12
 * classes in addition to content-type component specific types.
13
 */
14
class FieldExtension extends AbstractExtension
15
{
16
    /**
17
     * @var MetadataFactoryInterface
18
     */
19
    private $metadataFactory;
20
21
    /**
22
     * @var FieldRegistry
23
     */
24
    private $fieldRegistry;
25
26
    public function __construct(
27
        MetadataFactoryInterface $metadataFactory,
28
        FieldRegistry $fieldRegistry
29
    ) {
30
        $this->metadataFactory = $metadataFactory;
31
        $this->fieldRegistry = $fieldRegistry;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function hasType($type)
38
    {
39
        if ($this->metadataFactory->getMetadataForClass($type)) {
40
            return true;
41
        }
42
43
        return parent::hasType($type);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function loadTypes()
50
    {
51
        return [
52
        ];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getType($type)
59
    {
60
        if (parent::hasType($type)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (hasType() instead of getType()). Are you sure this is correct? If so, you might want to change this to $this->hasType().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
61
            return parent::getType($type);
62
        }
63
64
        $metadata = $this->metadataFactory->getMetadataForClass($type);
65
66
        if (!$metadata) {
67
            throw new \InvalidArgumentException(sprintf(
68
                'The type "%s" cannot be loaded by this extension',
69
                $type
70
            ));
71
        }
72
73
        $surrogateType = new SurrogateType(
74
            $type,
75
            $this->fieldRegistry,
76
            $metadata
0 ignored issues
show
Documentation introduced by
$metadata is of type object<Metadata\ClassHie...MergeableClassMetadata>, but the function expects a object<Psi\Component\Con...Metadata\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        );
78
79
        return $surrogateType;
80
    }
81
}
82