Skip to content

Conversation

@gchehami
Copy link

@gchehami gchehami commented Jan 27, 2026

Description

This PR fixes the form detection logic to check if a class implements FormTypeInterface instead of checking if the class name ends with "Type" only.

Fixes #184

Changes

  • Modified FormTrait.php to check for FormTypeInterface implementation
  • Now supports forms with any naming convention (e.g., MyForm, MyFormType, CustomFormType)

Motivation

There are no rules that impose naming forms with the word "Type" at the end of classnames. Developers may name their forms with different suffixes like "Form" or any other convention. The correct way is to check if the class implements the FormTypeInterface.

Testing

  • Existing tests should pass
  • Forms not ending with "Type" are now correctly detected

@bocharsky-bw
Copy link
Member

Yeah, you're right, checking for the interface is much better... I would say maybe we should drop the legacy way of checking for Type suffix in favor of your way based on the interface implementation? WDYT?

Also, could you please check the failed SA tools?

if ($node instanceof Stmt\Class_) {
$this->isFormType = 'Type' === substr($node->name, -4);
$this->isFormType = 'Type' === substr($node->name, -4) || in_array(
'Symfony\Component\Form\FormTypeInterface',
Copy link
Member

Choose a reason for hiding this comment

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

Can't we use FormTypeInterface::class here instead of the string?

Copy link
Author

Choose a reason for hiding this comment

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

If you want to use FormTypeInterface::class here instead of the string you need to have the symfony/form package installed as a dependency

Copy link
Member

@bocharsky-bw bocharsky-bw Jan 27, 2026

Choose a reason for hiding this comment

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

OK, let's keep it simple for now

@gchehami
Copy link
Author

Yeah, you're right, checking for the interface is much better... I would say maybe we should drop the legacy way of checking for Type suffix in favor of your way based on the interface implementation? WDYT?

Also, could you please check the failed SA tools?

I initially considered this approach, but a colleague pointed out that it could be a breaking change for users who have classes ending with "Type" that don't implement the interface.

i check for the failed SA tools

@bocharsky-bw
Copy link
Member

I initially considered this approach, but a colleague pointed out that it could be a breaking change for users who have classes ending with "Type" that don't implement the interface.

Hm, but we're talking about forms here, and you always have to implement that interface for your form types, right? If so, I would not consider it as the BC break actually. I can't think of when you have a form type but don't extend that interface. Do you have any use cases for this?

@gchehami
Copy link
Author

gchehami commented Jan 28, 2026

I initially considered this approach, but a colleague pointed out that it could be a breaking change for users who have classes ending with "Type" that don't implement the interface.

Hm, but we're talking about forms here, and you always have to implement that interface for your form types, right? If so, I would not consider it as the BC break actually. I can't think of when you have a form type but don't extend that interface. Do you have any use cases for this?

No i don't have any use case for this so okay i remove the check

Guillaume Chehami added 2 commits January 28, 2026 09:08
…nterface instead of checking the classname to determinate if the class is a form
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

Successfully merging this pull request may close these issues.

Symfony FormTrait: should check instanceof instead of name

2 participants