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:
Complex classes like Func often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Func, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Func extends SimpleVar { |
||
| 34 | |||
| 35 | const REGEX_FUNCSEL = '[a-zA-Z0-9\*\.,#_:\\^\\-@\\${}[\]]'; |
||
| 36 | |||
| 37 | protected $sub = null; |
||
| 38 | |||
| 39 | /** @var bool|Scope */ |
||
| 40 | protected $scope = false; |
||
| 41 | |||
| 42 | protected $filt; |
||
| 43 | |||
| 44 | protected $sortlimit; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Func constructor. |
||
| 49 | * @param string $s |
||
| 50 | */ |
||
| 51 | public function __construct($s) { |
||
| 52 | $m = array(); |
||
| 53 | if (!preg_match('/^\$\((' . self::REGEX_FUNCSEL . '*)(.*)\)$/', $s, $m)) |
||
| 54 | throw new ParseError("Unable to read \$ func in '$s'"); |
||
| 55 | if (trim($m[2])) |
||
| 56 | $this->sub = new FuncSub($m[2]); |
||
| 57 | if (!trim($m[1])) { |
||
| 58 | $this->scope = true; |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | if ($m[1][0] == '$' && $m[1][1] == '[') { |
||
| 62 | $this->scope = new Scope($m[1]); |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | $this->sortlimit = $this->attSortLimit($m[1]); |
||
| 66 | $this->filt = $this->attIdTag($m[1]); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function attIdTag(&$s) { |
||
| 70 | $m = array(); |
||
| 71 | $att = array('id' => array(), 'tag' => array()); |
||
| 72 | foreach (explode(",", $s) as $str) { |
||
| 73 | if (preg_match('/^[a-zA-Z0-9\\_]+/', $str, $m)) $type = $m[0]; |
||
| 74 | else $type = "*"; |
||
| 75 | if (preg_match('/#([a-zA-Z0-9\_\\${}]+)/', $str, $m)) $att['id'][$type][] = $m[1]; |
||
| 76 | elseif (preg_match_all('/\\.([a-zA-Z0-9\_\-\\${}]+)/', $str, $m)) |
||
| 77 | foreach ($m[1] as $tag) |
||
| 78 | $att['tag'][$type][] = new Text($tag, Text::TOKEN_CODE); |
||
| 79 | else $att['tag'][$type] = array(); |
||
| 80 | } |
||
| 81 | if (!(count($att['id']) xor count($att['tag']))) |
||
| 82 | throw new ParseError("Only tag, type or id can be combined"); |
||
| 83 | return $att; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function attSortLimit(&$s) { |
||
| 87 | $att = array('limit' => 0, 'offset' => 0, 'sort'=> []); |
||
| 88 | $m = array(); |
||
| 89 | if (preg_match('/:(?:([0-9]+)\-)?([0-9]+)/', $s, $m)) { |
||
| 90 | $att['limit'] = $m[2]; |
||
| 91 | $att['offset'] = $m[1] ? $m[1] : 0; |
||
| 92 | } |
||
| 93 | $rand = false; |
||
| 94 | if (preg_match_all('/\\^(-?)([a-zA-Z0-9\_]*)/', $s, $m)) { |
||
| 95 | foreach($m[0] as $k=>$mv) |
||
| 96 | if ($m[2][$k]) { |
||
| 97 | $dir = $m[1][$k] == "-"?Hamle\Hamle::SORT_DESCENDING:Hamle\Hamle::SORT_ASCENDING; |
||
| 98 | $att['sort'][$m[2][$k]] = $dir; |
||
| 99 | } else $rand = true; |
||
| 100 | } |
||
| 101 | if($rand) |
||
| 102 | $att['sort'] = [""=>$att['dir'] = Hamle\Hamle::SORT_RANDOM]; |
||
| 103 | return $att; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function attGroupType(&$s) { |
||
| 107 | $att = array('grouptype' => 0); |
||
| 108 | $m = array(); |
||
| 109 | if (preg_match('/@([0-9]+)/', $s, $m)) { |
||
| 110 | $att['grouptype'] = $m[1]; |
||
| 111 | } |
||
| 112 | return $att; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string PHP Code |
||
| 117 | */ |
||
| 118 | public function toPHP() { |
||
| 119 | $sub = $this->sub ? "->" . $this->sub->toPHP() : ""; |
||
| 120 | if($this->scope instanceof Scope) { |
||
| 121 | return $this->scope->toPHP() . $sub; |
||
| 122 | } elseif($this->scope === true) { |
||
| 123 | return "Hamle\\Scope::get(0)$sub"; |
||
| 124 | } |
||
| 125 | $limit = Text::varToCode($this->sortlimit['sort']) . "," . |
||
| 126 | $this->sortlimit['limit'] . "," . $this->sortlimit['offset']; |
||
| 127 | if (count($this->filt['tag'])) |
||
| 128 | return "Hamle\\Run::modelTypeTags(" . |
||
| 129 | Text::varToCode($this->filt['tag']) . ",$limit)$sub"; |
||
| 130 | if (count($this->filt['id'])) |
||
| 131 | if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) == 1) |
||
| 132 | return "Hamle\\Run::modelId(" . |
||
| 133 | Text::varToCode(current($this->filt['id']['*'])) . |
||
| 134 | ",$limit)$sub"; |
||
| 135 | else |
||
| 136 | return "Hamle\\Run::modelTypeId(" . |
||
| 137 | Text::varToCode($this->filt['id']) . ",$limit)$sub"; |
||
| 138 | return ""; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param Model|null $parent |
||
| 143 | * @return Model |
||
| 144 | */ |
||
| 145 | public function getOrCreateModel(Model $parent = null) { |
||
| 146 | if($this->scope instanceof Scope) { |
||
| 147 | $parent = $this->scope->getOrCreateModel(); |
||
| 148 | } elseif ($this->scope === true) |
||
| 149 | $parent = \Seufert\Hamle\Scope::get(0); |
||
| 150 | if ($this->filt && count($this->filt['tag'])) |
||
| 151 | $parent = \Seufert\Hamle\Run::modelTypeTags( |
||
| 152 | $this->filt['tag'], |
||
| 153 | $this->sortlimit['sort'], |
||
| 154 | $this->sortlimit['limit'], |
||
| 155 | $this->sortlimit['offset'] |
||
| 156 | ); |
||
| 157 | if ($this->filt && count($this->filt['id'])) |
||
| 158 | if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) === 1) |
||
| 159 | $parent = \Seufert\Hamle\Run::modelId( |
||
| 160 | current($this->filt['id']['*']), |
||
| 161 | $this->sortlimit['sort'], |
||
| 162 | $this->sortlimit['limit'], |
||
| 163 | $this->sortlimit['offset'] |
||
| 164 | ); |
||
| 165 | else |
||
| 166 | $parent = \Seufert\Hamle\Run::modelTypeId( |
||
| 167 | $this->filt['id'], |
||
| 168 | $this->sortlimit['sort'], |
||
| 169 | $this->sortlimit['limit'], |
||
| 170 | $this->sortlimit['offset'] |
||
| 171 | ); |
||
| 172 | if($this->sub) |
||
| 173 | return $this->sub->getOrCreateModel($parent); |
||
| 174 | if(!$parent) |
||
| 175 | throw new \RuntimeException('Unable to create model with no relation'); |
||
| 176 | return $parent; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function toHTML($escape = false) { |
||
| 180 | throw new ParseError("Unable to use Scope operator in HTML Code"); |
||
| 181 | } |
||
| 182 | } |