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\Parser\Sheet
Detected issues
Issue | Method | Line number |
---|---|---|
Using `new` in constructor | __construct | 24 |
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\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
- Tight Coupling
- Hidden dependencies
- Broken Encapsulatioin
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 theEngine
instance. This is inflexible as it forces allCar
objects to use the exact sameEngine
type. Instead, it would encourage reuse if the program supported different engine types (e.g.DieselEngine
,PetrolEngine
orHybridEngine
).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 theEngine
class.For example, if the
Engine
class required aGearbox
instance as a constructor argument, theCar
class would need to instantiate and pass in the relevantGearbox
instance. And provide any dependencies of theGearbox
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 theCar
class. Instead, if the fully constructedEngine
instanceBy loosely coupling the
Engine
class to theCar
class, the author of theCar
class does not need to know anything about the implementation ofEngine
class or have knowledge of what dependencies it has.
public Car() {
this.engine = new Engine(new Gearbox());
}Further reading
Additional resources:
References
- 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/
- 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\Parser\Tokenizer
is instantiated inside the constructor ofTransphporm\Parser\Sheet
1) Remove the new expression and replace it with a variable:
$this->tss = (new Tokenizer($tss))->getTokens();
becomes:
$this->tss = ($tokenizer)->getTokens();
2) Add a constructor argument for the new variable: Replace:
public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) {
with:
public function __construct(\Transphporm\Parser\Tokenizer $tokenizer, $tss, CssToXpath $xPath, Value $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:0Replace:
return $tss == null ? [] : (new \Transphporm\Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
With:
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:0Replace:
return (new \Transphporm\Parser\Sheet($this->str, $cssToXpath, $valueParser, $this->filePath, $sheetLoader))->parse($indexStart);
With:
return (new \Transphporm\Parser\Sheet(new \Transphporm\Parser\Tokenizer($arg0))->getTokens(), $arg0, $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..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/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/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);}
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;
}
}