Completed
Pull Request — master (#36)
by Daniel
04:59
created

ObjectType   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B createView() 0 29 3
A configureOptions() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType\Standard\View;
6
7
use Metadata\MetadataFactory;
8
use Metadata\NullMetadata;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Psi\Component\ContentType\View\TypeInterface;
11
use Psi\Component\ContentType\View\ViewFactory;
12
use Psi\Component\ContentType\View\ViewInterface;
13
use Psi\Component\ContentType\FieldLoader;
14
15
class ObjectType implements TypeInterface
16
{
17
    private $metadataFactory;
18
    private $fieldLoader;
19
20
    public function __construct(
21
        MetadataFactory $metadataFactory,
22
        FieldLoader $fieldLoader
23
    ) {
24
        $this->metadataFactory = $metadataFactory;
25
        $this->fieldLoader = $fieldLoader;
26
    }
27
28
    public function createView(ViewFactory $factory, $data, array $options): ViewInterface
29
    {
30
        $classFqn = get_class($data);
31
        $metadata = $this->metadataFactory->getMetadataForClass($classFqn);
32
33
        if ($metadata instanceof NullMetadata) {
34
            throw new \RuntimeException(sprintf(
35
                'Class "%s" is not mapped',
36
                $classFqn
37
            ));
38
        }
39
40
        foreach ($metadata->getPropertyMetadata() as $propertyMetadata) {
41
            $map[$propertyMetadata->getName()] = function () use ($propertyMetadata, $factory, $data) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
$map was never initialized. Although not strictly required by PHP, it is generally a good practice to add $map = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
42
                $field = $this->fieldLoader->load(
43
                    $propertyMetadata->getType(),
44
                    $propertyMetadata->getOptions()
45
                );
46
47
                return $factory->create(
48
                    $field->getInnerField()->getViewType(),
49
                    $propertyMetadata->getValue($data),
50
                    $field->getViewOptions()
51
                );
52
            };
53
        }
54
55
        return new ObjectView($options['template'], $map);
0 ignored issues
show
Bug introduced by
The variable $map does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
56
    }
57
58
    public function configureOptions(OptionsResolver $options)
59
    {
60
        $options->setDefault('template', 'psi/object');
61
    }
62
}
63