StructValue::getCleanName()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\Model;
6
7
use WsdlToPhp\PackageGenerator\Generator\Generator;
8
use WsdlToPhp\PackageGenerator\Generator\Utils;
9
10
/**
11
 * Class StructValue stands for an enumeration value.
12
 */
13
final class StructValue extends AbstractModel
14
{
15
    public const MATCH_PATTERN = '/([[:upper:]]+[[:lower:]]*)|([[:lower:]]+)|(\d+)/';
16
    public const REPLACEMENT_PATTERN = '$1$2$3_';
17
    public const GENERIC_NAME_PREFIX = 'ENUM_VALUE_';
18
    public const VALUE_NAME_PREFIX = 'VALUE_';
19
20
    protected static array $uniqueConstants = [];
21
22
    protected int $index = 0;
23
24
    /**
25
     * Cleaned name of the element stored in order to avoid multiple call that would generate incremental name.
26
     */
27
    private ?string $cleanedName = null;
28
29 236
    public function __construct(Generator $generator, $name, int $index = 0, ?Struct $struct = null)
30
    {
31 236
        parent::__construct($generator, $name);
32 236
        $this
33 236
            ->setIndex($index)
34 236
            ->setOwner($struct)
35 236
        ;
36
    }
37
38 46
    public function getCleanName(bool $keepMultipleUnderscores = false): string
39
    {
40 46
        if ($this->cleanedName) {
41 42
            return $this->cleanedName;
42
        }
43
44 46
        if ($this->getGenerator()->getOptionGenericConstantsNames()) {
45 2
            return self::GENERIC_NAME_PREFIX.$this->getIndex();
46
        }
47
48 44
        $nameWithSeparatedWords = $this->getNameWithSeparatedWords($keepMultipleUnderscores);
49 44
        $key = self::constantSuffix($this->getOwner()->getName(), $nameWithSeparatedWords, $this->getIndex());
50
51 44
        return $this->cleanedName = self::VALUE_NAME_PREFIX.mb_strtoupper($nameWithSeparatedWords.($key ? '_'.$key : ''));
52
    }
53
54 44
    public function getNameWithSeparatedWords(bool $keepMultipleUnderscores = false): string
55
    {
56 44
        return trim(self::cleanString(preg_replace(self::MATCH_PATTERN, self::REPLACEMENT_PATTERN, (string) $this->getName()), $keepMultipleUnderscores), '_');
57
    }
58
59 46
    public function getValue()
60
    {
61 46
        return Utils::getValueWithinItsType($this->getName());
62
    }
63
64 46
    public function getIndex(): int
65
    {
66 46
        return $this->index;
67
    }
68
69 236
    public function setIndex(int $index): StructValue
70
    {
71 236
        if (0 > $index) {
72 4
            throw new \InvalidArgumentException(sprintf('The value\'s index must be a positive integer, "%s" given', var_export($index, true)));
73
        }
74 234
        $this->index = $index;
75
76 234
        return $this;
77
    }
78
79 44
    public function getOwner(): Struct
80
    {
81 44
        return parent::getOwner();
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getOwner() returns the type null which is incompatible with the type-hinted return WsdlToPhp\PackageGenerator\Model\Struct.
Loading history...
82
    }
83
84 44
    protected static function constantSuffix(string $structName, string $value, int $index): int
85
    {
86 44
        $key = mb_strtoupper($structName.'_'.$value);
87 44
        $indexedKey = $key.'_'.$index;
88 44
        if (array_key_exists($indexedKey, self::$uniqueConstants)) {
89 18
            return self::$uniqueConstants[$indexedKey];
90
        }
91 28
        if (!array_key_exists($key, self::$uniqueConstants)) {
92 28
            self::$uniqueConstants[$key] = 0;
93
        } else {
94 4
            ++self::$uniqueConstants[$key];
95
        }
96 28
        self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key];
97
98 28
        return self::$uniqueConstants[$key];
99
    }
100
101 2
    protected function toJsonSerialize(): array
102
    {
103 2
        return [
104 2
            'index' => $this->index,
105 2
        ];
106
    }
107
}
108