Conditions | 25 |
Paths | 330 |
Total Lines | 123 |
Code Lines | 84 |
Lines | 0 |
Ratio | 0 % |
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 |
||
44 | public static function compile(Compiler $compiler, $file) |
||
45 | { |
||
46 | list($l, $r) = $compiler->getDelimiters(); |
||
47 | self::$l = preg_quote($l, '/'); |
||
48 | self::$r = preg_quote($r, '/'); |
||
49 | self::$regex = '/ |
||
50 | ' . self::$l . 'block\s(["\']?)(.+?)\1' . self::$r . '(?:\r?\n?) |
||
51 | ((?: |
||
52 | (?R) |
||
53 | | |
||
54 | [^' . self::$l . ']* |
||
55 | (?: |
||
56 | (?! ' . self::$l . '\/?block\b ) |
||
57 | ' . self::$l . ' |
||
58 | [^' . self::$l . ']*+ |
||
59 | )* |
||
60 | )*) |
||
61 | ' . self::$l . '\/block' . self::$r . ' |
||
62 | /six'; |
||
63 | |||
64 | if ($compiler->getLooseOpeningHandling()) { |
||
65 | self::$l .= '\s*'; |
||
66 | self::$r = '\s*' . self::$r; |
||
67 | } |
||
68 | $inheritanceTree = array(array('source' => $compiler->getTemplateSource())); |
||
69 | $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR; |
||
70 | $curTpl = $compiler->getCore()->getTemplate(); |
||
71 | |||
72 | while (!empty($file)) { |
||
73 | if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) { |
||
74 | throw new CompilationException($compiler, 'Inherits : The file name must be a non-empty string'); |
||
75 | } |
||
76 | |||
77 | if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) { |
||
78 | // resource:identifier given, extract them |
||
79 | $resource = $m[1]; |
||
80 | $identifier = $m[2]; |
||
81 | } else { |
||
82 | // get the current template's resource |
||
83 | $resource = $curTpl->getResourceName(); |
||
84 | $identifier = substr($file, 1, - 1); |
||
85 | } |
||
86 | |||
87 | try { |
||
88 | $templateDirs = $compiler->getCore()->getTemplateDir(); |
||
89 | $curTemplateDir = str_replace($identifier, '', $curTpl->getResourceIdentifier()); |
||
90 | $curTemplateKey = array_search($curTemplateDir, $templateDirs); |
||
91 | if ($curTemplateKey !== false && isset($templateDirs[$curTemplateKey+1])) { |
||
92 | $nextTemplateDir = $templateDirs[$curTemplateKey+1]; |
||
93 | $nextTpl = $nextTemplateDir . $identifier; |
||
94 | foreach ($templateDirs as $key => $dir) { |
||
95 | if ($key <= $curTemplateKey) { |
||
96 | unset($templateDirs[$key]); |
||
97 | } |
||
98 | } |
||
99 | $curTpl = new File( |
||
100 | $identifier, |
||
101 | null, |
||
102 | null, |
||
103 | null, |
||
104 | $templateDirs |
||
105 | ); |
||
106 | } |
||
107 | $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
||
108 | } |
||
109 | catch (SecurityException $e) { |
||
110 | throw new CompilationException($compiler, 'Inherits : Security restriction : ' . $e->getMessage()); |
||
111 | } |
||
112 | catch (Exception $e) { |
||
113 | throw new CompilationException($compiler, 'Inherits : ' . $e->getMessage()); |
||
114 | } |
||
115 | |||
116 | if ($parent === null) { |
||
117 | throw new CompilationException($compiler, 'Inherits : Resource "' . $resource . ':' . $identifier . '" not found.'); |
||
118 | } elseif ($parent === false) { |
||
119 | throw new CompilationException($compiler, 'Inherits : Resource "' . $resource . '" does not support inherits.'); |
||
120 | } |
||
121 | |||
122 | $curTpl = $parent; |
||
123 | $newParent = array( |
||
124 | 'source' => $parent->getSource(), |
||
125 | 'resource' => $resource, |
||
126 | 'identifier' => $parent->getResourceIdentifier(), |
||
127 | 'uid' => $parent->getUid() |
||
128 | ); |
||
129 | if (array_search($newParent, $inheritanceTree, true) !== false) { |
||
130 | throw new CompilationException($compiler, 'Inherits : Recursive template inheritance detected'); |
||
131 | } |
||
132 | $inheritanceTree[] = $newParent; |
||
133 | |||
134 | if (preg_match('/^' . self::$l . 'inherits(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?' . self::$r . '/i', $parent->getSource(), $match)) { |
||
135 | $curPath = dirname($identifier) . DIRECTORY_SEPARATOR; |
||
136 | if (isset($match[2]) && $match[2] == '"') { |
||
137 | $file = '"' . str_replace('"', '\\"', substr($match[1], 1, - 1)) . '"'; |
||
138 | } elseif (isset($match[2]) && $match[2] == "'") { |
||
139 | $file = '"' . substr($match[1], 1, - 1) . '"'; |
||
140 | } else { |
||
141 | $file = '"' . $match[1] . '"'; |
||
142 | } |
||
143 | } else { |
||
144 | $file = false; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | while (true) { |
||
149 | $parent = array_pop($inheritanceTree); |
||
150 | $child = end($inheritanceTree); |
||
151 | self::$childSource = $child['source']; |
||
152 | self::$lastReplacement = count($inheritanceTree) === 1; |
||
153 | if (!isset($newSource)) { |
||
154 | $newSource = $parent['source']; |
||
155 | } |
||
156 | $newSource = preg_replace_callback(self::$regex, array( |
||
157 | 'Dwoo\Plugins\Functions\PluginInherits', |
||
158 | 'replaceBlock' |
||
159 | ), $newSource); |
||
160 | |||
161 | if (self::$lastReplacement) { |
||
162 | break; |
||
163 | } |
||
164 | } |
||
165 | $compiler->setTemplateSource($newSource); |
||
166 | $compiler->recompile(); |
||
167 | } |
||
206 |