Completed
Push — feature/issue-49 ( 48d7fc...8a592c )
by Mikaël
30:45
created

LengthRule::applyRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
class LengthRule extends AbstractRule
6
{
7
    /**
8
     * @see \WsdlToPhp\PackageGenerator\File\Validation\AbstractValidation::addRule()
9
     * @param string $parameterName
10
     * @param mixed $value
11
     * @param bool $itemType
12
     * @return LengthRule
13
     */
14 4
    public function applyRule($parameterName, $value, $itemType = false)
15
    {
16 3
        $this
17 4
            ->getMethod()
18 4
                ->addChild('// validation for constraint: length')
19 4
                ->addChild(sprintf('if ((is_scalar(%1$s) && strlen(%1$s) !== %2$d) || (is_array(%1$s) && count(%1$s) !== %2$d)) {', $parameterName, $value))
20 4
                    ->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(\'Invalid length, please provide an array with %1$d element(s) or a scalar of %1$d character(s)\', __LINE__);', $value), 1))
21 4
                ->addChild('}');
22 4
        return $this;
23
    }
24
}
25