DocumentProperties   F
last analyzed

Complexity

Total Complexity 112

Size/Duplication

Total Lines 559
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 112
lcom 3
cbo 0
dl 0
loc 559
ccs 202
cts 202
cp 1
rs 2
c 0
b 0
f 0

30 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCreator() 0 4 1
A setCreator() 0 5 1
A getLastModifiedBy() 0 4 1
A setLastModifiedBy() 0 5 1
A getCreated() 0 4 1
A setCreated() 0 15 4
A getModified() 0 4 1
A setModified() 0 15 4
A getTitle() 0 4 1
A setTitle() 0 5 1
A getDescription() 0 4 1
A setDescription() 0 5 1
A getSubject() 0 4 1
A setSubject() 0 5 1
A getKeywords() 0 4 1
A setKeywords() 0 5 1
A getCategory() 0 4 1
A setCategory() 0 5 1
A getCompany() 0 4 1
A setCompany() 0 5 1
A getManager() 0 4 1
A setManager() 0 5 1
A getCustomProperties() 0 4 1
A isCustomPropertySet() 0 4 1
A getCustomPropertyValue() 0 7 2
A getCustomPropertyType() 0 7 2
A setCustomProperty() 0 17 6
D convertProperty() 0 56 36
D convertPropertyType() 0 54 35

How to fix   Complexity   

Complex Class

Complex classes like DocumentProperties 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 DocumentProperties, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of PHPProject - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPProject is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https: // github.com/PHPOffice/PHPWord/contributors.
12
 *
13
 * @link        https: // github.com/PHPOffice/PHPProject
14
 * @copyright   2009-2014 PHPProject contributors
15
 * @license     http: // www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpProject;
19
20
/**
21
 * PHPProject_DocumentProperties
22
 *
23
 * @category    PHPProject
24
 * @package        PHPProject
25
 * @copyright    Copyright (c) 2012 - 2012 PHPProject (https: // github.com/PHPOffice/PHPProject)
26
 */
27
class DocumentProperties
28
{
29
    /** constants */
30
    const PROPERTY_TYPE_BOOLEAN        = 'b';
31
    const PROPERTY_TYPE_INTEGER        = 'i';
32
    const PROPERTY_TYPE_FLOAT        = 'f';
33
    const PROPERTY_TYPE_DATE        = 'd';
34
    const PROPERTY_TYPE_STRING        = 's';
35
    const PROPERTY_TYPE_UNKNOWN        = 'u';
36
37
38
    /**
39
     * Creator
40
     *
41
     * @var    string
42
     */
43
    private $creator    = '';
44
45
    /**
46
     * LastModifiedBy
47
     *
48
     * @var    string
49
     */
50
    private $lastModifiedBy;
51
52
    /**
53
     * Created
54
     *
55
     * @var int
56
     */
57
    private $created;
58
59
    /**
60
     * Modified
61
     *
62
     * @var    int
63
     */
64
    private $modified;
65
66
    /**
67
     * Title
68
     *
69
     * @var    string
70
     */
71
    private $title    = '';
72
73
    /**
74
     * Description
75
     *
76
     * @var    string
77
     */
78
    private $description    = '';
79
80
    /**
81
     * Subject
82
     *
83
     * @var    string
84
     */
85
    private $subject        = '';
86
87
    /**
88
     * Keywords
89
     *
90
     * @var    string
91
     */
92
    private $keywords        = '';
93
94
    /**
95
     * Category
96
     *
97
     * @var    string
98
     */
99
    private $category        = '';
100
101
    /**
102
     * Manager
103
     *
104
     * @var    string
105
     */
106
    private $manager        = '';
107
108
    /**
109
     * Company
110
     *
111
     * @var    string
112
     */
113
    private $company        = '';
114
115
    /**
116
     * Custom Properties
117
     *
118
     * @var    string[]
119
     */
120
    private $customProperties    = array();
121
122
123
    /**
124
     * Create a new PHPProject_DocumentProperties
125
     */
126 37
    public function __construct()
127
    {
128
        // Initialise values
129 37
        $this->lastModifiedBy    = $this->creator;
130 37
    }
131
132
    /**
133
     * Get Creator
134
     *
135
     * @return    string
136
     */
137 1
    public function getCreator()
138
    {
139 1
        return $this->creator;
140
    }
141
142
    /**
143
     * Set Creator
144
     *
145
     * @param    string    $pValue
146
     * @return    PHPProject_DocumentProperties
147
     */
148 1
    public function setCreator($pValue = '')
149
    {
150 1
        $this->creator = $pValue;
151 1
        return $this;
152
    }
153
154
    /**
155
     * Get Last Modified By
156
     *
157
     * @return    string
158
     */
159 1
    public function getLastModifiedBy()
160
    {
161 1
        return $this->lastModifiedBy;
162
    }
163
164
    /**
165
     * Set Last Modified By
166
     *
167
     * @param    string    $pValue
168
     * @return    PHPProject_DocumentProperties
169
     */
170 1
    public function setLastModifiedBy($pValue = '')
171
    {
172 1
        $this->lastModifiedBy = $pValue;
173 1
        return $this;
174
    }
175
176
    /**
177
     * Get Created
178
     *
179
     * @return    int
180
     */
181 1
    public function getCreated()
182
    {
183 1
        return $this->created;
184
    }
185
186
    /**
187
     * Set Created
188
     *
189
     * @param    int    $pValue
190
     * @return    PHPProject_DocumentProperties
191
     */
192 1
    public function setCreated($pValue = null)
193
    {
194 1
        if ($pValue === null) {
195 1
            $pValue = time();
196 1
        } elseif (is_string($pValue)) {
197 1
            if (is_numeric($pValue)) {
198 1
                $pValue = intval($pValue);
199 1
            } else {
200 1
                $pValue = strtotime($pValue);
201
            }
202 1
        }
203
204 1
        $this->created = $pValue;
205 1
        return $this;
206
    }
207
208
    /**
209
     * Get Modified
210
     *
211
     * @return    int
212
     */
213 1
    public function getModified()
214
    {
215 1
        return $this->modified;
216
    }
217
218
    /**
219
     * Set Modified
220
     *
221
     * @param    int    $pValue
222
     * @return    PHPProject_DocumentProperties
223
     */
224 1
    public function setModified($pValue = null)
225
    {
226 1
        if ($pValue === null) {
227 1
            $pValue = time();
228 1
        } elseif (is_string($pValue)) {
229 1
            if (is_numeric($pValue)) {
230 1
                $pValue = intval($pValue);
231 1
            } else {
232 1
                $pValue = strtotime($pValue);
233
            }
234 1
        }
235
236 1
        $this->modified = $pValue;
237 1
        return $this;
238
    }
239
240
    /**
241
     * Get Title
242
     *
243
     * @return    string
244
     */
245 1
    public function getTitle()
246
    {
247 1
        return $this->title;
248
    }
249
250
    /**
251
     * Set Title
252
     *
253
     * @param    string    $pValue
254
     * @return    PHPProject_DocumentProperties
255
     */
256 1
    public function setTitle($pValue = '')
257
    {
258 1
        $this->title = $pValue;
259 1
        return $this;
260
    }
261
262
    /**
263
     * Get Description
264
     *
265
     * @return    string
266
     */
267 1
    public function getDescription()
268
    {
269 1
        return $this->description;
270
    }
271
272
    /**
273
     * Set Description
274
     *
275
     * @param    string    $pValue
276
     * @return    PHPProject_DocumentProperties
277
     */
278 2
    public function setDescription($pValue = '')
279
    {
280 2
        $this->description = $pValue;
281 2
        return $this;
282
    }
283
284
    /**
285
     * Get Subject
286
     *
287
     * @return    string
288
     */
289 1
    public function getSubject()
290
    {
291 1
        return $this->subject;
292
    }
293
294
    /**
295
     * Set Subject
296
     *
297
     * @param    string    $pValue
298
     * @return    PHPProject_DocumentProperties
299
     */
300 1
    public function setSubject($pValue = '')
301
    {
302 1
        $this->subject = $pValue;
303 1
        return $this;
304
    }
305
306
    /**
307
     * Get Keywords
308
     *
309
     * @return    string
310
     */
311 1
    public function getKeywords()
312
    {
313 1
        return $this->keywords;
314
    }
315
316
    /**
317
     * Set Keywords
318
     *
319
     * @param    string    $pValue
320
     * @return    PHPProject_DocumentProperties
321
     */
322 1
    public function setKeywords($pValue = '')
323
    {
324 1
        $this->keywords = $pValue;
325 1
        return $this;
326
    }
327
328
    /**
329
     * Get Category
330
     *
331
     * @return    string
332
     */
333 1
    public function getCategory()
334
    {
335 1
        return $this->category;
336
    }
337
338
    /**
339
     * Set Category
340
     *
341
     * @param    string    $pValue
342
     * @return    PHPProject_DocumentProperties
343
     */
344 1
    public function setCategory($pValue = '')
345
    {
346 1
        $this->category = $pValue;
347 1
        return $this;
348
    }
349
350
    /**
351
     * Get Company
352
     *
353
     * @return    string
354
     */
355 1
    public function getCompany()
356
    {
357 1
        return $this->company;
358
    }
359
360
    /**
361
     * Set Company
362
     *
363
     * @param    string    $pValue
364
     * @return    PHPProject_DocumentProperties
365
     */
366 1
    public function setCompany($pValue = '')
367
    {
368 1
        $this->company = $pValue;
369 1
        return $this;
370
    }
371
372
    /**
373
     * Get Manager
374
     *
375
     * @return    string
376
     */
377 1
    public function getManager()
378
    {
379 1
        return $this->manager;
380
    }
381
382
    /**
383
     * Set Manager
384
     *
385
     * @param    string    $pValue
386
     * @return    PHPProject_DocumentProperties
387
     */
388 1
    public function setManager($pValue = '')
389
    {
390 1
        $this->manager = $pValue;
391 1
        return $this;
392
    }
393
394
    /**
395
     * Get a List of Custom Property Names
396
     *
397
     * @return string[]
398
     */
399 1
    public function getCustomProperties()
400
    {
401 1
        return array_keys($this->customProperties);
402
    }
403
404
    /**
405
     * Check if a Custom Property is defined
406
     *
407
     * @param    string    $propertyName
408
     * @return    boolean
409
     */
410 1
    public function isCustomPropertySet($propertyName)
411
    {
412 1
        return isset($this->customProperties[$propertyName]);
413
    }
414
415
    /**
416
     * Get a Custom Property Value
417
     *
418
     * @param    string    $propertyName
419
     * @return    string
420
     */
421 1
    public function getCustomPropertyValue($propertyName)
422
    {
423 1
        if (isset($this->customProperties[$propertyName])) {
424 1
            return $this->customProperties[$propertyName]['value'];
425
        }
426
427 1
    }
428
429
    /**
430
     * Get a Custom Property Type
431
     *
432
     * @param    string    $propertyName
433
     * @return    string
434
     */
435 1
    public function getCustomPropertyType($propertyName)
436
    {
437 1
        if (isset($this->customProperties[$propertyName])) {
438 1
            return $this->customProperties[$propertyName]['type'];
439
        }
440
441 1
    }
442
443
    /**
444
     * Set a Custom Property
445
     *
446
     * @param    string    $propertyName
447
     * @param    mixed    $propertyValue
448
     * @param    string    $propertyType
449
     *                        'i': Integer
450
     *                        'f': Floating Point
451
     *                        's': String
452
     *                        'd': Date/Time
453
     *                        'b': Boolean
454
     * @return    PHPProject_DocumentProperties
455
     */
456 1
    public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null)
