refactoring - How to refactor my php methods? -
i have question regarding simplify codes.
i have
public function gettext($text){ if(!empty($text)){ $dom = new domdocument(); $dom->loadhtml($text); $xpath=new domxpath($dom); $result = $xpath->query('//a'); if($result->length > 0){ $atags=$dom->getelementsbytagname('a'); foreach($atags $atag){ $style = $atag ->getattribute('style'); $atag->setattribute('style',$style.' text-decoration:none;color:black;'); } $returntext .= $dom->savehtml(); return $returntext; } $result = $xpath->query('//table'); if($result->length > 0){ $tables = $dom->getelementsbytagname('table'); $inputs = $dom->getelementsbytagname('input'); foreach ($inputs $input) { $input->setattribute('style','text-align:center;'); } foreach ($tables $table) { $table->setattribute('width',500); $table->setattribute('style','border:2px solid #8c8c8c;text-align:center;table-layout:fixed;'); } $returntext .= $dom->savehtml(); return $returntext; } } return $text; } public function gettextwithindex($text,$index=''){ if(!empty($text[$index])){ $dom = new domdocument(); $dom->loadhtml($text[$index]); $xpath=new domxpath($dom); $result = $xpath->query('//a'); if($result->length > 0){ $atags=$dom->getelementsbytagname('a'); foreach($atags $atag){ $style = $atag ->getattribute('style'); $atag->setattribute('style',$style.' text-decoration:none;color:black;'); } $returntext .= $dom->savehtml(); return $returntext; } $result = $xpath->query('//tbody'); if($result->length > 0){ $tbodies = $dom->getelementsbytagname('tbody'); $cells = $dom->getelementsbytagname('td'); $inputs = $dom->getelementsbytagname('input'); foreach ($inputs $input) { $input->setattribute('style','text-align:center;'); } foreach ($cells $cell) { $cell->setattribute('style','border:1px solid black;'); } foreach ($tbodies $tbody) { $table = $dom->createelement('table'); $table->setattribute('width',500); $table->setattribute('style','border:2px solid #8c8c8c;text-align:center;table-layout:fixed;'); $tbody->parentnode->replacechild($table, $tbody); $table->appendchild($tbody); } $returntext .= $dom->savehtml(); return $returntext; } } return $text; }
the difference between method $index , modification of domdocument. feel it's cumbersome , use refactoring. have suggestions? thanks!
how this:
public function gettextwithindex($text,$index='') { if (empty($index)) return gettext($text); //not sure how $text works, line might different. return gettext($text[$index]); }
or this:
public function gettext($text, $index = false){ if ($index) $text = $text[$index]; if(!empty($text)){ $dom = new domdocument(); $dom->loadhtml($text); $xpath=new domxpath($dom); $result = $xpath->query('//a'); if($result->length > 0){ $atags=$dom->getelementsbytagname('a'); foreach($atags $atag){ $style = $atag ->getattribute('style'); $atag->setattribute('style',$style.' text-decoration:none;color:black;'); } $returntext .= $dom->savehtml(); return $returntext; } $result = $xpath->query('//table'); if($result->length > 0){ if ($index) { //do 'gettextwithindex' dom stuff } else { $tables = $dom->getelementsbytagname('table'); $inputs = $dom->getelementsbytagname('input'); } foreach ($inputs $input) { $input->setattribute('style','text-align:center;'); } foreach ($tables $table) { $table->setattribute('width',500); $table->setattribute('style','border:2px solid #8c8c8c;text-align:center;table-layout:fixed;'); } $returntext .= $dom->savehtml(); return $returntext; } } return $text;
}
Comments
Post a Comment