From ccb11368cd999d95cffb79493de1455d363ba47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 25 Oct 2022 14:04:07 -0300 Subject: [PATCH] Remove OutputBuffering::getInstance() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The OutputBuffering class is only used inside the ResponseRendering class and since it's already a singleton, there is no reason to use the OutputBuffering class as a singleton as well. Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/OutputBuffering.php | 50 +++++--------------------- libraries/classes/ResponseRenderer.php | 31 ++++++++-------- phpstan-baseline.neon | 15 -------- psalm-baseline.xml | 11 ------ 4 files changed, 23 insertions(+), 84 deletions(-) diff --git a/libraries/classes/OutputBuffering.php b/libraries/classes/OutputBuffering.php index 0b1c42834f..6d0f979f8d 100644 --- a/libraries/classes/OutputBuffering.php +++ b/libraries/classes/OutputBuffering.php @@ -1,7 +1,4 @@ mode = $this->getMode(); - $this->on = false; } /** @@ -56,7 +45,7 @@ class OutputBuffering private function getMode() { $mode = 0; - if ($GLOBALS['cfg']['OBGzip'] && function_exists('ob_start')) { + if (! defined('TESTSUITE') && $GLOBALS['cfg']['OBGzip'] && function_exists('ob_start')) { if (ini_get('output_handler') === 'ob_gzhandler') { // If a user sets the output_handler in php.ini to ob_gzhandler, then // any right frame file in phpMyAdmin will not be handled properly by @@ -79,20 +68,6 @@ class OutputBuffering return $mode; } - /** - * Returns the singleton OutputBuffering object - * - * @return OutputBuffering object - */ - public static function getInstance() - { - if (empty(self::$instance)) { - self::$instance = new OutputBuffering(); - } - - return self::$instance; - } - /** * This function will need to run at the top of all pages if output * output buffering is turned on. It also needs to be passed $mode from @@ -100,7 +75,7 @@ class OutputBuffering */ public function start(): void { - if ($this->on) { + if (defined('TESTSUITE') || $this->on) { return; } @@ -111,12 +86,6 @@ class OutputBuffering ob_start(); $this->sendHeader('X-ob_mode', (string) $this->mode); - register_shutdown_function( - [ - self::class, - 'stop', - ] - ); $this->on = true; } @@ -134,15 +103,14 @@ class OutputBuffering * buffering is turned on. It also needs to be passed $mode from the * PMA_outBufferModeGet() function or it will be useless. */ - public static function stop(): void + public function stop(): void { - $buffer = self::getInstance(); - if (! $buffer->on) { + if (! $this->on) { return; } - $buffer->on = false; - $buffer->content = ob_get_contents(); + $this->on = false; + $this->content = (string) ob_get_contents(); if (ob_get_length() <= 0) { return; } diff --git a/libraries/classes/ResponseRenderer.php b/libraries/classes/ResponseRenderer.php index 01454fefea..4306973cc8 100644 --- a/libraries/classes/ResponseRenderer.php +++ b/libraries/classes/ResponseRenderer.php @@ -28,12 +28,11 @@ use const PHP_SAPI; class ResponseRenderer { /** - * Response instance - * * @static - * @var ResponseRenderer + * @var ResponseRenderer|null */ - private static $instance; + private static $instance = null; + /** * Header instance * @@ -156,14 +155,14 @@ class ResponseRenderer 511 => 'Network Authentication Required', ]; - /** - * Creates a new class instance - */ + /** @var OutputBuffering */ + private $buffer; + private function __construct() { + $this->buffer = new OutputBuffering(); + $this->buffer->start(); if (! defined('TESTSUITE')) { - $buffer = OutputBuffering::getInstance(); - $buffer->start(); register_shutdown_function([$this, 'response']); } @@ -191,13 +190,11 @@ class ResponseRenderer } /** - * Returns the singleton Response object - * - * @return ResponseRenderer object + * Returns the singleton object */ - public static function getInstance() + public static function getInstance(): ResponseRenderer { - if (empty(self::$instance)) { + if (self::$instance === null) { self::$instance = new ResponseRenderer(); } @@ -392,9 +389,9 @@ class ResponseRenderer */ public function response(): void { - $buffer = OutputBuffering::getInstance(); + $this->buffer->stop(); if (empty($this->HTML)) { - $this->HTML = $buffer->getContents(); + $this->HTML = $this->buffer->getContents(); } if ($this->isAjax()) { @@ -403,7 +400,7 @@ class ResponseRenderer echo $this->getDisplay(); } - $buffer->flush(); + $this->buffer->flush(); exit; } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 494947c678..8cd662527f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5450,16 +5450,6 @@ parameters: count: 1 path: libraries/classes/Operations.php - - - message: "#^Property PhpMyAdmin\\\\OutputBuffering\\:\\:\\$content \\(string\\) does not accept string\\|false\\.$#" - count: 1 - path: libraries/classes/OutputBuffering.php - - - - message: "#^Static property PhpMyAdmin\\\\OutputBuffering\\:\\:\\$instance \\(PhpMyAdmin\\\\OutputBuffering\\) in empty\\(\\) is not falsy\\.$#" - count: 1 - path: libraries/classes/OutputBuffering.php - - message: "#^Method PhpMyAdmin\\\\Partitioning\\\\Maintenance\\:\\:analyze\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -7045,11 +7035,6 @@ parameters: count: 1 path: libraries/classes/ResponseRenderer.php - - - message: "#^Static property PhpMyAdmin\\\\ResponseRenderer\\:\\:\\$instance \\(PhpMyAdmin\\\\ResponseRenderer\\) in empty\\(\\) is not falsy\\.$#" - count: 1 - path: libraries/classes/ResponseRenderer.php - - message: "#^Casting to array\\ something that's already array\\\\.$#" count: 4 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c02306c37e..a5964cf071 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -9114,14 +9114,6 @@ 'data' - - - empty(self::$instance) - - - $content - - $row['Table'] @@ -12262,9 +12254,6 @@ - - empty(self::$instance) - $value