Skip to content

Conversation

@dereuromark
Copy link
Collaborator

@dereuromark dereuromark commented Oct 12, 2025

I really wonder how this wasnt an issue so far, and I now run into this in my apps..

Resolves #51

The AssociationTableMixinClassReflectionExtension was handling magic findBy* methods correctly for direct Table classes, but when these
methods were called on Association properties, PHPStan would:

  1. Check if the Association has the method via hasMethod()
  2. Delegate to Table::hasMethod() without first checking for the findBy* pattern, returning false for magic methods
  3. Try to get the method via getNativeMethod(), which throws an error since magic methods don't exist natively

Solution

Added checks in both hasMethod() and getMethod() in AssociationTableMixinClassReflectionExtension to properly handle findBy* methods on Association classes:

In hasMethod():

  // magic findBy* method on Association
  if (preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
      return true;
  }

In getMethod():

  // magic findBy* method on Association
  if ($classReflection->isSubclassOf(Association::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
      return new TableFindByPropertyMethodReflection($methodName, $this->getTableReflection());
  }

@dereuromark dereuromark added the bug Something isn't working label Oct 12, 2025
@dereuromark dereuromark marked this pull request as draft October 12, 2025 11:07
@rochamarcelo
Copy link
Contributor

Does it happen only with latest CakePHP version?

I wonder if this is related to CakePHP, annotation or something else.

@dereuromark
Copy link
Collaborator Author

No idea
I just updated a project and ran into it.

The combine PR contains fix and enhancement

@rochamarcelo
Copy link
Contributor

As reference it worked before because we had \App\Model\Table\UsersTable&\Cake\ORM\Association\HasMany annotations and now we use \Cake\ORM\Association\HasMany<\App\Model\Table\UsersTable>. That's why we didn't noticed the issue before.

Thank you.

@rochamarcelo rochamarcelo marked this pull request as ready for review October 12, 2025 13:32
@rochamarcelo rochamarcelo self-requested a review October 12, 2025 13:33
@rochamarcelo rochamarcelo merged commit 2c77907 into CakeDC:4.next-cake5 Oct 12, 2025
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal error on find()

2 participants