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

LogicalOr   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 30
loc 30
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 17 17 6

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 LogicalOr 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\Core\REST\Server\Input\Parser\Criterion as CriterionParser;
12
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
13
use eZ\Publish\Core\REST\Common\Exceptions;
14
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOr as LogicalOrCriterion;
15
16
/**
17
 * Parser for LogicalOr Criterion.
18
 */
19 View Code Duplication
class LogicalOr extends CriterionParser
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
20
{
21
    /**
22
     * Parses input structure to a LogicalAnd Criterion object.
23
     *
24
     * @param array $data
25
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
26
     *
27
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
28
     *
29
     * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOr
30
     */
31
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
32
    {
33
        if (!array_key_exists('OR', $data) && !is_array($data['OR'])) {
34
            throw new Exceptions\Parser('Invalid <OR> format');
35
        }
36
37
        $criteria = array();
38
        foreach ($data['OR'] as $criterionName => $criterionData) {
39
            if (is_array($criterionData) && !array_key_exists(0, $criterionData)) {
40
                $criterionName = key($criterionData);
41
                $criterionData = current($criterionData);
42
            }
43
            $criteria[] = $this->dispatchCriterion($criterionName, $criterionData, $parsingDispatcher);
44
        }
45
46
        return new LogicalOrCriterion($criteria);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\A...n\LogicalOr($criteria); (eZ\Publish\API\Repositor...ery\Criterion\LogicalOr) 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...
47
    }
48
}
49