Passed
Pull Request — master (#2)
by tsms
01:49
created

Week::thisYear()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
 * Contains the Calendar_Week class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Date and Time
31
 * @package   Calendar
32
 * @author    Harry Fuecks <[email protected]>
33
 * @author    Lorenzo Alberton <[email protected]>
34
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
35
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
36
 * @version   CVS: $Id$
37
 * @link      http://pear.php.net/package/Calendar
38
 */
39
namespace PEAR\Calendar;
40
41
use PEAR\Calendar\Table\Helper;
42
43
/**
44
 * Represents a Week and builds Days in tabular format<br>
45
 * <code>
46
 * $Week = new \PEAR\Calendar\Week(2003, 10, 1); Oct 2003, 1st tabular week
47
 * echo '<tr>';
48
 * while ($Day = & $Week->fetch()) {
49
 *     if ($Day->isEmpty()) {
50
 *         echo '<td>&nbsp;</td>';
51
 *     } else {
52
 *         echo '<td>'.$Day->thisDay().'</td>';
53
 *      }
54
 * }
55
 * echo '</tr>';
56
 * </code>
57
 *
58
 * @category  Date and Time
59
 * @package   Calendar
60
 * @author    Harry Fuecks <[email protected]>
61
 * @author    Lorenzo Alberton <[email protected]>
62
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
63
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
64
 * @link      http://pear.php.net/package/Calendar
65
 */
66
class Week extends Calendar
67
{
68
    /**
69
     * Instance of Calendar_Table_Helper
0 ignored issues
show
Bug introduced by
The type PEAR\Calendar\Calendar_Table_Helper 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...
70
     * @var Calendar_Table_Helper
71
     * @access private
72
     */
73
    var $tableHelper;
74
75
    /**
76
     * Stores the timestamp of the first day of this week
77
     * @access private
78
     * @var object
79
     */
80
    var $thisWeek;
81
82
    /**
83
     * Stores the timestamp of first day of previous week
84
     * @access private
85
     * @var object
86
     */
87
    var $prevWeek;
88
89
    /**
90
     * Stores the timestamp of first day of next week
91
     * @access private
92
     * @var object
93
     */
94
    var $nextWeek;
95
96
    /**
97
     * Used by build() to set empty days
98
     * @access private
99
     * @var boolean
100
     */
101
    var $firstWeek = false;
102
103
    /**
104
     * Used by build() to set empty days
105
     * @access private
106
     * @var boolean
107
     */
108
    var $lastWeek = false;
109
110
    /**
111
     * First day of the week (0=sunday, 1=monday...)
112
     * @access private
113
     * @var boolean
114
     */
115
    var $firstDay = 1;
116
117
    /**
118
     * Constructs Week
119
     *
120
     * @param int $y        year e.g. 2003
121
     * @param int $m        month e.g. 5
122
     * @param int $d        a day of the desired week
123
     * @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.)
124
     *
125
     * @access public
126
     */
127
    function __construct($y, $m, $d, $firstDay = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
    {
129
        parent::__construct($y, $m, $d);
130
        $this->firstDay    = $this->defineFirstDayOfWeek($firstDay);
0 ignored issues
show
Documentation Bug introduced by
The property $firstDay was declared of type boolean, but $this->defineFirstDayOfWeek($firstDay) is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
131
        $this->tableHelper = new Helper($this, $this->firstDay);
0 ignored issues
show
Documentation Bug introduced by
It seems like new PEAR\Calendar\Table\...$this, $this->firstDay) of type PEAR\Calendar\Table\Helper is incompatible with the declared type PEAR\Calendar\Calendar_Table_Helper of property $tableHelper.

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..

Loading history...
132
        $this->thisWeek    = $this->tableHelper->getWeekStart($y, $m, $d, $this->firstDay);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->tableHelper->getW...m, $d, $this->firstDay) of type integer is incompatible with the declared type object of property $thisWeek.

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..

Loading history...
133
        $this->prevWeek    = $this->tableHelper->getWeekStart(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->tableHelper->getW...ay()), $this->firstDay) of type integer is incompatible with the declared type object of property $prevWeek.

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..

Loading history...
134
            $y, 
135
            $m, 
136
            $d - $this->cE->getDaysInWeek(
137
                $this->thisYear(),
138
                $this->thisMonth(),
139
                $this->thisDay()
140
            ), 
141
            $this->firstDay
142
        );
