Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties with nullable types have incorrect type hint set #28

Open
Gert-dev opened this issue Dec 7, 2019 · 1 comment
Open

Properties with nullable types have incorrect type hint set #28

Gert-dev opened this issue Dec 7, 2019 · 1 comment

Comments

@Gert-dev
Copy link

Gert-dev commented Dec 7, 2019

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()};
    }
`
@sparrowek
Copy link

Same problem for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants