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

Conversation

ClemRiviere
Copy link
Contributor

  • Refactoring the getter prefixes for make:entity
  • Introducing exceptions for boolean fields : not adding the prefix to getter name if field begins with "is" or "has"

Related to #1492

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes sense to me. It's consistent with the autodetection of the type (boolean) when asking for the type.
I'd suggest doing the same for setters, but only for the "is" prefix: isInternational property => setInternational. (but hasColor => setHasColor)

src/Util/ClassSourceManipulator.php Outdated Show resolved Hide resolved
Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good @ClemRiviere! - 1 minor comment. Also if you're up for doing the same for addSetter() as @nicolas-grekas suggested, that would be even more awesome!

src/Util/ClassSourceManipulator.php Outdated Show resolved Hide resolved
@jrushlow jrushlow added Feature New Feature Status: Needs Work Additional work is needed labels Mar 27, 2024
Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ClemRiviere!

@jrushlow jrushlow added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Work Additional work is needed labels Mar 27, 2024
@jrushlow jrushlow merged commit 876c779 into symfony:main Mar 27, 2024
6 checks passed
@ClemRiviere ClemRiviere deleted the 1492_boolean_getter_prefix branch April 2, 2024 08:27
@jrushlow jrushlow mentioned this pull request Apr 5, 2024
2 tasks
@stof
Copy link
Member

stof commented Jun 19, 2024

This change does not make sense to me as it makes those setter incompatible with the property-access component (and so with the form component that relies on it)

@toby-griffiths
Copy link

A link wasn't added automatically (maybe becaues it was a discussion, not an issue), but this is now causing property access issues as discussed in symfony/symfony#54885 (reply in thread).

@ClemRiviere
Copy link
Contributor Author

@stof I understand, I think we have two potential solutions for this :

  • rollback my work on setters so isInternational property becomes isInternational() getter and setIsInternational(bool $isInternational) setter in maker-bundle
  • adapt property-access component to manage is and has prefixes

I would personally go for the first solution, @jrushlow or @nicolas-grekas do you guys have any advices on this topic ?

kbond added a commit that referenced this pull request Dec 10, 2024
… setters (octoseth)

This PR was merged into the 1.x-dev branch.

Discussion
----------

[make-entity]Keep the 'is' prefixes for booleans properties setters

When generating a new entity, if we name a boolean property with the prefix "is", this prefix is removed to construct the setter.

example:
 ```
bool $isInternational;

public function isInternational(): ?bool
{
    return $this->isInternational;
}

public function setInternational(bool $isInternational): static
{
    $this->isInternational = $isInternational;

    return $this;
}
```
This is breaking when we want to access the property via a form :

> [NoSuchPropertyException]
> HTTP 500 Internal Server Error
> The method "isInternational" in class "App\Entity\Conference" requires 0 arguments, but should accept only 1.. Make the property public, add a setter, or set the "mapped" field option in the form type to be false.

This PR revert this change introduce in [PR 1493](#1493) to keep the 'is' prefix on the setter.

example :

```
public function setIsInternational(bool $isInternational): static
{
    $this->isInternational = $isInternational;

    return $this;
}

```

Commits
-------

9fa9221 Keep the 'is' prefix on a boolean property when making the setter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants