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

Weeks   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A build() 0 24 3
A setSelection() 0 9 5
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4
/**
5
 * Contains the Calendar_Month_Weeks 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\Month;
40
41
use Pear\Calendar\Month;
42
use Pear\Calendar\Table\Helper;
43
use Pear\Calendar\Week;
44
45
/**
46
 * Allows Calendar include path to be redefined
47
 * @ignore
48
 */
49
if (!defined('CALENDAR_ROOT')) {
50
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
51
}
52
53
/**
54
 * Load Calendar base class
55
 */
56
require_once CALENDAR_ROOT.'Calendar.php';
57
58
/**
59
 * Load base month
60
 */
61
require_once CALENDAR_ROOT.'Month.php';
62
63
/**
64
 * Represents a Month and builds Weeks
65
 * <code>
66
 * require_once 'Calendar'.DIRECTORY_SEPARATOR.'Month'.DIRECTORY_SEPARATOR.'Weeks.php';
67
 * $Month = new Calendar_Month_Weeks(2003, 10); // Oct 2003
68
 * $Month->build(); // Build Calendar_Day objects
69
 * while ($Week = & $Month->fetch()) {
70
 *     echo $Week->thisWeek().'<br />';
71
 * }
72
 * </code>
73
 *
74
 * @category  Date and Time
75
 * @package   Calendar
76
 * @author    Harry Fuecks <[email protected]>
77
 * @author    Lorenzo Alberton <[email protected]>
78
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
79
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
80
 * @link      http://pear.php.net/package/Calendar
81
 * @access    public
82
 */
83
class Weeks extends Month
84
{
85
    /**
86
     * Instance of Calendar_Table_Helper
0 ignored issues
show
Bug introduced by
The type Pear\Calendar\Month\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...
87
     * @var Calendar_Table_Helper
88
     * @access private
89
     */
90
    var $tableHelper;
91
92
    /**
93
     * First day of the week
94
     * @access private
95
     * @var string
96
     */
97
    var $firstDay;
98
99
    /**
100
     * Constructs Calendar_Month_Weeks
101
     *
102
     * @param int $y        year e.g. 2003
103
     * @param int $m        month e.g. 5
104
     * @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.)
105
     *
106
     * @access public
107
     */
108
    function __construct($y, $m, $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...
109
    {
110
        parent::__construct($y, $m, $firstDay);
111
    }
112
113
    /**
114
     * Builds Calendar_Week objects for the Month. Note that Calendar_Week
115
     * builds Calendar_Day object in tabular form (with Calendar_Day->empty)
116
     *
117
     * @param array $sDates (optional) Calendar_Week objects representing selected dates
118
     *
119
     * @return boolean
120
     * @access public
121
     */
122
    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...
123
    {
124
        $this->tableHelper = new Helper($this, $this->firstDay);
0 ignored issues
show
Bug introduced by
$this->firstDay of type string is incompatible with the type integer expected by parameter $firstDay of Pear\Calendar\Table\Helper::__construct(). ( Ignorable by Annotation )

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

124
        $this->tableHelper = new Helper($this, /** @scrutinizer ignore-type */ $this->firstDay);
Loading history...
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\Month\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...
125
        include_once CALENDAR_ROOT.'Week.php';
126
        $numWeeks = $this->tableHelper->getNumWeeks();
127
        for ($i=1, $d=1; $i<=$numWeeks; $i++,
128
            $d+=$this->cE->getDaysInWeek(
129
                $this->thisYear(),
130
                $this->thisMonth(),
131
                $this->thisDay()
132
            )
133
        ) {
134
            $this->children[$i] = new Week(
135
                $this->year, $this->month, $d, $this->tableHelper->getFirstDay());
136
        }
137
        //used to set empty days
138
        $this->children[1]->setFirst(true);
139
        $this->children[$numWeeks]->setLast(true);
140
141
        // Handle selected weeks here
142
        if (count($sDates) > 0) {
143
            $this->setSelection($sDates);
144
        }
145
        return true;
146
    }
147
148
    /**
149
     * Called from build()
150
     *
151
     * @param array $sDates Calendar_Week objects representing selected dates
152
     *
153
     * @return void
154
     * @access private
155
     */
156
    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...
157
    {
158
        foreach ($sDates as $sDate) {
159
            if ($this->year == $sDate->thisYear()
160
                && $this->month == $sDate->thisMonth())
161
            {
162
                $key = $sDate->thisWeek('n_in_month');
163
                if (isset($this->children[$key])) {
164
                    $this->children[$key]->setSelected();
165
                }
166
            }
167
        }
168
    }
169
}
170