Passed
Pull Request — develop (#279)
by Mikaël
07:26 queued 05:12
created

AbstractLengthRule::getErrorMessageVariableName()   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(($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen((string) $%1$s) %3$s %2$d', $parameterName, $value, $this->symbol());
16 20
        } else {
17
            $this->addArrayValidationMethod($parameterName, $value);
18 20
            $test = sprintf(
19 20
                '\'\' !== (%s = self::%s($%s))',
20
                $this->getArrayErrorMessageVariableName($parameterName),
21 6
                $this->getArrayValidationMethodName($parameterName),
22 6
                $parameterName
23
            );
24
        }
25 20
26
        return $test;
27
    }
28 20
29
    final public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
30 20
    {
31 20
        if ($itemType || !$this->getAttribute()->isArray()) {
32
            $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 6
        } else {
34
            $message = $this->getArrayErrorMessageVariableName($parameterName);
35
        }
36 20
37
        return $message;
38
    }
39 6
40
    final protected function getArrayExceptionMessageOnTestFailure(string $parameterName, $value): string
41 6
    {
42
        return sprintf(
43
            'Invalid length for value(s) %%s, the number of characters/octets contained by the literal must be %s %s',
44 6
            $this->comparisonString(),
45
            $value
46 6
        );
47 6
    }
48
}
49