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

ArrayRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
lcom 1
cbo 8
dl 0
loc 35
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B applyRule() 0 25 3
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 ArrayRule 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 ArrayRule
16
     */
17 60
    public function applyRule($parameterName, $value, $itemType = false)
18
    {
19 60
        if ($this->getAttribute()->isArray()) {
20 60
            $model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
21 60
            $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
22 60
            if ($model instanceof Struct) {
23 12
                $this
24 16
                    ->getMethod()
25 16
                        ->addChild('$invalidValues = array();')
26 16
                        ->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName))
27 16
                            ->addChild($this->getMethod()->getIndentedString(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $itemName), 1))
28 16
                                ->addChild($this->getMethod()->getIndentedString(sprintf('$invalidValues[] = var_export($%s);', $itemName), 2))
29 16
                            ->addChild($this->getMethod()->getIndentedString('}', 1))
30 16
                        ->addChild('}')
31 16
                        ->addChild('if (!empty($invalidValues)) {')
32 16
                            ->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'Value(s) "%%s" is/are invalid, please use one of: %%s\', implode(\', \', $invalidValues), implode(\', \', %s::%s())), __LINE__);', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES), 1))
33 16
                        ->addChild('}');
34 12
            } else {
35 56
                $this->getMethod()->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName));
36 56
                $this->getRules()->getItemTypeRule()->applyRule($itemName, true);
37 56
                $this->getMethod()->addChild('}');
38
            }
39 45
        }
40 60
        return $this;
41
    }
42
}
43