Insphpect

This tool is currently proof-of-concept. Your feedback and evaluation is valuable in helping to improve it and ensure its reports are meaninful.

Please click here to complete a short survey to tell us what you think. It should take less than 5 minutes and help further this research project!

Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator

Detected issues

Issue Method Line number
Using `new` in constructor __construct 33
Using `new` in constructor __construct 34

Code

Click highlighted lines for details

<?php/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */namespace Symfony\Component\Routing\Loader\Configurator;use Symfony\Component\Routing\Route;use Symfony\Component\Routing\RouteCollection;/** * @author Nicolas Grekas <p@tchwork.com> */class CollectionConfigurator{    use Traits\AddTrait;    use Traits\RouteTrait;    private $parent;    private $parentConfigurator;    private $parentPrefixes;    public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)    {        $this->parent = $parent;        $this->name = $name;

Usage of the new keyword in a constructor

Summary of issues

Background

If a dependency is constructed inside the object that requires it rather than passed in as a reference then flexibility is lost[1][2]

 public class Car {

    private 
Engine engine;
    
    public 
Car() {
        
this.engine = new Engine();
    }
}

Here, the Car constructor creates the Engine instance. This is inflexible as it forces all Car objects to use the exact same Engine type. Instead, it would encourage reuse if the program supported different engine types (e.g. DieselEngine, PetrolEngine or HybridEngine).

The same is true when an instance variable is created when the class is defined:

 public class Car {
    private 
Engine engine = new Engine();
}

By using the new keyword to instantiate a dependency, the specific implementation of that dependency is hardcoded and cannot be substituted.

Instead, the dependency should be constructed outside the class and injected in:

 public class Car {
    private 
Engine engine;

    public 
Car(Engine engine) {
        
this.engine engine;
    }
}

Using dependency injection it is possible to pass in any engine type:

 //Instead of
Car myCar = new Car();

//It's now possible to construct different types of car:
Car petrolCar = new Car(new PetrolEngine);
Car electricCar = new Car(new ElectricEngine);

A secondary advantage to Dependency Injection with regards to flexibility and encapsulation is that the class which has the dependency (Car, in this example) it not aware of the dependencies of the Engine class.

For example, if the Engine class required a Gearbox instance as a constructor argument, the Car class would need to instantiate and pass in the relevant Gearbox instance. And provide any dependencies of the Gearbox class when instantiating it.

If the constructor arguments of any of the classes which need to be instantiated are modified during development, any class which creates an instance of the class must also be modified. A change to the constructor for Engine would require modifying the Car class. Instead, if the fully constructed Engine instance

By loosely coupling the Engine class to the Car class, the author of the Car class does not need to know anything about the implementation of Engine class or have knowledge of what dependencies it has.

 
public Car() {

    
this.engine = new Engine(new Gearbox());
}

Further reading

Additional resources:

References

  1. Hevery, M. (2008) How to Think About the “new” Operator with Respect to Unit Testing [online]. Available from: http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/
  2. Hevery, M. (2008) Code Reviewers Guide [online]. Available from: http://misko.hevery.com/code-reviewers-guide/

Please note: This feature is currently proof-of-concept, the instructions may not always be completely accurate.

\Symfony\Component\Routing\RouteCollection is instantiated inside the constructor of Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator

1) Remove the new expression and replace it with a variable:

 

$this->collection = new RouteCollection();

becomes:

 

        $this->collection = new RouteCollection();

2) Add a constructor argument for the new variable: Replace:

 

public function __construct(RouteCollection $parentstring $nameself $parentConfigurator null, array $parentPrefixes null)

with:

 

public function __construct(RouteCollection $parentstring $nameself $parentConfigurator null, array $parentPrefixes null)

3) Find any occurance of new Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator and provide the new dependency.

\Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator is being instantiated in Loader/Configurator/RoutingConfigurator.php:0

Replace:

 

return new CollectionConfigurator($this->collection$name);

With:

Please note: This feature is currently proof-of-concept, this patch may not work, please don't blindly apply it.

diff --git a/Loader/Configurator/CollectionConfigurator.php b/Loader/Configurator/CollectionConfigurator.php
index 79c1100..a3c1b43 100644
--- a/Loader/Configurator/CollectionConfigurator.php
+++ b/Loader/Configurator/CollectionConfigurator.php
@@ -26,11 +26,11 @@ class CollectionConfigurator
     private $parentConfigurator;
     private $parentPrefixes;

-    public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
+    public function __construct(\Symfony\Component\Routing\RouteCollection $routeCollection, RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
     {
         $this->parent = $parent;
         $this->name = $name;
-        $this->collection = new RouteCollection();
+        $this->collection = $routeCollection;
         $this->route = new Route('');
         $this->parentConfigurator = $parentConfigurator; // for GC control
         $this->parentPrefixes = $parentPrefixes;
@@ -90,4 +90,4 @@ class CollectionConfigurator
     {
         return (clone $this->route)->setPath($path);
     }
-}
+}
\ No newline at end of file
diff --git a/Loader/Configurator/RoutingConfigurator.php b/Loader/Configurator/RoutingConfigurator.php
index e5086e2..8cfad98 100644
--- a/Loader/Configurator/RoutingConfigurator.php
+++ b/Loader/Configurator/RoutingConfigurator.php
@@ -55,7 +55,7 @@ class RoutingConfigurator

     final public function collection(string $name = ''): CollectionConfigurator
     {
-        return new CollectionConfigurator($this->collection, $name);
+        return new CollectionConfigurator(new \Symfony\Component\Routing\RouteCollection(), $this->collection, $name);
     }

     /**
diff --git a/Router.php b/Router.php
index 031749b..d5e6093 100644
--- a/Router.php
+++ b/Router.php
@@ -97,12 +97,12 @@ class Router implements RouterInterface, RequestMatcherInterface
     /**
      * @param mixed $resource The main resource to load
      */
-    public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
+    public function __construct(\Symfony\Component\Routing\RequestContext $requestContext, LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
     {
         $this->loader = $loader;
         $this->resource = $resource;
         $this->logger = $logger;
-        $this->context = $context ?: new RequestContext();
+        $this->context = $context ?: $requestContext;
         $this->setOptions($options);
         $this->defaultLocale = $defaultLocale;
     }
@@ -387,4 +387,4 @@ class Router implements RouterInterface, RequestMatcherInterface

         return self::$cache[$path] = require $path;
     }
-}
+}
\ No newline at end of file

Usage of the new keyword in a constructor

Summary of issues

Background

If a dependency is constructed inside the object that requires it rather than passed in as a reference then flexibility is lost[1][2]

 public class Car {

    private 
Engine engine;
    
    public 
Car() {
        
this.engine = new Engine();
    }
}

Here, the Car constructor creates the Engine instance. This is inflexible as it forces all Car objects to use the exact same Engine type. Instead, it would encourage reuse if the program supported different engine types (e.g. DieselEngine, PetrolEngine or HybridEngine).

The same is true when an instance variable is created when the class is defined:

 public class Car {
    private 
Engine engine = new Engine();
}

By using the new keyword to instantiate a dependency, the specific implementation of that dependency is hardcoded and cannot be substituted.

Instead, the dependency should be constructed outside the class and injected in:

 public class Car {
    private 
Engine engine;

    public 
Car(Engine engine) {
        
this.engine engine;
    }
}

Using dependency injection it is possible to pass in any engine type:

 //Instead of
Car myCar = new Car();

//It's now possible to construct different types of car:
Car petrolCar = new Car(new PetrolEngine);
Car electricCar = new Car(new ElectricEngine);

A secondary advantage to Dependency Injection with regards to flexibility and encapsulation is that the class which has the dependency (Car, in this example) it not aware of the dependencies of the Engine class.

For example, if the Engine class required a Gearbox instance as a constructor argument, the Car class would need to instantiate and pass in the relevant Gearbox instance. And provide any dependencies of the Gearbox class when instantiating it.

If the constructor arguments of any of the classes which need to be instantiated are modified during development, any class which creates an instance of the class must also be modified. A change to the constructor for Engine would require modifying the Car class. Instead, if the fully constructed Engine instance

By loosely coupling the Engine class to the Car class, the author of the Car class does not need to know anything about the implementation of Engine class or have knowledge of what dependencies it has.

 
public Car() {

    
this.engine = new Engine(new Gearbox());
}

Further reading

Additional resources:

References

  1. Hevery, M. (2008) How to Think About the “new” Operator with Respect to Unit Testing [online]. Available from: http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/
  2. Hevery, M. (2008) Code Reviewers Guide [online]. Available from: http://misko.hevery.com/code-reviewers-guide/

Please note: This feature is currently proof-of-concept, the instructions may not always be completely accurate.

\Symfony\Component\Routing\Route is instantiated inside the constructor of Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator

1) Remove the new expression and replace it with a variable:

 

$this->route = new Route('');

becomes:

 

        $this->route = new Route('');

2) Add a constructor argument for the new variable: Replace:

 

