| Total Complexity | 78 |
| Total Lines | 568 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TimeTracker 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 TimeTracker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class TimeTracker implements SingletonInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * If set to true (see constructor) then then the timetracking is enabled |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $isEnabled = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Is loaded with the millisecond time when this object is created |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | public $starttime = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Is set via finish() with the millisecond time when the request handler is finished. |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $finishtime = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Log Rendering flag. If set, ->push() and ->pull() is called from the cObj->cObjGetSingle(). |
||
| 50 | * This determines whether or not the TypoScript parsing activity is logged. But it also slows down the rendering |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | public $LR = true; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | public $printConf = [ |
||
| 60 | 'showParentKeys' => 1, |
||
| 61 | 'contentLength' => 10000, |
||
| 62 | // Determines max length of displayed content before it gets cropped. |
||
| 63 | 'contentLength_FILE' => 400, |
||
| 64 | // Determines max length of displayed content FROM FILE cObjects before it gets cropped. Reason is that most FILE cObjects are huge and often used as template-code. |
||
| 65 | 'flag_tree' => 1, |
||
| 66 | 'flag_messages' => 1, |
||
| 67 | 'flag_content' => 0, |
||
| 68 | 'allTime' => 0, |
||
| 69 | 'keyLgd' => 40 |
||
| 70 | ]; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | public $wrapError = [ |
||
| 76 | 0 => ['', ''], |
||
| 77 | 1 => ['<strong>', '</strong>'], |
||
| 78 | 2 => ['<strong style="color:#ff6600;">', '</strong>'], |
||
| 79 | 3 => ['<strong style="color:#ff0000;">', '</strong>'] |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | public $wrapIcon = [ |
||
| 86 | 0 => '', |
||
| 87 | 1 => 'actions-document-info', |
||
| 88 | 2 => 'status-dialog-warning', |
||
| 89 | 3 => 'status-dialog-error' |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | public $uniqueCounter = 0; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | public $tsStack = [[]]; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | public $tsStackLevel = 0; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | public $tsStackLevelMax = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | public $tsStackLog = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var int |
||
| 119 | */ |
||
| 120 | public $tsStackPointer = 0; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | public $currentHashPointer = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Log entries that take than this number of milliseconds (own time) will be highlighted during log display. Set 0 to disable highlighting. |
||
| 129 | * |
||
| 130 | * @var int |
||
| 131 | */ |
||
| 132 | public $highlightLongerThan = 0; |
||
| 133 | |||
| 134 | /******************************************* |
||
| 135 | * |
||
| 136 | * Logging parsing times in the scripts |
||
| 137 | * |
||
| 138 | *******************************************/ |
||
| 139 | |||
| 140 | /** |
||
| 141 | * TimeTracker constructor. |
||
| 142 | * |
||
| 143 | * @param bool $isEnabled |
||
| 144 | */ |
||
| 145 | public function __construct($isEnabled = true) |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param bool $isEnabled |
||
| 152 | */ |
||
| 153 | public function setEnabled(bool $isEnabled = true) |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets the starting time |
||
| 160 | * |
||
| 161 | * @see finish() |
||
| 162 | * @param float|null $starttime |
||
| 163 | */ |
||
| 164 | public function start(?float $starttime = null) |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Pushes an element to the TypoScript tracking array |
||
| 174 | * |
||
| 175 | * @param string $tslabel Label string for the entry, eg. TypoScript property name |
||
| 176 | * @param string $value Additional value(?) |
||
| 177 | * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::cObjGetSingle() |
||
| 178 | * @see pull() |
||
| 179 | */ |
||
| 180 | public function push($tslabel, $value = '') |
||
| 181 | { |
||
| 182 | if (!$this->isEnabled) { |
||
| 183 | return; |
||
| 184 | } |
||
| 185 | $this->tsStack[$this->tsStackPointer][] = $tslabel; |
||
| 186 | $this->currentHashPointer[] = 'timetracker_' . $this->uniqueCounter++; |
||
| 187 | $this->tsStackLevel++; |
||
| 188 | $this->tsStackLevelMax[] = $this->tsStackLevel; |
||
| 189 | // setTSlog |
||
| 190 | $k = end($this->currentHashPointer); |
||
| 191 | $this->tsStackLog[$k] = [ |
||
| 192 | 'level' => $this->tsStackLevel, |
||
| 193 | 'tsStack' => $this->tsStack, |
||
| 194 | 'value' => $value, |
||
| 195 | 'starttime' => microtime(true), |
||
| 196 | 'stackPointer' => $this->tsStackPointer |
||
| 197 | ]; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Pulls an element from the TypoScript tracking array |
||
| 202 | * |
||
| 203 | * @param string $content The content string generated within the push/pull part. |
||
| 204 | * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::cObjGetSingle() |
||
| 205 | * @see push() |
||
| 206 | */ |
||
| 207 | public function pull($content = '') |
||
| 208 | { |
||
| 209 | if (!$this->isEnabled) { |
||
| 210 | return; |
||
| 211 | } |
||
| 212 | $k = end($this->currentHashPointer); |
||
| 213 | $this->tsStackLog[$k]['endtime'] = microtime(true); |
||
| 214 | $this->tsStackLog[$k]['content'] = $content; |
||
| 215 | $this->tsStackLevel--; |
||
| 216 | array_pop($this->tsStack[$this->tsStackPointer]); |
||
| 217 | array_pop($this->currentHashPointer); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Logs the TypoScript entry |
||
| 222 | * |
||
| 223 | * @param string $content The message string |
||
| 224 | * @param int $num Message type: 0: information, 1: message, 2: warning, 3: error |
||
| 225 | * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::CONTENT() |
||
| 226 | */ |
||
| 227 | public function setTSlogMessage($content, $num = 0) |
||
| 228 | { |
||
| 229 | if (!$this->isEnabled) { |
||
| 230 | return; |
||
| 231 | } |
||
| 232 | end($this->currentHashPointer); |
||
| 233 | $k = current($this->currentHashPointer); |
||
| 234 | $placeholder = ''; |
||
| 235 | // Enlarge the "details" column by adding a span |
||
| 236 | if (strlen($content) > 30) { |
||
| 237 | $placeholder = '<br /><span style="width: 300px; height: 1px; display: inline-block;"></span>'; |
||
| 238 | } |
||
| 239 | $iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
| 240 | $this->tsStackLog[$k]['message'][] = $iconFactory->getIcon($this->wrapIcon[$num], Icon::SIZE_SMALL)->render() . $this->wrapError[$num][0] . htmlspecialchars($content) . $this->wrapError[$num][1] . $placeholder; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Set TSselectQuery - for messages in TypoScript debugger. |
||
| 245 | * |
||
| 246 | * @param array $data Query array |
||
| 247 | * @param string $msg Message/Label to attach |
||
| 248 | */ |
||
| 249 | public function setTSselectQuery(array $data, $msg = '') |
||
| 250 | { |
||
| 251 | if (!$this->isEnabled) { |
||
| 252 | return; |
||
| 253 | } |
||
| 254 | end($this->currentHashPointer); |
||
| 255 | $k = current($this->currentHashPointer); |
||
| 256 | if ($msg !== '') { |
||
| 257 | $data['msg'] = $msg; |
||
| 258 | } |
||
| 259 | $this->tsStackLog[$k]['selectQuery'][] = $data; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Increases the stack pointer |
||
| 264 | * |
||
| 265 | * @see decStackPointer() |
||
| 266 | * @see \TYPO3\CMS\Frontend\Page\PageGenerator::renderContent() |
||
| 267 | * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::cObjGetSingle() |
||
| 268 | */ |
||
| 269 | public function incStackPointer() |
||
| 270 | { |
||
| 271 | if (!$this->isEnabled) { |
||
| 272 | return; |
||
| 273 | } |
||
| 274 | $this->tsStackPointer++; |
||
| 275 | $this->tsStack[$this->tsStackPointer] = []; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Decreases the stack pointer |
||
| 280 | * |
||
| 281 | * @see incStackPointer() |
||
| 282 | * @see \TYPO3\CMS\Frontend\Page\PageGenerator::renderContent() |
||
| 283 | * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::cObjGetSingle() |
||
| 284 | */ |
||
| 285 | public function decStackPointer() |
||
| 286 | { |
||
| 287 | if (!$this->isEnabled) { |
||
| 288 | return; |
||
| 289 | } |
||
| 290 | unset($this->tsStack[$this->tsStackPointer]); |
||
| 291 | $this->tsStackPointer--; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Gets a microtime value as milliseconds value. |
||
| 296 | * |
||
| 297 | * @param float $microtime The microtime value - if not set the current time is used |
||
| 298 | * @return int The microtime value as milliseconds value |
||
| 299 | */ |
||
| 300 | public function getMilliseconds($microtime = null) |
||
| 301 | { |
||
| 302 | if (!$this->isEnabled) { |
||
| 303 | return 0; |
||
| 304 | } |
||
| 305 | if (!isset($microtime)) { |
||
| 306 | $microtime = microtime(true); |
||
| 307 | } |
||
| 308 | return (int)round($microtime * 1000); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Gets the difference between a given microtime value and the starting time as milliseconds. |
||
| 313 | * |
||
| 314 | * @param float $microtime The microtime value - if not set the current time is used |
||
| 315 | * @return int The difference between a given microtime value and starting time as milliseconds |
||
| 316 | */ |
||
| 317 | public function getDifferenceToStarttime($microtime = null) |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Usually called when the page generation and output is prepared. |
||
| 324 | * |
||
| 325 | * @see start() |
||
| 326 | */ |
||
| 327 | public function finish(): void |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get total parse time in milliseconds |
||
| 336 | * |
||
| 337 | * @return int |
||
| 338 | */ |
||
| 339 | public function getParseTime(): int |
||
| 340 | { |
||
| 341 | if (!$this->starttime) { |
||
| 342 | $this->start(microtime(true)); |
||
| 343 | } |
||
| 344 | if (!$this->finishtime) { |
||
| 345 | $this->finish(); |
||
| 346 | } |
||
| 347 | return $this->getDifferenceToStarttime($this->finishtime ?? null); |
||
| 348 | } |
||
| 349 | |||
| 350 | /******************************************* |
||
| 351 | * |
||
| 352 | * Printing the parsing time information (for Admin Panel) |
||
| 353 | * |
||
| 354 | *******************************************/ |
||
| 355 | /** |
||
| 356 | * Print TypoScript parsing log |
||
| 357 | * |
||
| 358 | * @return string HTML table with the information about parsing times. |
||
| 359 | */ |
||
| 360 | public function printTSlog() |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Recursively generates the content to display |
||
| 484 | * |
||
| 485 | * @param array $arr Array which is modified with content. Reference |
||
| 486 | * @param string $content Current content string for the level |
||
| 487 | * @param string $depthData Prefixed icons for new PM icons |
||
| 488 | * @param string $vKey Seems to be the previous tsStackLog key |
||
| 489 | * @return string Returns the $content string generated/modified. Also the $arr array is modified! |
||
| 490 | */ |
||
| 491 | protected function fixContent(&$arr, $content, $depthData = '', $vKey = '') |
||
| 492 | { |
||
| 493 | $entriesCount = 0; |
||
| 494 | $c = 0; |
||
| 495 | // First, find number of entries |
||
| 496 | foreach ($arr as $k => $v) { |
||
| 497 | //do not count subentries (the one ending with dot, eg. '9.' |
||
| 498 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($k)) { |
||
| 499 | $entriesCount++; |
||
| 500 | } |
||
| 501 | } |
||
| 502 | // Traverse through entries |
||
| 503 | $subtime = 0; |
||
| 504 | foreach ($arr as $k => $v) { |
||
| 505 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($k)) { |
||
| 506 | $c++; |
||
| 507 | $hasChildren = isset($arr[$k . '.']); |
||
| 508 | $lastEntry = $entriesCount === $c; |
||
| 509 | |||
| 510 | $PM = '<span class="treeline-icon treeline-icon-join' . ($lastEntry ? 'bottom' : '') . '"></span>'; |
||
| 511 | |||
| 512 | $this->tsStackLog[$v]['icons'] = $depthData . $PM; |
||
| 513 | if ($this->tsStackLog[$v]['content'] !== '') { |
||
| 514 | $content = str_replace($this->tsStackLog[$v]['content'], $v, $content); |
||
| 515 | } |
||
| 516 | if ($hasChildren) { |
||
| 517 | $lineClass = $lastEntry ? 'treeline-icon-clear' : 'treeline-icon-line'; |
||
| 518 | $this->tsStackLog[$v]['content'] = $this->fixContent($arr[$k . '.'], $this->tsStackLog[$v]['content'], $depthData . '<span class="treeline-icon ' . $lineClass . '"></span>', $v); |
||
| 519 | } else { |
||
| 520 | $this->tsStackLog[$v]['content'] = $this->fixCLen($this->tsStackLog[$v]['content'], $this->tsStackLog[$v]['value']); |
||
| 521 | $this->tsStackLog[$v]['subtime'] = ''; |
||
| 522 | $this->tsStackLog[$v]['owntime'] = $this->tsStackLog[$v]['deltatime']; |
||
| 523 | } |
||
| 524 | $subtime += $this->tsStackLog[$v]['deltatime']; |
||
| 525 | } |
||
| 526 | } |
||
| 527 | // Set content with special chars |
||
| 528 | if (isset($this->tsStackLog[$vKey])) { |
||
| 529 | $this->tsStackLog[$vKey]['subtime'] = $subtime; |
||
| 530 | $this->tsStackLog[$vKey]['owntime'] = $this->tsStackLog[$vKey]['deltatime'] - $subtime; |
||
| 531 | } |
||
| 532 | $content = $this->fixCLen($content, $this->tsStackLog[$vKey]['value']); |
||
| 533 | // Traverse array again, this time substitute the unique hash with the red key |
||
| 534 | foreach ($arr as $k => $v) { |
||
| 535 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($k)) { |
||
| 536 | if ($this->tsStackLog[$v]['content'] !== '') { |
||
| 537 | $content = str_replace($v, '<strong style="color:red;">[' . $this->tsStackLog[$v]['key'] . ']</strong>', $content); |
||
| 538 | } |
||
| 539 | } |
||
| 540 | } |
||
| 541 | // Return the content |
||
| 542 | return $content; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Wraps the input content string in green colored span-tags IF the length of the input string exceeds $this->printConf['contentLength'] (or $this->printConf['contentLength_FILE'] if $v == "FILE" |
||
| 547 | * |
||
| 548 | * @param string $c The content string |
||
| 549 | * @param string $v Command: If "FILE" then $this->printConf['contentLength_FILE'] is used for content length comparison, otherwise $this->printConf['contentLength'] |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | protected function fixCLen($c, $v) |
||
| 553 | { |
||
| 554 | $len = $v === 'FILE' ? $this->printConf['contentLength_FILE'] : $this->printConf['contentLength']; |
||
| 555 | if (strlen($c) > $len) { |
||
| 556 | $c = '<span style="color:green;">' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($c, $len)) . '</span>'; |
||
| 557 | } else { |
||
| 558 | $c = htmlspecialchars($c); |
||
| 559 | } |
||
| 560 | return $c; |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Wraps input string in a <span> tag |
||
| 565 | * |
||
| 566 | * @param string $str The string to be wrapped |
||
| 567 | * @return string |
||
| 568 | */ |
||
| 569 | protected function fw($str) |
||
| 570 | { |
||
| 571 | return '<span>' . $str . '</span>'; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Helper function for internal data manipulation |
||
| 576 | * |
||
| 577 | * @param array $arr Array (passed by reference) and modified |
||
| 578 | * @param int $pointer Pointer value |
||
| 579 | * @param string $uniqueId Unique ID string |
||
| 580 | * @internal |
||
| 581 | * @see printTSlog() |
||
| 582 | */ |
||
| 583 | protected function createHierarchyArray(&$arr, $pointer, $uniqueId) |
||
| 594 | } |
||
| 595 | } |
||
| 596 | } |
||
| 597 |