Skip to content

fix: auto-register services with attribute-based constructors#315

Merged
usernane merged 1 commit intomainfrom
dev
May 4, 2026
Merged

fix: auto-register services with attribute-based constructors#315
usernane merged 1 commit intomainfrom
dev

Conversation

@usernane
Copy link
Copy Markdown
Member

@usernane usernane commented May 4, 2026

Summary

Generic fix for App::autoRegisterHelper() to handle classes whose constructors cannot accept the provided arguments, such as attribute-based services using #[RestController].

Motivation

registerServices() passes [$this] (a manager instance) as constructor arguments. Attribute-based services have __construct(string $name = ''), causing a TypeError that was silently swallowed by catch (Error $ex) {}. Services were skipped without any warning, resulting in "Service not supported" errors at runtime. Fixes #313.

Changes

  • Modified autoRegisterHelper() to check constructor parameter compatibility before calling newInstanceArgs()
  • If args are incompatible, falls back to no-arg instantiation via newInstance()
  • Added canAcceptArgs() — uses reflection to verify argument/parameter type compatibility (handles builtin types, class types, union types, nullable)
  • Added argMatchesType() — checks a single argument against a ReflectionNamedType
  • Added test fixtures: App/Apis/AttributedTest/ and App/Apis/AttributedTest2/
  • Added AutoRegisterTest with 3 test cases

How to Test / Verify

Unit tests:

composer test -- --filter AutoRegisterTest
  • testAutoRegisterAttributedService — verifies attribute-based services are discovered and registered
  • testAutoRegisterAttributedServiceWithIncompatibleConstructorArgs — reproduces the exact bug (manager object passed as constructor arg)
  • testAutoRegisterConstructorBasedServiceWithSuffix — verifies existing constructor-based registration still works

Breaking Changes and Migration Steps

None. The fix is backward-compatible — constructor-based services continue to work as before. The only behavioral change is that classes with incompatible constructor args are now instantiated with no args instead of being silently skipped.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • The title of the pull request follows Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed)
  • I ran lint/cs-fixer (if applicable)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #313

@usernane usernane merged commit a897a10 into main May 4, 2026
0 of 9 checks passed
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.

bug(App::autoRegister): registerServices() silently fails with attribute-based services

1 participant