AbstractBoundRule::testConditions()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 20
c 1
b 1
f 0
nc 7
nop 3
dl 0
loc 30
ccs 18
cts 18
cp 1
crap 6
rs 8.9777
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
/**
8
 * Gathers [min|max][In|Ex]clusive rules.
9
 */
10
abstract class AbstractBoundRule extends AbstractMinMaxRule
11
{
12 44
    final public function testConditions(string $parameterName, $value, bool $itemType = false): string
13
    {
14 44
        $method = '';
15 44
        $checkValueDomain = '';
16 44
        if (is_numeric($value)) {
17 20
            $test = '$%1$s %3$s %2$s';
18
        } else {
19 24
            if (false === mb_strpos($value, '-')) {
20 12
                $method = 'add';
21
            } else {
22 12
                $method = 'sub';
23 12
                $value = mb_substr($value, 1);
24
            }
25
26 24
            switch ($this->symbol()) {
27 24
                case self::SYMBOL_MAX_EXCLUSIVE:
28 18
                case self::SYMBOL_MAX_INCLUSIVE:
29 12
                    $checkValueDomain = '===';
30
31 12
                    break;
32
33
                default:
34 12
                    $checkValueDomain = '!==';
35
36 12
                    break;
37
            }
38 24
            $test = 'false %5$s mb_strpos((string) $%1$s, \'-\') && ($time = (string) time()) && \DateTime::createFromFormat(\'U\', $time)->%4$s(new \DateInterval(preg_replace(\'/(.*)(\.[0-9]*S)/\', \'$1S\', str_replace(\'-\', \'\', $%1$s)))) %3$s \DateTime::createFromFormat(\'U\', $time)->%4$s(new \DateInterval(preg_replace(\'/(.*)(\.[0-9]*S)/\', \'$1S\', \'%2$s\')))';
39
        }
40
41 44
        return sprintf(($itemType ? '' : '!is_null($%1$s) && ').$test, $parameterName, $value, $this->symbol(), $method, $checkValueDomain);
42
    }
43
44 44
    final public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
45
    {
46 44
        return sprintf('sprintf(\'Invalid value %%s, the value must be %s %s %s\', var_export($%4$s, true))', is_numeric($value) ? 'numerically' : 'chronologically', $this->comparisonString(), is_array($value) ? implode(',', array_unique($value)) : $value, $parameterName);
47
    }
48
}
49