Completed
Push — feature/issue-44 ( cdce8d...269bb1 )
by Mikaël
23:35
created

StructValue::getIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Model;
4
5
use WsdlToPhp\PackageGenerator\Generator\Generator;
6
use WsdlToPhp\PackageGenerator\Generator\Utils;
7
8
/**
9
 * Class StructValue stands for an enumeration value
10
 */
11
class StructValue extends AbstractModel
12
{
13
    /**
14
     * @var string
15
     */
16
    const MATCH_PATTERN = '/([[:upper:]]+[[:lower:]]*)|([[:lower:]]+)|(\d+)/';
17
    /**
18
     * @var string
19
     */
20
    const REAPLCEMENT_PATTERN = '$1$2$3_';
21
    /**
22
     * Store the constants generated per structName
23
     * @var array
24
     */
25
    private static $uniqueConstants = array();
26
    /**
27
     * The index of the value in the enumeration struct
28
     * @var int
29
     */
30
    private $index = 0;
31
    /**
32
     * Main constructor
33
     * @see AbstractModel::__construct()
34
     * @uses AbstractModel::setOwner()
35
     * @uses StructValue::setIndex()
36
     * @param Generator $generator
37
     * @param string $name the original name
38
     * @param string $index the index of the value in the enumeration struct
39
     * @param Struct $struct defines the struct which owns this value
40
     */
41 268
    public function __construct(Generator $generator, $name, $index, Struct $struct)
42
    {
43 268
        parent::__construct($generator, $name);
44 268
        $this->setIndex($index);
45 264
        $this->setOwner($struct);
46 264
    }
47
    /**
48
     * Returns the name of the value as constant
49
     * @see AbstractModel::getCleanName()
50
     * @uses AbstractModel::getCleanName()
51
     * @uses AbstractModel::getName()
52
     * @uses AbstractModel::getOwner()
53
     * @uses StructValue::constantSuffix()
54
     * @uses StructValue::getIndex()
55
     * @uses StructValue::getOwner()
56
     * @uses Generator::getOptionGenericConstantsNames()
57
     * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores
58
     * @return string
59
     */
60 52
    public function getCleanName($keepMultipleUnderscores = false)
61
    {
62 52
        if ($this->getGenerator()->getOptionGenericConstantsNames()) {
63 4
            return 'ENUM_VALUE_' . $this->getIndex();
64
        } else {
65 48
            $nameWithSeparatedWords = $this->getNameWithSeparatedWords($keepMultipleUnderscores);
66 48
            $key = self::constantSuffix($this->getOwner()->getName(), $nameWithSeparatedWords, $this->getIndex());
67 48
            return 'VALUE_' . strtoupper($nameWithSeparatedWords . ($key ? '_' . $key : ''));
68
        }
69
    }
70
    /**
71
     * @return string
72
     */
73 48
    public function getNameWithSeparatedWords($keepMultipleUnderscores)
74
    {
75 48
        return trim(parent::cleanString(preg_replace(self::MATCH_PATTERN, self::REAPLCEMENT_PATTERN, $this->getName()), $keepMultipleUnderscores), '_');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (cleanString() instead of getNameWithSeparatedWords()). Are you sure this is correct? If so, you might want to change this to $this->cleanString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
76
    }
77
    /**
78
     * Returns the value with good type
79
     * @uses AbstractModel::getName()
80
     * @uses Utils::getValueWithinItsType()
81
     * @return mixed
82
     */
83 52
    public function getValue()
84
    {
85 52
        return Utils::getValueWithinItsType($this->getName());
86
    }
87
    /**
88
     * Gets the index attribute value
89
     * @return int
90
     */
91 52
    public function getIndex()
92
    {
93 52
        return $this->index;
94
    }
95
    /**
96
     * Sets the index attribute value
97
     * @throws \InvalidArgumentException
98
     * @param int $index
99
     * @return StructValue
100
     */
101 268
    public function setIndex($index)
102
    {
103 268
        if (!is_int($index) || $index < 0) {
104 8
            throw new \InvalidArgumentException(sprintf('The value\'s index must be aa positive integer, "%s" given', var_export($index, true)));
105
        }
106 264
        $this->index = $index;
107 264
        return $this;
108
    }
109
    /**
110
     * Returns the index which has to be added at the end of natural constant name defined with the value cleaned
111
     * Allows to avoid multiple constant name to be indentic
112
     * @param string $structName the struct name
113
     * @param string|int|float $value the value
114
     * @param int $index the position of the value
115
     * @return int
116
     */
117 48
    private static function constantSuffix($structName, $value, $index)
118
    {
119 48
        $key = strtoupper($structName . '_' . $value);
120 48
        $indexedKey = $key . '_' . $index;
121 48
        if (array_key_exists($indexedKey, self::$uniqueConstants)) {
122 44
            return self::$uniqueConstants[$indexedKey];
123 28
        } elseif (!array_key_exists($key, self::$uniqueConstants)) {
124 28
            self::$uniqueConstants[$key] = 0;
125 21
        } else {
126 4
            self::$uniqueConstants[$key]++;
127
        }
128 28
        self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key];
129 28
        return self::$uniqueConstants[$key];
130
    }
131
    /**
132
     * Returns the owner model object, meaning a Struct object
133
     * @see AbstractModel::getOwner()
134
     * @uses AbstractModel::getOwner()
135
     * @return Struct
136
     */
137 48
    public function getOwner()
138
    {
139 48
        return parent::getOwner();
140
    }
141
}
142