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!

Transphporm\Builder

Detected issues

Issue Method Line number
Using `new` in constructor __construct 28
Using `new` in constructor __construct 29
Using `new` in constructor __construct 31

Code

Click highlighted lines for details

<?php/* @description     Transformation Style Sheets - Revolutionising PHP templating    * * @author          Tom Butler tom@r.je                                             * * @copyright       2017 Tom Butler <tom@r.je> | https://r.je/                      * * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License * * @version         1.2                                                             */namespace Transphporm;/** Builds a Transphporm instance from the 3 constituent parts. XML template string, TSS string and data */class Builder {	private $template;	private $tss;	private $cache;	private $time;	private $modules = [];	private $config;	private $filePath;	private $cacheKey;	private $defaultModules = [		'\\Transphporm\\Module\\Basics',		'\\Transphporm\\Module\\Pseudo',		'\\Transphporm\\Module\\Format',		'\\Transphporm\\Module\\Functions'	];	public function __construct($template, $tss = '', $modules = null) {		$this->template = $template;		$this->tss = $tss;

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.

\Transphporm\Cache is instantiated inside the constructor of Transphporm\Builder

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

 

$this->cache = new Cache(new \ArrayObject());

becomes:

 

        $this->cache $cache);

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

 

public function __construct($template$tss ''$modules null) {

with:

 

public function __construct(\Transphporm\Cache $cache$template$tss ''$modules null) {

3) Find any occurance of new Transphporm\Builder and provide the new dependency.

\Transphporm\Builder is being instantiated in src/TSSFunction/Template.php:0

Replace:

 

$newTemplate = new \Transphporm\Builder($xml$tss $this->filePath->getFilePath($tss) : null);

With:

 

$newTemplate = new \Transphporm\Builder(new \Transphporm\Cache(new \ArrayObject()), $xml$tss $this->filePath->getFilePath($tss) : null);



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

diff --git a/src/Builder.php b/src/Builder.php
index 78c369b..33db779 100644
--- a/src/Builder.php
+++ b/src/Builder.php
@@ -22,10 +22,10 @@ class Builder {
 		'\\Transphporm\\Module\\Functions'
 	];

-	public function __construct($template, $tss = '', $modules = null) {
+	public function __construct(\Transphporm\Cache $cache, $template, $tss = '', $modules = null) {
 		$this->template = $template;
 		$this->tss = $tss;
-		$this->cache = new Cache(new \ArrayObject());
+		$this->cache = $cache);
 		$this->filePath = new FilePath();
 		$modules = is_array($modules) ? $modules : $this->defaultModules;
 		foreach ($modules as $module) $this->loadModule(new $module);
@@ -122,4 +122,4 @@ class Builder {
 		//Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks
 		if (isset($this->config)) $this->config->getCssToXpath()->cleanup();
 	}
-}
+}
\ No newline at end of file
diff --git a/src/TSSFunction/Template.php b/src/TSSFunction/Template.php
index c2cdc23..e16ac8b 100644
--- a/src/TSSFunction/Template.php
+++ b/src/TSSFunction/Template.php
@@ -28,7 +28,7 @@ class Template implements \Transphporm\TSSFunction {
 		if (trim($args[0])[0] === '<') $xml = $args[0];
 		else $xml = $this->filePath->getFilePath($args[0]);

-		$newTemplate = new \Transphporm\Builder($xml, $tss ? $this->filePath->getFilePath($tss) : null);
+		$newTemplate = new \Transphporm\Builder(new \Transphporm\Cache(new \ArrayObject()), $xml, $tss ? $this->filePath->getFilePath($tss) : null);

 		$doc = $newTemplate->output($this->elementData->getData($element), true)->body;
 		if ($selector != '') return $this->templateSubsection($doc, $selector);

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.

\Transphporm\FilePath is instantiated inside the constructor of Transphporm\Builder

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

 

$this->filePath = new FilePath();

becomes:

 

        $this->filePath $filePath;

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

 

public function __construct($template$tss ''$modules null) {

with:

 

public function __construct(\Transphporm\FilePath $filePath$template$tss ''$modules null) {

3) Find any occurance of new Transphporm\Builder and provide the new dependency.

\Transphporm\Builder is being instantiated in src/TSSFunction/Template.php:0

Replace:

 

$newTemplate = new \Transphporm\Builder(new \Transphporm\Cache(new \ArrayObject()), $xml$tss $this->filePath->getFilePath($tss) : null);

With:

 

$newTemplate = new \Transphporm\Builder(new \Transphporm\FilePath(), new \Transphporm\Cache(new \ArrayObject()), $xml$tss $this->filePath->getFilePath($tss) : null);



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

diff --git a/src/Builder.php b/src/Builder.php
index 78c369b..aea855d 100644
--- a/src/Builder.php
+++ b/src/Builder.php
@@ -22,11 +22,11 @@ class Builder {
 		'\\Transphporm\\Module\\Functions'
 	];

-	public function __construct($template, $tss = '', $modules = null) {
+	public function __construct(\Transphporm\FilePath $filePath, $template, $tss = '', $modules = null) {
 		$this->template = $template;
 		$this->tss = $tss;
-		$this->cache = new Cache(new \ArrayObject());
-		$this->filePath = new FilePath();
+		$this->cache = $cache);
+		$this->filePath = $filePath;
 		$modules = is_array($modules) ? $modules : $this->defaultModules;
 		foreach ($modules as $module) $this->loadModule(new $module);
 	}
@@ -122,4 +122,4 @@ class Builder {
 		//Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks
 		if (isset($this->config)) $this->config->getCssToXpath()->cleanup();
 	}
-}
+}
\ No newline at end of file
diff --git a/src/TSSFunction/Template.php b/src/TSSFunction/Template.php
index c2cdc23..b661532 100644
--- a/src/TSSFunction/Template.php
+++ b/src/TSSFunction/Template.php
@@ -28,7 +28,7 @@ class Template implements \Transphporm\TSSFunction {
 		if (trim($args[0])[0] === '<') $xml = $args[0];
 		else $xml = $this->filePath->getFilePath($args[0]);

-		$newTemplate = new \Transphporm\Builder($xml, $tss ? $this->filePath->getFilePath($tss) : null);
+		$newTemplate = new \Transphporm\Builder(new \Transphporm\FilePath(), new \Transphporm\Cache(new \ArrayObject()), $xml, $tss ? $this->filePath->getFilePath($tss) : null);

 		$doc = $newTemplate->output($this->elementData->getData($element), true)->body;
 		if ($selector != '') return $this->templateSubsection($doc, $selector);
$modules = is_array($modules) ? $modules : $this->defaultModules;

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.

\Transphporm\__ARG is instantiated inside the constructor of Transphporm\Builder

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

 

foreach ($modules as $module$this->loadModule(new $module);

becomes:

 

        foreach ($modules as $module$this->loadModule($__ARG;

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

 

public function __construct($template$tss ''$modules null) {

with:

 

public function __construct(\Transphporm\__ARG $__ARG$template$tss ''$modules null) {

3) Find any occurance of new Transphporm\Builder and provide the new dependency.

\Transphporm\Builder is being instantiated in src/TSSFunction/Template.php:0

Replace:

 

$newTemplate = new \Transphporm\Builder(new \Transphporm\FilePath(), new \Transphporm\Cache(new \ArrayObject()), $xml$tss $this->filePath->getFilePath($tss) : null);

With:

 

$newTemplate = new \Transphporm\Builder(new \Transphporm\$arg0), $arg0, new \Transphporm\Cache(new \ArrayObject()), $xml$tss $this->filePath->getFilePath($tss) : null);



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

diff --git a/src/Builder.php b/src/Builder.php
index 78c369b..1ab7360 100644
--- a/src/Builder.php
+++ b/src/Builder.php
@@ -22,13 +22,13 @@ class Builder {
 		'\\Transphporm\\Module\\Functions'
 	];

-	public function __construct($template, $tss = '', $modules = null) {
+	public function __construct(\Transphporm\__ARG $__ARG, $template, $tss = '', $modules = null) {
 		$this->template = $template;
 		$this->tss = $tss;
-		$this->cache = new Cache(new \ArrayObject());
-		$this->filePath = new FilePath();
+		$this->cache = $cache);
+		$this->filePath = $filePath;
 		$modules = is_array($modules) ? $modules : $this->defaultModules;
-		foreach ($modules as $module) $this->loadModule(new $module);
+		foreach ($modules as $module) $this->loadModule($__ARG;
 	}

 	//Allow setting the time used by Transphporm for caching. This is for testing purposes
@@ -122,4 +122,4 @@ class Builder {
 		//Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks
 		if (isset($this->config)) $this->config->getCssToXpath()->cleanup();
 	}
-}
+}
\ No newline at end of file
diff --git a/src/TSSFunction/Template.php b/src/TSSFunction/Template.php
index c2cdc23..eb266c3 100644
--- a/src/TSSFunction/Template.php
+++ b/src/TSSFunction/Template.php
@@ -28,7 +28,9 @@ class Template implements \Transphporm\TSSFunction {
 		if (trim($args[0])[0] === '<') $xml = $args[0];
 		else $xml = $this->filePath->getFilePath($args[0]);

-		$newTemplate = new \Transphporm\Builder($xml, $tss ? $this->filePath->getFilePath($tss) : null);
+$arg0 = new \Transphporm\FilePath();
+
+		$newTemplate = new \Transphporm\Builder(new \Transphporm\$arg0), $arg0, new \Transphporm\Cache(new \ArrayObject()), $xml, $tss ? $this->filePath->getFilePath($tss) : null);

 		$doc = $newTemplate->output($this->elementData->getData($element), true)->body;
 		if ($selector != '') return $this->templateSubsection($doc, $selector);
} //Allow setting the time used by Transphporm for caching. This is for testing purposes //Would be better if PHP allowed setting the script clock, but this is the simplest way of overriding it public function setTime($time) { $this->time = $time; } public function loadModule(Module $module) { $this->modules[get_class($module)] = $module; } public function setLocale($locale) { $format = new \Transphporm\Module\Format($locale); $this->modules[get_class($format)] = $format; } public function addPath($dir) { $this->filePath->addPath($dir); } private function getSheetLoader() { $tssRules = is_file($this->tss) ? new SheetLoader\TSSFile($this->tss, $this->filePath, $this->cache, $this->time) : new SheetLoader\TSSString($this->tss, $this->filePath); return new SheetLoader\SheetLoader($this->cache, $this->filePath, $tssRules, $this->time); } public function output($data = null, $document = false) { $headers = []; $tssCache = $this->getSheetLoader(); $this->cacheKey = $tssCache->getCacheKey($data); $result = $this->loadTemplate(); //If an update is required, run any rules that need to be run. Otherwise, return the result from cache //without creating any further objects, loading a DomDocument, etc if (empty($result['renderTime']) || $tssCache->updateRequired($data) === true) { $template = $this->createAndProcessTemplate($data, $result['cache'], $headers); $tssCache->processRules($template, $this->config); $result = ['cache' => $template->output($document), 'renderTime' => time(), 'headers' => array_merge($result['headers'], $headers), 'body' => $this->doPostProcessing($template)->output($document) ]; $this->cache->write($tssCache->getCacheKey($data) . $this->template, $result); } unset($result['cache'], $result['renderTime']); return (object) $result; } private function createAndProcessTemplate($data, $body, &$headers) { $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); $functionSet = new FunctionSet($elementData); //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>' . $body . '</template>' ); $valueParser = new Parser\Value($functionSet); $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); foreach ($this->modules as $module) $module->load($this->config); return $template; } //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed private function doPostProcessing($template) { $template->addHook('//*[@transphporm]', new Hook\PostProcess()); return $template; } //Load a template, firstly check if it's a file or a valid string private function loadTemplate() { $result = ['cache' => $this->template, 'headers' => []]; if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); return $result; } private function loadTemplateFromFile($file) { $xml = $this->cache->load($this->cacheKey . $file, filemtime($file)); return $xml ? $xml : ['cache' => file_get_contents($file) ?: "", 'headers' => []]; } public function setCache(\ArrayAccess $cache) { $this->cache = new Cache($cache); } private function isValidDoc($xml) { return (strpos($xml, '<!') === 0 && strpos($xml, '<!--') !== 0) || strpos($xml, '<?') === 0 || strpos($xml, '<html') === 0; } public function __destruct() { //Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks if (isset($this->config)) $this->config->getCssToXpath()->cleanup(); }}