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

AbstractLengthRule   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 34
dl 0
loc 59
ccs 34
cts 36
cp 0.9444
rs 10
c 1
b 1
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessageVariableName() 0 3 1
A getValidationMethodName() 0 3 1
A exceptionMessageOnTestFailure() 0 9 4
A addValidationMethod() 0 24 1
A testConditions() 0 10 4
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\PhpGenerator\Element\PhpFunctionParameter;
9
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
10
11
/**
12
 * Gathers [min|max|]Length rules.
13
 */
14
abstract class AbstractLengthRule extends AbstractMinMaxRule
15
{
16 9
    final public function testConditions(string $parameterName, $value, bool $itemType = false): string
17
    {
18 9
        if ($itemType || !$this->getAttribute()->isArray()) {
19 9
            $test = sprintf(($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen((string) $%1$s) %3$s %2$d', $parameterName, $value, $this->symbol());
20
        } else {
21 2
            $this->addValidationMethod($parameterName, $value);
22 2
            $test = sprintf('\'\' !== (%s = self::%s($%s))', $this->getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName);
23
        }
24
25 9
        return $test;
26
    }
27
28 9
    final public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
29
    {
30 9
        if ($itemType || !$this->getAttribute()->isArray()) {
31 9
            $message = sprintf('sprintf(\'Invalid length of %%s, the number of characters/octets contained by the literal must be %s %s\', mb_strlen((string) $%s))', $this->comparisonString(), is_array($value) ? implode(',', array_unique($value)) : $value, $parameterName);
32
        } else {
33 2
            $message = $this->getErrorMessageVariableName($parameterName);
34
        }
35
36 9
        return $message;
37
    }
38
39 2
    public function getErrorMessageVariableName(string $parameterName): string
40
    {
41 2
        return sprintf('$%s%sErrorMessage', $parameterName, ucfirst($this->name()));
42
    }
43
44 2
    protected function addValidationMethod(string $parameterName, $value): void
45
    {
46 2
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
47 2
            new PhpFunctionParameter('values', PhpFunctionParameter::NO_VALUE),
48
        ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true);
49 2
        $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
50
51
        $method
52 2
            ->addChild('$message = \'\';')
53 2
            ->addChild('$invalidValues = [];')
54 2
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
55 2
            ->addChild($method->getIndentedString($this->validationRuleComment($value), 1))
56 2
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $this->testConditions($itemName, $value, true)), 1))
57 2
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = var_export($%s, true);', $itemName), 2))
58 2
            ->addChild($method->getIndentedString('}', 1))
59 2
            ->addChild('}')
60 2
            ->addChild('if (!empty($invalidValues)) {')
61 2
            ->addChild($method->getIndentedString(sprintf('$message = sprintf(\'Invalid length for value(s) %%s, the number of characters/octets contained by the literal must be %s %s\', implode(\', \', $invalidValues));', $this->comparisonString(), $value), 1))
62 2
            ->addChild('}')
63 2
            ->addChild('unset($invalidValues);')
64 2
            ->addChild('')
65 2
            ->addChild('return $message;')
66
        ;
67 2
        $this->getMethods()->add($method);
68
    }
69
70 2
    protected function getValidationMethodName(string $parameterName): string
71
    {
72 2
        return sprintf('validate%sFor%sConstraintFrom%s', ucfirst($parameterName), ucfirst($this->name()), ucfirst($this->getMethod()->getName()));
73
    }
74
}
75