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

AbstractSetOfValuesRule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 97.62%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 39
dl 0
loc 73
ccs 41
cts 42
cp 0.9762
rs 10
c 2
b 0
f 1
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessageVariableName() 0 3 1
A exceptionMessageOnTestFailure() 0 3 1
A testConditions() 0 9 2
A getParameterPassedValue() 0 3 1
A getValidationMethodName() 0 3 1
A addValidationMethod() 0 35 2
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