457
    {
458 1
        if (($propertyType === null) || (!in_array($propertyType, array(self::PROPERTY_TYPE_INTEGER, self::PROPERTY_TYPE_FLOAT, self::PROPERTY_TYPE_STRING, self::PROPERTY_TYPE_DATE, self::PROPERTY_TYPE_BOOLEAN)))) {
459 1
            if (is_float($propertyValue)) {
460 1
                $propertyType = self::PROPERTY_TYPE_FLOAT;
461 1
            } elseif (is_int($propertyValue)) {
462 1
                $propertyType = self::PROPERTY_TYPE_INTEGER;
463 1
            } elseif (is_bool($propertyValue)) {
464 1
                $propertyType = self::PROPERTY_TYPE_BOOLEAN;
465 1
            } else {
466 1
                $propertyType = self::PROPERTY_TYPE_STRING;
467
            }
468 1
        }
469
470 1
        $this->customProperties[$propertyName] = array('value' => $propertyValue, 'type' => $propertyType);
471 1
        return $this;
472
    }
473
474 1
    public static function convertProperty($propertyValue, $propertyType)
475
    {
476
        switch ($propertyType) {
477 1
            case 'empty': // Empty
478 1
                $propertyValue = '';
479 1
                break;
480 1
            case 'null': // Null
481 1
                $propertyValue = null;
482 1
                break;
483 1
            case 'i1': // 1-Byte Signed Integer
484 1
            case 'i2': // 2-Byte Signed Integer
485 1
            case 'i4': // 4-Byte Signed Integer
486 1
            case 'i8': // 8-Byte Signed Integer
487 1
            case 'int': // Integer
488 1
                $propertyValue = (int) $propertyValue;
489 1
                break;
490 1
            case 'ui1': // 1-Byte Unsigned Integer
491 1
            case 'ui2': // 2-Byte Unsigned Integer
492 1
            case 'ui4': // 4-Byte Unsigned Integer
493 1
            case 'ui8': // 8-Byte Unsigned Integer
494 1
            case 'uint': // Unsigned Integer
495 1
                $propertyValue = abs((int) $propertyValue);
496 1
                break;
497 1
            case 'r4': // 4-Byte Real Number
498 1
            case 'r8': // 8-Byte Real Number
499 1
            case 'decimal': // Decimal
500 1
                $propertyValue = (float) $propertyValue;
501 1
                break;
502 1
            case 'lpstr': // LPSTR
503 1
            case 'lpwstr': // LPWSTR
504 1
            case 'bstr': // Basic String
505 1
                break;
506 1
            case 'date': // Date and Time
507 1
            case 'filetime': // File Time
508 1
                $propertyValue = strtotime($propertyValue);
509 1
                break;
510 1
            case 'bool': // Boolean
511 1
                $propertyValue = ($propertyValue == 'true') ? true: false;
512 1
                break;
513 1
            case 'cy': // Currency
514 1
            case 'error': // Error Status Code
515 1
            case 'vector': // Vector
516 1
            case 'array': // Array
517 1
            case 'blob': // Binary Blob
518 1
            case 'oblob': // Binary Blob Object
519 1
            case 'stream': // Binary Stream
520 1
            case 'ostream': // Binary Stream Object
521 1
            case 'storage': // Binary Storage
522 1
            case 'ostorage': // Binary Storage Object
523 1
            case 'vstream': // Binary Versioned Stream
524 1
            case 'clsid': // Class ID
525 1
            case 'cf': // Clipboard Data
526 1
                break;
527
        }
528 1
        return $propertyValue;
529
    }
530
531 1
    public static function convertPropertyType($propertyType)
532
    {
533
        switch ($propertyType) {
534 1
            case 'i1': // 1-Byte Signed Integer
535 1
            case 'i2': // 2-Byte Signed Integer
536 1
            case 'i4': // 4-Byte Signed Integer
537 1
            case 'i8': // 8-Byte Signed Integer
538 1
            case 'int': // Integer
539 1
            case 'ui1': // 1-Byte Unsigned Integer
540 1
            case 'ui2': // 2-Byte Unsigned Integer
541 1
            case 'ui4': // 4-Byte Unsigned Integer
542 1
            case 'ui8': // 8-Byte Unsigned Integer
543 1
            case 'uint': // Unsigned Integer
544 1
                $propertyType = self::PROPERTY_TYPE_INTEGER;
545 1
                break;
546 1
            case 'r4': // 4-Byte Real Number
547 1
            case 'r8': // 8-Byte Real Number
548 1
            case 'decimal': // Decimal
549 1
                $propertyType = self::PROPERTY_TYPE_FLOAT;
550 1
                break;
551 1
            case 'empty': // Empty
552 1
            case 'null': // Null
553 1
            case 'lpstr': // LPSTR
554 1
            case 'lpwstr': // LPWSTR
555 1
            case 'bstr': // Basic String
556 1
                $propertyType = self::PROPERTY_TYPE_STRING;
557 1
                break;
558 1
            case 'date': // Date and Time
559 1
            case 'filetime': // File Time
560 1
                $propertyType = self::PROPERTY_TYPE_DATE;
561 1
                break;
562 1
            case 'bool': // Boolean
563 1
                $propertyType = self::PROPERTY_TYPE_BOOLEAN;
564 1
                break;
565 1
            case 'cy': // Currency
566 1
            case 'error': // Error Status Code
567 1
            case 'vector': // Vector
568 1
            case 'array': // Array
569 1
            case 'blob': // Binary Blob
570 1
            case 'oblob': // Binary Blob Object
571 1
            case 'stream': // Binary Stream
572 1
            case 'ostream': // Binary Stream Object
573 1
            case 'storage': // Binary Storage
574 1
            case 'ostorage': // Binary Storage Object
575 1
            case 'vstream': // Binary Versioned Stream
576 1
            case 'clsid': // Class ID
577 1
            case 'cf': // Clipboard Data
578 1
                $propertyType = self::PROPERTY_TYPE_UNKNOWN;
579 1
                break;
580 1
            default:
581 1
                $propertyType = self::PROPERTY_TYPE_UNKNOWN;
582 1
        }
583 1
        return $propertyType;
584
    }
585
}
586