We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 60 | 
| Total Lines | 276 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| 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 { | ||
| 39 | return isset($this->data[$var]); | ||
| 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 { | ||
| 60 | // If we already started output buffering before calling `display`, | ||
| 61 | // we may have unwanted content in the buffer that we need to remove | ||
| 62 | // before we send the Content-Type headers below. | ||
| 63 | // Skip this for debug builds to help discover offending output. | ||
| 64 | 		if (!ENABLE_DEBUG) { | ||
| 65 | 			if (ob_get_length() > 0) { | ||
| 66 | ob_clean(); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | ob_start(); | ||
| 70 | $this->includeTemplate($templateName); | ||
| 71 | $output = ob_get_clean(); | ||
| 72 | 		if ($output === false) { | ||
| 73 | 			throw new Exception('Output buffering is not active!'); | ||
| 74 | } | ||
| 75 | |||
| 76 | $ajaxEnabled = ($this->data['AJAX_ENABLE_REFRESH'] ?? false) !== false; | ||
| 77 | 		if ($ajaxEnabled) { | ||
| 78 | $ajaxXml = $this->convertHtmlToAjaxXml($output, $outputXml); | ||
| 79 | 			if ($outputXml) { | ||
| 80 | /* Left out for size: <?xml version="1.0" encoding="ISO-8859-1"?>*/ | ||
| 81 | $output = '<all>' . $ajaxXml . '</all>'; | ||
| 82 | } | ||
| 83 | $session = Session::getInstance(); | ||
| 84 | $session->saveAjaxReturns(); | ||
| 85 | } | ||
| 86 | |||
| 87 | // Now that we are completely done processing, we can output | ||
| 88 | 		if ($outputXml) { | ||
| 89 | 			header('Content-Type: text/xml; charset=utf-8'); | ||
| 90 | 		} else { | ||
| 91 | 			header('Content-Type: text/html; charset=utf-8'); | ||
| 92 | } | ||
| 93 | echo $output; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | 	protected function getTemplateLocation(string $templateName): string { | ||
| 98 | 		if (isset($this->data['ThisAccount'])) { | ||
| 99 | $templateDir = $this->data['ThisAccount']->getTemplate() . '/'; | ||
| 100 | 		} else { | ||
| 101 | $templateDir = 'Default/'; | ||
| 102 | } | ||
| 103 | $templateDirs = array_unique([$templateDir, 'Default/']); | ||
| 104 | |||
| 105 | 		foreach ($templateDirs as $templateDir) { | ||
| 106 | $filePath = TEMPLATES . $templateDir . 'engine/Default/' . $templateName; | ||
| 107 | 			if (is_file($filePath)) { | ||
| 108 | return $filePath; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | 		foreach ($templateDirs as $templateDir) { | ||
| 112 | $filePath = TEMPLATES . $templateDir . $templateName; | ||
| 113 | 			if (is_file($filePath)) { | ||
| 114 | return $filePath; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | 		throw new Exception('No template found for ' . $templateName); | ||
| 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 | 		foreach ($this->data as $key => $value) { | ||
| 128 | $$key = $value; | ||
| 129 | } | ||
| 130 | 		if ($assignVars !== null) { | ||
| 131 | 			foreach ($assignVars as $key => $value) { | ||
| 132 | $$key = $value; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | $this->nestedIncludes++; | ||
| 136 | require($this->getTemplateLocation($templateName)); | ||
| 137 | $this->nestedIncludes--; | ||
| 138 | } | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Check if the HTML includes input elements where the user is able to | ||
| 142 | * input data (i.e. we don't want to AJAX update a form that they may | ||
| 143 | * have already started filling out). | ||
| 144 | */ | ||
| 145 | 	protected function checkDisableAJAX(string $html): bool { | ||
| 146 | 		return preg_match('/<input (?![^>]*(submit|hidden|image))/i', $html) != 0; | ||
| 147 | } | ||
| 148 | |||
| 149 | 	protected function doDamageTypeReductionDisplay(int &$damageTypes): void { | ||
| 150 | 		if ($damageTypes == 3) { | ||
| 151 | echo ', '; | ||
| 152 | 		} elseif ($damageTypes == 2) { | ||
| 153 | echo ' and '; | ||
| 154 | } | ||
| 155 | $damageTypes--; | ||
| 156 | } | ||
| 157 | |||
| 158 | 	protected function doAn(string $wordAfter): string { | ||
| 159 | $char = strtoupper($wordAfter[0]); | ||
| 160 | 		return str_contains('AEIOU', $char) ? 'an' : 'a'; | ||
| 161 | } | ||
| 162 | |||
| 163 | /* | ||
| 164 | * EVAL is special (well, will be when needed and implemented in the javascript). | ||
| 165 | */ | ||
| 166 | 	public function addJavascriptForAjax(string $varName, mixed $obj): string { | ||
| 167 | 		if ($varName == 'EVAL') { | ||
| 168 | 			if (!isset($this->ajaxJS['EVAL'])) { | ||
| 169 | return $this->ajaxJS['EVAL'] = $obj; | ||
| 170 | } | ||
| 171 | return $this->ajaxJS['EVAL'] .= ';' . $obj; | ||
| 172 | } | ||
| 173 | |||
| 174 | 		if (isset($this->ajaxJS[$varName])) { | ||
| 175 | 			throw new Exception('Trying to set javascript val twice: ' . $varName); | ||
| 176 | } | ||
| 177 | $json = json_encode($obj); | ||
| 178 | 		if ($json === false) { | ||
| 179 | 			throw new Exception('Failed to encode to json: ' . $varName); | ||
| 180 | } | ||
| 181 | return $this->ajaxJS[$varName] = $json; | ||
| 182 | } | ||
| 183 | |||
| 184 | 	protected function addJavascriptAlert(string $string): void { | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | /** | ||
| 192 | * Registers a JS target for inclusion at the end of the HTML body. | ||
| 193 | */ | ||
| 194 | 	protected function addJavascriptSource(string $src): void { | ||
| 196 | } | ||
| 197 | |||
| 198 | 	protected function convertHtmlToAjaxXml(string $str, bool $returnXml): string { | ||
| 199 | 		if (empty($str)) { | ||
| 288 | } | ||
| 289 | |||
| 290 | } | ||
| 291 |