Passed
Push — feature/issue-124 ( 91e4aa...dde646 )
by Mikaël
09:46
created

AbstractSetOfValuesRule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 36
c 1
b 0
f 1
dl 0
loc 70
ccs 40
cts 40
cp 1
rs 10
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 addValidationMethod() 0 32 2
A getValidationMethodName() 0 3 1
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 56
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
15
    {
16 56
        $test = '';
17 56
        if ($this->mustApplyRuleOnAttribute()) {
18 56
            $this->addValidationMethod($parameterName, $value);
19 56
            $test = sprintf('\'\' !== (%s = self::%s(%s))', static::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), static::getParameterPassedValue($parameterName));
20
        }
21
22 56
        return $test;
23
    }
24
25 56
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
26
    {
27 56
        return static::getErrorMessageVariableName($parameterName);
28
    }
29
30 56
    public static function getErrorMessageVariableName(string $parameterName): string
31
    {
32 56
        return sprintf('$%sArrayErrorMessage', $parameterName);
33
    }
34
35 52
    public static function getParameterPassedValue(string $parameterName): string
36
    {
37 52
        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 56
    protected function addValidationMethod(string $parameterName, $value)
46
    {
47 56
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
48 56
            new PhpFunctionParameter('values', [], 'array'),
49 56
        ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true);
50 56
        $model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
51 56
        $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
52 56
        $rules = clone $this->getRules();
53
54 56
        if ($model instanceof Struct) {
55 14
            $rule = $rules->getEnumerationRule();
56
        } else {
57 50
            $rule = $rules->setMethod($method)->getItemTypeRule();
58
        }
59
60
        $method
61 56
            ->addChild('$message = \'\';')
62 56
            ->addChild('$invalidValues = [];')
63 56
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
64 56
            ->addChild($method->getIndentedString($rule->validationRuleComment($value), 1))
65 56
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $rule->testConditions($itemName, null)), 1))
66 56
            ->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))
67 56
            ->addChild($method->getIndentedString('}', 1))
68 56
            ->addChild('}')
69 56
            ->addChild('if (!empty($invalidValues)) {')
70 56
            ->addChild($method->getIndentedString(sprintf('$message = %s;', $rule->exceptionMessageOnTestFailure('invalidValues', null)), 1))
71 56
            ->addChild('}')
72 56
            ->addChild('unset($invalidValues);')
73 56
            ->addChild('')
74 56
            ->addChild('return $message;')
75
        ;
76 56
        $this->getMethods()->add($method);
77 56
    }
78
79 56
    protected function getValidationMethodName(string $parameterName): string
80
    {
81 56
        return sprintf('validate%sForArrayConstraintsFrom%s', ucfirst($parameterName), ucfirst($this->getMethod()->getName()));
82
    }
83
}
84