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

ArrayRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

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
    public function applyRule($parameterName, $value, $itemType = false)
18
    {
19
        if ($this->getAttribute()->isArray()) {
20
            $model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
21
            $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
22
            if ($model instanceof Struct) {
23
                $this
24
                    ->getMethod()
25
                        ->addChild('$invalidValues = array();')
26
                        ->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName))
27
                            ->addChild($this->getMethod()->getIndentedString(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $itemName), 1))
28
                                ->addChild($this->getMethod()->getIndentedString(sprintf('$invalidValues[] = var_export($%s);', $itemName), 2))
29
                            ->addChild($this->getMethod()->getIndentedString('}', 1))
30
                        ->addChild('}')
31
                        ->addChild('if (!empty($invalidValues)) {')
32
                            ->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
                        ->addChild('}');
34
            } else {
35
                $this->getMethod()->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName));
36
                $this->getRules()->getItemTypeRule()->applyRule($itemName, true);
37
                $this->getMethod()->addChild('}');
38
            }
39
        }
40
        return $this;
41
    }
42
}
43