Passed
Pull Request — develop (#279)
by Mikaël
10:56 queued 07:51
created

AbstractRule::getMethods()   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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
20 152
    public function __construct(Rules $rules)
21
    {
22 152
        $this->rules = $rules;
23
    }
24
25 150
    final public function applyRule(string $parameterName, $value, bool $itemType = false): void
26
    {
27 150
        $test = $this->testConditions($parameterName, $value, $itemType);
28 150
        if (!empty($test)) {
29 150
            $message = $this->exceptionMessageOnTestFailure($parameterName, $value, $itemType);
30 150
            $this
31 150
                ->getMethod()
32 150
                ->addChild($this->validationRuleComment($value))
33 150
                ->addChild(sprintf('if (%s) {', $test))
34 150
                ->addChild($this->getMethod()->getIndentedString(sprintf('throw new InvalidArgumentException(%s, __LINE__);', $message), 1))
35 150
                ->addChild('}')
36 150
            ;
37 150
            unset($message);
38 150
            Rules::ruleHasBeenAppliedToAttribute($this, $value, $this->getAttribute());
39
        }
40 150
        unset($test);
41
    }
42
43 150
    final public function validationRuleComment($value): string
44
    {
45 150
        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
54 66
    public function getRules(): Rules
55
    {
56 66
        return $this->rules;
57
    }
58
59 150
    public function getMethod(): PhpMethod
60
    {
61 150
        return $this->rules->getMethod();
62
    }
63
64 70
    public function getMethods(): MethodContainer
65
    {
66 70
        return $this->rules->getMethods();
67
    }
68
69 84
    public function getFile(): AbstractModelFile
70
    {
71 84
        return $this->rules->getFile();
72
    }
73
74 150
    public function getAttribute(): StructAttributeModel
75
    {
76 150
        return $this->rules->getAttribute();
77
    }
78
79 4
    public function getGenerator(): Generator
80
    {
81 4
        return $this->rules->getGenerator();
82
    }
83
84 12
    final protected function addArrayValidationMethod(string $parameterName, $value): void
85
    {
86 12
        $itemName = sprintf(
87 12
            '%s%sItem',
88 12
            lcfirst($this->getFile()->getModel()->getCleanName(false)),
89 12
            ucfirst($this->getAttribute()->getCleanName())
90 12
        );
91 12
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
92 12
            new PhpFunctionParameter('values', PhpFunctionParameter::NO_VALUE),
93 12
        ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true);
94
95 12
        $method
96 12
            ->addChild('$message = \'\';')
97 12
            ->addChild('$invalidValues = [];')
98 12
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
99 12
            ->addChild($method->getIndentedString($this->validationRuleComment($value), 1))
100 12
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $this->testConditions($itemName, $value, true)), 1))
101 12
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = var_export($%s, true);', $itemName), 2))
102 12
            ->addChild($method->getIndentedString('}', 1))
103 12
            ->addChild('}')
104 12
            ->addChild('if (!empty($invalidValues)) {')
105 12
            ->addChild($method->getIndentedString(
106 12
                sprintf(
107 12
                    '$message = sprintf(\'%s\', implode(\', \', $invalidValues));',
108 12
                    addslashes($this->getArrayExceptionMessageOnTestFailure($parameterName, $value)),
109 12
                ),
110 12
                1
111 12
            ))
112 12
            ->addChild('}')
113 12
            ->addChild('unset($invalidValues);')
114 12
            ->addChild('')
115 12
            ->addChild('return $message;')
116 12
        ;
117 12
        $this->getMethods()->add($method);
118
    }
119
120 6
    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 6
        return '';
123
    }
124
125 70
    final protected function getValidationMethodName(string $parameterName): string
126
    {
127 70
        return sprintf(
128 70
            'validate%sFor%sConstraintFrom%s',
129 70
            ucfirst($parameterName),
130 70
            ucfirst($this->name()),
131 70
            ucfirst($this->getMethod()->getName())
132 70
        );
133
    }
134
135 12
    final protected function getArrayErrorMessageVariableName(string $parameterName): string
136
    {
137 12
        return sprintf('$%s%sErrorMessage', $parameterName, ucfirst($this->name()));
138
    }
139
}
140