Failed Conditions
Push — master ( 42761f...d02352 )
by Adrien
16:26 queued 08:32
created

Properties::convertProperty()   D

Complexity

Conditions 35
Paths 35

Size

Total Lines 49
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 113.4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 35
eloc 45
c 1
b 0
f 0
nc 35
nop 2
dl 0
loc 49
ccs 27
cts 45
cp 0.6
crap 113.4
rs 4.1666

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