GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UpdateContentMapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mapToForm() 0 31 2
A mapFromForm() 0 29 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\Bundle\EzFormsBundle\Form\DataMapper;
6
7
use Netgen\Bundle\EzFormsBundle\Form\DataMapper;
8
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper;
9
use RuntimeException;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\PropertyAccess\PropertyPathInterface;
12
13
/**
14
 * A data mapper using property paths to read/write data.
15
 */
16
final class UpdateContentMapper extends DataMapper
17
{
18
    protected function mapToForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void
19
    {
20
        /** @var \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct */
21
        $contentUpdateStruct = $data->payload;
22
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
23
        $content = $data->target;
24
        /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */
25
        $contentType = $data->definition;
26
27
        $fieldDefinitionIdentifier = (string) $propertyPath;
28
        $fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier);
29
30
        if (null === $fieldDefinition) {
31
            throw new RuntimeException(
32
                "Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'"
33
            );
34
        }
35
36
        $fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier;
37
38
        $handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier);
39
        $form->setData(
40
            $handler->convertFieldValueToForm(
41
                $content->getFieldValue(
0 ignored issues
show
Bug introduced by
It seems like $content->getFieldValue(...t->initialLanguageCode) can be null; however, convertFieldValueToForm() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
                    $fieldDefinitionIdentifier,
43
                    $contentUpdateStruct->initialLanguageCode
44
                ),
45
                $fieldDefinition
46
            )
47
        );
48
    }
49
50
    protected function mapFromForm(FormInterface $form, DataWrapper $data, PropertyPathInterface $propertyPath): void
51
    {
52
        /** @var \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct */
53
        $contentUpdateStruct = $data->payload;
54
        /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */
55
        $contentType = $data->definition;
56
57
        $fieldDefinitionIdentifier = (string) $propertyPath;
58
        $fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier);
59
60
        if (null === $fieldDefinition) {
61
            throw new RuntimeException(
62
                "Data payload does not contain expected FieldDefinition '{$fieldDefinitionIdentifier}'"
63
            );
64
        }
65
66
        $fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier;
67
        $formData = $form->getData();
68
69
        // Set field to struct only if it is not empty or it has not been marked
70
        // to skip update if empty
71
        if (!$this->shouldSkipForEmptyUpdate($form, $formData, $fieldDefinitionIdentifier)) {
72
            $handler = $this->fieldTypeHandlerRegistry->get($fieldTypeIdentifier);
73
            $contentUpdateStruct->setField(
74
                $fieldDefinitionIdentifier,
75
                $handler->convertFieldValueFromForm($form->getData())
76
            );
77
        }
78
    }
79
}
80