Completed
Push — feature/issue-165 ( 83f642...d66719 )
by Mikaël
23:02 queued 18s
created

EnumerationRule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 52
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A getRestrictionModel() 0 6 2
A testConditions() 0 7 2
A exceptionMessageOnTestFailure() 0 4 2
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\File\StructEnum;
6
use WsdlToPhp\PackageGenerator\Model\Struct;
7
8
class EnumerationRule extends AbstractRule
9
{
10
    /**
11
     * @var Struct
12
     */
13
    protected $model;
14
15
    /**
16
     * @return string
17
     */
18 114
    public function name()
19
    {
20 114
        return 'enumeration';
21
    }
22
23
    /**
24
     * @param string $parameterName
25
     * @param mixed $value
26
     * @param bool $itemType
27
     * @return string
28
     */
29 114
    public function testConditions($parameterName, $value, $itemType = false)
30
    {
31 114
        $test = '';
32 114
        if ($this->getRestrictionModel()) {
33 114
            $test = sprintf('!%s::%s($%s)', $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName);
34 57
        }
35 114
        return $test;
36
    }
37
38
    /**
39
     * @param string $parameterName
40
     * @param mixed $value
41
     * @param bool $itemType
42
     * @return string
43
     */
44 114
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
45
    {
46 114
        if ($this->getRestrictionModel()) {
47 114
            return 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, $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES);
48
        }
49
    }
50
51
    /**
52
     * @return Struct|null
53
     */
54 114
    protected function getRestrictionModel()
55
    {
56 114
        if (!$this->model) {
57 114
            $this->model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
58 57
        }
59 114
        return $this->model;
60
    }
61
}
62