Completed
Push — develop ( de5d0b...06b47b )
by Mikaël
31:11
created

MinLengthRule   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 1 Features 2
Metric Value
wmc 1
c 2
b 1
f 2
lcom 0
cbo 3
dl 0
loc 20
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyRule() 0 10 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
class MinLengthRule 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 MinLengthRule
13
     */
14
    public function applyRule($parameterName, $value, $itemType = false)
15
    {
16
        $this
17
            ->getMethod()
18
                ->addChild('// validation for constraint: minLength')
19
                ->addChild(sprintf('if ((is_scalar(%1$s) && strlen(%1$s) < %2$d) || (is_array(%1$s) && count(%1$s) < %2$d)) {', $parameterName, $value))
20
                    ->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) at least\', __LINE__);', $value), 1))
21
                ->addChild('}');
22
        return $this;
23
    }
24
}
25