Skip to main content

Improved PHP Code Samples

ยท 7 min read

The generated PHP code samples are now idiomatic with no errors. This improves the developer experience by allowing developers to run the code samples without any error in their applications and to get started quickly.

Detailsโ€‹

This release introduces significant improvements to the code samples provided for your API. We've made several updates to the code samples to ensure that they are up-to-date, easier to understand, and more user-friendly. The new code samples are designed to help developers get started with your API quickly and easily, without having to spend a lot of time reading documentation or trying to figure out how to use the API.

What Has Changed?โ€‹

Following major improvements are now made part of the PHP code samples:

  • Updated code samples using PHP's phan analyzer checks and guidelines.

  • Simplified code samples using phpcs linter with PSR12 standards and ruleset to make them easier to read and understand using the publically available configuration.

  • Removed unused imports from generated code samples.

  • Added comments and explanations to the code samples to provide more context and help developers understand how to use the API.

  • Fixed bugs and errors in the code samples to ensure that they work as expected.

Parameters Initialization Section Improvementsโ€‹

Model Initializationโ€‹

Made the model's field initialization in-line which ensures that the object structure is intact, enhancing the code readability.

$employee = EmployeeBuilder::init(
'John',
22,
PersonBuilder::init(
'Doe',
32
)->build()
)
->hiredAt(DateTimeHelper::fromRfc1123DateTime('Mon, 15 Jun 2009 20:45:30 GMT'))
->build();

Array Initializationโ€‹

Made Array initialization more idiomatic and readable by writing it in multiple lines instead of cluttering it in the same line.

$stringArray = [
'Bob',
'Alice',
'John'
];

Take another example of nested map inside arrays. Array of Map initialization is now more idiomatic and readable.

$stringArrayOfMap = [
[
'key0' => 'Bob'
],
[
'key0' => 'Alice',
'key1' => 'John'
]
];

Map Initializationโ€‹

Made Map initialization more readable by writing it in multiple lines instead of cluttering it in the same line.

$stringMap = [
'key0' => 'Bob',
'key1' => 'Alice',
'key2' => 'John'
];

Take another example of nested array inside maps. Map of Array initialization is now more readable and idiomatic.

$stringMapOfArray = [
'key0' => [
'Bob',
'Alice'
],
'key1' => [
'Bob',
'Alice',
'John'
],
'key2' => [
'Doe'
]
];

Multi Dimensional Array Initializationโ€‹

We are now also supporting code samples for Multi dimensional arrays.

$string2dArray = [
[
'Bob',
'Alice'
],
[
'John',
'Doe'
]
];

Imports Section Improvementsโ€‹

Replaced Fully Qualified Names with importsโ€‹

Previously, Models and Utility Classes were referred by their full paths in the Parameters Declaration section but we have now resolved this issue by using the proper imports for these utility classes.

// imports section
use TesterLib\ApiHelper;

// parameters initialization section
$param = ApiHelper::deserialize('{"key1":"val1","key2":"val2"}');

Unused importsโ€‹

Previously, imports were added even if they were not required in any other sections, but now we are adding imports on demand for optional parameters:

// imports section
use TesterLib\Models\Builders\ModelBuilder;

// parameters initialization section
$model = ModelBuilder::init()->build();
$nativeString = 'some string value';

Without the model parameter, the above code sample would look like:

// empty imports section

// parameters initialization section
$nativeString = 'some string value';

Client Initialization Section Improvementsโ€‹

Proximity Principleโ€‹

Client initialization section now follows Principle of Proximity, as it is now declared close to API Call Section, where it is being used.

$param1 = 'string value 1';

$param2 = 'string value 2';

// client initialization section
$client = TesterClientBuilder::init()
->environment('production')
->port('8080')
->suites(SuiteCode::CLUBS)
->build();

// API call section
$client->getQueryParamController()->sendData(
$param1,
$param2
);

Default Configuration Setupโ€‹

We have now followed the style guideline of calling the build() method on the same line when no other configurations are customized.

$client = TesterClientBuilder::init()->build();

API Call Section Improvementsโ€‹

Multi line argumentsโ€‹

Following the style guideline of PHP, we have addressed the styling of an API call method with multiple parameters and converted them into multi-lines to have a better readability of an endpoint call.

$client->getQueryParamController()->sendData(
$param1,
$param2,
$param3
);

Elimination of controller's referenceโ€‹

We removed the initialization of the controller instance variable and gave direct access to it using the $client variable.

$client->getQueryParamController()->sendData($data);

Full Code Sample Comparisonโ€‹

The following code shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for Query Param Controller endpoint Send Employee.

Employee Modelโ€‹

{
"name": "John",
"age": 23,
"boss": {
"name": "Doe",
"age": 32,
},
"hiredAt": "Mon, 15 Jun 2009 20:45:30 GMT"
}

Generated code samplesโ€‹

<?php

require_once "vendor/autoload.php";

use TesterLib\TesterClientBuilder;
use TesterLib\Exceptions\ApiException;
use TesterLib\Models\Builders\EmployeeBuilder;
use TesterLib\Models\Builders\PersonBuilder;
use TesterLib\Utils\DateTimeHelper;
use TesterLib\Models\SuiteCode;

$employee = EmployeeBuilder::init(
'John',
22,
PersonBuilder::init(
'Doe',
32
)->build()
)
->hiredAt(DateTimeHelper::fromRfc1123DateTime('Mon, 15 Jun 2009 20:45:30 GMT'))
->build();

$client = TesterClientBuilder::init()
->environment('production')
->port('8080')
->suites(SuiteCode::CLUBS)
->build();

try {
$result = $client->getQueryParamController()->sendEmployee($employee);
var_dump($result);
} catch (ApiException $e) {
echo 'Caught ApiException: ', $e->getMessage(), "\n";
}

Bugs fixedโ€‹

  • Fixed multiple Indentation issues in parameters initialization section
  • Removed trailing comma from the endpoint call
  • Fixed issues with File and Binary request parameters