EnumerationRule   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 36
ccs 16
cts 16
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A getRestrictionModel() 0 7 2
A testConditions() 0 8 2
A exceptionMessageOnTestFailure() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
use WsdlToPhp\PackageGenerator\File\StructEnum;
8
use WsdlToPhp\PackageGenerator\Model\Struct;
9
10
/**
11
 * @see https://www.w3.org/TR/xmlschema-2/#rf-enumeration
12
 * Validation Rule: enumeration valid
13
 * A value in a ·value space· is facet-valid with respect to ·enumeration· if the value is one of the values specified in {value}
14
 */
15
final class EnumerationRule extends AbstractRule
16
{
17
    protected ?Struct $model = null;
18
19 54
    public function name(): string
20
    {
21 54
        return 'enumeration';
22
    }
23
24 54
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
25
    {
26 54
        $test = '';
27 54
        if ($this->getRestrictionModel()) {
28 54
            $test = sprintf('!%s::%s($%s)', $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName);
29
        }
30
31 54
        return $test;
32
    }
33
34 54
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
35
    {
36 54
        $exceptionMessage = '';
37 54
        if ($restrictionModel = $this->getRestrictionModel()) {
38 54
            $exceptionMessage = sprintf('sprintf(\'Invalid value(s) %%s, please use one of: %%s from enumeration class %2$s\', is_array($%1$s) ? implode(\', \', $%1$s) : var_export($%1$s, true), implode(\', \', %2$s::%3$s()))', $parameterName, $restrictionModel->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES);
39
        }
40
41 54
        return $exceptionMessage;
42
    }
43
44 54
    protected function getRestrictionModel(): ?Struct
45
    {
46 54
        if (!$this->model) {
47 54
            $this->model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
48
        }
49
50 54
        return $this->model;
51
    }
52
}
53