TotalDigitsRule::testConditions()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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
 * @see https://www.w3.org/TR/xmlschema-2/#rf-totalDigits
9
 * Validation Rule: totalDigits Valid
10
 * A value in a ·value space· is facet-valid with respect to ·totalDigits· if:
11
 *  - 1 that value is expressible as i × 10^-n where i and n are integers such that |i| < 10^{value} and 0 <= n <= {value}.
12
 */
13
final class TotalDigitsRule extends AbstractRule
14
{
15 6
    public function name(): string
16
    {
17 6
        return 'totalDigits';
18
    }
19
20 6
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
21
    {
22 6
        return sprintf(($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen(preg_replace(\'/(\D)/\', \'\', (string) $%1$s)) > %2$d', $parameterName, $value);
23
    }
24
25 6
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
26
    {
27 6
        return sprintf('sprintf(\'Invalid value %%s, the value must use at most %1$d digits, "%%d" given\', var_export($%2$s, true), mb_strlen(preg_replace(\'/(\D)/\', \'\', (string) $%2$s)))', $value, $parameterName);
28
    }
29
}
30