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

[make:entity] managing keyword prefixes (is, has) for boolean properties getters #1493

Merged
merged 10 commits into from
Mar 27, 2024
16 changes: 6 additions & 10 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,27 +270,23 @@ public function addAccessorMethod(string $propertyName, string $methodName, $ret

public function addGetter(string $propertyName, $returnType, bool $isReturnTypeNullable, array $commentLines = []): void
{
$prefix = $this->getGetterPrefix($propertyName, $returnType);
$methodName = $prefix ? $prefix.Str::asCamelCase($propertyName) : Str::asLowerCamelCase($propertyName);
$methodName = $this->getGetterName($propertyName, $returnType);
$this->addCustomGetter($propertyName, $methodName, $returnType, $isReturnTypeNullable, $commentLines);
}

/**
* @return string|null : getter prefix or null if no prefix to append
*/
private function getGetterPrefix($propertyName, $returnType): ?string
private function getGetterName($propertyName, $returnType): ?string
ClemRiviere marked this conversation as resolved.
Show resolved Hide resolved
ClemRiviere marked this conversation as resolved.
Show resolved Hide resolved
{
if ('bool' !== $returnType) {
return 'get';
return 'get' . Str::asCamelCase($propertyName);
}

// exclude is & has from getter definition if already in property name
$propertyName = strtolower($propertyName);
if (!str_starts_with($propertyName, 'is') && !str_starts_with($propertyName, 'has')) {
return 'is';
if (!strncasecmp($propertyName, 'is', 2) && !strncasecmp($propertyName, 'has', 3)) {
return 'is' . Str::asCamelCase($propertyName);
}

return null;
return Str::asLowerCamelCase($propertyName);
}

public function addSetter(string $propertyName, ?string $type, bool $isNullable, array $commentLines = []): void
Expand Down
Loading