Completed
Push — master ( 1bff5d...2a2446 )
by Mikaël
75:38 queued 45:53
created

EnumerationRule   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 2
Metric Value
wmc 2
c 2
b 1
f 2
lcom 1
cbo 5
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyRule() 0 13 2
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\File\StructEnum;
6
use WsdlToPhp\PackageGenerator\Model\Struct;
7
8
class EnumerationRule extends AbstractRule
9
{
10
    /**
11
     * @see \WsdlToPhp\PackageGenerator\File\Validation\AbstractValidation::addRule()
12
     * @param string $parameterName
13
     * @param mixed $value
14
     * @param bool $itemType
15
     * @return EnumerationRule
16
     */
17 48
    public function applyRule($parameterName, $value, $itemType = false)
18
    {
19 48
        $attribute = $this->getAttribute();
20 48
        if (($model = $this->getFile()->getRestrictionFromStructAttribute($attribute)) instanceof Struct) {
21 36
            $this
22 48
                ->getMethod()
23 48
                    ->addChild('// validation for constraint: enumeration')
24 48
                    ->addChild(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName))
25 48
                        ->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'Value "%%s" is invalid, please use one of: %%s\', $%s, implode(\', \', %s::%s())), __LINE__);', $parameterName, $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES), 1))
26 48
                    ->addChild('}');
27 36
        }
28 48
        return $this;
29
    }
30
}
31