Completed
Push — feature/issue-133 ( 6faf89...46a72f )
by Mikaël
23:25 queued 49s
created

ChoiceRule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyRule() 0 18 6
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\Model\Struct;
6
7
class ChoiceRule extends AbstractRule
8
{
9
    /**
10
     * @see \WsdlToPhp\PackageGenerator\File\Validation\AbstractValidation::addRule()
11
     * @param string $parameterName
12
     * @param mixed $value
13
     * @param bool $itemType
14
     * @return ChoiceRule
15
     */
16 12
    public function applyRule($parameterName, $value, $itemType = false)
17
    {
18 12
        if (is_array($value) && 0 < count($value)) {
19 12
            $this->getMethod()
20 12
                ->addChild('// validation(s) for constraint: choice');
21
22 12
            $attribute = $this->getAttribute();
23 12
            $struct = $attribute->getOwner();
24 12
            foreach ($value as $choiceName) {
25 12
                if ($choiceName !== $attribute->getName() && $choiceAttribute = $struct->getAttribute($choiceName)) {
26 12
                    $this->getMethod()
27 12
                        ->addChild(sprintf('if (isset($this->%s)) {', $choiceAttribute->getCleanName()))
28 12
                        ->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(\'The property %s can\\\'t be set as the property %s is already set. Only one property must be set among these properties: %s.\');', $attribute->getName(), $choiceAttribute->getName(), implode(', ', $value)), 1))
29 12
                        ->addChild(sprintf('}'));
30 6
                }
31 6
            }
32 6
        }
33 12
        return $this;
34
    }
35
}
36