143
        $this->nextWeek = $this->tableHelper->getWeekStart(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->tableHelper->getW...ay()), $this->firstDay) of type integer is incompatible with the declared type object of property $nextWeek.

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..

Loading history...
144
            $y, 
145
            $m, 
146
            $d + $this->cE->getDaysInWeek(
147
                $this->thisYear(),
148
                $this->thisMonth(),
149
                $this->thisDay()
150
            ), 
151
            $this->firstDay
152
        );
153
    }
154
155
    /**
156
     * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values
157
     * passed to the constructor
158
     *
159
     * @param int|string $ts Unix or ISO-8601 timestamp
160
     *
161
     * @return void
162
     * @access public
163
     */
164
    function setTimestamp($ts)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165
    {
166
        parent::setTimestamp($ts);
167
        $this->thisWeek = $this->tableHelper->getWeekStart(
168
            $this->year, $this->month, $this->day, $this->firstDay
169
        );
170
        $this->prevWeek = $this->tableHelper->getWeekStart(
171
            $this->year, 
172
            $this->month, 
173
            $this->day - $this->cE->getDaysInWeek(
174
                $this->thisYear(),
175
                $this->thisMonth(),
176
                $this->thisDay()
177
            ), 
178
            $this->firstDay
179
        );
180
        $this->nextWeek = $this->tableHelper->getWeekStart(
181
            $this->year, 
182
            $this->month, 
183
            $this->day + $this->cE->getDaysInWeek(
184
                $this->thisYear(),
185
                $this->thisMonth(),
186
                $this->thisDay()
187
            ), 
188
            $this->firstDay
189
        );
190
    }
191
192
    /**
193
     * Builds Calendar_Day objects for this Week
194
     *
195
     * @param array $sDates (optional) Calendar_Day objects representing selected dates
196
     *
197
     * @return boolean
198
     * @access public
199
     */
200
    function build($sDates = array())
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
201
    {
202
        include_once CALENDAR_ROOT.'Day.php';
203
        $year  = $this->cE->stampToYear($this->thisWeek);
204
        $month = $this->cE->stampToMonth($this->thisWeek);
205
        $day   = $this->cE->stampToDay($this->thisWeek);
206
        $end   = $this->cE->getDaysInWeek(
207
            $this->thisYear(),
208
            $this->thisMonth(),
209
            $this->thisDay()
210
        );
211
212
        for ($i=1; $i <= $end; $i++) {
213
            $stamp = $this->cE->dateToStamp($year, $month, $day++);
214
            $this->children[$i] = new Day(
215
                $this->cE->stampToYear($stamp),
216
                $this->cE->stampToMonth($stamp),
217
                $this->cE->stampToDay($stamp)
218
            );
219
        }
220
221
        //set empty days (@see Calendar_Month_Weeks::build())
222
        if ($this->firstWeek) {
223
            $eBefore = $this->tableHelper->getEmptyDaysBefore();
224
            for ($i=1; $i <= $eBefore; $i++) {
225
                $this->children[$i]->setEmpty();
226
            }
227
        }
228
        if ($this->lastWeek) {
229
            $eAfter = $this->tableHelper->getEmptyDaysAfterOffset();
230
            for ($i = $eAfter+1; $i <= $end; $i++) {
231
                $this->children[$i]->setEmpty();
232
            }
233
        }
234
235
        if (count($sDates) > 0) {
236
            $this->setSelection($sDates);
237
        }
238
        return true;
239
    }
240
241
    /**
242
     * Set as first week of the month
243
     *
244
     * @param boolean $state whether it's first or not
245
     *
246
     * @return void
247
     * @access private
248
     */
