Passed
Pull Request — develop (#279)
by Mikaël
10:56 queued 07:51
created

AbstractLengthRule::addValidationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 2
dl 0
loc 24
ccs 19
cts 19
cp 1
crap 1
rs 9.6
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 20
    final public function testConditions(string $parameterName, $value, bool $itemType = false): string
13
    {
14 20
        if ($itemType || !$this->getAttribute()->isArray()) {
15 20
            $test = sprintf(($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen((string) $%1$s) %3$s %2$d', $parameterName, $value, $this->symbol());
16
        } else {
17 6
            $this->addArrayValidationMethod($parameterName, $value);
18 6
            $test = sprintf(
19 6
                '\'\' !== (%s = self::%s($%s))',
20 6
                $this->getArrayErrorMessageVariableName($parameterName),
21 6
                $this->getValidationMethodName($parameterName),
22 6
                $parameterName
23 6
            );
24
        }
25
26 20
        return $test;
27
    }
28
29 20
    final public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
30
    {
31 20
        if ($itemType || !$this->getAttribute()->isArray()) {
32 20
            $message = sprintf('sprintf(\'Invalid length of %%s, the number of characters/octets contained by the literal must be %s %s\', mb_strlen((string) $%s))', $this->comparisonString(), is_array($value) ? implode(',', array_unique($value)) : $value, $parameterName);
33
        } else {
34 6
            $message = $this->getArrayErrorMessageVariableName($parameterName);
35
        }
36
37 20
        return $message;
38
    }
39
40 6
    final protected function getArrayExceptionMessageOnTestFailure(string $parameterName, $value): string
41
    {
42 6
        return sprintf(
43 6
            'Invalid length for value(s) %%s, the number of characters/octets contained by the literal must be %s %s',
44 6
            $this->comparisonString(),
45 6
            $value
46 6
        );
47
    }
48
}
49