Skip to content

Conversation

@dereuromark
Copy link
Member

Summary

This PR fixes two bugs reported in #350:

Bug 1: EntityPatchRector was overly broad

  • Problem: Changed ANY ->set([array]) to ->patch([array]) without checking object type
  • Solution: Added type checking to only transform Cake\ORM\Entity instances
  • Implementation: Uses ObjectType check and isInstanceOf(Entity::class) validation

Bug 2: newExpr()->count() pattern not handled correctly

  • Problem: $query->newExpr()->count() was being transformed to $query->expr()->count() which is invalid
  • Expected: Should transform to $query->func()->count('*') since it's creating aggregate functions
  • Solution: Created NewExprToFuncRector that runs before the general newExpr → expr rename
  • Applied to: Both cakephp44 and cakephp53 rulesets

Test plan

  • Code style checks pass
  • Existing tests pass (1 pre-existing unrelated test failure in FileRenameCommandTest)
  • NewExprToFuncRector only transforms Query objects with newExpr()->count() pattern
  • EntityPatchRector only transforms Entity objects

Related Issues

Fixes #350

🤖 Generated with Claude Code

This commit addresses two bugs reported in issue #350:

1. EntityPatchRector now only transforms Entity objects
   - Added type checking to ensure only Cake\ORM\Entity instances
     have their set() calls converted to patch()
   - Previously transformed ANY object's set([array]) to patch([array])
   - Now checks object type using ObjectType and isInstanceOf()

2. NewExprToFuncRector handles newExpr()->count() pattern
   - Created new rector to transform $query->newExpr()->count()
     to $query->func()->count('*')
   - This pattern requires func() not expr() as newExpr()->count()
     creates aggregate functions
   - Runs before general newExpr → expr rename in both cakephp44
     and cakephp53 rulesets to catch this specific pattern first

Fixes #350

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@LordSimal
Copy link
Contributor

please add tests like we have for all the other custom rector rules

dereuromark and others added 2 commits November 10, 2025 16:24
Extended the rector to transform all common FunctionsBuilder method calls,
not just count(). Now handles:

- Aggregate functions: count(), sum(), avg(), min(), max(), rowNumber(),
  lag(), lead()
- Date/time functions: dateDiff(), datePart(), extract(), dateAdd(), now(),
  weekday(), dayOfWeek()
- Other SQL functions: concat(), coalesce(), cast(), rand(), jsonValue(),
  aggregate()

This ensures that any pattern like $query->newExpr()->sum() is correctly
transformed to $query->func()->sum() instead of $query->expr()->sum().

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Added test coverage for both rector rules with multiple test cases:

EntityPatchRector tests:
- Transforms Entity->set([...]) to Entity->patch([...])
- Skips non-Entity objects (the key bug fix)
- Skips set() calls with variables (not array literals)
- Skips set() calls with non-array arguments

NewExprToFuncRector tests:
- Transforms all aggregate functions (count, sum, avg, min, max)
- Transforms date/time functions (now, dateDiff, extract)
- Transforms other SQL functions (concat, coalesce, rand)
- Adds '*' argument to count() when missing
- Skips non-FunctionsBuilder methods (eq, gt, and, etc.)
- Skips non-Query objects

Also fixed NewExprToFuncRector to check for ObjectType before calling
isInstanceOf() to prevent ErrorType crashes.

All tests pass (42 tests, 123 assertions).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@dereuromark
Copy link
Member Author

Should be all done.

@LordSimal LordSimal merged commit 7ff6c9e into 5.x Nov 10, 2025
2 of 3 checks passed
@LordSimal LordSimal deleted the fix-issue-350-rector-bugs branch November 10, 2025 15:33
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.

EntityPatchRector issues

3 participants