Skip to content

Instantly share code, notes, and snippets.

@mdalaminbey
Last active September 19, 2024 05:47
Show Gist options
  • Select an option

  • Save mdalaminbey/81e68d998905f189592f817e43cc4a9a to your computer and use it in GitHub Desktop.

Select an option

Save mdalaminbey/81e68d998905f189592f817e43cc4a9a to your computer and use it in GitHub Desktop.
VS Code PHP Getter And Setter SnakeCase Template https://github.com/cvergne/vscode-php-getters-setters
// Getter
module.exports = (property) => {
// Determine the return type based on the property type
const returnType = property.isNullable() ? `?${property.getType()}` : property.getType();
return `
/**
* ${property.getterDescription()}
*
* @return ${returnType || 'mixed'}
*/
public function ${toSnakeCase(property.getterName())}(): ${returnType || 'mixed'} {
return $this->${property.getName()};
}
`;
};
function toSnakeCase(str) {
return str.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
}
//Setter
module.exports = (property) => {
// Determine the type hint based on whether the property is nullable
const typeHint = property.isNullable() ? `?${property.getTypeHint()}` : property.getTypeHint();
return `
/**
* ${property.setterDescription()}
*
* @param ${typeHint || 'mixed'} \$${property.getName()} ${property.getDescription() || ''}
*
* @return self
*/
public function ${toSnakeCase(property.setterName())}( ${typeHint ? typeHint + ' ' : '' }\$${property.getName()} ): self {
$this->${property.getName()} = \$${property.getName()};
return $this;
}
`;
};
function toSnakeCase(str) {
return str.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment