Completed
Push — master ( ab2db9...9b8f8b )
by Mikaël
23:04
created

MaxLengthRule::name()   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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
class MaxLengthRule extends AbstractRule
6
{
7
    /**
8
     * @return string
9
     */
10 72
    public function name()
11
    {
12 72
        return 'maxLength';
13
    }
14
15
    /**
16
     * @param string $parameterName
17
     * @param mixed $value
18
     * @param bool $itemType
19
     * @return string
20
     */
21 72
    public function testConditions($parameterName, $value, $itemType = false)
22
    {
23 72
        return sprintf('(is_scalar($%1$s) && strlen($%1$s) > %2$d) || (is_array($%1$s) && count($%1$s) > %2$d)', $parameterName, $value);
24
    }
25
26
    /**
27
     * @param string $parameterName
28
     * @param mixed $value
29
     * @param bool $itemType
30
     * @return string
31
     */
32 72
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
33
    {
34 72
        return sprintf('sprintf(\'Invalid length for %%s, please provide an array with %1$d element(s) or a scalar of %1$d character(s) at most\', var_export($%2$s, true), is_scalar($%2$s) ? strlen($%2$s) : count($%2$s))', $value, $parameterName);
35
    }
36
}
37