Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2830

Class constructors should not create other objects

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this object instantiation; make "xxx" a parameter instead.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      JavaScript
    • Covered Languages:
      PHP
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min

      Description

      Dependency injection is a software design pattern in which one or more dependencies (or services) are injected, or passed by reference, into a dependent object (or client) and are made part of the client's state. The pattern separates the creation of a client's dependencies from its own behavior, which allows program designs to be loosely coupled and to follow the dependency inversion and single responsibility principles.

      Noncompliant Code Example

      class SomeClass {
      
        public function __construct() {
          $this->object = new SomeOtherClass();  // Noncompliant
        }
      }
      

      Compliant Solution

      class SomeClass {
      
        public function __construct(SomeOtherClass $object) {
          $this->object = $object;
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              linda.martin Linda Martin (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: