Skip to content

Commit

Permalink
LangFileCleaner: Merge global and local version of variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Sauvat committed Jan 12, 2016
1 parent 90c5f54 commit fed08c7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 31 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Changelog
=========

(unreleased)
(unrelease)
-----
* LangFileCleaner: Merge global and local version of variables.

1.0.0
-----
* Add UsersManager class.
* Reworked Bean::updateBean
Expand Down
25 changes: 13 additions & 12 deletions src/LangFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,23 @@ public function checkVarName($var_name)
if (array_key_exists($var_name, $this->var_blocks)) {
$this->logger->warning("Found duplicate definition for $var_name.");
}
}

/**
* Remove the global part of a variable definition. All variables must be set to their local equivalent.
*/
public function normalizeVariableName($var_name)
{
if (substr($var_name, 0, 8) == '$GLOBALS') {
// Replaces:
// $GLOBALS['test'] => $test
// $GLOBALS [ 'test' ] => $test
$reg = <<<'EOS'
/\$GLOBALS\s*\[\s*'([^']+)'\s*\]/
/^\$GLOBALS\s*\[\s*'([^']+)'\s*\]/
EOS;
$local_name = preg_replace($reg, '$\1', $var_name);
if (array_key_exists($local_name, $this->var_blocks)) {
$this->logger->warning("Found duplicate local definition for $var_name.");
}
} else {
// Replaces:
// $test => $GLOBAL['test']
$global_name = preg_replace('/\$(\w+)/', '$GLOBALS[\'\1\']', $var_name);
if (array_key_exists($global_name, $this->var_blocks)) {
$this->logger->warning("Found duplicate GLOBAL definition for $var_name.");
}
$var_name = preg_replace($reg, '$\1', $var_name);
}
return $var_name;
}

/**
Expand Down Expand Up @@ -237,6 +235,9 @@ public function parseNextBlock()
// Try to recreate the file as is.
$this->var_blocks[] = $var_value;
} else {
// Strip $GLOBALS from variables names for sorting.
// This will merge local and global version of the same var name.
$var_name = $this->normalizeVariableName($var_name);
// Warnings if key already exists.
$this->checkVarName($var_name);
$this->var_blocks[$var_name] = $var_value;
Expand Down
79 changes: 61 additions & 18 deletions tests/LangFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,19 @@ public function testGetSortedFile($expected_log, $expected, $src, $test_mode, $s

public function fileProvider()
{
$php_expected = <<<'EOF'
$php_org = <<<'EOF'
<?php
$GLOBALS['foo'] = array(
$GLOBALS['foo'] = 2;
// comment
$GLOBALS['test']['foo'] = 1;$GLOBALS['foo'] = array(
'test' => 'foo',
'bar' => 'baz',
);
// comment
$GLOBALS['test']['foo'] = 1;
$bar = test;
EOF;
$bar = test;
EOF;
$php_not_sorted = <<<'EOF'
<?php
$GLOBALS['foo'] = array(
Expand All @@ -115,36 +116,60 @@ public function fileProvider()
$bar = test;

EOF;
$php_org = <<<'EOF'
$php_sorted = <<<'EOF'
<?php
$GLOBALS['foo'] = 2;
// comment
$GLOBALS['test']['foo'] = 1;$GLOBALS['foo'] = array(
$bar = test;
$GLOBALS['foo'] = array(
'test' => 'foo',
'bar' => 'baz',
);
// comment
$GLOBALS['test']['foo'] = 1;

$bar = test;
EOF;


$log = <<<'EOF'
[warning] Found duplicate definition for $GLOBALS['foo'].
[warning] Found duplicate definition for $foo.

EOF;

$log_duplicates = <<<'EOF'
[warning] Found duplicate definition for $GLOBALS["test"].
[warning] Found duplicate local definition for $GLOBALS["test"].

EOF;

// Remove globals but keep latest version of a variable.
$php_duplicate_globals = <<<'EOF'
<?php
$GLOBALS['foo'] = 1;
$foo = 2;
$GLOBALS['foo'] = 4;
// Comment will be deleted
$GLOBALS['bar'] = 'bar';
// Comment will be kept
$bar = 'baz';

EOF;
$php_expected_from_duplicates = <<<'EOF'
<?php
// Comment will be kept
$bar = 'baz';
$GLOBALS['foo'] = 4;

EOF;
$log_duplicates_globals = <<<'EOF'
[warning] Found duplicate definition for $foo.
[warning] Found duplicate definition for $foo.
[warning] Found duplicate definition for $bar.

EOF;

return array(
array('', $php_org, $php_org, true, false),
array('', $php_org, $php_org, true, true),
array($log, $php_not_sorted, $php_org, false, false),
array($log, $php_expected, $php_org, false, true),
array($log, $php_sorted, $php_org, false, true),
// Test empty file
array('', '', '', false, false),
array('', "<?php \n;\n?>\n", '<?php ; ?>', false, true),
Expand All @@ -155,6 +180,7 @@ public function fileProvider()
false,
true
),
array($log_duplicates_globals, $php_expected_from_duplicates, $php_duplicate_globals, false, true),
);

}
Expand Down Expand Up @@ -184,6 +210,25 @@ public function fileFailureProvider()
);
}

public function normalizedVariablesProvider()
{
return array(
array('$foo', '$foo'),
array('$foo', '$GLOBALS[\'foo\']'),
array('$foo', '$GLOBALS [ \'foo\' ]'),
);
}

/**
* @dataProvider normalizedVariablesProvider
*/
public function testNormalizeVariableName($expected, $actual)
{
$logger = new TestLogger();
$lang_file = new LangFile($logger, '', false);
$this->assertEquals($expected, $lang_file->normalizeVariableName($actual));
}

public function testCheckVarName()
{
$local = '$foo';
Expand All @@ -198,8 +243,6 @@ public function testCheckVarName()
$lang->checkVarName($local);
$log = <<<'EOF'
[warning] Found duplicate definition for $foo.
[warning] Found duplicate local definition for $GLOBALS['foo'].
[warning] Found duplicate GLOBAL definition for $foo.

EOF;
$this->assertEquals($log, $logger->getLines());
Expand Down

0 comments on commit fed08c7

Please sign in to comment.