Passed
Push — feature/issue-165 ( 2b7e50...101bca )
by Mikaël
14:10
created

EnumerationRule::name()   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
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 102
     */
18
    public function name()
19 102
    {
20 102
        return 'enumeration';
21 102
    }
22 102
23 102
    /**
24 102
     * @param string $parameterName
25 102
     * @param mixed $value
26 51
     * @param bool $itemType
27 102
     * @return string
28
     */
29
    public function testConditions($parameterName, $value, $itemType = false)
30
    {
31
        $test = '';
32
        if ($this->getRestrictionModel()) {
33
            $test = sprintf('!%s::%s($%s)', $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName);
34
        }
35
        return $test;
36
    }
37
38
    /**
39
     * @param string $parameterName
40
     * @param mixed $value
41
     * @param bool $itemType
42
     * @return string
43
     */
44
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
45
    {
46
        if ($this->getRestrictionModel()) {
47
            return sprintf('sprintf(\'Invalid value(s) %%s, please use one of: %%s from enumeration class %2$s\', is_array($%1$s) ? implode(\', \', $%1$s) : $%1$s, 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
    protected function getRestrictionModel()
55
    {
56
        if (!$this->model) {
57
            $this->model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
58
        }
59
        return $this->model;
60
    }
61
}
62