Completed
Push — master ( 3b4efc...fefeeb )
by Andres
02:22 queued 02:19
created

JsonValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 34.78 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 4
Metric Value
c 5
b 0
f 4
dl 16
loc 46
rs 10
wmc 4
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A keys() 8 8 1
A items() 8 8 1

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
namespace Comfort\Validator;
3
4
use Comfort\Comfort;
5
6
/**
7
 * Class JsonValidator
8
 * @package Comfort\Validator
9
 * @method $this array()
10
 */
11
class JsonValidator extends AbstractValidator
12
{
13
    public function __construct(Comfort $comfort)
14
    {
15
        parent::__construct($comfort);
16
17
        $this->toBool(false);
18
19
        $this->add(function($value, $nameKey) {
20
            json_decode($value);
21
            if (json_last_error() != JSON_ERROR_NONE) {
22
                return $this->createError('json.invalid', $value, $nameKey);
23
            }
24
        });
25
    }
26
27
    /**
28
     * Apply rules to a given schema
29
     *
30
     * @param array $definition
31
     * @return $this
32
     */
33 View Code Duplication
    public function keys(array $definition)
0 ignored issues
show
Duplication introduced by
This method 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...
34
    {
35
        return $this->add(function($value, $nameKey) use($definition) {
0 ignored issues
show
Unused Code introduced by
The parameter $nameKey is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
            $decodedValue = json_decode($value, true);
37
            $validation = $this->array()->keys($definition);
38
            return $validation($decodedValue);
39
        });
40
    }
41
42
    /**
43
     * Apply a definition to items in a json structure
44
     *
45
     * @param AbstractValidator $definition
46
     * @return $this
47
     */
48 View Code Duplication
    public function items(AbstractValidator $definition)
0 ignored issues
show
Duplication introduced by
This method 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...
49
    {
50
        return $this->add(function($value, $nameKey) use($definition) {
0 ignored issues
show
Unused Code introduced by
The parameter $nameKey is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
            $decodedValue = json_decode($value, true);
52
            $validation = $this->array()->items($definition);
53
            return $validation($decodedValue);
54
        });
55
    }
56
}