diff --git a/CHANGELOG.md b/CHANGELOG.md index 022d368..475a262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,11 @@ Changelog ========= -(unreleased) +(unrelease) +----- +* LangFileCleaner: Merge global and local version of variables. + +1.0.0 ----- * Add UsersManager class. * Reworked Bean::updateBean diff --git a/src/LangFile.php b/src/LangFile.php index e28c6ed..1c67b32 100644 --- a/src/LangFile.php +++ b/src/LangFile.php @@ -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; } /** @@ -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; diff --git a/tests/LangFileTest.php b/tests/LangFileTest.php index 1678858..c6bdf42 100644 --- a/tests/LangFileTest.php +++ b/tests/LangFileTest.php @@ -92,18 +92,19 @@ public function testGetSortedFile($expected_log, $expected, $src, $test_mode, $s public function fileProvider() { - $php_expected = <<<'EOF' + $php_org = <<<'EOF' 'foo', 'bar' => 'baz', ); -// comment -$GLOBALS['test']['foo'] = 1; -$bar = test; -EOF; + +$bar = test; +EOF; $php_not_sorted = <<<'EOF' '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' +\n", '', false, true), @@ -155,6 +180,7 @@ public function fileProvider() false, true ), + array($log_duplicates_globals, $php_expected_from_duplicates, $php_duplicate_globals, false, true), ); } @@ -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'; @@ -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());