Skip to content

Commit

Permalink
[php-*] Explicitly declare nullable parameters (#20524)
Browse files Browse the repository at this point in the history
* [php-nextgen] Explicitly declare nullable parameters explicitly

* Fix some deprecation warnings in other php generators

* [php-nextgen] Fix PHP 8.4 deprecation warnings with nullable/optional array parameters
  • Loading branch information
JulianVennen authored Jan 23, 2025
1 parent ad8de61 commit a68ad56
Show file tree
Hide file tree
Showing 104 changed files with 209 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,16 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
}

for (CodegenParameter param : operation.allParams) {
String paramType;
if (param.isArray || param.isMap) {
param.vendorExtensions.putIfAbsent("x-php-param-type", "array");
paramType = "array";
} else {
String paramType = param.dataType;
if ((!param.required || param.isNullable) && !paramType.equals("mixed")) { // optional or nullable but not mixed
paramType = "?" + paramType;
}
param.vendorExtensions.putIfAbsent("x-php-param-type", paramType);
paramType = param.dataType;
}
if ((!param.required || param.isNullable) && !paramType.equals("mixed")) { // optional or nullable but not mixed
paramType = "?" + paramType;
}
param.vendorExtensions.putIfAbsent("x-php-param-type", paramType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ApiClientFactory implements PM\ServiceFactoryInterface
$this->configKey = $configKey;
}

public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ApiClient
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): ApiClient
{
$config = new OAGAC\ApiClientOptions(\array_merge($this->getServiceConfig($container), $options ?? []));
return new ApiClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ApiClientFactory implements PM\ServiceFactoryInterface
$this->configKey = $configKey;
}

