Skip to content

Commit

Permalink
issue #300 - fix default method parameter value (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelcom authored Aug 25, 2023
1 parent 5ba2f17 commit e388039
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/File/Struct.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected function getStructMethodParameter(StructAttributeModel $attribute): Ph
}

try {
$defaultValue = $attribute->getDefaultValue();
$defaultValue = $attribute->getDefaultValue($this->getStructAttributeTypeAsPhpType($attribute));

return new PhpFunctionParameter(
lcfirst($attribute->getUniqueString($attribute->getCleanName(), 'method')),
Expand Down
10 changes: 2 additions & 8 deletions src/Generator/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,17 @@ public static function getStreamContextOptions(?string $basicAuthLogin = null, ?
public static function getValueWithinItsType($value, ?string $knownType = null)
{
if (is_int($value) || (!is_null($value) && in_array($knownType, [
'time',
'positiveInteger',
'unsignedLong',
'unsignedInt',
'short',
'long',
'int',
'integer',
], true))) {
return intval($value);
return (int) $value;
}
if (is_float($value) || (!is_null($value) && in_array($knownType, [
'float',
'double',
'decimal',
], true))) {
return floatval($value);
return (float) $value;
}
if (is_bool($value) || (!is_null($value) && in_array($knownType, [
'bool',
Expand Down
4 changes: 2 additions & 2 deletions src/Model/StructAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function isList(): bool
/**
* @return null|array|bool|float|int|string
*/
public function getDefaultValue()
public function getDefaultValue(?string $type = null)
{
if (($struct = $this->getTypeStruct()) && $struct->isStruct()) {
return null;
Expand All @@ -149,7 +149,7 @@ public function getDefaultValue()
'DefaultValue',
'defaultValue',
'defaultvalue',
]), $this->getType(true));
]), $type);
}

public function isRequired(): bool
Expand Down
1 change: 1 addition & 0 deletions tests/Generator/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testGetValueWithinItsType(): void
{
$this->assertSame('020', Utils::getValueWithinItsType('020', 'string'));
$this->assertSame('01', Utils::getValueWithinItsType('01', 'string'));
$this->assertSame(1, Utils::getValueWithinItsType('1', 'int'));
$this->assertSame(2.568, Utils::getValueWithinItsType('2.568', 'float'));
$this->assertSame(true, Utils::getValueWithinItsType('true', 'bool'));
$this->assertSame(false, Utils::getValueWithinItsType('false', 'bool'));
Expand Down
2 changes: 1 addition & 1 deletion tests/Model/StructAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testStructAttributeTypeMustBeBool(): void

$this->assertSame('boolean', $structAttribute->getType(true));
$this->assertSame('Success', $structAttribute->getType());
$this->assertFalse($structAttribute->getDefaultValue());
$this->assertFalse($structAttribute->getDefaultValue('boolean'));
}

public function testIsNullableMustReturnFalse(): void
Expand Down

0 comments on commit e388039

Please sign in to comment.