Skip to content

Properties with nullable types have incorrect type hint set #28

@Gert-dev

Description

@Gert-dev

This property:

/**
 * @var StorageInterface|null
 */
private $storage;

Generates the following getter and setter:

/**
 * Retrieves the storage that is currently set.
 *
 * @return StorageInterface|null
 */
public function getStorage(): null
{
    return $this->storage;
}

/**
 * Sets the storage to use.
 *
 * @param StorageInterface|null $storage
 *
 * @return void
 */
public function setStorage($storage): void
{
    $this->storage = $storage;
}

As you can see, the setter is missing a type hint and the getter has an incorrect type hint. This is likely due to the use of nullability.

The expected behavior is that the return type hint for the getter and the type hint of the setter parameter is ?StorageInterface. In PHP 8.0, StorageInterface|null would also be valid due to union types, but the former would still be supported.

For completeness, my templates:

module.exports = (property) => `
    /**
     * Sets the ${property.getName()} to use.
     *
     * @param ${property.getType() ? property.getType() : 'mixed'} \$${property.getName()}
     *
     * @return void
     */
    public function ${property.setterName()}(${property.getTypeHint() ? property.getTypeHint() + ' ' : ''}\$${property.getName()}): void
    {
        $this->${property.getName()} = \$${property.getName()};
    }
`
module.exports = (property) => `
    /**
     * Retrieves the ${property.getName()} that is currently set.
     *
     * @return ${property.getType() ? property.getType() : 'mixed'}
     */
    public function ${property.getterName()}()${property.getType() ? (': ' + property.getTypeHint()) : ''}
    {
        return $this->${property.getName()};
    }
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions