Passed
Push — master ( d2520f...3f8ec2 )
by Michael
02:50
created

Calendar_Decorator   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 526
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 51
dl 0
loc 526
rs 8.3206
c 0
b 0
f 0

42 Methods

Rating   Name   Duplication   Size   Complexity  
A adjust() 0 3 1
A thisDay() 0 3 1
A isLast() 0 4 2
A nextWeek() 0 9 2
A fetchAll() 0 3 1
A setSelected() 0 3 1
A __construct() 0 3 1
A isSelected() 0 3 1
A getTimestamp() 0 3 1
A isEmpty() 0 4 2
A build() 0 3 1
A setFirst() 0 4 2
A getValidator() 0 5 1
A fetch() 0 3 1
A setLast() 0 4 2
A size() 0 3 1
A thisYear() 0 3 1
A prevMonth() 0 3 1
A setTimestamp() 0 3 1
A thisMinute() 0 3 1
A toArray() 0 3 1
A thisHour() 0 3 1
A returnValue() 0 3 1
A nextMonth() 0 3 1
A nextMinute() 0 3 1
A prevSecond() 0 3 1
A nextHour() 0 3 1
A isFirst() 0 4 2
A getEngine() 0 5 1
A prevYear() 0 3 1
A thisSecond() 0 3 1
A nextYear() 0 3 1
A prevWeek() 0 9 2
A nextDay() 0 3 1
A thisMonth() 0 3 1
A prevMinute() 0 3 1
A prevHour() 0 3 1
A prevDay() 0 3 1
A setEmpty() 0 4 2
A nextSecond() 0 3 1
A isValid() 0 3 1
A thisWeek() 0 9 2

How to fix   Complexity   

Complex Class

Complex classes like Calendar_Decorator 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 Calendar_Decorator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4: */
4
5
/**
6
 * Contains the Calendar_Decorator class.
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * LICENSE: Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. The name of the author may not be used to endorse or promote products
18
 *    derived from this software without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
21
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
24
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 *
31
 * @category  Date and Time
32
 *
33
 * @author    Harry Fuecks <[email protected]>
34
 * @copyright 2003-2007 Harry Fuecks
35
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
36
 *
37
 * @link      http://pear.php.net/package/Calendar
38
 */
39
40
/**
41
 * Decorates any calendar class.
42
 * Create a subclass of this class for your own "decoration".
43
 * Used for "selections"
44
 * <code>
45
 * class DayDecorator extends Calendar_Decorator
46
 * {
47
 *     function thisDay($format = 'int')
48
 *     {
49
 * .*         $day = parent::thisDay('timestamp');
50
 * .*         return date('D', $day);
51
 *     }
52
 * }
53
 * $Day = new Calendar_Day(2003, 10, 25);
54
 * $DayDecorator = new DayDecorator($Day);
55
 * echo $DayDecorator->thisDay(); // Outputs "Sat"
56
 * </code>.
57
 *
58
 * @category  Date and Time
59
 *
60
 * @author    Harry Fuecks <[email protected]>
61
 * @copyright 2003-2007 Harry Fuecks
62
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
63
 *
64
 * @link      http://pear.php.net/package/Calendar
65
 * @abstract
66
 */
67
class Calendar_Decorator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
68
{
69
    /**
70
     * Subclass of Calendar being decorated.
71
     *
72
     * @var object
73
     */
74
    public $calendar;
75
76
    /**
77
     * Constructs the Calendar_Decorator.
78
     *
79
     * @param object &$calendar subclass to Calendar to decorate
80
     */
81
    public function __construct(&$calendar)
82
    {
83
        $this->calendar =& $calendar;
84
    }
85
86
    /**
87
     * Defines the calendar by a Unix timestamp, replacing values
88
     * passed to the constructor.
89
     *
90
     * @param int $ts Unix timestamp
91
     */
92
    public function setTimestamp($ts)
93
    {
94
        $this->calendar->setTimestamp($ts);
95
    }
96
97
    /**
98
     * Returns a timestamp from the current date / time values. Format of
99
     * timestamp depends on Calendar_Engine implementation being used.
100
     *
101
     * @return int $ts timestamp
102
     */
103
    public function getTimestamp()
104
    {
105
        return $this->calendar->getTimestamp();
106
    }
107
108
    /**
109
     * Defines calendar object as selected (e.g. for today).
110
     *
111
     * @param bool $state whether Calendar subclass must be selected
112
     */
113
    public function setSelected($state = true)
0 ignored issues
show
Unused Code introduced by
The parameter $state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function setSelected(/** @scrutinizer ignore-unused */ $state = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
        $this->calendar->setSelected($state = true);
116
    }
117
118
    /**
119
     * True if the calendar subclass object is selected (e.g. today).
120
     *
121
     * @return bool
122
     */
123
    public function isSelected()
124
    {
125
        return $this->calendar->isSelected();
126
    }
127
128
    /**
129
     * Adjusts the date (helper method).
130
     */
131
    public function adjust()
132
    {
133
        $this->calendar->adjust();
134
    }
135
136
    /**
137
     * Returns the date as an associative array (helper method).
138
     *
139
     * @param mixed $stamp timestamp (leave empty for current timestamp)
140
     *
141
     * @return array
142
     */
143
    public function toArray($stamp = null)
144
    {
145
        return $this->calendar->toArray($stamp);
146
    }
147
148
    /**
149
     * Returns the value as an associative array (helper method).
150
     *
151
     * @param string $returnType type of date object that return value represents
152
     * @param string $format     ['int'|'timestamp'|'object'|'array']
153
     * @param mixed  $stamp      timestamp (depending on Calendar engine being used)
154
     * @param int    $default    default value (i.e. give me the answer quick)
155
     *
156
     * @return mixed
157
     */
158
    public function returnValue($returnType, $format, $stamp, $default)
159
    {
160
        return $this->calendar->returnValue($returnType, $format, $stamp, $default);
161
    }
162
163
    /**
164
     * Defines Day object as first in a week
165
     * Only used by Calendar_Month_Weekdays::build().
166
     *
167
     * @param bool $state whether it's first or not
168
     */
169
    public function setFirst($state = true)
170
    {
171
        if (method_exists($this->calendar, 'setFirst')) {
172
            $this->calendar->setFirst($state);
173
        }
174
    }
175
176
    /**
177
     * Defines Day object as last in a week
178
     * Used only following Calendar_Month_Weekdays::build().
179
     *
180
     * @param bool $state whether it's last or not
181
     */
182
    public function setLast($state = true)
183
    {
184
        if (method_exists($this->calendar, 'setLast')) {
185
            $this->calendar->setLast($state);
186
        }
187
    }
188
189
    /**
190
     * Returns true if Day object is first in a Week
191
     * Only relevant when Day is created by Calendar_Month_Weekdays::build().
192
     *
193
     * @return bool
194
     */
195
    public function isFirst()
196
    {
197
        if (method_exists($this->calendar, 'isFirst')) {
198
            return $this->calendar->isFirst();
199
        }
200
    }
201
202
    /**
203
     * Returns true if Day object is last in a Week
204
     * Only relevant when Day is created by Calendar_Month_Weekdays::build().
205
     *
206
     * @return bool
207
     */
208
    public function isLast()
209
    {
210
        if (method_exists($this->calendar, 'isLast')) {
211
            return $this->calendar->isLast();
212
        }
213
    }
214
215
    /**
216
     * Defines Day object as empty
217
     * Only used by Calendar_Month_Weekdays::build().
218
     *
219
     * @param bool $state whether it's empty or not
220
     */
221
    public function setEmpty($state = true)
222
    {
223
        if (method_exists($this->calendar, 'setEmpty')) {
224
            $this->calendar->setEmpty($state);
225
        }
226
    }
227
228
    /**
229
     * Check if the current object is empty.
230
     *
231
     * @return bool
232
     */
233
    public function isEmpty()
234
    {
235
        if (method_exists($this->calendar, 'isEmpty')) {
236
            return $this->calendar->isEmpty();
237
        }
238
    }
239
240
    /**
241
     * Build the children.
242
     *
243
     * @param array $sDates array containing Calendar objects to select (optional)
244
     *
245
     * @return bool
246
     * @abstract
247
     */
248
    public function build($sDates = [])
249
    {
250
        $this->calendar->build($sDates);
251
    }
252
253
    /**
254
     * Iterator method for fetching child Calendar subclass objects
255
     * (e.g. a minute from an hour object). On reaching the end of
256
     * the collection, returns false and resets the collection for
257
     * further iteratations.
258
     *
259
     * @return mixed either an object subclass of Calendar or false
260
     */
261
    public function fetch()
262
    {
263
        return $this->calendar->fetch();
264
    }
265
266
    /**
267
     * Fetches all child from the current collection of children.
268
     *
269
     * @return array
270
     */
271
    public function fetchAll()
272
    {
273
        return $this->calendar->fetchAll();
274
    }
275
276
    /**
277
     * Get the number Calendar subclass objects stored in the internal collection.
278
     *
279
     * @return int
280
     */
281
    public function size()
282
    {
283
        return $this->calendar->size();
284
    }
285
286
    /**
287
     * Determine whether this date is valid, with the bounds determined by
288
     * the Calendar_Engine. The call is passed on to Calendar_Validator::isValid.
289
     *
290
     * @return bool
291
     */
292
    public function isValid()
293
    {
294
        return $this->calendar->isValid();
295
    }
296
297
    /**
298
     * Returns an instance of Calendar_Validator.
299
     *
300
     * @return Calendar_Validator
301
     */
302
    public function &getValidator()
303
    {
304
        $validator = $this->calendar->getValidator();
305
306
        return $validator;
307
    }
308
309
    /**
310
     * Returns a reference to the current Calendar_Engine being used. Useful
311
     * for Calendar_Table_Helper and Calendar_Validator.
312
     *
313
     * @return object implementing Calendar_Engine_Inteface
314
     */
315
    public function &getEngine()
316
    {
317
        $engine = $this->calendar->getEngine();
318
319
        return $engine;
320
    }
321
322
    /**
323
     * Returns the value for the previous year.
324
     *
325
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
326
     *
327
     * @return int e.g. 2002 or timestamp
328
     */
329
    public function prevYear($format = 'int')
330
    {
331
        return $this->calendar->prevYear($format);
332
    }
333
334
    /**
335
     * Returns the value for this year.
336
     *
337
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
338
     *
339
     * @return int e.g. 2003 or timestamp
340
     */
341
    public function thisYear($format = 'int')
342
    {
343
        return $this->calendar->thisYear($format);
344
    }
345
346
    /**
347
     * Returns the value for next year.
348
     *
349
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
350
     *
351
     * @return int e.g. 2004 or timestamp
352
     */
353
    public function nextYear($format = 'int')
354
    {
355
        return $this->calendar->nextYear($format);
356
    }
357
358
    /**
359
     * Returns the value for the previous month.
360
     *
361
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
362
     *
363
     * @return int e.g. 4 or Unix timestamp
364
     */
365
    public function prevMonth($format = 'int')
366
    {
367
        return $this->calendar->prevMonth($format);
368
    }
369
370
    /**
371
     * Returns the value for this month.
372
     *
373
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
374
     *
375
     * @return int e.g. 5 or timestamp
376
     */
377
    public function thisMonth($format = 'int')
378
    {
379
        return $this->calendar->thisMonth($format);
380
    }
381
382
    /**
383
     * Returns the value for next month.
384
     *
385
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
386
     *
387
     * @return int e.g. 6 or timestamp
388
     */
389
    public function nextMonth($format = 'int')
390
    {
391
        return $this->calendar->nextMonth($format);
392
    }
393
394
    /**
395
     * Returns the value for the previous week.
396
     *
397
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
398
     *
399
     * @return int e.g. 4 or Unix timestamp
400
     */
401
    public function prevWeek($format = 'n_in_month')
402
    {
403
        if (method_exists($this->calendar, 'prevWeek')) {
404
            return $this->calendar->prevWeek($format);
405
        } else {
406
            require_once __DIR__ . '/PEAR.php';
407
            PEAR::raiseError('Cannot call prevWeek on Calendar object of type: ' . get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Decorator::prevWeek()');
0 ignored issues
show
Bug introduced by
The type PEAR 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...
Bug introduced by
The constant PEAR_ERROR_TRIGGER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
408
409
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
410
        }
411
    }
412
413
    /**
414
     * Returns the value for this week.
415
     *
416
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
417
     *
418
     * @return int e.g. 5 or timestamp
419
     */
420
    public function thisWeek($format = 'n_in_month')
421
    {
422
        if (method_exists($this->calendar, 'thisWeek')) {
423
            return $this->calendar->thisWeek($format);
424
        } else {
425
            require_once __DIR__ . '/PEAR.php';
426
            PEAR::raiseError('Cannot call thisWeek on Calendar object of type: ' . get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Decorator::thisWeek()');
0 ignored issues
show
Bug introduced by
The constant PEAR_ERROR_TRIGGER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
427
428
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
429
        }
430
    }
431
432
    /**
433
     * Returns the value for next week.
434
     *
435
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
436
     *
437
     * @return int e.g. 6 or timestamp
438
     */
439
    public function nextWeek($format = 'n_in_month')
440
    {
441
        if (method_exists($this->calendar, 'nextWeek')) {
442
            return $this->calendar->nextWeek($format);
443
        } else {
444
            require_once __DIR__ . '/PEAR.php';
445
            PEAR::raiseError('Cannot call thisWeek on Calendar object of type: ' . get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Decorator::nextWeek()');
0 ignored issues
show
Bug introduced by
The constant PEAR_ERROR_TRIGGER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
446
447
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
448
        }
449
    }
450
451
    /**
452
     * Returns the value for the previous day.
453
     *
454
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
455
     *
456
     * @return int e.g. 10 or timestamp
457
     */
458
    public function prevDay($format = 'int')
459
    {
460
        return $this->calendar->prevDay($format);
461
    }
462
463
    /**
464
     * Returns the value for this day.
465
     *
466
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
467
     *
468
     * @return int e.g. 11 or timestamp
469
     */
470
    public function thisDay($format = 'int')
471
    {
472
        return $this->calendar->thisDay($format);
473
    }
474
475
    /**
476
     * Returns the value for the next day.
477
     *
478
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
479
     *
480
     * @return int e.g. 12 or timestamp
481
     */
482
    public function nextDay($format = 'int')
483
    {
484
        return $this->calendar->nextDay($format);
485
    }
486
487
    /**
488
     * Returns the value for the previous hour.
489
     *
490
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
491
     *
492
     * @return int e.g. 13 or timestamp
493
     */
494
    public function prevHour($format = 'int')
495
    {
496
        return $this->calendar->prevHour($format);
497
    }
498
499
    /**
500
     * Returns the value for this hour.
501
     *
502
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
503
     *
504
     * @return int e.g. 14 or timestamp
505
     */
506
    public function thisHour($format = 'int')
507
    {
508
        return $this->calendar->thisHour($format);
509
    }
510
511
    /**
512
     * Returns the value for the next hour.
513
     *
514
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
515
     *
516
     * @return int e.g. 14 or timestamp
517
     */
518
    public function nextHour($format = 'int')
519
    {
520
        return $this->calendar->nextHour($format);
521
    }
522
523
    /**
524
     * Returns the value for the previous minute.
525
     *
526
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
527
     *
528
     * @return int e.g. 23 or timestamp
529
     */
530
    public function prevMinute($format = 'int')
531
    {
532
        return $this->calendar->prevMinute($format);
533
    }
534
535
    /**
536
     * Returns the value for this minute.
537
     *
538
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
539
     *
540
     * @return int e.g. 24 or timestamp
541
     */
542
    public function thisMinute($format = 'int')
543
    {
544
        return $this->calendar->thisMinute($format);
545
    }
546
547
    /**
548
     * Returns the value for the next minute.
549
     *
550
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
551
     *
552
     * @return int e.g. 25 or timestamp
553
     */
554
    public function nextMinute($format = 'int')
555
    {
556
        return $this->calendar->nextMinute($format);
557
    }
558
559
    /**
560
     * Returns the value for the previous second.
561
     *
562
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
563
     *
564
     * @return int e.g. 43 or timestamp
565
     */
566
    public function prevSecond($format = 'int')
567
    {
568
        return $this->calendar->prevSecond($format);
569
    }
570
571
    /**
572
     * Returns the value for this second.
573
     *
574
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
575
     *
576
     * @return int e.g. 44 or timestamp
577
     */
578
    public function thisSecond($format = 'int')
579
    {
580
        return $this->calendar->thisSecond($format);
581
    }
582
583
    /**
584
     * Returns the value for the next second.
585
     *
586
     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
587
     *
588
     * @return int e.g. 45 or timestamp
589
     */
590
    public function nextSecond($format = 'int')
591
    {
592
        return $this->calendar->nextSecond($format);
593
    }
594
}
595