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

ArrayRule::applyRule()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 25
ccs 22
cts 22
cp 1
rs 8.8571
c 1
b 0
f 1
cc 3
eloc 21
nc 3
nop 3
crap 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