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\Finder\Comparator\DateComparator

Detected issues

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

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\Finder\Comparator;/** * DateCompare compiles date comparisons. * * @author Fabien Potencier <fabien@symfony.com> */class DateComparator extends Comparator{    /**     * @param string $test A comparison string     *     * @throws \InvalidArgumentException If the test is not understood     */    public function __construct(string $test)    {        if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {            throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));        }        try {

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.

\DateTime is instantiated inside the constructor of Symfony\Component\Finder\Comparator\DateComparator

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

 

$date = new \DateTime($matches[2]);

becomes:

 

            $date = new \DateTime($matches[2]);

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

 

public function __construct(string $test)

with:

 

public function __construct(string $test)

3) Find any occurance of new Symfony\Component\Finder\Comparator\DateComparator and provide the new dependency.

\Symfony\Component\Finder\Comparator\DateComparator is being instantiated in Finder.php:0

Replace:

 

$this->dates[] = new Comparator\DateComparator($date);

With:

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

diff --git a/Comparator/DateComparator.php b/Comparator/DateComparator.php
index d17c77a..0ad03ad 100644
--- a/Comparator/DateComparator.php
+++ b/Comparator/DateComparator.php
@@ -23,14 +23,14 @@ class DateComparator extends Comparator
      *
      * @throws \InvalidArgumentException If the test is not understood
      */
-    public function __construct(string $test)
+    public function __construct(\DateTime $dateTime, string $test)
     {
         if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
             throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));
         }

         try {
-            $date = new \DateTime($matches[2]);
+            $date = $dateTime;
             $target = $date->format('U');
         } catch (\Exception $e) {
             throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2]));
@@ -48,4 +48,4 @@ class DateComparator extends Comparator
         $this->setOperator($operator);
         $this->setTarget($target);
     }
-}
+}
\ No newline at end of file
diff --git a/Finder.php b/Finder.php
index e1bcea3..f86f3db 100644
--- a/Finder.php
+++ b/Finder.php
@@ -150,7 +150,9 @@ class Finder implements \IteratorAggregate, \Countable
     public function date($dates)
     {
         foreach ((array) $dates as $date) {
-            $this->dates[] = new Comparator\DateComparator($date);
+
+$arg0 = $date;
+            $this->dates[] = new Comparator\DateComparator(new \DateTime($arg0[2]), $arg0);
         }

         return $this;
$target = $date->format('U'); } catch (\Exception $e) { throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2])); } $operator = isset($matches[1]) ? $matches[1] : '=='; if ('since' === $operator || 'after' === $operator) { $operator = '>'; } if ('until' === $operator || 'before' === $operator) { $operator = '<'; } $this->setOperator($operator); $this->setTarget($target); }}