public function __construct(RouteCollection $parentstring $nameself $parentConfigurator null, array $parentPrefixes null)

with:

 

public function __construct(RouteCollection $parentstring $nameself $parentConfigurator null, array $parentPrefixes null)

3) Find any occurance of new Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator and provide the new dependency.

\Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator is being instantiated in Loader/Configurator/RoutingConfigurator.php:0

Replace:

 

return new CollectionConfigurator(new \Symfony\Component\Routing\RouteCollection(), $this->collection$name);

With:

Please note: This feature is currently proof-of-concept, this patch may not work, please don't blindly apply it.

diff --git a/Loader/Configurator/CollectionConfigurator.php b/Loader/Configurator/CollectionConfigurator.php
index 79c1100..9ee9f62 100644
--- a/Loader/Configurator/CollectionConfigurator.php
+++ b/Loader/Configurator/CollectionConfigurator.php
@@ -26,12 +26,12 @@ class CollectionConfigurator
     private $parentConfigurator;
     private $parentPrefixes;

-    public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
+    public function __construct(\Symfony\Component\Routing\Route $route, RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
     {
         $this->parent = $parent;
         $this->name = $name;
-        $this->collection = new RouteCollection();
-        $this->route = new Route('');
+        $this->collection = $routeCollection;
+        $this->route = $route;
         $this->parentConfigurator = $parentConfigurator; // for GC control
         $this->parentPrefixes = $parentPrefixes;
     }
@@ -90,4 +90,4 @@ class CollectionConfigurator
     {
         return (clone $this->route)->setPath($path);
     }
-}
+}
\ No newline at end of file
diff --git a/Loader/Configurator/RoutingConfigurator.php b/Loader/Configurator/RoutingConfigurator.php
index e5086e2..82678ac 100644
--- a/Loader/Configurator/RoutingConfigurator.php
+++ b/Loader/Configurator/RoutingConfigurator.php
@@ -55,7 +55,7 @@ class RoutingConfigurator

     final public function collection(string $name = ''): CollectionConfigurator
     {
-        return new CollectionConfigurator($this->collection, $name);
+        return new CollectionConfigurator(new \Symfony\Component\Routing\Route(''), new \Symfony\Component\Routing\RouteCollection(), $this->collection, $name);
     }

     /**
diff --git a/Router.php b/Router.php
index 031749b..d5e6093 100644
--- a/Router.php
+++ b/Router.php
@@ -97,12 +97,12 @@ class Router implements RouterInterface, RequestMatcherInterface
     /**
      * @param mixed $resource The main resource to load
      */
-    public function __construct(LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
+    public function __construct(\Symfony\Component\Routing\RequestContext $requestContext, LoaderInterface $loader, $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
     {
         $this->loader = $loader;
         $this->resource = $resource;
         $this->logger = $logger;
-        $this->context = $context ?: new RequestContext();
+        $this->context = $context ?: $requestContext;
         $this->setOptions($options);
         $this->defaultLocale = $defaultLocale;
     }
@@ -387,4 +387,4 @@ class Router implements RouterInterface, RequestMatcherInterface

         return self::$cache[$path] = require $path;
     }
-}
+}
\ No newline at end of file
$this->parentConfigurator = $parentConfigurator; // for GC control $this->parentPrefixes = $parentPrefixes; } public function __destruct() { if (null === $this->prefixes) { $this->collection->addPrefix($this->route->getPath()); } $this->parent->addCollection($this->collection); } /** * Creates a sub-collection. */ final public function collection(string $name = ''): self { return new self($this->collection, $this->name.$name, $this, $this->prefixes); } /** * Sets the prefix to add to the path of all child routes. * * @param string|array $prefix the prefix, or the localized prefixes * * @return $this */ final public function prefix($prefix): self { if (\is_array($prefix)) { if (null === $this->parentPrefixes) { // no-op } elseif ($missing = array_diff_key($this->parentPrefixes, $prefix)) { throw new \LogicException(sprintf('Collection "%s" is missing prefixes for locale(s) "%s".', $this->name, implode('", "', array_keys($missing)))); } else { foreach ($prefix as $locale => $localePrefix) { if (!isset($this->parentPrefixes[$locale])) { throw new \LogicException(sprintf('Collection "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $this->name, $locale)); } $prefix[$locale] = $this->parentPrefixes[$locale].$localePrefix; } } $this->prefixes = $prefix; $this->route->setPath('/'); } else { $this->prefixes = null; $this->route->setPath($prefix); } return $this; } private function createRoute(string $path): Route { return (clone $this->route)->setPath($path); }}