Completed
Push — ezp26175-exception_on_non_defa... ( 77d2f3...ca5fc8 )
by
unknown
39:33
created

UserMetadata::parse()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 26
rs 8.439
1
<?php
2
3
/**
4
 * File containing the ContentIdCriterion parser 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\Input\Parser\Criterion;
12
13
use eZ\Publish\Core\REST\Common\Input\BaseParser;
14
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
15
use eZ\Publish\Core\REST\Common\Exceptions;
16
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\UserMetadata as UserMetadataCriterion;
17
18
/**
19
 * Parser for ViewInput.
20
 */
21
class UserMetadata extends BaseParser
22
{
23
    /**
24
     * Parses input structure to a Criterion object.
25
     *
26
     * @param array $data
27
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
28
     *
29
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
30
     *
31
     * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\UserMetadata
32
     */
33
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
34
    {
35
        if (!isset($data['UserMetadataCriterion'])) {
36
            throw new Exceptions\Parser('Invalid <UserMetadataCriterion> format');
37
        }
38
39
        if (!isset($data['UserMetadataCriterion']['Target'])) {
40
            throw new Exceptions\Parser('Invalid <Target> format');
41
        }
42
43
        $target = $data['UserMetadataCriterion']['Target'];
44
45
        if (!isset($data['UserMetadataCriterion']['Value'])) {
46
            throw new Exceptions\Parser('Invalid <Value> format');
47
        }
48
49
        if (!in_array(gettype($data['UserMetadataCriterion']['Value']), ['integer', 'string', 'array'])) {
50
            throw new Exceptions\Parser('Invalid <Value> format');
51
        }
52
53
        $value = is_array($data['UserMetadataCriterion']['Value'])
54
            ? $data['UserMetadataCriterion']['Value']
55
            : explode(',', $data['UserMetadataCriterion']['Value']);
56
57
        return new UserMetadataCriterion($target, null, $value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\A...$target, null, $value); (eZ\Publish\API\Repositor...\Criterion\UserMetadata) is incompatible with the return type declared by the abstract method eZ\Publish\Core\REST\Common\Input\Parser::parse of type eZ\Publish\API\Repository\Values\ValueObject.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
58
    }
59
}
60