I have a method that depends on many other classes like this
public function getProfileData(
ProfilesService $profile_service,
ContactInfoService $contact_info_service,
CoursesService $courses_service,
InterestsService $interests_service,
LanguagesService $languages_service,
PersonalInfoService $personal_info_service,
ProjectsService $projects_service,
SkillsService $skills_service,
AwardsService $awards_service,
EducationsService $education_service,
ExperiencesService $experiences_service,
TargetJobsService $target_jobs_service,
ProfileHiddenSectionsService $hidden_sections_service) { }
I read about dependency injection and I know that if you exceed 6-10 dependencies it leads you to a code smell.
But this getProfileData()
method really need all of these dependencies so what is the best practice to solve this problem ?!
The code smell you are experiencing is called Constructor over-injection (and this particular variation is Method over-injection). As @Nkosi said in the comments, the source of this is a Single Responsibility Principle violation.
How to solve this problem, however, depends pretty much on the situation. Chapter 6 from the book Dependency Injection: Principles, Practices, and Patterns actually contains a very elaborate description of your options. In short, among other things, you can use the following refactorings: