Total Complexity | 42 |
Total Lines | 444 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Settings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Settings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Settings |
||
6 | { |
||
7 | /** |
||
8 | * Zip libraries |
||
9 | * |
||
10 | * @const string |
||
11 | */ |
||
12 | public const ZIPARCHIVE = 'ZipArchive'; |
||
13 | public const PCLZIP = 'PclZip'; |
||
14 | public const OLD_LIB = 'PhpDocxTemplate\\Zip\\ZipArchive'; |
||
15 | |||
16 | /** |
||
17 | * PDF rendering libraries |
||
18 | * |
||
19 | * @const string |
||
20 | */ |
||
21 | public const PDF_RENDERER_DOMPDF = 'DomPDF'; |
||
22 | public const PDF_RENDERER_TCPDF = 'TCPDF'; |
||
23 | public const PDF_RENDERER_MPDF = 'MPDF'; |
||
24 | |||
25 | /** |
||
26 | * Measurement units multiplication factor |
||
27 | * |
||
28 | * Applied to: |
||
29 | * - Section: margins, header/footer height, gutter, column spacing |
||
30 | * - Tab: position |
||
31 | * - Indentation: left, right, firstLine, hanging |
||
32 | * - Spacing: before, after |
||
33 | * |
||
34 | * @const string |
||
35 | */ |
||
36 | public const UNIT_TWIP = 'twip'; // = 1/20 point |
||
37 | public const UNIT_CM = 'cm'; |
||
38 | public const UNIT_MM = 'mm'; |
||
39 | public const UNIT_INCH = 'inch'; |
||
40 | public const UNIT_POINT = 'point'; // = 1/72 inch |
||
41 | public const UNIT_PICA = 'pica'; // = 1/6 inch = 12 points |
||
42 | |||
43 | /** |
||
44 | * Default font settings |
||
45 | * |
||
46 | * OOXML defined font size values in halfpoints, i.e. twice of what PhpWord |
||
47 | * use, and the conversion will be conducted during XML writing. |
||
48 | */ |
||
49 | public const DEFAULT_FONT_NAME = 'Arial'; |
||
50 | public const DEFAULT_FONT_SIZE = 10; |
||
51 | public const DEFAULT_FONT_COLOR = '000000'; |
||
52 | public const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs |
||
53 | public const DEFAULT_PAPER = 'A4'; |
||
54 | |||
55 | /** |
||
56 | * Compatibility option for XMLWriter |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | private static $xmlWriterCompatibility = true; |
||
61 | |||
62 | /** |
||
63 | * Name of the class used for Zip file management |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private static $zipClass = self::PCLZIP; |
||
68 | |||
69 | /** |
||
70 | * Name of the external Library used for rendering PDF files |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private static $pdfRendererName = null; |
||
75 | |||
76 | /** |
||
77 | * Directory Path to the external Library used for rendering PDF files |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private static $pdfRendererPath = null; |
||
82 | |||
83 | /** |
||
84 | * Measurement unit |
||
85 | * |
||
86 | * @var int|float |
||
87 | */ |
||
88 | private static $measurementUnit = self::UNIT_TWIP; |
||
89 | |||
90 | /** |
||
91 | * Default font name |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | private static $defaultFontName = self::DEFAULT_FONT_NAME; |
||
96 | |||
97 | /** |
||
98 | * Default font size |
||
99 | * @var int |
||
100 | */ |
||
101 | private static $defaultFontSize = self::DEFAULT_FONT_SIZE; |
||
102 | |||
103 | /** |
||
104 | * Default paper |
||
105 | * @var string |
||
106 | */ |
||
107 | private static $defaultPaper = self::DEFAULT_PAPER; |
||
108 | |||
109 | /** |
||
110 | * The user defined temporary directory. |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | private static $tempDir = ''; |
||
115 | |||
116 | /** |
||
117 | * Enables built-in output escaping mechanism. |
||
118 | * Default value is `false` for backward compatibility with versions below 0.13.0. |
||
119 | * |
||
120 | * @var bool |
||
121 | */ |
||
122 | private static $outputEscapingEnabled = false; |
||
123 | |||
124 | /** |
||
125 | * Return the compatibility option used by the XMLWriter |
||
126 | * |
||
127 | * @return bool Compatibility |
||
128 | */ |
||
129 | public static function hasCompatibility(): bool |
||
130 | { |
||
131 | return self::$xmlWriterCompatibility; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Set the compatibility option used by the XMLWriter |
||
136 | * |
||
137 | * This sets the setIndent and setIndentString for better compatibility |
||
138 | * |
||
139 | * @param bool $compatibility |
||
140 | * @return bool |
||
141 | */ |
||
142 | public static function setCompatibility(bool $compatibility): bool |
||
143 | { |
||
144 | $compatibility = (bool) $compatibility; |
||
145 | self::$xmlWriterCompatibility = $compatibility; |
||
146 | |||
147 | return true; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Get zip handler class |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public static function getZipClass(): string |
||
156 | { |
||
157 | return self::$zipClass; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Set zip handler class |
||
162 | * |
||
163 | * @param string $zipClass |
||
164 | * @return bool |
||
165 | */ |
||
166 | public static function setZipClass(string $zipClass): bool |
||
167 | { |
||
168 | if (in_array($zipClass, array(self::PCLZIP, self::ZIPARCHIVE, self::OLD_LIB))) { |
||
169 | self::$zipClass = $zipClass; |
||
170 | |||
171 | return true; |
||
172 | } |
||
173 | |||
174 | return false; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Set details of the external library for rendering PDF files |
||
179 | * |
||
180 | * @param string $libraryName |
||
181 | * @param string $libraryBaseDir |
||
182 | * @return bool Success or failure |
||
183 | */ |
||
184 | public static function setPdfRenderer(string $libraryName, string $libraryBaseDir): bool |
||
185 | { |
||
186 | if (!self::setPdfRendererName($libraryName)) { |
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | return self::setPdfRendererPath($libraryBaseDir); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Return the PDF Rendering Library. |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public static function getPdfRendererName(): string |
||
199 | { |
||
200 | return self::$pdfRendererName; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Identify the external library to use for rendering PDF files |
||
205 | * |
||
206 | * @param string $libraryName |
||
207 | * @return bool |
||
208 | */ |
||
209 | public static function setPdfRendererName(string $libraryName): bool |
||
210 | { |
||
211 | $pdfRenderers = array(self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF); |
||
212 | if (!in_array($libraryName, $pdfRenderers)) { |
||
213 | return false; |
||
214 | } |
||
215 | self::$pdfRendererName = $libraryName; |
||
216 | |||
217 | return true; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Return the directory path to the PDF Rendering Library. |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public static function getPdfRendererPath(): string |
||
226 | { |
||
227 | return self::$pdfRendererPath; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Location of external library to use for rendering PDF files |
||
232 | * |
||
233 | * @param string $libraryBaseDir Directory path to the library's base folder |
||
234 | * @return bool Success or failure |
||
235 | */ |
||
236 | public static function setPdfRendererPath(string $libraryBaseDir): bool |
||
237 | { |
||
238 | if (false === file_exists($libraryBaseDir) || false === is_readable($libraryBaseDir)) { |
||
239 | return false; |
||
240 | } |
||
241 | self::$pdfRendererPath = $libraryBaseDir; |
||
242 | |||
243 | return true; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Get measurement unit |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public static function getMeasurementUnit(): string |
||
252 | { |
||
253 | return self::$measurementUnit; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Set measurement unit |
||
258 | * |
||
259 | * @param string $value |
||
260 | * @return bool |
||
261 | */ |
||
262 | public static function setMeasurementUnit(string $value): bool |
||
263 | { |
||
264 | $units = array(self::UNIT_TWIP, self::UNIT_CM, self::UNIT_MM, self::UNIT_INCH, |
||
265 | self::UNIT_POINT, self::UNIT_PICA, ); |
||
266 | if (!in_array($value, $units)) { |
||
267 | return false; |
||
268 | } |
||
269 | self::$measurementUnit = $value; |
||
|
|||
270 | |||
271 | return true; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Sets the user defined path to temporary directory. |
||
276 | * |
||
277 | * @param string $tempDir The user defined path to temporary directory |
||
278 | */ |
||
279 | public static function setTempDir(string $tempDir): void |
||
280 | { |
||
281 | self::$tempDir = $tempDir; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Returns path to temporary directory. |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | public static function getTempDir(): string |
||
290 | { |
||
291 | if (!empty(self::$tempDir)) { |
||
292 | $tempDir = self::$tempDir; |
||
293 | } else { |
||
294 | $tempDir = sys_get_temp_dir(); |
||
295 | } |
||
296 | |||
297 | return $tempDir; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @return bool |
||
302 | */ |
||
303 | public static function isOutputEscapingEnabled(): bool |
||
304 | { |
||
305 | return self::$outputEscapingEnabled; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param bool $outputEscapingEnabled |
||
310 | */ |
||
311 | public static function setOutputEscapingEnabled(bool $outputEscapingEnabled): void |
||
312 | { |
||
313 | self::$outputEscapingEnabled = $outputEscapingEnabled; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Get default font name |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public static function getDefaultFontName(): string |
||
322 | { |
||
323 | return self::$defaultFontName; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Set default font name |
||
328 | * |
||
329 | * @param string $value |
||
330 | * @return bool |
||
331 | */ |
||
332 | public static function setDefaultFontName(string $value): bool |
||
333 | { |
||
334 | if (is_string($value) && trim($value) !== '') { |
||
335 | self::$defaultFontName = $value; |
||
336 | |||
337 | return true; |
||
338 | } |
||
339 | |||
340 | return false; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get default font size |
||
345 | * |
||
346 | * @return int |
||
347 | */ |
||
348 | public static function getDefaultFontSize(): int |
||
349 | { |
||
350 | return self::$defaultFontSize; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Set default font size |
||
355 | * |
||
356 | * @param int $value |
||
357 | * @return bool |
||
358 | */ |
||
359 | public static function setDefaultFontSize(int $value): bool |
||
360 | { |
||
361 | $value = (int) $value; |
||
362 | if ($value > 0) { |
||
363 | self::$defaultFontSize = $value; |
||
364 | |||
365 | return true; |
||
366 | } |
||
367 | |||
368 | return false; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Load setting from phpword.yml or phpword.yml.dist |
||
373 | * |
||
374 | * @param string $filename |
||
375 | * @return array |
||
376 | */ |
||
377 | public static function loadConfig(?string $filename = null): array |
||
378 | { |
||
379 | // Get config file |
||
380 | $configFile = null; |
||
381 | $configPath = __DIR__ . '/../../'; |
||
382 | if ($filename !== null) { |
||
383 | $files = array($filename); |
||
384 | } else { |
||
385 | $files = array("{$configPath}phpword.ini", "{$configPath}phpword.ini.dist"); |
||
386 | } |
||
387 | foreach ($files as $file) { |
||
388 | if (file_exists($file)) { |
||
389 | $configFile = realpath($file); |
||
390 | break; |
||
391 | } |
||
392 | } |
||
393 | |||
394 | // Parse config file |
||
395 | $config = array(); |
||
396 | if ($configFile !== null) { |
||
397 | $config = @parse_ini_file($configFile); |
||
398 | if ($config === false) { |
||
399 | return $config; |
||
400 | } |
||
401 | } |
||
402 | |||
403 | // Set config value |
||
404 | foreach ($config as $key => $value) { |
||
405 | $method = "set{$key}"; |
||
406 | if (method_exists(__CLASS__, $method)) { |
||
407 | self::$method($value); |
||
408 | } |
||
409 | } |
||
410 | |||
411 | return $config; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Get default paper |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public static function getDefaultPaper(): string |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Set default paper |
||
426 | * |
||
427 | * @param string $value |
||
428 | * @return bool |
||
429 | */ |
||
430 | public static function setDefaultPaper(string $value): bool |
||
431 | { |
||
432 | if (is_string($value) && trim($value) !== '') { |
||
433 | self::$defaultPaper = $value; |
||
434 | |||
435 | return true; |
||
436 | } |
||
437 | |||
438 | return false; |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Return the compatibility option used by the XMLWriter |
||
443 | * |
||
444 | * @codeCoverageIgnore |
||
445 | */ |
||
446 | public static function getCompatibility(): bool |
||
449 | } |
||
450 | } |
||
451 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..