Skip to main content

Updating Type Hints for Dictionaries in PHP SDKs

· 2 min read

APIMatic CodeGen has made improvements by updating type hints of dictionaries in PHP SDKs to better capture the type information.

Details

Previously, PHP SDKs were using the type array for a dictionary of any type. For example, following is a model named TeamMembersRequest with a field teamMembers of type Dictionary<string,TeamMember>. In PHP, this model would look like:

class TeamMembersRequest
{
/**
* @var array
*/
private $teamMembers;
}

Using array not only loses the dictionary's inner type information but also affects the users Developer Experience. Likewise in PHP, type array means a dictionary of mixed types, which leads to type hint array in PhpStrom IDE, as shown:

PHP Image IDE

What has Changed?

To improve the code readability, correctness and to preserve the inner type of a dictionary in PHP, APIMatic now uses the Phan analyzer's approach of dealing with maps. Therefore, the above-mentioned snippet of a model class TeamMembersRequest would now look like:

class TeamMembersRequest
{
/**
* @var array<string,TeamMember>
*/
private $teamMembers;
}

This code leads to TeamMember[] type hint in PhpStorm IDE which is equivalent to a PHP map of TeamMember. These type hints are later used by JsonMapper to deserialize a JSON string into the model and the suppport for map of type A i.e. array<string,A> with the release of JsonMapper.

PHP Image 2