Skip to content

Commit

Permalink
Refactor FormDisplayTemplate
Browse files Browse the repository at this point in the history
Uses a class member instead of a global.

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
  • Loading branch information
MauricioFauth committed Jun 6, 2018
1 parent b25be9e commit 8cad698
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
28 changes: 13 additions & 15 deletions libraries/classes/Config/FormDisplayTemplate.php
Expand Up @@ -21,6 +21,11 @@
*/
class FormDisplayTemplate
{
/**
* @var int
*/
public $group;

/**
* Displays top part of the form
*
Expand Down Expand Up @@ -105,9 +110,7 @@ public function displayFieldsetTop(
$errors = null,
array $attributes = []
): string {
global $_FormDisplayGroup;

$_FormDisplayGroup = 0;
$this->group = 0;

$attributes = array_merge(['class' => 'optbox'], $attributes);

Expand Down Expand Up @@ -156,7 +159,6 @@ public function displayInput(
$valueIsDefault = true,
$opts = null
): string {
global $_FormDisplayGroup;
static $icons; // An array of IMG tags used further below in the function

if (defined('TESTSUITE')) {
Expand Down Expand Up @@ -213,13 +215,13 @@ public function displayInput(
. ($hasErrors ? 'custom field-error' : 'custom');
}
$fieldClass = $fieldClass ? ' class="' . $fieldClass . '"' : '';
$trClass = $_FormDisplayGroup > 0
? 'group-field group-field-' . $_FormDisplayGroup
$trClass = $this->group > 0
? 'group-field group-field-' . $this->group
: '';
if (isset($opts['setvalue']) && $opts['setvalue'] == ':group') {
unset($opts['setvalue']);
$_FormDisplayGroup++;
$trClass = 'group-header-field group-header-' . $_FormDisplayGroup;
$this->group++;
$trClass = 'group-header-field group-header-' . $this->group;
}
if ($optionIsDisabled) {
$trClass .= ($trClass ? ' ' : '') . 'disabled-field';
Expand Down Expand Up @@ -383,16 +385,14 @@ public function displayInput(
*/
public function displayGroupHeader(string $headerText): string
{
global $_FormDisplayGroup;

$_FormDisplayGroup++;
$this->group++;
if ($headerText === '') {
return '';
}
$colspan = $GLOBALS['PMA_Config']->get('is_setup') ? 3 : 2;

return Template::get('config/form_display/group_header')->render([
'group' => $_FormDisplayGroup,
'group' => $this->group,
'colspan' => $colspan,
'header_text' => $headerText,
]);
Expand All @@ -405,9 +405,7 @@ public function displayGroupHeader(string $headerText): string
*/
public function displayGroupFooter(): void
{
global $_FormDisplayGroup;

$_FormDisplayGroup--;
$this->group--;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/classes/Config/FormDisplayTemplateTest.php
Expand Up @@ -410,7 +410,7 @@ public function testDisplayGroupHeader()
$this->formDisplayTemplate->displayGroupHeader('')
);

$GLOBALS['_FormDisplayGroup'] = 3;
$this->formDisplayTemplate->group = 3;

$GLOBALS['PMA_Config']->set('is_setup', true);

Expand All @@ -424,7 +424,7 @@ public function testDisplayGroupHeader()
// without PMA_SETUP
$GLOBALS['PMA_Config']->set('is_setup', false);

$GLOBALS['_FormDisplayGroup'] = 3;
$this->formDisplayTemplate->group = 3;

$result = $this->formDisplayTemplate->displayGroupHeader('headerText');

Expand All @@ -441,11 +441,11 @@ public function testDisplayGroupHeader()
*/
public function testDisplayGroupFooter()
{
$GLOBALS['_FormDisplayGroup'] = 3;
$this->formDisplayTemplate->group = 3;
$this->formDisplayTemplate->displayGroupFooter();
$this->assertEquals(
2,
$GLOBALS['_FormDisplayGroup']
$this->formDisplayTemplate->group
);
}

Expand Down

0 comments on commit 8cad698

Please sign in to comment.