Completed
Pull Request — master (#108)
by Matt
11:29
created

MinLength   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 32
loc 32
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A validate() 11 11 3

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
namespace League\JsonGuard\Constraints\DraftFour;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Assert;
7
use League\JsonGuard\Constraint;
8
use League\JsonGuard\Validator;
9
use function League\JsonGuard\error;
10
11 View Code Duplication
class MinLength implements Constraint
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...
12
{
13
    const KEYWORD = 'minLength';
14
15
    /**
16
     * @var string
17
     */
18
    private $charset;
19
20
    /**
21
     * @param string $charset
22
     */
23
    public function __construct($charset = 'UTF-8')
24
    {
25
        $this->charset = $charset;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function validate($value, $parameter, Validator $validator)
32
    {
33
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath());
34
        Assert::nonNegative($parameter, self::KEYWORD, $validator->getSchemaPath());
35
36
        if (!is_string($value) || JsonGuard\strlen($value, $this->charset) >= $parameter) {
37
            return null;
38
        }
39
40
        return error('The string must be at least {parameter} characters long.', $validator);
41
    }
42
}
43