Passed
Push — develop ( e6f84d...350782 )
by Mikaël
09:01 queued 28s
created

AbstractSetOfValuesRule::addValidationMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 2.0001

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 29
nc 2
nop 2
dl 0
loc 35
ccs 27
cts 28
cp 0.9643
crap 2.0001
rs 9.456
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
8
use WsdlToPhp\PackageGenerator\Model\Struct;
9
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
10
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
11
12
abstract class AbstractSetOfValuesRule extends AbstractRule
13
{
14 29
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
15
    {
16 29
        $test = '';
17 29
        if ($this->mustApplyRuleOnAttribute()) {
18 29
            $this->addValidationMethod($parameterName, $value);
19 29
            $test = sprintf('\'\' !== (%s = self::%s(%s))', static::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), static::getParameterPassedValue($parameterName));
20
        }
21
22 29
        return $test;
23
    }
24
25 29
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
26
    {
27 29
        return static::getErrorMessageVariableName($parameterName);
28
    }
29
30 29
    public static function getErrorMessageVariableName(string $parameterName): string
31
    {
32 29
        return sprintf('$%sArrayErrorMessage', $parameterName);
33
    }
34
35 27
    public static function getParameterPassedValue(string $parameterName): string
36
    {
37 27
        return sprintf('$%s', $parameterName);
38
    }
39
40
    /**
41
     * Must check the attribute validity according to the current rule.
42
     */
43
    abstract protected function mustApplyRuleOnAttribute(): bool;
44
45 29
    protected function addValidationMethod(string $parameterName, $value)
46
    {
47 29
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
48 29
            new PhpFunctionParameter('values', [], '?array'),
49
        ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true);
50 29
        $model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
51 29
        $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
52 29
        $rules = clone $this->getRules();
53
54 29
        if ($model instanceof Struct) {
55 7
            $rule = $rules->getEnumerationRule();
56
        } else {
57 26
            $rule = $rules->setMethod($method)->getItemTypeRule();
58
        }
59
60
        $method
61 29
            ->addChild('if (!is_array($values)) {')
62 29
            ->addChild($method->getIndentedString('return \'\';', 1))
63 29
            ->addChild('}')
64 29
            ->addChild('$message = \'\';')
65 29
            ->addChild('$invalidValues = [];')
66 29
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
67 29
            ->addChild($method->getIndentedString($rule->validationRuleComment($value), 1))
68 29
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $rule->testConditions($itemName, null)), 1))
69 29
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = %s;', sprintf('is_object($%1$s) ? get_class($%1$s) : sprintf(\'%%s(%%s)\', gettype($%1$s), var_export($%1$s, true))', $itemName)), 2))
70 29
            ->addChild($method->getIndentedString('}', 1))
71 29
            ->addChild('}')
72 29
            ->addChild('if (!empty($invalidValues)) {')
73 29
            ->addChild($method->getIndentedString(sprintf('$message = %s;', $rule->exceptionMessageOnTestFailure('invalidValues', null)), 1))
74 29
            ->addChild('}')
75 29
            ->addChild('unset($invalidValues);')
76 29
            ->addChild('')
77 29
            ->addChild('return $message;')
78
        ;
79 29
        $this->getMethods()->add($method);
80
    }
81
82 29
    protected function getValidationMethodName(string $parameterName): string
83
    {
84 29
        return sprintf('validate%sForArrayConstraintsFrom%s', ucfirst($parameterName), ucfirst($this->getMethod()->getName()));
85
    }
86
}
87