Completed
Push — ezp24853-enhance_rest_when_fai... ( 02baca...8760d4 )
by
unknown
24:08
created

ContentFieldValidationException::visit()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 53
Code Lines 34

Duplication

Lines 10
Ratio 18.87 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 34
c 3
b 0
f 0
nc 12
nop 3
dl 10
loc 53
rs 8.7155

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the ContentFieldValidationException ValueObjectVisitor class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
12
13
use eZ\Publish\API\Repository\Values\Translation;
14
use eZ\Publish\Core\REST\Common\Output\Generator;
15
use eZ\Publish\Core\REST\Common\Output\Visitor;
16
17
/**
18
 * ContentFieldValidationException value object visitor.
19
 */
20
class ContentFieldValidationException extends BadRequestException
21
{
22
    /**
23
     * Visit struct returned by controllers.
24
     *
25
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
26
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
27
     * @param \eZ\Publish\Core\REST\Server\Exceptions\ContentFieldValidationException $data
28
     */
29
    public function visit(Visitor $visitor, Generator $generator, $data)
30
    {
31
        $generator->startObjectElement('ErrorMessage');
32
33
        $statusCode = $this->getStatus();
34
        $visitor->setStatus($statusCode);
35
        $visitor->setHeader('Content-Type', $generator->getMediaType('ErrorMessage'));
36
37
        $generator->startValueElement('errorCode', $statusCode);
38
        $generator->endValueElement('errorCode');
39
40
        $generator->startValueElement('errorMessage', $this->httpStatusCodes[$statusCode]);
41
        $generator->endValueElement('errorMessage');
42
43
        $generator->startValueElement('errorDescription', $data->getMessage());
44
        $generator->endValueElement('errorDescription');
45
46
        $errorDetails = ['fields' => []];
47
        foreach ($data->getFieldErrors() as $fieldTypeId => $translations) {
48
            foreach ($translations as $languageCode => $validationErrors) {
0 ignored issues
show
Bug introduced by
The expression $translations of type object<eZ\Publish\Core\FieldType\ValidationError> is not traversable.
Loading history...
49
                if (!is_array($validationErrors)) {
50
                    $validationErrors = [$validationErrors];
51
                }
52
53
                foreach ($validationErrors as $validationError) {
54
                    $translation = $validationError->getTranslatableMessage();
55
                    $errorDetails['fields'][$fieldTypeId][] = [
56
                        'type' => $validationError->getTarget(),
57
                        'message' => $this->translator->trans(
58
                            $this->translationToString($translation),
59
                            $translation->values,
60
                            'repository_exceptions'
61
                        ),
62
                    ];
63
                }
64
            }
65
        }
66
        $generator->startValueElement('errorDetails', $errorDetails);
0 ignored issues
show
Documentation introduced by
$errorDetails is of type array<string,array,{"fields":"array"}>, but the function expects a string.

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...
67
        $generator->endValueElement('errorDetails');
68
69 View Code Duplication
        if ($this->debug) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
            $generator->startValueElement('trace', $data->getTraceAsString());
71
            $generator->endValueElement('trace');
72
73
            $generator->startValueElement('file', $data->getFile());
74
            $generator->endValueElement('file');
75
76
            $generator->startValueElement('line', $data->getLine());
77
            $generator->endValueElement('line');
78
        }
79
80
        $generator->endObjectElement('ErrorMessage');
81
    }
82
83
    /**
84
     * Convert a Translation object to a string, detecting singular/plural as needed.
85
     *
86
     * @param Translation $translation The Translation object
87
     * @return string
88
     */
89
    private function translationToString(Translation $translation)
90
    {
91
        if ($translation instanceof Translation\Plural) {
92
            if (current($translation->values) === 1) {
0 ignored issues
show
Documentation introduced by
The property $values is declared protected in eZ\Publish\API\Repositor...lues\Translation\Plural. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
                return $translation->singular;
0 ignored issues
show
Documentation introduced by
The property $singular is declared protected in eZ\Publish\API\Repositor...lues\Translation\Plural. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
            } else {
95
                return $translation->plural;
0 ignored issues
show
Documentation introduced by
The property $plural is declared protected in eZ\Publish\API\Repositor...lues\Translation\Plural. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
            }
97
        } else {
98
            return $translation->message;
0 ignored issues
show
Documentation introduced by
The property message does not exist on object<eZ\Publish\API\Re...ory\Values\Translation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
        }
100
    }
101
}
102