Skip to content

Commit e388039

Browse files
authored
issue #300 - fix default method parameter value (#302)
1 parent 5ba2f17 commit e388039

File tree

5 files changed

+7
-12
lines changed

5 files changed

+7
-12
lines changed

src/File/Struct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ protected function getStructMethodParameter(StructAttributeModel $attribute): Ph
182182
}
183183

184184
try {
185-
$defaultValue = $attribute->getDefaultValue();
185+
$defaultValue = $attribute->getDefaultValue($this->getStructAttributeTypeAsPhpType($attribute));
186186

187187
return new PhpFunctionParameter(
188188
lcfirst($attribute->getUniqueString($attribute->getCleanName(), 'method')),

src/Generator/Utils.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,17 @@ public static function getStreamContextOptions(?string $basicAuthLogin = null, ?
104104
public static function getValueWithinItsType($value, ?string $knownType = null)
105105
{
106106
if (is_int($value) || (!is_null($value) && in_array($knownType, [
107-
'time',
108-
'positiveInteger',
109-
'unsignedLong',
110-
'unsignedInt',
111-
'short',
112-
'long',
113107
'int',
114108
'integer',
115109
], true))) {
116-
return intval($value);
110+
return (int) $value;
117111
}
118112
if (is_float($value) || (!is_null($value) && in_array($knownType, [
119113
'float',
120114
'double',
121115
'decimal',
122116
], true))) {
123-
return floatval($value);
117+
return (float) $value;
124118
}
125119
if (is_bool($value) || (!is_null($value) && in_array($knownType, [
126120
'bool',

src/Model/StructAttribute.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function isList(): bool
137137
/**
138138
* @return null|array|bool|float|int|string
139139
*/
140-
public function getDefaultValue()
140+
public function getDefaultValue(?string $type = null)
141141
{
142142
if (($struct = $this->getTypeStruct()) && $struct->isStruct()) {
143143
return null;
@@ -149,7 +149,7 @@ public function getDefaultValue()
149149
'DefaultValue',
150150
'defaultValue',
151151
'defaultvalue',
152-
]), $this->getType(true));
152+
]), $type);
153153
}
154154

155155
public function isRequired(): bool

tests/Generator/UtilsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function testGetValueWithinItsType(): void
3535
{
3636
$this->assertSame('020', Utils::getValueWithinItsType('020', 'string'));
3737
$this->assertSame('01', Utils::getValueWithinItsType('01', 'string'));
38+
$this->assertSame(1, Utils::getValueWithinItsType('1', 'int'));
3839
$this->assertSame(2.568, Utils::getValueWithinItsType('2.568', 'float'));
3940
$this->assertSame(true, Utils::getValueWithinItsType('true', 'bool'));
4041
$this->assertSame(false, Utils::getValueWithinItsType('false', 'bool'));

tests/Model/StructAttributeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testStructAttributeTypeMustBeBool(): void
7676

7777
$this->assertSame('boolean', $structAttribute->getType(true));
7878
$this->assertSame('Success', $structAttribute->getType());
79-
$this->assertFalse($structAttribute->getDefaultValue());
79+
$this->assertFalse($structAttribute->getDefaultValue('boolean'));
8080
}
8181

8282
public function testIsNullableMustReturnFalse(): void

0 commit comments

Comments
 (0)