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

Calendar_Month_Weekdays   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 40
dl 0
loc 126
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Weekdays::build() 0 9 1
A Weekdays::buildEmptyDaysAfter() 0 9 2
A Weekdays::shiftDays() 0 5 2
A Weekdays::__construct() 0 3 1
A Weekdays::buildEmptyDaysBefore() 0 12 2
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4
/**
5
 * Contains the Calendar_Month_Weekdays 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
 * @copyright 2003-2007 Harry Fuecks
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id$
36
 * @link      http://pear.php.net/package/Calendar
37
 */
38
namespace Pear\Calendar\Month;
39
40
use Pear\Calendar\Day;
41
use Pear\Calendar\Month;
42
use Pear\Calendar\Table\Helper;
43
44
/**
45
 * Allows Calendar include path to be redefined
46
 * @ignore
47
 */
48
if (!defined('CALENDAR_ROOT')) {
49
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
50
}
51
52
/**
53
 * Load Calendar base class
54
 */
55
require_once CALENDAR_ROOT.'Calendar.php';
56
57
/**
58
 * Load base month
59
 */
60
require_once CALENDAR_ROOT.'Month.php';
61
62
/**
63
 * Represents a Month and builds Days in tabular form<br>
64
 * <code>
65
 * require_once 'Calendar/Month/Weekdays.php';
66
 * $Month = new Calendar_Month_Weekdays(2003, 10); // Oct 2003
67
 * $Month->build(); // Build Calendar_Day objects
68
 * while ($Day = & $Month->fetch()) {
69
 *     if ($Day->isFirst()) {
70
 *         echo '<tr>';
71
 *     }
72
 *     if ($Day->isEmpty()) {
73
 *         echo '<td>&nbsp;</td>';
74
 *     } else {
75
 *         echo '<td>'.$Day->thisDay().'</td>';
76
 *     }
77
 *     if ($Day->isLast()) {
78
 *         echo '</tr>';
79
 *     }
80
 * }
81
 * </code>
82
 *
83
 * @category  Date and Time
84
 * @package   Calendar
85
 * @author    Harry Fuecks <[email protected]>
86
 * @copyright 2003-2007 Harry Fuecks
87
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
88
 * @link      http://pear.php.net/package/Calendar
89
 * @access    public
90
 */
91
class Weekdays extends Month
92
{
93
    /**
94
     * 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...
95
     * @var Calendar_Table_Helper
96
     * @access private
97
     */
98
    var $tableHelper;
99
100
    /**
101
     * First day of the week
102
     * @access private
103
     * @var string
104
     */
105
    var $firstDay;
106
107
    /**
108
     * Constructs Calendar_Month_Weekdays
109
     *
110
     * @param int $y        year e.g. 2003
111
     * @param int $m        month e.g. 5
112
     * @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.)
113
     *
114
     * @access public
115
     */
116
    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...
117
    {
118
        parent::__construct($y, $m, $firstDay);
119
    }
120
121
    /**
122
     * Builds Day objects in tabular form, to allow display of calendar month
123
     * with empty cells if the first day of the week does not fall on the first
124
     * day of the month.
125
     *
126
     * @param array $sDates (optional) Calendar_Day objects representing selected dates
127
     *
128
     * @return boolean
129
     * @access public
130
     * @see Day::isEmpty()
131
     * @see Calendar_Day_Base::isFirst()
132
     * @see Calendar_Day_Base::isLast()
133
     */
134
    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...
135
    {
136
        $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\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...
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

136
        $this->tableHelper = new Helper($this, /** @scrutinizer ignore-type */ $this->firstDay);
Loading history...
137
        Month::build($sDates);
138
        $this->buildEmptyDaysBefore();
139
        $this->shiftDays();
140
        $this->buildEmptyDaysAfter();
141
        $this->setWeekMarkers();
142
        return true;
143
    }
144
145
    /**
146
     * Prepends empty days before the real days in the month
147
     *
148
     * @return void
149
     * @access private
150
     */
151
    function buildEmptyDaysBefore()
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...
152
    {
153
        $eBefore = $this->tableHelper->getEmptyDaysBefore();
154
        for ($i=0; $i < $eBefore; $i++) {
155
            $stamp = $this->cE->dateToStamp($this->year, $this->month, -$i);
156
            $Day = new Day(
157
                                $this->cE->stampToYear($stamp),
158
                                $this->cE->stampToMonth($stamp),
159
                                $this->cE->stampToDay($stamp));
160
            $Day->setEmpty();
161
            $Day->adjust();
162
            array_unshift($this->children, $Day);
163
        }
164
    }
165
166
    /**
167
     * Shifts the array of children forward, if necessary
168
     *
169
     * @return void
170
     * @access private
171
     */
172
    function shiftDays()
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...
173
    {
174
        if (isset($this->children[0])) {
175
            array_unshift($this->children, null);
176
            unset($this->children[0]);
177
        }
178
    }
179
180
    /**
181
     * Appends empty days after the real days in the month
182
     *
183
     * @return void
184
     * @access private
185
     */
186
    function buildEmptyDaysAfter()
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...
187
    {
188
        $eAfter = $this->tableHelper->getEmptyDaysAfter();
189
        $sDOM   = $this->tableHelper->getNumTableDaysInMonth();
190
        for ($i=1; $i <= $sDOM-$eAfter; $i++) {
191
            $Day = new Day($this->year, $this->month+1, $i);
192
            $Day->setEmpty();
193
            $Day->adjust();
194
            array_push($this->children, $Day);
195
        }
196
    }
197
198
    /**
199
     * Sets the "markers" for the beginning and of a of week, in the
200
     * built Calendar_Day children
201
     *
202
     * @return void
203
     * @access private
204
     */
205
    function setWeekMarkers()
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...
206
    {
207
        $dIW = $this->cE->getDaysInWeek(
208
            $this->thisYear(),
209
            $this->thisMonth(),
210
            $this->thisDay()
211
        );
212
        $sDOM = $this->tableHelper->getNumTableDaysInMonth();
213
        for ($i=1; $i <= $sDOM; $i+= $dIW) {
214
            $this->children[$i]->setFirst();
215
            $this->children[$i+($dIW-1)]->setLast();
216
        }
217
    }
218
}
219