Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
34 | class Control extends H\Tag { |
||
35 | /** |
||
36 | * @var string Variable passed to Control Tag |
||
37 | */ |
||
38 | protected $var; |
||
39 | public $o, $else = false; |
||
40 | static $instCount = 1; |
||
41 | |||
42 | /** |
||
43 | * Crate new Control Tag |
||
44 | * @param string $tag Type of Control Tag |
||
45 | * @param \Seufert\Hamle\Tag $parentTag |
||
46 | * @throws ParseError |
||
47 | */ |
||
48 | function __construct($tag, $parentTag = null) { |
||
49 | parent::__construct(); |
||
50 | $this->o = "\$o" . self::$instCount++; |
||
51 | $this->type = strtolower($tag); |
||
52 | $this->var = ""; |
||
53 | if ($parentTag && $this->type == "else") { |
||
54 | if($parentTag instanceof H\Tag) { |
||
55 | $elseTag = $parentTag->tags[count($parentTag->tags) - 1]; |
||
56 | if($elseTag instanceof H\Tag\Control && |
||
57 | in_array($elseTag->type, array('with', 'if')) |
||
58 | ) { |
||
59 | $elseTag->else = true; |
||
60 | } else { |
||
61 | throw new ParseError("You can only use else with |with and |if, you tried |{$parentTag->type}"); |
||
62 | } |
||
63 | } else { |
||
64 | throw new ParseError("Unable to use else here"); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | function renderStTag() { |
||
70 | $out = "<" . "?php "; |
||
71 | $scopeName = ""; |
||
72 | if (preg_match('/ as ([a-zA-Z]+)$/', $this->var, $m)) { |
||
73 | $scopeName = $m[1]; |
||
74 | $lookup = substr($this->var, 0, strlen($this->var) - strlen($m[0])); |
||
75 | $hsv = new H\Text(trim($lookup), H\Text::TOKEN_CONTROL); |
||
76 | } else |
||
77 | $hsv = new H\Text($this->var, H\Text::TOKEN_CONTROL); |
||
78 | switch ($this->type) { |
||
79 | case "each": |
||
80 | if ($this->var) |
||
81 | $out .= "foreach(" . $hsv->toPHP() . " as {$this->o}) { \n"; |
||
82 | else |
||
83 | $out .= "foreach(Hamle\\Scope::get() as {$this->o}) { \n"; |
||
84 | $out .= "Hamle\\Scope::add({$this->o}); "; |
||
85 | break; |
||
86 | case "if": |
||
87 | $hsvcomp = new H\Text\Comparison($this->var); |
||
88 | $out .= "if(" . $hsvcomp->toPHP() . ") {"; |
||
89 | break; |
||
90 | case "with": |
||
91 | if ($scopeName) |
||
92 | $out .= "Hamle\\Scope::add(" . $hsv->toPHP() . ", \"$scopeName\");\n;"; |
||
93 | else { |
||
94 | $out .= "if(({$this->o} = " . $hsv->toPHP() . ") && " . |
||
95 | "{$this->o}->valid()) {\n"; |
||
96 | $out .= "Hamle\\Scope::add({$this->o});\n;"; |
||
97 | } |
||
98 | break; |
||
99 | case "else": |
||
100 | $out .= "/* else */"; |
||
101 | break; |
||
102 | case "include": |
||
103 | $file = $hsv->toHTML(); |
||
104 | if($file[0] == "#") |
||
105 | $out .= "echo Hamle\\Run::includeFragment(".$hsv->toPHP().");"; |
||
106 | else |
||
107 | $out .= "echo Hamle\\Run::includeFile(" . $hsv->toPHP() . ");"; |
||
108 | break; |
||
109 | } |
||
110 | return $out . ' ?>'; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $s Variable String for control tag |
||
115 | */ |
||
116 | function setVar($s) { |
||
117 | $this->var = trim($s); |
||
118 | } |
||
119 | |||
120 | function renderEnTag() { |
||
121 | $out = '<' . '?php '; |
||
122 | switch ($this->type) { |
||
123 | case "each"; |
||
124 | $out .= 'Hamle\\Scope::done(); '; |
||
125 | $out .= '}'; |
||
126 | if (!$this->var) |
||
127 | $out .= "Hamle\\Scope::get()->rewind();\n"; |
||
128 | break; |
||
129 | case "if": |
||
130 | case "else": |
||
131 | $out .= "}"; |
||
132 | break; |
||
133 | case "with"; |
||
134 | if (!preg_match('/ as ([a-zA-Z]+)$/', $this->var, $m)) { |
||
135 | $out .= 'Hamle\\Scope::done(); '; |
||
136 | $out .= '}'; |
||
137 | } |
||
138 | break; |
||
139 | case "include": |
||
140 | return ""; |
||
141 | break; |
||
142 | } |
||
143 | if ($this->else) $out .= "else{"; |
||
144 | return $out . ' ?>'; |
||
145 | } |
||
146 | |||
147 | function render($indent = 0, $doIndent = true) { |
||
148 | return parent::render($indent - self::INDENT_SIZE, false); |
||
149 | } |
||
150 | } |