Conditions | 16 |
Paths | 113 |
Total Lines | 58 |
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 |
||
47 | public static function compile(Compiler $compiler, array $rest, array $tokens) |
||
48 | { |
||
49 | // load if plugin |
||
50 | if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
||
51 | try { |
||
52 | $compiler->getCore()->getLoader()->loadPlugin('if'); |
||
53 | } |
||
54 | catch (Exception $e) { |
||
55 | throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | if (count($rest) == 1) { |
||
60 | return $rest[0]; |
||
61 | } |
||
62 | |||
63 | // fetch false result and remove the ":" if it was present |
||
64 | $falseResult = array_pop($rest); |
||
65 | |||
66 | if (trim(end($rest), '"\'') === ':') { |
||
67 | // remove the ':' if present |
||
68 | array_pop($rest); |
||
69 | } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { |
||
70 | if ($falseResult === '?' || $falseResult === ':') { |
||
71 | throw new CompilationException($compiler, |
||
72 | 'Tif: incomplete tif statement, value missing after ' . $falseResult); |
||
73 | } |
||
74 | // there was in fact no false result provided, so we move it to be the true result instead |
||
75 | $trueResult = $falseResult; |
||
76 | $falseResult = "''"; |
||
77 | } |
||
78 | |||
79 | // fetch true result if needed |
||
80 | if (!isset($trueResult)) { |
||
81 | $trueResult = array_pop($rest); |
||
82 | // no true result provided so we use the expression arg |
||
83 | if ($trueResult === '?') { |
||
84 | $trueResult = true; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | // remove the '?' if present |
||
89 | if (trim(end($rest), '"\'') === '?') { |
||
90 | array_pop($rest); |
||
91 | } |
||
92 | |||
93 | // check params were correctly provided |
||
94 | if (empty($rest) || $trueResult === null || $falseResult === null) { |
||
95 | throw new CompilationException($compiler, |
||
96 | 'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>'); |
||
97 | } |
||
98 | |||
99 | // parse condition |
||
100 | $condition = PluginIf::replaceKeywords($rest, $tokens, $compiler); |
||
101 | |||
102 | return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', |
||
103 | $condition) : $trueResult) . ' : ' . $falseResult . ')'; |
||
104 | } |
||
105 | } |