EntityExists   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 36
loc 36
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 14 14 2

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
 * Polder Knowledge / entityservice-zend-validator (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/entityservice-zend-validator for the canonical source repository
6
 * @copyright Copyright (c) 2016 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/entityservice-zend-validator/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\EntityService\Validator;
11
12
/**
13
 * The EntityExists validator will validate a certain entity was found. Validation will
14
 * fail when the entity is not present
15
 */
16 View Code Duplication
class EntityExists extends AbstractEntityValidator
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...
17
{
18
    /**
19
     * Error constants
20
     */
21
    const ERROR_NO_OBJECT_FOUND = 'noObjectFound';
22
23
    /**
24
     * @var array Message templates
25
     */
26
    protected $messageTemplates = array(
27
        self::ERROR_NO_OBJECT_FOUND => "No object matching '%value%' was found",
28
    );
29
30
    /**
31
     * Returns true when $this->entityService returns an entity
32
     * when $this->method is called with the given criteria
33
     *
34
     * @param  mixed $value
35
     * @return boolean
36
     */
37 12
    public function isValid($value)
38
    {
39 12
        $result = $this->fetchResult($value);
40
41
        // The following code will check if we have a valid result. In case of an empty array (we did not get a valid
42
        // match) the not-operator will result to true, causing the error to be set. In case of a null value (no object
43
        // has been found), the not-operator will also result to true.
44 12
        if (!$result) {
45 3
            $this->error(self::ERROR_NO_OBJECT_FOUND, $value);
46 3
            return false;
47
        }
48
49 9
        return true;
50
    }
51
}
52