249
    function setFirst($state = true)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
250
    {
251
        $this->firstWeek = $state;
252
    }
253
254
    /**
255
     * Set as last week of the month
256
     *
257
     * @param boolean $state whether it's lasst or not
258
     *
259
     * @return void
260
     * @access private
261
     */
262
    function setLast($state = true)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
263
    {
264
        $this->lastWeek = $state;
265
    }
266
267
    /**
268
     * Called from build()
269
     *
270
     * @param array $sDates Calendar_Day objects representing selected dates
271
     *
272
     * @return void
273
     * @access private
274
     */
275
    function setSelection($sDates)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
276
    {
277
        foreach ($sDates as $sDate) {
278
            foreach ($this->children as $key => $child) {
279
                if ($child->thisDay() == $sDate->thisDay() &&
280
                    $child->thisMonth() == $sDate->thisMonth() &&
281
                    $child->thisYear() == $sDate->thisYear()
282
                ) {
283
                    $this->children[$key] = $sDate;
284
                    $this->children[$key]->setSelected();
285
                }
286
            }
287
        }
288
        reset($this->children);
289
    }
290
291
    /**
292
     * Returns the value for this year
293
     *
294
     * When a on the first/last week of the year, the year of the week is
295
     * calculated according to ISO-8601
296
     *
297
     * @param string $format return value format ['int' | 'timestamp' | 'object' | 'array']
298
     *
299
     * @return int e.g. 2003 or timestamp
300
     * @access public
301
     */
302
    function thisYear($format = 'int')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
303
    {
304
        if (null !== $this->thisWeek) {
305
            $tmp_cal = new Calendar();
306
            $tmp_cal->setTimestamp($this->thisWeek);
0 ignored issues
show
Bug introduced by
$this->thisWeek of type object is incompatible with the type integer|string expected by parameter $ts of PEAR\Calendar\Calendar::setTimestamp(). ( Ignorable by Annotation )

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

306
            $tmp_cal->setTimestamp(/** @scrutinizer ignore-type */ $this->thisWeek);
Loading history...
307
            $first_dow = $tmp_cal->thisDay('array');
308
            $days_in_week = $tmp_cal->cE->getDaysInWeek($tmp_cal->year, $tmp_cal->month, $tmp_cal->day);
309
            $tmp_cal->day += $days_in_week;
310
            $last_dow  = $tmp_cal->thisDay('array');
311
312
            if ($first_dow['year'] == $last_dow['year']) {
313
                return $first_dow['year'];
314
            }
315
316
            if ($last_dow['day'] > floor($days_in_week / 2)) {
317
                return $last_dow['year'];
318
            }
319
            return $first_dow['year'];
320
        }
321
        return parent::thisYear();
322
    }
323
324
    /**
325
     * Gets the value of the previous week, according to the requested format
326
     *
327
     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
328
     *
329
     * @return mixed
330
     * @access public
331
     */
332
    function prevWeek($format = 'n_in_month')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
333
    {
334
        switch (strtolower($format)) {
335
        case 'int':
336
        case 'n_in_month':
337
            return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1;
338
        case 'n_in_year':
339
            return $this->cE->getWeekNInYear(
340
                $this->cE->stampToYear($this->prevWeek),
341
                $this->cE->stampToMonth($this->prevWeek),
342
                $this->cE->stampToDay($this->prevWeek));
343
        case 'array':
344
            return $this->toArray($this->prevWeek);
345
        case 'object':
346
            include_once CALENDAR_ROOT.'Factory.php';
347
            return Factory::createByTimestamp('Week', $this->prevWeek);
0 ignored issues
show
Bug Best Practice introduced by
The method PEAR\Calendar\Factory::createByTimestamp() is not static, but was called statically. ( Ignorable by Annotation )

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

347
            return Factory::/** @scrutinizer ignore-call */ createByTimestamp('Week', $this->prevWeek);
Loading history...
348
        case 'timestamp':
349
        default:
350
            return $this->prevWeek;
351
        }
352
    }
