2

When using phpdoc doc blocks, is it possible to describe the keys for an array?

/**
 * @var array
 */
private $smtpSettings
    = array(
        'SMTPDebug' => (bool)true,
        'SMTPSecure' => (string) 'tls',
        'SMTPAuth' => (string)'',
        'Username' => (string)'',
        'Password' => (string)'',
        'Port' => (int)25,
        'Host' => (string)'',
    );

I want to show in the documentation that SMTPSecure can be tls/ssl/none along with details of what the other array keys are used for.

Also, what is best practice for documenting INI files and there default values? Can phpdoc generate any kind of docs for ini files? If not, is there a way to include an ini file in the generated phpdocs with links etc to it?

Thanks

Additional Info :

I had forgotten about the JB ArrayShape, but on testing this is great when your developing the software, but when running phpdoc, the output is not rendered very well :

array{ SMTPDebug: bool Enable debug, SMTPSecure: string Security type to use, SMTPAuth: string, Username: string SMTP Auth username, Password: string SMTP Auth password, Port: int, Host: string }

The info is just formatted into a single line of text that, although it has the details needed, is not very easy to read. Is this maybe more of a phpdoc template issue or is it just a restriction within phpdoc? Thanks

1
  • 2
    PHPStorm has JetBrains\PhpStorm\ArrayShape attribute that can be used for that. Look for similar alternative for your IDE Commented Sep 28, 2022 at 9:16

1 Answer 1

0

If you're using PhpStorm, you can use array shape syntax. It has some limitations, but works fine. Can be described like that:

/**
 * -- You can just describe all the keys in `{}` braces:
 *
 * @var array{key: string, key2: string} $variable Description of variable.
 *
 * -- Also, you can make it multiline:
 *
 * @var array{
 *     key: string,
 *     key2: string
 * } $variable Description of variable
 */
$variable = [
    'key'  => 'value',
    'key2' => 'value2'
];

/**
 * -- You can also describe deeper arrays:
 *
 * @var array{
 *     key: array{innerKey: string},
 *     key2: string
 * } $deepArray
 */
$deepArray = [
    'key' => [
        'innerKey' => 'value'
    ],
    'key2' => 'value2'
];

Example for the array provided in your question:

/**
 * @var array{
 *     SMTPDebug: bool,
 *     SMTPSecure: string,
 *     SMPTAuth: string,
 *     Username: string,
 *     Password: string,
 *     Port: int,
 *     Host: string
 * } $smtpSettings
 */
private $smtpSettings = array(
    'SMTPDebug'  => (bool)true,
    'SMTPSecure' => (string)'tls',
    'SMTPAuth'   => (string)'',
    'Username'   => (string)'',
    'Password'   => (string)'',
    'Port'       => (int)25,
    'Host'       => (string)'',
);

Preview of the feature working in PhpStorm

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.