Passed
Push — master ( f0393b...d8ac0a )
by Mikaël
12:30 queued 13s
created

AbstractLengthRule::getValidationMethodName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
/**
8
 * Gathers [min|max|]Length rules.
9
 */
10
abstract class AbstractLengthRule extends AbstractMinMaxRule
11
{
12
    final public function testConditions(string $parameterName, $value, bool $itemType = false): string
13
    {
14
        if ($itemType || !$this->getAttribute()->isArray()) {
15
            $test = sprintf(
16 18
                ($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen((string) $%1$s) %3$s %2$d',
17
                $parameterName,
18 18
                $value,
19 18
                $this->symbol()
20
            );
21 4
        } else {
22 4
            $this->addArrayValidationMethod($parameterName, $value);
23
            $test = sprintf(
24
                '\'\' !== (%s = self::%s($%s))',
25 18
                $this->getArrayErrorMessageVariableName($parameterName),
26
                $this->getValidationMethodName($parameterName),
27
                $parameterName
28 18
            );
29
        }
30 18
31 18
        return $test;
32
    }
33 4
34
    final public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
35
    {
36 18
        if ($itemType || !$this->getAttribute()->isArray()) {
37
            $message = sprintf(
38
                'sprintf(\'Invalid length of %%s, the number of characters/octets contained by the literal must be %s %s\', mb_strlen((string) $%s))',
39 4
                $this->comparisonString(),
40
                is_array($value) ? implode(',', array_unique($value)) : $value,
41 4
                $parameterName
42
            );
43
        } else {
44 4
            $message = $this->getArrayErrorMessageVariableName($parameterName);
45
        }
46 4
47 4
        return $message;
48 4
    }
49 4
50
    final protected function getArrayExceptionMessageOnTestFailure($value): string
51
    {
52 4
        return sprintf(
53 4
            'Invalid length for value(s) %%s, the number of characters/octets contained by the literal must be %s %s',
54 4
            $this->comparisonString(),
55 4
            $value
56 4
        );
57 4
    }
58
}
59