Completed
Push — develop ( 2b7e50...d1ae17 )
by Mikaël
23:57
created

ChoiceRule::getValidationMethodName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter;
6
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
7
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
8
9
class ChoiceRule extends AbstractRule
10
{
11
12
    /**
13
     * @return string
14
     */
15 24
    public function name()
16
    {
17 24
        return 'choice';
18
    }
19
20
    /**
21
     * @param string $parameterName
22
     * @param mixed $value
23
     * @param bool $itemType
24
     * @return string
25
     */
26 24
    public function testConditions($parameterName, $value, $itemType = false)
27
    {
28 24
        if (is_array($value) && 0 < count($value)) {
29 24
            $this->addValidationMethod($parameterName, $value);
30 24
            $test = sprintf('\'\' !== ($message = self::%s($%s))', $this->getValidationMethodName($parameterName), $parameterName);
31 12
        }
32 24
        return $test;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $test does not seem to be defined for all execution paths leading up to this point.
Loading history...
33
    }
34
35
    /**
36
     * @param string $parameterName
37
     * @param mixed $value
38
     * @param bool $itemType
39
     * @return string
40
     */
41 24
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
42
    {
43 24
        return '$message';
44
    }
45
46
    /**
47
     * @param $parameterName
48
     * @param string[] $choiceNames
49
     */
50 24
    protected function addValidationMethod($parameterName, array $choiceNames)
51
    {
52 24
        $attribute = $this->getAttribute();
53 24
        $struct = $attribute->getOwner();
54 24
        $choiceAttributes = [];
55 24
        foreach ($choiceNames as $choiceName) {
56 24
            if ($choiceName !== $attribute->getName() && $choiceAttribute = $struct->getAttribute($choiceName)) {
57 24
                $choiceAttributes[] = $choiceAttribute;
58 12
            }
59 12
        }
60
61 24
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
62 24
            new PhpFunctionParameter('value', PhpFunctionParameter::NO_VALUE),
63 12
        ]);
64
65
        $method
66 24
            ->addChild('$message = \'\';')
67 24
            ->addChild('if (is_null($value)) {')
68 24
            ->addChild($method->getIndentedString('return $message;', 1))
69 24
            ->addChild('}')
70 24
            ->addChild('$properties = [');
71
72 24
        array_walk($choiceAttributes, function (StructAttribute $choiceAttribute) use ($method) {
73 24
            $method->addChild($method->getIndentedString(sprintf('%s,', var_export($choiceAttribute->getCleanName(), true)), 1));
74 24
        });
75
76
        $method
77 24
            ->addChild('];')
78 24
            ->addChild('try {')
79 24
            ->addChild($method->getIndentedString('foreach ($properties as $property) {', 1))
80 24
            ->addChild($method->getIndentedString('if (isset($this->{$property})) {', 2))
81 24
            ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'The property %1$s can\\\'t be set as the property %%s is already set. Only one property must be set among these properties: %1$s, %%s.\', $property, implode(\', \', $properties)), __LINE__);', $attribute->getName()), 3))
82 24
            ->addChild($method->getIndentedString(sprintf('}'), 2))
83 24
            ->addChild($method->getIndentedString('}', 1))
84 24
            ->addChild('} catch (\InvalidArgumentException $e) {')
85 24
            ->addChild($method->getIndentedString('$message = $e->getMessage();', 1))
86 24
            ->addChild('}')
87 24
            ->addChild('return $message;');
88
89 24
        $this->getMethods()->add($method);
90 24
    }
91
92
    /**
93
     * @param string $parameterName
94
     * @return string
95
     */
96 24
    protected function getValidationMethodName($parameterName)
97
    {
98 24
        return sprintf('validate%sForChoiceConstraintsFrom%s', ucfirst($parameterName), ucFirst($this->getMethod()->getName()));
99
    }
100
}
101