Completed
Push — ezp26001-permissions_lookup_re... ( 2e76c0...5b8460 )
by
unknown
25:19
created

ContentFieldValidationException   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 10.1 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 99
rs 10
wmc 9
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B visit() 10 69 6
A translationToString() 0 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
10
11
use eZ\Publish\API\Repository\Values\Translation;
12
use eZ\Publish\Core\REST\Common\Output\Generator;
13
use eZ\Publish\Core\REST\Common\Output\Visitor;
14
15
/**
16
 * ContentFieldValidationException value object visitor.
17
 */
18
class ContentFieldValidationException extends BadRequestException
19
{
20
    /**
21
     * Visit struct returned by controllers.
22
     *
23
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
24
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
25
     * @param \eZ\Publish\Core\REST\Server\Exceptions\ContentFieldValidationException $data
26
     */
27
    public function visit(Visitor $visitor, Generator $generator, $data)
28
    {
29
        $generator->startObjectElement('ErrorMessage');
30
31
        $statusCode = $this->getStatus();
32
        $visitor->setStatus($statusCode);
33
        $visitor->setHeader('Content-Type', $generator->getMediaType('ErrorMessage'));
34
35
        $generator->startValueElement('errorCode', $statusCode);
36
        $generator->endValueElement('errorCode');
37
38
        $generator->startValueElement('errorMessage', $this->httpStatusCodes[$statusCode]);
39
        $generator->endValueElement('errorMessage');
40
41
        $generator->startValueElement('errorDescription', $data->getMessage());
42
        $generator->endValueElement('errorDescription');
43
44
        $generator->startHashElement('errorDetails');
45
        $generator->startList('fields');
46
        foreach ($data->getFieldErrors() as $fieldTypeId => $translations) {
47
            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...
48
                if (!is_array($validationErrors)) {
49
                    $validationErrors = [$validationErrors];
50
                }
51
52
                foreach ($validationErrors as $validationError) {
53
                    $generator->startHashElement('field');
54
                    $generator->startAttribute('fieldTypeId', $fieldTypeId);
55
                    $generator->endAttribute('fieldTypeId');
56
57
                    $generator->startList('errors');
58
                    $generator->startHashElement('error');
59
60
                    $generator->startValueElement('type', $validationError->getTarget());
61
                    $generator->endValueElement('type');
62
63
                    $translation = $validationError->getTranslatableMessage();
64
                    $generator->startValueElement(
65
                        'message',
66
                        $this->translator->trans(
67
                            $this->translationToString($translation),
68
                            $translation->values,
69
                            'repository_exceptions'
70
                        )
71
                    );
72
                    $generator->endValueElement('message');
73
74
                    $generator->endHashElement('error');
75
                    $generator->endList('errors');
76
                    $generator->endHashElement('field');
77
                }
78
            }
79
        }
80
        $generator->endList('fields');
81
        $generator->endHashElement('errorDetails');
82
83 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...
84
            $generator->startValueElement('trace', $data->getTraceAsString());
85
            $generator->endValueElement('trace');
86
87
            $generator->startValueElement('file', $data->getFile());
88
            $generator->endValueElement('file');
89
90
            $generator->startValueElement('line', $data->getLine());
91
            $generator->endValueElement('line');
92
        }
93
94
        $generator->endObjectElement('ErrorMessage');
95
    }
96
97
    /**
98
     * Convert a Translation object to a string, detecting singular/plural as needed.
99
     *
100
     * @param Translation $translation The Translation object
101
     * @return string
102
     */
103
    private function translationToString(Translation $translation)
104
    {
105
        $values = $translation->values;
0 ignored issues
show
Documentation introduced by
The property values does not exist on object<eZ\Publish\API\Re...ory\Values\Translation>. Since you implemented __set, maybe consider adding a @property annotation.

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...
106
        if ($translation instanceof Translation\Plural) {
107
            if (current($values) === 1) {
108
                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...
109
            } else {
110
                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...
111
            }
112
        } else {
113
            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...
114
        }
115
    }
116
}
117