Passed
Pull Request — develop (#279)
by Mikaël
07:26 queued 05:12
created

AbstractRule   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 124
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 16

13 Methods

Rating   Name   Duplication   Size   Complexity  
A applyRule() 0 16 2
A addArrayValidationMethod() 0 34 1
A validationRuleComment() 0 3 3
A getArrayErrorMessageVariableName() 0 3 1
A getMethod() 0 3 1
A getMethods() 0 3 1
A getAttribute() 0 3 1
A getArrayExceptionMessageOnTestFailure() 0 3 1
A getFile() 0 3 1
A getGenerator() 0 3 1
A __construct() 0 3 1
A getRules() 0 3 1
A getArrayValidationMethodName() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
8
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
9
use WsdlToPhp\PackageGenerator\Generator\Generator;
10
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel;
11
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
12
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
13
14
abstract class AbstractRule
15
{
16
    public const VALIDATION_RULE_COMMENT_SENTENCE = 'validation for constraint:';
17
18
    protected Rules $rules;
19 150
20
    public function __construct(Rules $rules)
21 150
    {
22
        $this->rules = $rules;
23
    }
24 148
25
    final public function applyRule(string $parameterName, $value, bool $itemType = false): void
26 148
    {
27 148
        $test = $this->testConditions($parameterName, $value, $itemType);
28 148
        if (!empty($test)) {
29 148
            $message = $this->exceptionMessageOnTestFailure($parameterName, $value, $itemType);
30 148
            $this
31 148
                ->getMethod()
32 148
                ->addChild($this->validationRuleComment($value))
33 148
                ->addChild(sprintf('if (%s) {', $test))
34 148
                ->addChild($this->getMethod()->getIndentedString(sprintf('throw new InvalidArgumentException(%s, __LINE__);', $message), 1))
35 148
                ->addChild('}')
36 148
            ;
37 148
            unset($message);
38
            Rules::ruleHasBeenAppliedToAttribute($this, $value, $this->getAttribute());
39 148
        }
40
        unset($test);
41
    }
42 148
43
    final public function validationRuleComment($value): string
44 148
    {
45
        return sprintf('// %s %s%s', self::VALIDATION_RULE_COMMENT_SENTENCE, $this->name(), is_array($value) ? sprintf('(%s)', implode(', ', array_unique($value))) : (empty($value) ? '' : sprintf('(%s)', $value)));
46
    }
47
48
    abstract public function name(): string;
49
50
    abstract public function testConditions(string $parameterName, $value, bool $itemType = false): string;
51
52
    abstract public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string;
53 64
54
    public function getRules(): Rules
55 64
    {
56
        return $this->rules;
57
    }
58 148
59
    public function getMethod(): PhpMethod
60 148
    {
61
        return $this->rules->getMethod();
62
    }
63 68
64
    public function getMethods(): MethodContainer
65 68
    {
66
        return $this->rules->getMethods();
67
    }
68 82
69
    public function getFile(): AbstractModelFile
70 82
    {
71
        return $this->rules->getFile();
72
    }
73 148
74
    public function getAttribute(): StructAttributeModel
75 148
    {
76
        return $this->rules->getAttribute();
77
    }
78 4
79
    public function getGenerator(): Generator
80 4
    {
81
        return $this->rules->getGenerator();
82
    }
83
84
    final protected function addArrayValidationMethod(string $parameterName, $value): void
85
    {
86
        $itemName = sprintf(
87
            '%s%sItem',
88
            lcfirst($this->getFile()->getModel()->getCleanName(false)),
89
            ucfirst($this->getAttribute()->getCleanName())
90
        );
91
        $method = new PhpMethod($this->getArrayValidationMethodName($parameterName), [
92
            new PhpFunctionParameter('values', PhpFunctionParameter::NO_VALUE),
93
        ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true);
94
95
        $method
96
            ->addChild('$message = \'\';')
97
            ->addChild('$invalidValues = [];')
98
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
99
            ->addChild($method->getIndentedString($this->validationRuleComment($value), 1))
100
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $this->testConditions($itemName, $value, true)), 1))
101
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = var_export($%s, true);', $itemName), 2))
102
            ->addChild($method->getIndentedString('}', 1))
103
            ->addChild('}')
104
            ->addChild('if (!empty($invalidValues)) {')
105
            ->addChild($method->getIndentedString(
106
                sprintf(
107
                    '$message = sprintf(\'%s\', implode(\', \', $invalidValues));',
108
                    addslashes($this->getArrayExceptionMessageOnTestFailure($parameterName, $value)),
109
                ),
110
                1
111
            ))
112
            ->addChild('}')
113
            ->addChild('unset($invalidValues);')
114
            ->addChild('')
115
            ->addChild('return $message;')
116
        ;
117
        $this->getMethods()->add($method);
118
    }
119
120
    protected function getArrayExceptionMessageOnTestFailure(string $parameterName, $value): string
0 ignored issues
show
Unused Code introduced by
The parameter $parameterName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
    protected function getArrayExceptionMessageOnTestFailure(/** @scrutinizer ignore-unused */ string $parameterName, $value): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
    protected function getArrayExceptionMessageOnTestFailure(string $parameterName, /** @scrutinizer ignore-unused */ $value): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        return '';
123
    }
124
125
    final protected function getArrayValidationMethodName(string $parameterName): string
126
    {
127
        return sprintf(
128
            'validate%sFor%sConstraintFrom%s',
129
            ucfirst($parameterName),
130
            ucfirst($this->name()),
131
            ucfirst($this->getMethod()->getName())
132
        );
133
    }
134
135
    final protected function getArrayErrorMessageVariableName(string $parameterName): string
136
    {
137
        return sprintf('$%s%sErrorMessage', $parameterName, ucfirst($this->name()));
138
    }
139
}
140