Completed
Push — 3 ( d27970...2b05d8 )
by Luke
21s
created

i18nSSLegacyAdapter::_loadTranslationData()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 14
nop 3
dl 0
loc 35
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once 'Zend/Locale.php';
3
require_once 'Zend/Translate/Adapter.php';
4
5
/**
6
 * @package framework
7
 * @subpackage i18n
8
 */
9
class i18nSSLegacyAdapter extends Zend_Translate_Adapter implements i18nTranslateAdapterInterface {
10
11
	/**
12
	 * Generates the adapter
13
	 *
14
	 * @param  array|Zend_Config $options Translation content
15
	 */
16
	public function __construct($options = array()) {
17
		$this->_options['keyDelimiter'] = ".";
18
		parent::__construct($options);
19
	}
20
21
	protected function _loadTranslationData($data, $locale, array $options = array()) {
22
		$options = array_merge($this->_options, $options);
23
24
		if ($options['clear']  ||  !isset($this->_translate[$locale])) {
25
			$this->_translate[$locale] = array();
26
		}
27
28
		if(is_array($data)) return array($locale => $data);
29
30
		$this->_filename = $data;
0 ignored issues
show
Bug introduced by
The property _filename does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
32
		// Ignore files with other extensions
33
		if(pathinfo($this->_filename, PATHINFO_EXTENSION) != 'php') return;
34
35
		if (!is_readable($this->_filename)) {
36
			require_once 'Zend/Translate/Exception.php';
37
			throw new Zend_Translate_Exception('Error opening translation file \'' . $this->_filename . '\'.');
38
		}
39
40
		global $lang;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
41
		if(!isset($lang['en_US'])) $lang['en_US'] = array();
42
		// TODO Diff locale array to avoid re-parsing all previous translations whenever a new module is included.
43
		require_once($this->_filename);
44
45
		$flattened = array();
46
		if($lang[$locale]) {
47
			$iterator = new i18nSSLegacyAdapter_Iterator(new RecursiveArrayIterator($lang[$locale]));
48
			foreach($iterator as $k => $v) {
49
				$flattenedKey = implode($options['keyDelimiter'], array_filter($iterator->getKeyStack()));
50
				$flattened[$flattenedKey] = (is_array($v)) ? $v[0] : $v;
51
			}
52
		}
53
54
		return array($locale => $flattened);
55
	}
56
57
	public function toString() {
58
		return "i18nSSLegacy";
59
	}
60
61
	public function getFilenameForLocale($locale) {
62
		return "{$locale}.php";
63
	}
64
65
}
66
67
/**
68
 * @package framework
69
 * @subpackage i18n
70
 */
71
class i18nSSLegacyAdapter_Iterator extends RecursiveIteratorIterator {
72
73
	protected $keyStack = array();
74
75
	public function callGetChildren() {
76
		$this->keyStack[] = parent::key();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (key() instead of callGetChildren()). Are you sure this is correct? If so, you might want to change this to $this->key().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
77
		return parent::callGetChildren();
78
	}
79
80
	public function endChildren() {
81
		array_pop($this->keyStack);
82
		parent::endChildren();
83
	}
84
85
	public function key() {
86
		return json_encode($this->getKeyStack());
87
	}
88
89
	public function getKeyStack() {
90
		return array_merge($this->keyStack, array(parent::key()));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (key() instead of getKeyStack()). Are you sure this is correct? If so, you might want to change this to $this->key().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
91
	}
92
}
93