We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 58 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Template 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.
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 Template, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 12 | class Template { |
||
| 13 | |||
| 14 | /** @var array<string, mixed> */ |
||
| 15 | private array $data = []; |
||
| 16 | private int $nestedIncludes = 0; |
||
| 17 | /** @var array<string, mixed> */ |
||
| 18 | private array $ajaxJS = []; |
||
| 19 | /** @var array<string> */ |
||
| 20 | protected array $jsAlerts = []; |
||
| 21 | /** @var array<string> */ |
||
| 22 | protected array $jsSources = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Defines a listjs_include.js function to call at the end of the HTML body. |
||
| 26 | */ |
||
| 27 | public ?string $listjsInclude = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Return the Smr\Template in the DI container. |
||
| 31 | * If one does not exist yet, it will be created. |
||
| 32 | * This is the intended way to construct this class. |
||
| 33 | */ |
||
| 34 | public static function getInstance(): self { |
||
| 36 | } |
||
| 37 | |||
| 38 | public function hasTemplateVar(string $var): bool { |
||
| 40 | } |
||
| 41 | |||
| 42 | public function assign(string $var, mixed $value): void { |
||
| 43 | if (!isset($this->data[$var])) { |
||
| 44 | $this->data[$var] = $value; |
||
| 45 | } else { |
||
| 46 | // We insist that template variables not change once they are set |
||
| 47 | throw new Exception("Cannot re-assign template variable '$var'!"); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | public function unassign(string $var): void { |
||
| 52 | unset($this->data[$var]); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Displays the template HTML. Stores any ajax-enabled elements for future |
||
| 57 | * comparison, and outputs modified elements in XML for ajax if requested. |
||
| 58 | */ |
||
| 59 | public function display(string $templateName, bool $outputXml = false): void { |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | protected function getTemplateLocation(string $templateName): string { |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param array<string, mixed> $assignVars |
||
| 122 | */ |
||
| 123 | protected function includeTemplate(string $templateName, array $assignVars = null): void { |
||
| 124 | if ($this->nestedIncludes > 15) { |
||
| 125 | throw new Exception('Nested more than 15 template includes, is something wrong?'); |
||
| 126 | } |
||
| 127 | extract($this->data); |
||
| 128 | if ($assignVars !== null) { |
||
| 129 | extract($assignVars); |
||
| 130 | } |
||
| 131 | $this->nestedIncludes++; |
||
| 132 | require($this->getTemplateLocation($templateName)); |
||
| 133 | $this->nestedIncludes--; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if the HTML includes input elements where the user is able to |
||
| 138 | * input data (i.e. we don't want to AJAX update a form that they may |
||
| 139 | * have already started filling out). |
||
| 140 | */ |
||
| 141 | protected function checkDisableAJAX(string $html): bool { |
||
| 142 | return preg_match('/<input (?![^>]*(submit|hidden|image))/i', $html) != 0; |
||
| 143 | } |
||
| 144 | |||
| 145 | protected function doDamageTypeReductionDisplay(int &$damageTypes): void { |
||
| 146 | if ($damageTypes == 3) { |
||
| 147 | echo ', '; |
||
| 148 | } elseif ($damageTypes == 2) { |
||
| 149 | echo ' and '; |
||
| 150 | } |
||
| 151 | $damageTypes--; |
||
| 152 | } |
||
| 153 | |||
| 154 | protected function doAn(string $wordAfter): string { |
||
| 155 | $char = strtoupper($wordAfter[0]); |
||
| 156 | return str_contains('AEIOU', $char) ? 'an' : 'a'; |
||
| 157 | } |
||
| 158 | |||
| 159 | /* |
||
| 160 | * EVAL is special (well, will be when needed and implemented in the javascript). |
||
| 161 | */ |
||
| 162 | public function addJavascriptForAjax(string $varName, mixed $obj): string { |
||
| 163 | if ($varName == 'EVAL') { |
||
| 164 | if (!isset($this->ajaxJS['EVAL'])) { |
||
| 165 | return $this->ajaxJS['EVAL'] = $obj; |
||
| 166 | } |
||
| 167 | return $this->ajaxJS['EVAL'] .= ';' . $obj; |
||
| 168 | } |
||
| 169 | |||
| 170 | if (isset($this->ajaxJS[$varName])) { |
||
| 171 | throw new Exception('Trying to set javascript val twice: ' . $varName); |
||
| 172 | } |
||
| 173 | $json = json_encode($obj); |
||
| 174 | if ($json === false) { |
||
| 175 | throw new Exception('Failed to encode to json: ' . $varName); |
||
| 176 | } |
||
| 177 | return $this->ajaxJS[$varName] = $json; |
||
| 178 | } |
||
| 179 | |||
| 180 | protected function addJavascriptAlert(string $string): void { |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Registers a JS target for inclusion at the end of the HTML body. |
||
| 189 | */ |
||
| 190 | protected function addJavascriptSource(string $src): void { |
||
| 192 | } |
||
| 193 | |||
| 194 | protected function convertHtmlToAjaxXml(string $str, bool $returnXml): string { |
||
| 284 | } |
||
| 285 | |||
| 286 | } |
||
| 287 |