Passed
Pull Request — development (#3540)
by Emanuele
06:47
created

Loader::fix_calendar_text()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 63
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 56
nc 1
nop 0
dl 0
loc 63
rs 8.9599
c 1
b 0
f 0

How to fix   Long Method   

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
2
3
/**
4
 * This class takes care of loading language files
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\Languages;
15
16
use ElkArte\Debug;
17
18
/**
19
 * This class takes care of loading language files
20
 */
21
class Loader
22
{
23
	/** @var string */
24
	protected $path = '';
25
26
	/** @var string */
27
	protected $language = 'English';
28
29
	/** @var bool */
30
	protected $load_fallback;
31
32
	/** @var string[] Holds the name of the files already loaded to load them only once */
33
	protected $loaded = [];
34
35
	public function __construct($lang = null, $path = null, bool $load_fallback = true)
36
	{
37
		if ($lang !== null)
38
		{
39
			$this->language = ucfirst($lang);
40
		}
41
		if ($path === null)
42
		{
43
			$this->path = SOURCEDIR . '/ElkArte/Languages/';
44
		}
45
		else
46
		{
47
			$this->path = $path;
48
		}
49
		$this->load_fallback = $load_fallback;
50
	}
51
52
	public function load($file_name, $fatal = true, $fix_calendar_arrays = false)
53
	{
54
		global $db_show_debug;
55
56
		$file_names = explode('+', $file_name);
57
58
		// For each file open it up and write it out!
59
		foreach ($file_names as $file)
60
		{
61
			$file = ucfirst($file);
62
			if (isset($this->loaded[$file]))
63
			{
64
				continue;
65
			}
66
67
			$found = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $found is dead and can be removed.
Loading history...
68
			$found_fallback = false;
69
			if ($this->load_fallback)
70
			{
71
				$found_fallback = $this->loadFile($file, 'English');
72
			}
73
			$found = $this->loadFile($file, $this->language);
74
75
			$this->loaded[$file] = true;
76
77
			// Keep track of what we're up to, soldier.
78
			if ($found && $db_show_debug === true)
79
			{
80
				Debug::instance()->add(
81
					'language_files',
82
					$file . '.' . $this->language .
83
					' (' . str_replace(BOARDDIR, '', $this->path) . ')'
84
				);
85
			}
86
87
			// That couldn't be found!  Log the error, but *try* to continue normally.
88
			if (!$found && $fatal)
89
			{
90
				Errors::instance()->log_error(
0 ignored issues
show
Bug introduced by
The type ElkArte\Languages\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
					sprintf(
92
						$txt['theme_language_error'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $txt seems to be never defined.
Loading history...
93
						$file . '.' . $this->language,
94
						'template'
95
					)
96
				);
97
				// If we do have a fallback it may not be necessary to break out.
98
				if ($found_fallback === false)
99
				{
100
					break;
101
				}
102
			}
103
		}
104
105
		if ($fix_calendar_arrays)
106
		{
107
			$this->fix_calendar_text();
108
		}
109
	}
110
111
	protected function loadFile($name, $lang)
112
	{
113
		/*
114
		 * I know this looks weird but this is used to include $txt files.
115
		 * If the parent doesn't declare them global, the scope will be
116
		 * local to this function. IOW, don't remove this line!
117
		 */
118
		global $txt;
119
120
		$filepath = $this->path . $name . '/' . $lang . '.php';
121
		if (file_exists($filepath))
122
		{
123
			require($filepath);
124
			return true;
125
		}
126
		return false;
127
	}
128
129
	/**
130
	 * Loads / Sets arrays for use in date display
131
	 * This is here and not in a language file for two reasons:
132
	 *  1. the structure is required by the code, so better be sure
133
	 *     to have it the way we are supposed to have it
134
	 *  2. Transifex (that we use for translating the strings) doesn't
135
	 *     support array of arrays, so if we move this to a language file
136
	 *     we'd need to move away from Tx.
137
	 */
138
	protected function fix_calendar_text()
139
	{
140
		global $txt;
141
142
		$txt['days'] = array(
143
			$txt['sunday'],
144
			$txt['monday'],
145
			$txt['tuesday'],
146
			$txt['wednesday'],
147
			$txt['thursday'],
148
			$txt['friday'],
149
			$txt['saturday'],
150
		);
151
		$txt['days_short'] = array(
152
			$txt['sunday_short'],
153
			$txt['monday_short'],
154
			$txt['tuesday_short'],
155
			$txt['wednesday_short'],
156
			$txt['thursday_short'],
157
			$txt['friday_short'],
158
			$txt['saturday_short'],
159
		);
160
		$txt['months'] = array(
161
			1 => $txt['january'],
162
			$txt['february'],
163
			$txt['march'],
164
			$txt['april'],
165
			$txt['may'],
166
			$txt['june'],
167
			$txt['july'],
168
			$txt['august'],
169
			$txt['september'],
170
			$txt['october'],
171
			$txt['november'],
172
			$txt['december'],
173
		);
174
		$txt['months_titles'] = array(
175
			1 => $txt['january_titles'],
176
			$txt['february_titles'],
177
			$txt['march_titles'],
178
			$txt['april_titles'],
179
			$txt['may_titles'],
180
			$txt['june_titles'],
181
			$txt['july_titles'],
182
			$txt['august_titles'],
183
			$txt['september_titles'],
184
			$txt['october_titles'],
185
			$txt['november_titles'],
186
			$txt['december_titles'],
187
		);
188
		$txt['months_short'] = array(
189
			1 => $txt['january_short'],
190
			$txt['february_short'],
191
			$txt['march_short'],
192
			$txt['april_short'],
193
			$txt['may_short'],
194
			$txt['june_short'],
195
			$txt['july_short'],
196
			$txt['august_short'],
197
			$txt['september_short'],
198
			$txt['october_short'],
199
			$txt['november_short'],
200
			$txt['december_short'],
201
		);
202
	}
203
}