Skip to content

Commit

Permalink
[#13998][Bug][PHP] Move isNullable section to the top of the setter f…
Browse files Browse the repository at this point in the history
…unction in templates (#14005)

* Move isNullable section to the top

* Manage extra lines
  • Loading branch information
MustansirS committed Nov 21, 2022
1 parent 903ff0b commit a96777b
Show file tree
Hide file tree
Showing 43 changed files with 105 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,27 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
*/
public function {{setter}}(${{name}})
{
{{#isNullable}}
if (is_null(${{name}})) {
array_push($this->openAPINullablesSetToNull, '{{name}}');
} else {
$nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
$index = array_search('{{name}}', $nullablesSetToNull);
if ($index !== FALSE) {
unset($nullablesSetToNull[$index]);
$this->setOpenAPINullablesSetToNull($nullablesSetToNull);
}
}
{{/isNullable}}
{{^isNullable}}
if (is_null(${{name}})) {
throw new \InvalidArgumentException('non-nullable {{name}} cannot be null');
}
{{/isNullable}}
{{#isEnum}}
$allowedValues = $this->{{getter}}AllowableValues();
{{^isContainer}}
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues, true)) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}!in_array(${{{name}}}, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value '%s' for '{{name}}', must be one of '%s'",
Expand All @@ -401,7 +418,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
}
{{/isContainer}}
{{#isContainer}}
if ({{^required}}!is_null(${{name}}) && {{/required}}array_diff(${{{name}}}, $allowedValues)) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}array_diff(${{{name}}}, $allowedValues)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for '{{name}}', must be one of '%s'",
Expand All @@ -413,58 +430,39 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
{{/isEnum}}
{{#hasValidation}}
{{#maxLength}}
if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) > {{maxLength}})) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}(mb_strlen(${{name}}) > {{maxLength}})) {
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
}{{/maxLength}}
{{#minLength}}
if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) < {{minLength}})) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}(mb_strlen(${{name}}) < {{minLength}})) {
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
}
{{/minLength}}
{{#maximum}}
if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}(${{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
}
{{/maximum}}
{{#minimum}}
if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}(${{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
}
{{/minimum}}
{{#pattern}}
if ({{^required}}!is_null(${{name}}) && {{/required}}(!preg_match("{{{pattern}}}", ${{name}}))) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}(!preg_match("{{{pattern}}}", ${{name}}))) {
throw new \InvalidArgumentException("invalid value for \${{name}} when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.");
}
{{/pattern}}
{{#maxItems}}
if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) > {{maxItems}})) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}(count(${{name}}) > {{maxItems}})) {
throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
}{{/maxItems}}
{{#minItems}}
if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) < {{minItems}})) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}(count(${{name}}) < {{minItems}})) {
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
}
{{/minItems}}
{{/hasValidation}}

{{#isNullable}}
if (is_null(${{name}})) {
array_push($this->openAPINullablesSetToNull, '{{name}}');
} else {
$nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
$index = array_search('{{name}}', $nullablesSetToNull);
if ($index !== FALSE) {
unset($nullablesSetToNull[$index]);
$this->setOpenAPINullablesSetToNull($nullablesSetToNull);
}
}
{{/isNullable}}
{{^isNullable}}
if (is_null(${{name}})) {
throw new \InvalidArgumentException('non-nullable {{name}} cannot be null');
}
{{/isNullable}}

$this->container['{{name}}'] = ${{name}};

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,9 @@ public function getMapProperty()
*/
public function setMapProperty($map_property)
{

if (is_null($map_property)) {
throw new \InvalidArgumentException('non-nullable map_property cannot be null');
}

$this->container['map_property'] = $map_property;

return $this;
Expand All @@ -344,11 +342,9 @@ public function getMapOfMapProperty()
*/
public function setMapOfMapProperty($map_of_map_property)
{

if (is_null($map_of_map_property)) {
throw new \InvalidArgumentException('non-nullable map_of_map_property cannot be null');
}

$this->container['map_of_map_property'] = $map_of_map_property;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,9 @@ public function getUsername()
*/
public function setUsername($username)
{

if (is_null($username)) {
throw new \InvalidArgumentException('non-nullable username cannot be null');
}

$this->container['username'] = $username;

return $this;
Expand All @@ -344,11 +342,9 @@ public function getSingleRefType()
*/
public function setSingleRefType($single_ref_type)
{

if (is_null($single_ref_type)) {
throw new \InvalidArgumentException('non-nullable single_ref_type cannot be null');
}

$this->container['single_ref_type'] = $single_ref_type;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,9 @@ public function getClassName()
*/
public function setClassName($class_name)
{

if (is_null($class_name)) {
throw new \InvalidArgumentException('non-nullable class_name cannot be null');
}

$this->container['class_name'] = $class_name;

return $this;
Expand All @@ -350,11 +348,9 @@ public function getColor()
*/
public function setColor($color)
{

if (is_null($color)) {
throw new \InvalidArgumentException('non-nullable color cannot be null');
}

$this->container['color'] = $color;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,9 @@ public function getCode()
*/
public function setCode($code)
{

if (is_null($code)) {
throw new \InvalidArgumentException('non-nullable code cannot be null');
}

$this->container['code'] = $code;

return $this;
Expand All @@ -351,11 +349,9 @@ public function getType()
*/
public function setType($type)
{

if (is_null($type)) {
throw new \InvalidArgumentException('non-nullable type cannot be null');
}

$this->container['type'] = $type;

return $this;
Expand All @@ -380,11 +376,9 @@ public function getMessage()
*/
public function setMessage($message)
{

if (is_null($message)) {
throw new \InvalidArgumentException('non-nullable message cannot be null');
}

$this->container['message'] = $message;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,9 @@ public function getArrayArrayNumber()
*/
public function setArrayArrayNumber($array_array_number)
{

if (is_null($array_array_number)) {
throw new \InvalidArgumentException('non-nullable array_array_number cannot be null');
}

$this->container['array_array_number'] = $array_array_number;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,9 @@ public function getArrayNumber()
*/
public function setArrayNumber($array_number)
{

if (is_null($array_number)) {
throw new \InvalidArgumentException('non-nullable array_number cannot be null');
}

$this->container['array_number'] = $array_number;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,16 @@ public function getArrayOfString()
*/
public function setArrayOfString($array_of_string)
{
if (is_null($array_of_string)) {
throw new \InvalidArgumentException('non-nullable array_of_string cannot be null');
}

if (!is_null($array_of_string) && (count($array_of_string) > 3)) {
if ((count($array_of_string) > 3)) {
throw new \InvalidArgumentException('invalid value for $array_of_string when calling ArrayTest., number of items must be less than or equal to 3.');
}
if (!is_null($array_of_string) && (count($array_of_string) < 0)) {
if ((count($array_of_string) < 0)) {
throw new \InvalidArgumentException('invalid length for $array_of_string when calling ArrayTest., number of items must be greater than or equal to 0.');
}

if (is_null($array_of_string)) {
throw new \InvalidArgumentException('non-nullable array_of_string cannot be null');
}

$this->container['array_of_string'] = $array_of_string;

return $this;
Expand All @@ -366,11 +364,9 @@ public function getArrayArrayOfInteger()
*/
public function setArrayArrayOfInteger($array_array_of_integer)
{

if (is_null($array_array_of_integer)) {
throw new \InvalidArgumentException('non-nullable array_array_of_integer cannot be null');
}

$this->container['array_array_of_integer'] = $array_array_of_integer;

return $this;
Expand All @@ -395,11 +391,9 @@ public function getArrayArrayOfModel()
*/
public function setArrayArrayOfModel($array_array_of_model)
{

if (is_null($array_array_of_model)) {
throw new \InvalidArgumentException('non-nullable array_array_of_model cannot be null');
}

$this->container['array_array_of_model'] = $array_array_of_model;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,9 @@ public function getSmallCamel()
*/
public function setSmallCamel($small_camel)
{

if (is_null($small_camel)) {
throw new \InvalidArgumentException('non-nullable small_camel cannot be null');
}

$this->container['small_camel'] = $small_camel;

return $this;
Expand All @@ -372,11 +370,9 @@ public function getCapitalCamel()
*/
public function setCapitalCamel($capital_camel)
{

if (is_null($capital_camel)) {
throw new \InvalidArgumentException('non-nullable capital_camel cannot be null');
}

$this->container['capital_camel'] = $capital_camel;

return $this;
Expand All @@ -401,11 +397,9 @@ public function getSmallSnake()
*/
public function setSmallSnake($small_snake)
{

if (is_null($small_snake)) {
throw new \InvalidArgumentException('non-nullable small_snake cannot be null');
}

$this->container['small_snake'] = $small_snake;

return $this;
Expand All @@ -430,11 +424,9 @@ public function getCapitalSnake()
*/
public function setCapitalSnake($capital_snake)
{

if (is_null($capital_snake)) {
throw new \InvalidArgumentException('non-nullable capital_snake cannot be null');
}

$this->container['capital_snake'] = $capital_snake;

return $this;
Expand All @@ -459,11 +451,9 @@ public function getScaEthFlowPoints()
*/
public function setScaEthFlowPoints($sca_eth_flow_points)
{

if (is_null($sca_eth_flow_points)) {
throw new \InvalidArgumentException('non-nullable sca_eth_flow_points cannot be null');
}

$this->container['sca_eth_flow_points'] = $sca_eth_flow_points;

return $this;
Expand All @@ -488,11 +478,9 @@ public function getAttName()
*/
public function setAttName($att_name)
{

if (is_null($att_name)) {
throw new \InvalidArgumentException('non-nullable att_name cannot be null');
}

$this->container['att_name'] = $att_name;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,9 @@ public function getDeclawed()
*/
public function setDeclawed($declawed)
{

if (is_null($declawed)) {
throw new \InvalidArgumentException('non-nullable declawed cannot be null');
}

$this->container['declawed'] = $declawed;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,9 @@ public function getDeclawed()
*/
public function setDeclawed($declawed)
{

if (is_null($declawed)) {
throw new \InvalidArgumentException('non-nullable declawed cannot be null');
}

$this->container['declawed'] = $declawed;

return $this;
Expand Down
Loading

0 comments on commit a96777b

Please sign in to comment.