public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ApiClient
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): ApiClient
{
$config = new OAGAC\ApiClientOptions(\array_merge($this->getServiceConfig($container), $options ?? []));
return new ApiClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use Laminas\Stratigility\MiddlewarePipe;

class Factory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Application
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Application
{
$errorMiddleware = self::getErrorMiddleware($container);
$pipeline = new MiddlewarePipe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use Laminas\Stratigility\MiddlewarePipe;

class Factory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Application
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Application
{
$errorMiddleware = self::getErrorMiddleware($container);
$pipeline = new MiddlewarePipe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class Configuration
* @param array|null $variables hash of variable and the corresponding value (optional)
* @return string URL based on host settings
*/
public static function getHostString(array $hostSettings, int $hostIndex, array $variables = null): string
public static function getHostString(array $hostSettings, int $hostIndex, ?array $variables = null): string
{
if (null === $variables) {
$variables = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ObjectSerializer
*
* @return scalar|object|array|null serialized form of $data
*/
public static function sanitizeForSerialization(mixed $data, string $type = null, string $format = null): mixed
public static function sanitizeForSerialization(mixed $data, ?string $type = null, ?string $format = null): mixed
{
if (is_scalar($data) || null === $data) {
return $data;
Expand Down Expand Up @@ -388,7 +388,7 @@ class ObjectSerializer
*
* @return mixed a single or an array of $class instances
*/
public static function deserialize(mixed $data, string $class, array $httpHeaders = null): mixed
public static function deserialize(mixed $data, string $class, ?array $httpHeaders = null): mixed
{
if (null === $data) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ use {{invokerPackage}}\ObjectSerializer;
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
?ClientInterface $client = null,
?Configuration $config = null,
?HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
{{#discriminator}}
// Initialize discriminator property with the model name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class AuthApi
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
?ClientInterface $client = null,
?Configuration $config = null,
?HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ class BodyApi
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
?ClientInterface $client = null,
?Configuration $config = null,
?HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
Expand Down Expand Up @@ -1745,7 +1745,7 @@ public function testEchoBodyAllOfPetRequest(
* @return string
*/
public function testEchoBodyFreeFormObjectResponseString(
array $body = null,
?array $body = null,
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
): string
{
Expand All @@ -1766,7 +1766,7 @@ public function testEchoBodyFreeFormObjectResponseString(
* @return array of string, HTTP status code, HTTP response headers (array of strings)
*/
public function testEchoBodyFreeFormObjectResponseStringWithHttpInfo(
array $body = null,
?array $body = null,
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
): array
{
Expand Down Expand Up @@ -1893,7 +1893,7 @@ public function testEchoBodyFreeFormObjectResponseStringWithHttpInfo(
* @return PromiseInterface
*/
public function testEchoBodyFreeFormObjectResponseStringAsync(
array $body = null,
?array $body = null,
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
): PromiseInterface
{
Expand All @@ -1917,7 +1917,7 @@ function ($response) {
* @return PromiseInterface
*/
public function testEchoBodyFreeFormObjectResponseStringAsyncWithHttpInfo(
array $body = null,
?array $body = null,
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
): PromiseInterface
{
Expand Down Expand Up @@ -1970,7 +1970,7 @@ function ($exception) {
* @return \GuzzleHttp\Psr7\Request
*/
public function testEchoBodyFreeFormObjectResponseStringRequest(
array $body = null,
?array $body = null,
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
): Request
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class FormApi
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
?ClientInterface $client = null,
?Configuration $config = null,
?HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class HeaderApi
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
?ClientInterface $client = null,
?Configuration $config = null,
?HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class PathApi
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
?ClientInterface $client = null,
?Configuration $config = null,
?HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
Expand Down
26 changes: 13 additions & 13 deletions samples/client/echo_api/php-nextgen-streaming/src/Api/QueryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ class QueryApi
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
?ClientInterface $client = null,
?Configuration $config = null,
?HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
Expand Down Expand Up @@ -1853,7 +1853,7 @@ public function testQueryStyleDeepObjectExplodeTrueObjectAllOfRequest(
* @return string
*/
public function testQueryStyleFormExplodeFalseArrayInteger(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayInteger'][0]
): string
{
Expand All @@ -1874,7 +1874,7 @@ public function testQueryStyleFormExplodeFalseArrayInteger(
* @return array of string, HTTP status code, HTTP response headers (array of strings)
*/
public function testQueryStyleFormExplodeFalseArrayIntegerWithHttpInfo(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayInteger'][0]
): array
{
Expand Down Expand Up @@ -2001,7 +2001,7 @@ public function testQueryStyleFormExplodeFalseArrayIntegerWithHttpInfo(
* @return PromiseInterface
*/
public function testQueryStyleFormExplodeFalseArrayIntegerAsync(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayInteger'][0]
): PromiseInterface
{
Expand All @@ -2025,7 +2025,7 @@ function ($response) {
* @return PromiseInterface
*/
public function testQueryStyleFormExplodeFalseArrayIntegerAsyncWithHttpInfo(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayInteger'][0]
): PromiseInterface
{
Expand Down Expand Up @@ -2078,7 +2078,7 @@ function ($exception) {
* @return \GuzzleHttp\Psr7\Request
*/
public function testQueryStyleFormExplodeFalseArrayIntegerRequest(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayInteger'][0]
): Request
{
Expand Down Expand Up @@ -2171,7 +2171,7 @@ public function testQueryStyleFormExplodeFalseArrayIntegerRequest(
* @return string
*/
public function testQueryStyleFormExplodeFalseArrayString(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayString'][0]
): string
{
Expand All @@ -2192,7 +2192,7 @@ public function testQueryStyleFormExplodeFalseArrayString(
* @return array of string, HTTP status code, HTTP response headers (array of strings)
*/
public function testQueryStyleFormExplodeFalseArrayStringWithHttpInfo(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayString'][0]
): array
{
Expand Down Expand Up @@ -2319,7 +2319,7 @@ public function testQueryStyleFormExplodeFalseArrayStringWithHttpInfo(
* @return PromiseInterface
*/
public function testQueryStyleFormExplodeFalseArrayStringAsync(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayString'][0]
): PromiseInterface
{
Expand All @@ -2343,7 +2343,7 @@ function ($response) {
* @return PromiseInterface
*/
public function testQueryStyleFormExplodeFalseArrayStringAsyncWithHttpInfo(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayString'][0]
): PromiseInterface
{
Expand Down Expand Up @@ -2396,7 +2396,7 @@ function ($exception) {
* @return \GuzzleHttp\Psr7\Request
*/
public function testQueryStyleFormExplodeFalseArrayStringRequest(
array $query_object = null,
?array $query_object = null,
string $contentType = self::contentTypes['testQueryStyleFormExplodeFalseArrayString'][0]
): Request
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public function getHostSettings(): array
* @param array|null $variables hash of variable and the corresponding value (optional)
* @return string URL based on host settings
*/
public static function getHostString(array $hostSettings, int $hostIndex, array $variables = null): string
public static function getHostString(array $hostSettings, int $hostIndex, ?array $variables = null): string
{
if (null === $variables) {
$variables = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function getModelName(): string
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
$this->setIfExists('size', $data ?? [], null);
$this->setIfExists('color', $data ?? [], null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function getModelName(): string
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
$this->setIfExists('id', $data ?? [], null);
$this->setIfExists('name', $data ?? [], null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function getModelName(): string
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
parent::__construct($data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function getArrayStringEnumDefaultAllowableValues()
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
$this->setIfExists('array_string_enum_ref_default', $data ?? [], [["success","failure"]]);
$this->setIfExists('array_string_enum_default', $data ?? [], [["success","failure"]]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function getModelName(): string
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
$this->setIfExists('number', $data ?? [], null);
$this->setIfExists('float', $data ?? [], null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function getStatusAllowableValues()
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
$this->setIfExists('id', $data ?? [], null);
$this->setIfExists('name', $data ?? [], null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function getOutcomesAllowableValues()
*
* @param array $data Associated array of property values initializing the model
*/
public function __construct(array $data = null)
public function __construct(?array $data = null)
{
$this->setIfExists('id', $data ?? [], null);
$this->setIfExists('outcomes', $data ?? [], [["SUCCESS","FAILURE"]]);
Expand Down
Loading

0 comments on commit a68ad56

Please sign in to comment.