phpmyadmin/libraries/classes/OutputBuffering.php
Kamil Tekiela 2b35303aae
Remove redundant code (#18063)
PHPStorm has been a little annoying with showing all these code issues. I took some time to analyse most of them and fix whichever were easy to fix. This kind of change should improve future maintenance as developers do not need to wonder what a line of code does. Code that has no impact on functionality should be removed.

* Remove redundant variables
* Invert condition for better clarity
* Remove some of the redundant casts
* Replace switch with an if
* Fix foreach variable
* Remove redundant variable concat
* Remove redundant concats
* Replace for loop with str_repeat
* Remove always true param
* Turn property $position into local var
* Turn Pdf properties into local vars
* Remove unused properties in Pdf
* Change ternary into condition
* Remove redundant assignments
   They are immediately overwritten with a different value.
* Redundant array append
* Collapse if statements into assignments
* Use boolean constants instead of variables
* Remove unneeded parameter
* Remove null-coalesce
* Unnecessary assignment
* Remove redundant isset
* Remove readOnly flag for InsertEdit fields
* Remove function_exists and $mode var
* Remove duplicate condition
* Remove redundant elseif
* Remove redundant isset
* Use hasBodyParam()
* Simplify isRoutesCacheFileValid()
   Psalm complains but is wrong.

https://github.com/phpmyadmin/phpmyadmin/pull/18063

---------

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
2023-02-15 09:11:05 -03:00

144 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use function defined;
use function flush;
use function function_exists;
use function header;
use function ini_get;
use function ob_end_clean;
use function ob_flush;
use function ob_get_contents;
use function ob_get_length;
use function ob_get_level;
use function ob_get_status;
use function ob_start;
use function sprintf;
/**
* Output buffering wrapper class
*/
class OutputBuffering
{
private int $mode;
/** @var string */
private $content = '';
/** @var bool */
private $on = false;
public function __construct()
{
$this->mode = $this->getMode();
}
/**
* This function could be used eventually to support more modes.
*
* @return int the output buffer mode
*/
private function getMode(): int
{
if (! defined('TESTSUITE') && $GLOBALS['cfg']['OBGzip']) {
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
// the browser. My fix was to check the ini file within the
// PMA_outBufferModeGet() function.
return 0;
}
if (ob_get_level() > 0) {
// happens when php.ini's output_buffering is not Off
ob_end_clean();
return 1;
}
return 1;
}
// Zero (0) is no mode or in other words output buffering is OFF.
// Follow 2^0, 2^1, 2^2, 2^3 type values for the modes.
// Useful if we ever decide to combine modes. Then a bitmask field of
// the sum of all modes will be the natural choice.
return 0;
}
/**
* 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
* the PMA_outBufferModeGet() function or it will be useless.
*/
public function start(): void
{
if (defined('TESTSUITE') || $this->on) {
return;
}
if ($this->mode && function_exists('ob_gzhandler')) {
ob_start('ob_gzhandler');
}
ob_start();
$this->sendHeader('X-ob_mode', (string) $this->mode);
$this->on = true;
}
private function sendHeader(string $name, string $value): void
{
if (defined('TESTSUITE')) {
return;
}
header(sprintf('%s: %s', $name, $value));
}
/**
* This function will need to run at the bottom of all pages if output
* buffering is turned on. It also needs to be passed $mode from the
* PMA_outBufferModeGet() function or it will be useless.
*/
public function stop(): void
{
if (! $this->on) {
return;
}
$this->on = false;
$this->content = (string) ob_get_contents();
if (ob_get_length() <= 0) {
return;
}
ob_end_clean();
}
/**
* Gets buffer content
*
* @return string buffer content
*/
public function getContents()
{
return $this->content;
}
/**
* Flushes output buffer
*/
public function flush(): void
{
if (ob_get_status() && $this->mode) {
ob_flush();
} else {
flush();
}
}
}