| Conditions | 21 |
| Paths | 112 |
| Total Lines | 106 |
| Lines | 5 |
| Ratio | 4.72 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 53 | public static function compile(Compiler $compiler, $file) |
||
| 54 | { |
||
| 55 | list($l, $r) = $compiler->getDelimiters(); |
||
| 56 | self::$l = preg_quote($l, '/'); |
||
| 57 | self::$r = preg_quote($r, '/'); |
||
| 58 | self::$regex = '/ |
||
| 59 | ' . self::$l . 'block\s(["\']?)(.+?)\1' . self::$r . '(?:\r?\n?) |
||
| 60 | ((?: |
||
| 61 | (?R) |
||
| 62 | | |
||
| 63 | [^' . self::$l . ']* |
||
| 64 | (?: |
||
| 65 | (?! ' . self::$l . '\/?block\b ) |
||
| 66 | ' . self::$l . ' |
||
| 67 | [^' . self::$l . ']*+ |
||
| 68 | )* |
||
| 69 | )*) |
||
| 70 | ' . self::$l . '\/block' . self::$r . ' |
||
| 71 | /six'; |
||
| 72 | |||
| 73 | if ($compiler->getLooseOpeningHandling()) { |
||
| 74 | self::$l .= '\s*'; |
||
| 75 | self::$r = '\s*' . self::$r; |
||
| 76 | } |
||
| 77 | $inheritanceTree = array(array('source' => $compiler->getTemplateSource())); |
||
| 78 | $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR; |
||
|
|
|||
| 79 | $curTpl = $compiler->getCore()->getTemplate(); |
||
| 80 | |||
| 81 | while (!empty($file)) { |
||
| 82 | if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) { |
||
| 83 | throw new CompilationException($compiler, 'Extends : The file name must be a non-empty string'); |
||
| 84 | } |
||
| 85 | |||
| 86 | if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) { |
||
| 87 | // resource:identifier given, extract them |
||
| 88 | $resource = $m[1]; |
||
| 89 | $identifier = $m[2]; |
||
| 90 | } else { |
||
| 91 | // get the current template's resource |
||
| 92 | $resource = $curTpl->getResourceName(); |
||
| 93 | $identifier = substr($file, 1, - 1); |
||
| 94 | } |
||
| 95 | |||
| 96 | try { |
||
| 97 | $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
||
| 98 | } |
||
| 99 | catch (SecurityException $e) { |
||
| 100 | throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); |
||
| 101 | } |
||
| 102 | catch (Exception $e) { |
||
| 103 | throw new CompilationException($compiler, 'Extends : ' . $e->getMessage()); |
||
| 104 | } |
||
| 105 | |||
| 106 | View Code Duplication | if ($parent === null) { |
|
| 107 | throw new CompilationException($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.'); |
||
| 108 | } elseif ($parent === false) { |
||
| 109 | throw new CompilationException($compiler, 'Extends : Resource "' . $resource . '" does not support extends.'); |
||
| 110 | } |
||
| 111 | |||
| 112 | $curTpl = $parent; |
||
| 113 | $newParent = array( |
||
| 114 | 'source' => $parent->getSource(), |
||
| 115 | 'resource' => $resource, |
||
| 116 | 'identifier' => $parent->getResourceIdentifier(), |
||
| 117 | 'uid' => $parent->getUid() |
||
| 118 | ); |
||
| 119 | if (array_search($newParent, $inheritanceTree, true) !== false) { |
||
| 120 | throw new CompilationException($compiler, 'Extends : Recursive template inheritance detected'); |
||
| 121 | } |
||
| 122 | $inheritanceTree[] = $newParent; |
||
| 123 | |||
| 124 | if (preg_match('/^' . self::$l . 'extends(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?' . self::$r . '/i', $parent->getSource(), $match)) { |
||
| 125 | $curPath = dirname($identifier) . DIRECTORY_SEPARATOR; |
||
| 126 | if (isset($match[2]) && $match[2] == '"') { |
||
| 127 | $file = '"' . str_replace('"', '\\"', substr($match[1], 1, - 1)) . '"'; |
||
| 128 | } elseif (isset($match[2]) && $match[2] == "'") { |
||
| 129 | $file = '"' . substr($match[1], 1, - 1) . '"'; |
||
| 130 | } else { |
||
| 131 | $file = '"' . $match[1] . '"'; |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | $file = false; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | while (true) { |
||
| 139 | $parent = array_pop($inheritanceTree); |
||
| 140 | $child = end($inheritanceTree); |
||
| 141 | self::$childSource = $child['source']; |
||
| 142 | self::$lastReplacement = count($inheritanceTree) === 1; |
||
| 143 | if (!isset($newSource)) { |
||
| 144 | $newSource = $parent['source']; |
||
| 145 | } |
||
| 146 | $newSource = preg_replace_callback(self::$regex, array( |
||
| 147 | 'Dwoo\Plugins\Functions\PluginExtends', |
||
| 148 | 'replaceBlock' |
||
| 149 | ), $newSource); |
||
| 150 | $newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource; |
||
| 151 | |||
| 152 | if (self::$lastReplacement) { |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | $compiler->setTemplateSource($newSource); |
||
| 157 | $compiler->recompile(); |
||
| 158 | } |
||
| 159 | |||
| 207 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.