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!


Detected issues

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


Click highlighted lines for details

<?php/* @description     Transformation Style Sheets - Revolutionising PHP templating    * * @author          Tom Butler                                             * * @copyright       2017 Tom Butler <> |                      * * @license  BSD License * * @version         1.2                                                             */namespace Transphporm\Parser;/** Parses a .tss file into individual rules, each rule has a query e,g, `ul li` and a set of rules e.g. `display: none; bind: iteration(id);` */class Sheet {	private $tss;	private $xPath;	private $valueParser;	private $filePath;	private $sheetLoader;	private $file;	private $rules;	public function __construct($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;

Usage of the new keyword in a constructor

Summary of issues


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 {

Engine engine;
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 {
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 {
Engine engine;

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:


  1. Hevery, M. (2008) How to Think About the “new” Operator with Respect to Unit Testing [online]. Available from:
  2. Hevery, M. (2008) Code Reviewers Guide [online]. Available from:

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

\Transphporm\Parser\Tokenizer is instantiated inside the constructor of Transphporm\Parser\Sheet

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


$this->tss = (new Tokenizer($tss))->getTokens();



        $this->tss = (new Tokenizer($tss))->getTokens();

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


public function __construct($tssCssToXpath $xPathValue $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader$file null) {



public function __construct($tssCssToXpath $xPathValue $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader$file null) {

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

\Transphporm\Parser\Sheet is being instantiated in src/SheetLoader/TSSFile.php:0



return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss$cssToXpath$valueParser$this->filePath$sheetLoader))->parse($indexStart);



return $tss == null ? [] : (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer($arg0))->getTokens(), $arg0$cssToXpath$valueParser$this->filePath$sheetLoader))->parse($indexStart);

\Transphporm\Parser\Sheet is being instantiated in src/SheetLoader/TSSString.php:0



return (new \Transphporm\Parser\Sheet($this->str$cssToXpath$valueParser$this->filePath$sheetLoader))->parse($indexStart);


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..225f265 100644
--- a/src/Builder.php
+++ b/src/Builder.php
@@ -82,7 +82,7 @@ class Builder {
 		$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>' );
+		$template = new Template(new \DomXPath(false), new \DomDocument, $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);
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/SheetLoader/TSSFile.php b/src/SheetLoader/TSSFile.php
index df55c42..bb1646e 100644
--- a/src/SheetLoader/TSSFile.php
+++ b/src/SheetLoader/TSSFile.php
@@ -73,7 +73,9 @@ 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;
+		return $tss == null ? [] : (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer($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..0961d06 100644
--- a/src/SheetLoader/TSSString.php
+++ b/src/SheetLoader/TSSString.php
@@ -24,7 +24,9 @@ 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;
+		return (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer($arg0))->getTokens(), $arg0, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);

 	public function write($rules, $imports = []) {
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->xpath = new \DomXPath($this->document);
+		$this->xpath = $domXPath;
 		$this->xpath->registerNamespace('php', '');

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

\ No newline at end of file
} public function parse($indexStart = 0) { if (!empty($this->rules)) return $this->rules['rules']; $rules = $this->parseTokens($indexStart); $this->checkError($rules); return $rules; } private function parseTokens($indexStart) { $this->rules = []; foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { $this->rules = array_merge($this->rules, $processing); } else if (!in_array($token['type'], [Tokenizer::NEW_LINE, Tokenizer::AT_SIGN])) $this->addRules($token, $indexStart++); } return $this->rules; } private function addRules($token, $indexStart) { $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); $this->tss->skip(count($selector)); if (count($selector) === 0) return; $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); $this->rules = $this->writeRule($this->rules, $newRules); } private function checkError($rules) { if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); } private function CssToRules($selector, $index, $properties, $line) { $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); $rules = []; foreach ($parts as $part) { $serialized = serialize($part->removeLine()); $rules[$serialized] = new \Transphporm\Rule($this->xPath->getXpath($part), $this->xPath->getPseudo($part), $this->xPath->getDepth($part), $index, $this->file, $line); $rules[$serialized]->properties = $properties; } return $rules; } private function writeRule($rules, $newRules) { foreach ($newRules as $selector => $newRule) { if (isset($rules[$selector])) { $newRule->properties = array_merge($rules[$selector]->properties, $newRule->properties); $newRule->index = $rules[$selector]->index; } $rules[$selector] = $newRule; } return $rules; } private function processingInstructions($token, $indexStart) { if ($token['type'] !== Tokenizer::AT_SIGN) return false; $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); $funcName = $tokens->from(Tokenizer::NAME, true)->read(); $funcToks = $tokens->from(Tokenizer::NAME); $args = $this->valueParser->parseTokens($funcToks); $rules = $this->$funcName($args, $indexStart, $funcToks); $this->tss->skip(count($tokens)+2); return $rules; } private function import($args, $indexStart, $tokens) { $fileName = $this->filePath->getFilePath($args[0]); $this->sheetLoader->addImport($fileName); $tssFile = new \Transphporm\SheetLoader\TSSString(file_get_contents($fileName), $this->filePath); return $tssFile->getRules($this->xPath, $this->valueParser, $this->sheetLoader, $indexStart); } private function cacheKey($args, $indexStart, $tokens) { $this->sheetLoader->setCacheKey($tokens); } private function getProperties($tokens) { $rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); $return = []; foreach ($rules as $rule) { $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); $tokens = $rule->from(Tokenizer::COLON)->trim(); if (count($tokens) > 0) $return[$name] = $rule->from(Tokenizer::COLON)->trim(); } return $return; }}