cvergne/vscode-php-getters-setters

Use of ?<property>

lecneri opened this issue · 5 comments

Example

/**
 * @var string|null
 */
private ?string $var;

Will generate as getter

public function getVar(): string|null { 
   return $var; 
}

It could identify this and generate the following code:

public function getVar(): ?string { 
   return $var; 
}

Hi @lecneri,

With the default behavior on my side, nothing is append after the getter.
Are you using the template feature ?
That something I haven't used yet and I need to take a look since I forked the project.

@lecneri By default it should support it and only has a bug, that the question mark is shown in the doc block. Therefore I am using the following templates, maybe these are helpful.

getter.js

module.exports = (property) => `
	/**
	 * ${property.getterDescription()}
	 *
	 * @return ${property.getType() ? (property.getType().substr(0, 1) === '?' ? property.getType().replace('?', '') + '|null' : property.getType()) : 'mixed'}
	 */
	public function ${property.getterName()}(): ${property.getType() ? property.getType() : 'mixed'} {
		return $this->${property.getName()};
	}
`

setter.js

module.exports = (property) => `
	/**
	 * ${property.setterDescription()}
	 *
	 * @param ${property.getType() ? (property.getType().substr(0, 1) === '?' ? property.getType().replace('?', '') + '|null' : property.getType()) : 'mixed'} \$${property.getName()}  ${property.getDescription() ? property.getDescription() : ''}
	 *
	 * @return self
	 */
	public function ${property.setterName()}(${property.getTypeHint() ? property.getTypeHint() + ' ' : ''}\$${property.getName()}): self {
		$this->${property.getName()} = \$${property.getName()};
		return $this;
	}
`

With the templates above it should correctly print

/**
 * Get the value of example
 *
 * @return string|null
 */
public function getExample(): ?string {
	return $this->example;
}

instead of the default:

/**
 * Get the value of example
 *
 * @return ?string
 */
public function getExample(): ?string {
	return $this->example;
}

I'll try it out. Thank you!

Nullable properties should now be correctly handlded in v1.5.0 (pre-release, see #17).
If you can give it a try, please give me a feedback :)

I close the issue as it has been release in v1.6.0.