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

EnumerationRule   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

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
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
    public function applyRule($parameterName, $value, $itemType = false)
18
    {
19
        $attribute = $this->getAttribute();
20
        if (($model = $this->getFile()->getRestrictionFromStructAttribute($attribute)) instanceof Struct) {
21
            $this
22
                ->getMethod()
23
                    ->addChild('// validation for constraint: enumeration')
24
                    ->addChild(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName))
25
                        ->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
                    ->addChild('}');
27
        }
28
        return $this;
29
    }
30
}
31