Completed
Push — master ( 0b53cc...a259cb )
by
unknown
26:05
created

Field   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 19 5
A getOperator() 0 14 2
1
<?php
2
3
/**
4
 * File containing the Field Criterion 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
namespace eZ\Publish\Core\REST\Server\Input\Parser\Criterion;
10
11
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Field as FieldCriterion;
12
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eZ\Publish\Core\REST\Ser...rser\Criterion\Operator.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
17
/**
18
 * Parser for Field Criterion.
19
 */
20
class Field extends BaseParser
21
{
22
    const OPERATORS = [
23
        'IN' => Operator::IN,
24
        'EQ' => Operator::EQ,
25
        'GT' => Operator::GT,
26
        'GTE' => Operator::GTE,
27
        'LT' => Operator::LT,
28
        'LTE' => Operator::LTE,
29
        'LIKE' => Operator::LIKE,
30
        'BETWEEN' => Operator::BETWEEN,
31
        'CONTAINS' => Operator::CONTAINS,
32
    ];
33
34
    /**
35
     * Parses input structure to a Criterion object.
36
     *
37
     * @param array $data
38
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
39
     *
40
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
41
     *
42
     * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\Field
43
     */
44
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
45
    {
46
        if (!array_key_exists('Field', $data)) {
47
            throw new Exceptions\Parser('Invalid <Field> format');
48
        }
49
50
        $fieldData = $data['Field'];
51
        if (empty($fieldData['name']) || empty($fieldData['operator']) || !array_key_exists('value', $fieldData)) {
52
            throw new Exceptions\Parser('<Field> format expects name, operator and value keys');
53
        }
54
55
        $operator = $this->getOperator($fieldData['operator']);
56
57
        return new FieldCriterion(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\A..., $fieldData['value']); (eZ\Publish\API\Repositor...t\Query\Criterion\Field) 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
            $fieldData['name'],
59
            $operator,
60
            $fieldData['value']
61
        );
62
    }
63
64
    /**
65
     * Get operator for the given literal name.
66
     *
67
     * For the full list of supported operators:
68
     * @see \eZ\Publish\Core\REST\Server\Input\Parser\Criterion\Field::OPERATORS
69
     *
70
     * @param string $operatorName operator literal operator name
71
     *
72
     * @return string
73
     */
74
    private function getOperator($operatorName)
75
    {
76
        $operatorName = strtoupper($operatorName);
77
        if (!isset(self::OPERATORS[$operatorName])) {
78
            throw new Exceptions\Parser(
79
                sprintf(
80
                    'Unexpected Field operator, expected one of the following: %s',
81
                    implode(', ', array_keys(self::OPERATORS))
82
                )
83
            );
84
        }
85
86
        return self::OPERATORS[$operatorName];
87
    }
88
}
89