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 = new Cache(new \ArrayObject());

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

 

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

with:

 

public function __construct($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:

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/Parser/Sheet.php b/src/Parser/Sheet.php
index 06384a2..3834958 100644
--- a/src/Parser/Sheet.php
+++ b/src/Parser/Sheet.php
@@ -15,13 +15,13 @@ class Sheet {
 	private $file;
 	private $rules;

-	public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) {
+	public function __construct(\Transphporm\Parser\Tokenizer $tokenizer, $tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) {
 		$this->xPath = $xPath;
 		$this->valueParser = $valueParser;
 		$this->filePath = $filePath;
 		$this->sheetLoader = $sheetLoader;
 		$this->file = $file;
-		$this->tss = (new Tokenizer($tss))->getTokens();
+		$this->tss = ($tokenizer)->getTokens();
 	}

 	public function parse($indexStart = 0) {
@@ -115,4 +115,4 @@ class Sheet {

         return $return;
     }
-}
+}
\ No newline at end of file
diff --git a/src/Parser/Tokenizer.php b/src/Parser/Tokenizer.php
index af6d1a3..c0f3f22 100644
--- a/src/Parser/Tokenizer.php
+++ b/src/Parser/Tokenizer.php
@@ -37,16 +37,11 @@ class Tokenizer {
 	const MULTIPLY = 'MULTIPLY';
 	const DIVIDE = 'DIVIDE';

-	public function __construct($str) {
-		$this->str = new Tokenizer\TokenizedString($str);
+	public function __construct(\Transphporm\Parser\Tokenizer\Comments $comments, $str) {
+		$this->str = $tokenizedString;

 		$this->tokenizeRules = [
-			new Tokenizer\Comments,
-			new Tokenizer\BasicChars,
-			new Tokenizer\Literals,
-			new Tokenizer\Strings,
-			new Tokenizer\Brackets
-		];
+			$comments;
 	}

 	public function getTokens() {
@@ -62,4 +57,4 @@ class Tokenizer {
 		return $tokens;
 	}

-}
+}
\ No newline at end of file
diff --git a/src/Parser/Value.php b/src/Parser/Value.php
index 14097e0..e4c8527 100644
--- a/src/Parser/Value.php
+++ b/src/Parser/Value.php
@@ -47,7 +47,14 @@ class Value {
 	}

 	public function parse($str) {
-		$tokenizer = new Tokenizer($str);
+
+$arg0 = $str;
+		$tokenizer = new Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 		$tokens = $tokenizer->getTokens();
 		$this->result = $this->parseTokens($tokens, $this->baseData);
 		return $this->result;
diff --git a/src/Pseudo/Not.php b/src/Pseudo/Not.php
index 384d0b2..d9c2f20 100644
--- a/src/Pseudo/Not.php
+++ b/src/Pseudo/Not.php
@@ -24,7 +24,14 @@ class Not implements \Transphporm\Pseudo {
 	private function notElement($css, $xpath, $element) {

 		foreach ($css as $selector) {
-			$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
+
+$arg0 = $selector;
+			$tokenizer = new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 			$xpathString = $this->cssToXpath->getXpath($tokenizer->getTokens());
             $pseudo = $this->cssToXpath->getPseudo($tokenizer->getTokens());
             $pseudoMatcher = $this->config->createPseudoMatcher($pseudo);
diff --git a/src/SheetLoader/TSSFile.php b/src/SheetLoader/TSSFile.php
index df55c42..02b4deb 100644
--- a/src/SheetLoader/TSSFile.php
+++ b/src/SheetLoader/TSSFile.php
@@ -73,7 +73,16 @@ class TSSFile implements TSSRules {
 		if (empty($rules)) $tss = file_get_contents($this->fileName);
 		else return $rules['rules'];

-		return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
+$arg0 = $tss;
+
+$arg0 = $arg0;
+
+		return $tss == null ? [] : (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens(), $arg0, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
 	}

 	//write the sheet to cache
diff --git a/src/SheetLoader/TSSString.php b/src/SheetLoader/TSSString.php
index 2e1e39a..78f94df 100644
--- a/src/SheetLoader/TSSString.php
+++ b/src/SheetLoader/TSSString.php
@@ -24,7 +24,16 @@ class TSSString implements TSSRules {
 	}

 	public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) {
-		return (new \Transphporm\Parser\Sheet($this->str, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
+
+$arg0 = $this->str;
+
+$arg0 = $arg0;
+		return (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens(), $arg0, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
 	}

 	public function write($rules, $imports = []) {
diff --git a/src/TSSFunction/Template.php b/src/TSSFunction/Template.php
index c2cdc23..fe3faa4 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);
@@ -46,7 +46,14 @@ class Template implements \Transphporm\TSSFunction {
 	}

 	private function templateSubsection($doc, $selector) {
-		$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
+
+$arg0 = $selector;
+		$tokenizer = new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 		$xpathStr = $this->xPath->getXpath($tokenizer->getTokens());
 		$xpath = new \DomXpath($doc);
 		$nodes = $xpath->query($xpathStr);
diff --git a/src/TSSValidator.php b/src/TSSValidator.php
index f3e5f4b..39ad561 100644
--- a/src/TSSValidator.php
+++ b/src/TSSValidator.php
@@ -45,6 +45,13 @@ class TSSValidator {

     private function tokenize($tss) {
         if (is_file($tss)) $tss = file_get_contents($tss);
-        return (new Parser\Tokenizer($tss))->getTokens();
+
+$arg0 = $tss;
+        return (new Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens();
     }
 }
diff --git a/src/Template.php b/src/Template.php
index f46567f..057ff6a 100644
--- a/src/Template.php
+++ b/src/Template.php
@@ -14,13 +14,13 @@ class Template {
 	private $save;

 	/** Takes an XML string and loads it into a DomDocument object */
-	public function __construct($doc) {
-		$this->document = new \DomDocument;
+	public function __construct(\DomXPath $domXPath, $doc) {
+		$this->document = $domDocument;
 		//This should remove whitespace left behind after ->removeChild but it doesn't
 		$this->document->preserveWhiteSpace = false;
 		$this->loadDocument($doc);

-		$this->xpath = new \DomXPath($this->document);
+		$this->xpath = $domXPath;
 		$this->xpath->registerNamespace('php', 'http://php.net/xpath');
 		$this->xpath->registerPhpFunctions();

@@ -104,4 +104,4 @@ class Template {
 		return trim($output);
 	}

-}
+}
\ 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.

\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 = new FilePath();

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

 

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

with:

 

public function __construct($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:

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/Parser/Sheet.php b/src/Parser/Sheet.php
index 06384a2..3834958 100644
--- a/src/Parser/Sheet.php
+++ b/src/Parser/Sheet.php
@@ -15,13 +15,13 @@ class Sheet {
 	private $file;
 	private $rules;

-	public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) {
+	public function __construct(\Transphporm\Parser\Tokenizer $tokenizer, $tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) {
 		$this->xPath = $xPath;
 		$this->valueParser = $valueParser;
 		$this->filePath = $filePath;
 		$this->sheetLoader = $sheetLoader;
 		$this->file = $file;
-		$this->tss = (new Tokenizer($tss))->getTokens();
+		$this->tss = ($tokenizer)->getTokens();
 	}

 	public function parse($indexStart = 0) {
@@ -115,4 +115,4 @@ class Sheet {

         return $return;
     }
-}
+}
\ No newline at end of file
diff --git a/src/Parser/Tokenizer.php b/src/Parser/Tokenizer.php
index af6d1a3..c0f3f22 100644
--- a/src/Parser/Tokenizer.php
+++ b/src/Parser/Tokenizer.php
@@ -37,16 +37,11 @@ class Tokenizer {
 	const MULTIPLY = 'MULTIPLY';
 	const DIVIDE = 'DIVIDE';

-	public function __construct($str) {
-		$this->str = new Tokenizer\TokenizedString($str);
+	public function __construct(\Transphporm\Parser\Tokenizer\Comments $comments, $str) {
+		$this->str = $tokenizedString;

 		$this->tokenizeRules = [
-			new Tokenizer\Comments,
-			new Tokenizer\BasicChars,
-			new Tokenizer\Literals,
-			new Tokenizer\Strings,
-			new Tokenizer\Brackets
-		];
+			$comments;
 	}

 	public function getTokens() {
@@ -62,4 +57,4 @@ class Tokenizer {
 		return $tokens;
 	}

-}
+}
\ No newline at end of file
diff --git a/src/Parser/Value.php b/src/Parser/Value.php
index 14097e0..e4c8527 100644
--- a/src/Parser/Value.php
+++ b/src/Parser/Value.php
@@ -47,7 +47,14 @@ class Value {
 	}

 	public function parse($str) {
-		$tokenizer = new Tokenizer($str);
+
+$arg0 = $str;
+		$tokenizer = new Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 		$tokens = $tokenizer->getTokens();
 		$this->result = $this->parseTokens($tokens, $this->baseData);
 		return $this->result;
diff --git a/src/Pseudo/Not.php b/src/Pseudo/Not.php
index 384d0b2..d9c2f20 100644
--- a/src/Pseudo/Not.php
+++ b/src/Pseudo/Not.php
@@ -24,7 +24,14 @@ class Not implements \Transphporm\Pseudo {
 	private function notElement($css, $xpath, $element) {

 		foreach ($css as $selector) {
-			$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
+
+$arg0 = $selector;
+			$tokenizer = new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 			$xpathString = $this->cssToXpath->getXpath($tokenizer->getTokens());
             $pseudo = $this->cssToXpath->getPseudo($tokenizer->getTokens());
             $pseudoMatcher = $this->config->createPseudoMatcher($pseudo);
diff --git a/src/SheetLoader/TSSFile.php b/src/SheetLoader/TSSFile.php
index df55c42..02b4deb 100644
--- a/src/SheetLoader/TSSFile.php
+++ b/src/SheetLoader/TSSFile.php
@@ -73,7 +73,16 @@ class TSSFile implements TSSRules {
 		if (empty($rules)) $tss = file_get_contents($this->fileName);
 		else return $rules['rules'];

-		return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
+$arg0 = $tss;
+
+$arg0 = $arg0;
+
+		return $tss == null ? [] : (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens(), $arg0, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
 	}

 	//write the sheet to cache
diff --git a/src/SheetLoader/TSSString.php b/src/SheetLoader/TSSString.php
index 2e1e39a..78f94df 100644
--- a/src/SheetLoader/TSSString.php
+++ b/src/SheetLoader/TSSString.php
@@ -24,7 +24,16 @@ class TSSString implements TSSRules {
 	}

 	public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) {
-		return (new \Transphporm\Parser\Sheet($this->str, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
+
+$arg0 = $this->str;
+
+$arg0 = $arg0;
+		return (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens(), $arg0, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
 	}

 	public function write($rules, $imports = []) {
diff --git a/src/TSSFunction/Template.php b/src/TSSFunction/Template.php
index c2cdc23..726d44b 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);
@@ -46,7 +46,14 @@ class Template implements \Transphporm\TSSFunction {
 	}

 	private function templateSubsection($doc, $selector) {
-		$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
+
+$arg0 = $selector;
+		$tokenizer = new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 		$xpathStr = $this->xPath->getXpath($tokenizer->getTokens());
 		$xpath = new \DomXpath($doc);
 		$nodes = $xpath->query($xpathStr);
diff --git a/src/TSSValidator.php b/src/TSSValidator.php
index f3e5f4b..39ad561 100644
--- a/src/TSSValidator.php
+++ b/src/TSSValidator.php
@@ -45,6 +45,13 @@ class TSSValidator {

     private function tokenize($tss) {
         if (is_file($tss)) $tss = file_get_contents($tss);
-        return (new Parser\Tokenizer($tss))->getTokens();
+
+$arg0 = $tss;
+        return (new Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens();
     }
 }
diff --git a/src/Template.php b/src/Template.php
index f46567f..057ff6a 100644
--- a/src/Template.php
+++ b/src/Template.php
@@ -14,13 +14,13 @@ class Template {
 	private $save;

 	/** Takes an XML string and loads it into a DomDocument object */
-	public function __construct($doc) {
-		$this->document = new \DomDocument;
+	public function __construct(\DomXPath $domXPath, $doc) {
+		$this->document = $domDocument;
 		//This should remove whitespace left behind after ->removeChild but it doesn't
 		$this->document->preserveWhiteSpace = false;
 		$this->loadDocument($doc);

-		$this->xpath = new \DomXPath($this->document);
+		$this->xpath = $domXPath;
 		$this->xpath->registerNamespace('php', 'http://php.net/xpath');
 		$this->xpath->registerPhpFunctions();

@@ -104,4 +104,4 @@ class Template {
 		return trim($output);
 	}

-}
+}
\ No newline at end of file
$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(new $module);

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

 

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

with:

 

public function __construct($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:

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/Parser/Sheet.php b/src/Parser/Sheet.php
index 06384a2..3834958 100644
--- a/src/Parser/Sheet.php
+++ b/src/Parser/Sheet.php
@@ -15,13 +15,13 @@ class Sheet {
 	private $file;
 	private $rules;

-	public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) {
+	public function __construct(\Transphporm\Parser\Tokenizer $tokenizer, $tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) {
 		$this->xPath = $xPath;
 		$this->valueParser = $valueParser;
 		$this->filePath = $filePath;
 		$this->sheetLoader = $sheetLoader;
 		$this->file = $file;
-		$this->tss = (new Tokenizer($tss))->getTokens();
+		$this->tss = ($tokenizer)->getTokens();
 	}

 	public function parse($indexStart = 0) {
@@ -115,4 +115,4 @@ class Sheet {

         return $return;
     }
-}
+}
\ No newline at end of file
diff --git a/src/Parser/Tokenizer.php b/src/Parser/Tokenizer.php
index af6d1a3..c0f3f22 100644
--- a/src/Parser/Tokenizer.php
+++ b/src/Parser/Tokenizer.php
@@ -37,16 +37,11 @@ class Tokenizer {
 	const MULTIPLY = 'MULTIPLY';
 	const DIVIDE = 'DIVIDE';

-	public function __construct($str) {
-		$this->str = new Tokenizer\TokenizedString($str);
+	public function __construct(\Transphporm\Parser\Tokenizer\Comments $comments, $str) {
+		$this->str = $tokenizedString;

 		$this->tokenizeRules = [
-			new Tokenizer\Comments,
-			new Tokenizer\BasicChars,
-			new Tokenizer\Literals,
-			new Tokenizer\Strings,
-			new Tokenizer\Brackets
-		];
+			$comments;
 	}

 	public function getTokens() {
@@ -62,4 +57,4 @@ class Tokenizer {
 		return $tokens;
 	}

-}
+}
\ No newline at end of file
diff --git a/src/Parser/Value.php b/src/Parser/Value.php
index 14097e0..e4c8527 100644
--- a/src/Parser/Value.php
+++ b/src/Parser/Value.php
@@ -47,7 +47,14 @@ class Value {
 	}

 	public function parse($str) {
-		$tokenizer = new Tokenizer($str);
+
+$arg0 = $str;
+		$tokenizer = new Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 		$tokens = $tokenizer->getTokens();
 		$this->result = $this->parseTokens($tokens, $this->baseData);
 		return $this->result;
diff --git a/src/Pseudo/Not.php b/src/Pseudo/Not.php
index 384d0b2..d9c2f20 100644
--- a/src/Pseudo/Not.php
+++ b/src/Pseudo/Not.php
@@ -24,7 +24,14 @@ class Not implements \Transphporm\Pseudo {
 	private function notElement($css, $xpath, $element) {

 		foreach ($css as $selector) {
-			$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
+
+$arg0 = $selector;
+			$tokenizer = new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 			$xpathString = $this->cssToXpath->getXpath($tokenizer->getTokens());
             $pseudo = $this->cssToXpath->getPseudo($tokenizer->getTokens());
             $pseudoMatcher = $this->config->createPseudoMatcher($pseudo);
diff --git a/src/SheetLoader/TSSFile.php b/src/SheetLoader/TSSFile.php
index df55c42..02b4deb 100644
--- a/src/SheetLoader/TSSFile.php
+++ b/src/SheetLoader/TSSFile.php
@@ -73,7 +73,16 @@ class TSSFile implements TSSRules {
 		if (empty($rules)) $tss = file_get_contents($this->fileName);
 		else return $rules['rules'];

-		return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
+$arg0 = $tss;
+
+$arg0 = $arg0;
+
+		return $tss == null ? [] : (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens(), $arg0, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
 	}

 	//write the sheet to cache
diff --git a/src/SheetLoader/TSSString.php b/src/SheetLoader/TSSString.php
index 2e1e39a..78f94df 100644
--- a/src/SheetLoader/TSSString.php
+++ b/src/SheetLoader/TSSString.php
@@ -24,7 +24,16 @@ class TSSString implements TSSRules {
 	}

 	public function getRules($cssToXpath, $valueParser, $sheetLoader, $indexStart) {
-		return (new \Transphporm\Parser\Sheet($this->str, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
+
+$arg0 = $this->str;
+
+$arg0 = $arg0;
+		return (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens(), $arg0, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
 	}

 	public function write($rules, $imports = []) {
diff --git a/src/TSSFunction/Template.php b/src/TSSFunction/Template.php
index c2cdc23..ba4cb03 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);
@@ -46,7 +48,14 @@ class Template implements \Transphporm\TSSFunction {
 	}

 	private function templateSubsection($doc, $selector) {
-		$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
+
+$arg0 = $selector;
+		$tokenizer = new \Transphporm\Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0);
 		$xpathStr = $this->xPath->getXpath($tokenizer->getTokens());
 		$xpath = new \DomXpath($doc);
 		$nodes = $xpath->query($xpathStr);
diff --git a/src/TSSValidator.php b/src/TSSValidator.php
index f3e5f4b..39ad561 100644
--- a/src/TSSValidator.php
+++ b/src/TSSValidator.php
@@ -45,6 +45,13 @@ class TSSValidator {

     private function tokenize($tss) {
         if (is_file($tss)) $tss = file_get_contents($tss);
-        return (new Parser\Tokenizer($tss))->getTokens();
+
+$arg0 = $tss;
+        return (new Parser\Tokenizer(new \Transphporm\Parser\Tokenizer\Comments,
+			new Tokenizer\BasicChars,
+			new Tokenizer\Literals,
+			new Tokenizer\Strings,
+			new Tokenizer\Brackets
+		], new \Transphporm\Parser\Tokenizer\TokenizedString($arg0), $arg0))->getTokens();
     }
 }
diff --git a/src/Template.php b/src/Template.php
index f46567f..057ff6a 100644
--- a/src/Template.php
+++ b/src/Template.php
@@ -14,13 +14,13 @@ class Template {
 	private $save;

 	/** Takes an XML string and loads it into a DomDocument object */
-	public function __construct($doc) {
-		$this->document = new \DomDocument;
+	public function __construct(\DomXPath $domXPath, $doc) {
+		$this->document = $domDocument;
 		//This should remove whitespace left behind after ->removeChild but it doesn't
 		$this->document->preserveWhiteSpace = false;
 		$this->loadDocument($doc);

-		$this->xpath = new \DomXPath($this->document);
+		$this->xpath = $domXPath;
 		$this->xpath->registerNamespace('php', 'http://php.net/xpath');
 		$this->xpath->registerPhpFunctions();

@@ -104,4 +104,4 @@ class Template {
 		return trim($output);
 	}

-}
+}
\ No newline at end of file
} //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(); }}