MaxLength   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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