vscode-php-getters-setters icon indicating copy to clipboard operation
vscode-php-getters-setters copied to clipboard

Properties with nullable types have incorrect type hint set

Open Gert-dev opened this issue 5 years ago • 1 comments

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()};
    }
`

Gert-dev avatar Dec 07 '19 17:12 Gert-dev

Same problem for me!

sparrowek avatar Aug 17 '20 10:08 sparrowek