| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Seboettg\CiteProc\Rendering; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Seboettg\CiteProc\CiteProc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Seboettg\CiteProc\Rendering\RenderingInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Seboettg\CiteProc\Styles\AffixesTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Seboettg\CiteProc\Styles\FormattingTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Seboettg\CiteProc\Styles\DelimiterTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Seboettg\CiteProc\Util\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Seboettg\Collection\ArrayList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class Layout | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package Seboettg\CiteProc\Rendering | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author Sebastian Böttger <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Layout implements RenderingInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private static $numberOfCitedItems = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use AffixesTrait, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         FormattingTrait, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         DelimiterTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var ArrayList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $children; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 34 |  | View Code Duplication |     public function __construct($node) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         self::$numberOfCitedItems = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->children = new ArrayList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         foreach ($node->children() as $child) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->children->append(Factory::create($child)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->initDelimiterAttributes($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->initAffixesAttributes($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->initFormattingAttributes($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function render($data) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $ret = ""; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $sorting = CiteProc::getContext()->getSorting(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if (!empty($sorting)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $sorting->sort($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         if (CiteProc::getContext()->isModeBibliography()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             if (is_array($data)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 $arr = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 foreach ($data as $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     ++self::$numberOfCitedItems; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                     $arr[] = $this->wrapBibEntry($this->renderSingle($item)); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 $ret .= implode($this->delimiter, $arr); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 $ret .= $this->wrapBibEntry($this->renderSingle($data)); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             return "<div class=\"csl-bib-body\">".$ret."\n</div>"; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } else if (CiteProc::getContext()->isModeCitation()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             if (is_array($data)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 $arr = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 foreach ($data as $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                     if (CiteProc::getContext()->hasCitationItems()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                         continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                     $arr[] = $this->renderSingle($item); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 $ret .= implode($this->delimiter, $arr); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 $ret .= $this->renderSingle($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         return $this->addAffixes($ret); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     private function renderSingle($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $ret = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         /** @var RenderingInterface $child */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         foreach ($this->children as $child) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $ret .= $child->render($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $this->format($ret); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public static function getNumberOfCitedItems() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return self::$numberOfCitedItems; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     private function wrapBibEntry($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return "\n  <div class=\"csl-entry\">" . $value . "</div>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 110 |  |  | } | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.