StringSize   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 37
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A validate() 14 14 4

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
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Constraint;
6
7 View Code Duplication
class StringSize extends Constraint
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $minSize;
13
14
    /**
15
     * @var int
16
     */
17
    protected $maxSize;
18
19
    public function __construct(int $minSize, int $maxSize = PHP_INT_MAX, string $errorMessage = null)
20
    {
21
        $this->minSize = $minSize;
22
        $this->maxSize = $maxSize;
23
24
        $this->setErrorMessage(
25
            $errorMessage ?? sprintf('Content out of min/max limit sizes [%s, %s]', $this->minSize, $this->maxSize)
26
        );
27
    }
28
29
    public function validate($content): bool
30
    {
31
        if (!is_scalar($content)) {
32
            return false;
33
        }
34
35
        if ($content === null) {
36
            return false;
37
        }
38
39
        $size = strlen($content);
40
41
        return $size >= $this->minSize && $size <= $this->maxSize;
42
    }
43
}
44