353
354
    /**
355
     * Gets the value of the current week, according to the requested format
356
     *
357
     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
358
     *
359
     * @return mixed
360
     * @access public
361
     */
362
    function thisWeek($format = 'n_in_month')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
363
    {
364
        switch (strtolower($format)) {
365
        case 'int':
366
        case 'n_in_month':
367
            if ($this->firstWeek) {
368
                return 1;
369
            }
370
            if ($this->lastWeek) {
371
                return $this->cE->getWeeksInMonth(
372
                    $this->thisYear(),
373
                    $this->thisMonth(),
374
                    $this->firstDay);
375
            }
376
            return $this->cE->getWeekNInMonth(
377
                $this->thisYear(),
378
                $this->thisMonth(),
379
                $this->thisDay(),
380
                $this->firstDay);
381
        case 'n_in_year':
382
            return $this->cE->getWeekNInYear(
383
                $this->cE->stampToYear($this->thisWeek),
384
                $this->cE->stampToMonth($this->thisWeek),
385
                $this->cE->stampToDay($this->thisWeek));
386
        case 'array':
387
            return $this->toArray($this->thisWeek);
388
        case 'object':
389
            include_once CALENDAR_ROOT.'Factory.php';
390
            return Factory::createByTimestamp('Week', $this->thisWeek);
0 ignored issues
show
Bug Best Practice introduced by
The method PEAR\Calendar\Factory::createByTimestamp() is not static, but was called statically. ( Ignorable by Annotation )

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

390
            return Factory::/** @scrutinizer ignore-call */ createByTimestamp('Week', $this->thisWeek);
Loading history...
391
        case 'timestamp':
392
        default:
393
            return $this->thisWeek;
394
        }
395
    }
396
397
    /**
398
     * Gets the value of the following week, according to the requested format
399
     *
400
     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
401
     *
402
     * @return mixed
403
     * @access public
404
     */
405
    function nextWeek($format = 'n_in_month')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
406
    {
407
        switch (strtolower($format)) {
408
        case 'int':
409
        case 'n_in_month':
410
            return ($this->lastWeek) ? null : $this->thisWeek('n_in_month') +1;
411
        case 'n_in_year':
412
            return $this->cE->getWeekNInYear(
413
                $this->cE->stampToYear($this->nextWeek),
414
                $this->cE->stampToMonth($this->nextWeek),
415
                $this->cE->stampToDay($this->nextWeek));
416
        case 'array':
417
            return $this->toArray($this->nextWeek);
418
        case 'object':
419
            include_once CALENDAR_ROOT.'Factory.php';
420
            return Factory::createByTimestamp('Week', $this->nextWeek);
0 ignored issues
show
Bug Best Practice introduced by
The method PEAR\Calendar\Factory::createByTimestamp() is not static, but was called statically. ( Ignorable by Annotation )

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

420
            return Factory::/** @scrutinizer ignore-call */ createByTimestamp('Week', $this->nextWeek);
Loading history...
421
        case 'timestamp':
422
        default:
423
            return $this->nextWeek;
424
        }
425
    }
426
427
    /**
428
     * Returns the instance of Calendar_Table_Helper.
429
     * Called from Calendar_Validator::isValidWeek
430
     *
431
     * @return Calendar_Table_Helper
432
     * @access protected
433
     */
434
    function & getHelper()
435
    {
436
        return $this->tableHelper;
437
    }
438
439
    /**
440
     * Makes sure theres a value for $this->day
441
     *
442
     * @return void
443
     * @access private
444
     */
445
    function findFirstDay()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
446
    {
447
        if (!count($this->children) > 0) {
448
            $this->build();
449
            foreach ($this->children as $Day) {
450
                if (!$Day->isEmpty()) {
451
                    $this->day = $Day->thisDay();
452
                    break;
453
                }
454
            }
455
        }
456
    }
457
}
458