Conditions | 3 |
Paths | 4 |
Total Lines | 71 |
Code Lines | 63 |
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 |
||
25 | public function compile(Twig_Compiler $compiler) |
||
26 | { |
||
27 | $compiler->addDebugInfo($this); |
||
28 | |||
29 | $compiler->write('$types = '); |
||
30 | if ($this->hasNode('types')) { |
||
31 | $compiler->subcompile($this->getNode('types')); |
||
32 | } else { |
||
33 | $compiler->repr(null); |
||
34 | } |
||
35 | $compiler->raw(";\n"); |
||
36 | |||
37 | $compiler->write('$catalog = '); |
||
38 | if ($this->hasNode('catalog')) { |
||
39 | $compiler->subcompile($this->getNode('catalog')); |
||
40 | } else { |
||
41 | $compiler->repr(null); |
||
42 | } |
||
43 | $compiler->raw(";\n"); |
||
44 | |||
45 | $compiler |
||
46 | ->write("\$savedContext = null;\n") |
||
47 | ->write("\$flashesByTypes = array();\n") |
||
48 | ->write("\$numFlashes = 0;\n") |
||
49 | ->write("foreach (\$this->env->getExtension('flash')->getFlashes(\$types) as \$type => \$flashes) {\n") |
||
50 | ->indent() |
||
51 | ->write("\$flashesByTypes[\$type] = \$flashes;\n") |
||
52 | ->write("\$numFlashes+= count(\$flashes);\n") |
||
53 | ->outdent() |
||
54 | ->write("}\n") |
||
55 | ->write("\$index = 0;\n") |
||
56 | ->write("foreach (\$flashesByTypes as \$type => \$flashes) {\n") |
||
57 | ->indent() |
||
58 | ->write("foreach (\$flashes as \$flash) {\n") |
||
59 | ->indent() |
||
60 | ->write("if (null === \$savedContext) {\n") |
||
61 | ->indent() |
||
62 | ->write("\$savedContext = \$context;\n") |
||
63 | ->outdent() |
||
64 | ->write("}\n") |
||
65 | ->write('$context = array(') |
||
66 | ->raw("\n") |
||
67 | ->indent() |
||
68 | ->write("'type' => \$type,\n") |
||
69 | ->write("'message' => \$this->env->getExtension('flash')->renderMessage(\$flash, \$catalog),\n") |
||
70 | ->write("'loop' => array(\n") |
||
71 | ->indent() |
||
72 | ->write("'first' => 0 === \$index,\n") |
||
73 | ->write("'last' => \$numFlashes === \$index,\n") |
||
74 | ->write("'length' => \$numFlashes,\n") |
||
75 | ->write("'index0' => \$index,\n") |
||
76 | ->write("'index' => \$index + 1\n") |
||
77 | ->outdent() |
||
78 | ->write(")\n") |
||
79 | ->outdent() |
||
80 | ->write(");\n") |
||
81 | ->subcompile($this->getNode('body')) |
||
82 | ->write("\$index++;\n") |
||
83 | ->outdent() |
||
84 | ->write("}\n") |
||
85 | ->outdent() |
||
86 | ->write("}\n") |
||
87 | ->write("if (null !== \$savedContext) {\n") |
||
88 | ->indent() |
||
89 | ->write("\$context = \$savedContext;\n") |
||
90 | ->write("unset(\$savedContext);\n") |
||
91 | ->outdent() |
||
92 | ->write("}\n") |
||
93 | ->write("unset(\$index, \$flashesByTypes, \$types, \$catalog);\n") |
||
94 | ; |
||
95 | } |
||
96 | } |
||
97 |
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: