ManyMonthsCalendar::cleanEventTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
4
5
class ManyMonthsCalendar extends ViewableData
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...
6
{
7
8
    //settings
9
    private static $day_names = array( 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
10
    private static $month_names = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
11
    private static $previous_month_nav_text = '&laquo';
12
    private static $next_month_nav_text = '&raquo';
13
    private static $start_day_of_the_week = 1;
14
    private static $show_year_in_calendar = true;
15
    private static $enable_navigation = true;
16
    private static $calendar_day_format = "j"; // see http://nz.php.net/manual/en/function.date.php for formatting options
17
    private static $table_cell_spacing = 0; // see http://nz.php.net/manual/en/function.date.php for formatting options
18
19
    //variables to be added at runtime
20
    private $events = array();
21
    private $calendarName = "MMC";
22
    private $pageLink = "pleasesetlink";
23
24
    //internal variables
25
    private static $count_of_calenders_shown = 0;
26
    private $month = 1;
27
    private $year = 1974;
28
    private $timeStamp = 0;
29
    private $daysInMonth = 25; //set to unrealistic number to make sure that it actually gets set correctly
30
31 View Code Duplication
    public static function setDayNames($dayArray)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        if (!is_array($dayArray) || count($dayArray) != 7) {
34
            debug::show($dayArray);
35
            user_error('There should be seven days in the array passed tot he setDayNames function in the ManyMonthsCalendar Class', E_USER_ERROR);
36
        }
37
        self::$day_names = $dayArray;
38
    }
39
40 View Code Duplication
    public static function setMonthNames($monthArray)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        if (!is_array($monthArray) || count($monthArray) != 12) {
43
            debug::show($monthArray);
44
            user_error('There should be twelve months in the array passed tot he setMonthNames function in the ManyMonthsCalendar Class', E_USER_ERROR);
45
        }
46
        self::$month_names = $monthArray;
47
    }
48
49
    public static function setPreviousMonthNavText($text)
50
    {
51
        self::$previous_month_nav_text = $text;
52
    }
53
54
    public static function setNextMonthNavText($text)
55
    {
56
        self::$next_month_nav_text = $text;
57
    }
58
59
    public static function setStartDayOfTheWeek($numericDay)
60
    {
61
        if (is_int($numericDay) && $numericDay >= 0 && $numericDay <=6) {
62
            self::$start_day_of_the_week = $numericDay;
63
            for ($i = 0 ; $i < $numericDay ; $i++) {
64
                array_unshift(self::$day_names, array_pop(self::$day_names));
65
            }
66
        } else {
67
            debug::show($numericDay);
68
            user_error('Start Day of the Week should be between 0 and 6, 0=monday, 1=sunday, 2=saturday etc...)', E_USER_ERROR);
69
        }
70
    }
71
72
    public static function setShowYearInCalendar($booleanValue)
73
    {
74
        self::$show_year_in_calendar = $booleanValue;
75
    }
76
77
    public static function setEnableNavigation($booleanValue)
78
    {
79
        self::$enable_navigation = $booleanValue;
80
    }
81
82
    public static function setCalendarDayFormat($format)
83
    {
84
        self::$calendar_day_format = $format;
85
    }
86
87
    public static function setTableCellSpacing($pixels)
88
    {
89
        if (is_int($pixels) && $pixels >= 0 && $pixels <=500) {
90
            self::$table_cell_spacing = $pixels;
91
        } else {
92
            debug::show($pixels);
93
            user_error('Please set the right number of pixels for table spacing (e.g. 0, 1, 2 or 3)', E_USER_ERROR);
94
        }
95
    }
96
97
    public function __construct($pageLink, $name, $year = 0, $month = 0)
98
    {
99
        $this->pageLink = $pageLink;
100
        // Assign name to calendar
101
        if (strpos($name, ' ') || strpos($name, '_') || is_numeric(substr($name, 0, 1))) {
102
            debug::show($name);
103
            user_error('Calendar should have a valid CSS name in the ManyMonthsCalendar Class', E_USER_ERROR);
104
        }
105
        $this->calendarName = $name;
106
        
107
        // Set day, month and year of calendar
108
        $this->month = (0 == $month) ?    date('n') : $month;
0 ignored issues
show
Documentation Bug introduced by
It seems like 0 == $month ? date('n') : $month can also be of type string. However, the property $month is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
109
        $this->year = (0 == $year) ? date('Y') : $year;
0 ignored issues
show
Documentation Bug introduced by
It seems like 0 == $year ? date('Y') : $year can also be of type string. However, the property $year is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
110
111
        // Check for valid input
112
        if (!preg_match('~[0-9]{4}~', $this->year)) {
113
            debug::show($this->year);
114
            user_error('Year should be a valid number in the ManyMonthsCalendar Class', E_USER_ERROR);
115
        }
116
        if (!is_numeric($this->month) || $this->month < 0 || $this->month > 13) {
117
            debug::show($this->month);
118
            user_error('Month should be a valid number in the ManyMonthsCalendar Class', E_USER_ERROR);
119
        }
120
        // Set the current timestamp
121
        $this->timeStamp = mktime(1, 1, 1, $this->month, 1, $this->year);
122
        // Set the number of days in the current month
123
        $this->daysInMonth = date('t', $this->timeStamp);
0 ignored issues
show
Documentation Bug introduced by
The property $daysInMonth was declared of type integer, but date('t', $this->timeStamp) is of type string. 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...
124
    }
125
126
127
128
    public function addEvent($timeStamp, $title, $link = '')
129
    {
130
        $this->events[$timeStamp][$this->cleanEventTitle($title)] = array(
131
            'Title' => $title,
132
            'Link' => $link
133
        );
134
    }
135
136
    public function removeEvent($timeStamp, $title)
137
    {
138
        if (isset($this->events[$timeStamp][$this->cleanEventTitle($title)])) {
139
            unset($this->events[$timeStamp][$this->cleanEventTitle($title)]);
140
        }
141
    }
142
143
    public function ManyMonthsCalendar()
144
    {
145
        Requirements::themedCSS("ManyMonthsCalendar");
146
        //make sure that all arrays added to calendarArray are DoSets in their own right
147
        self::$count_of_calenders_shown++;
148
        $calendarArray = array();
149
        //general variables
150
        $calendarArray["TableID"] = $this->calenderName.'-'.self::$count_of_calenders_shown;
0 ignored issues
show
Bug introduced by
The property calenderName does not seem to exist. Did you mean calendarName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
151
        $calendarArray["TableClass"] = $this->calenderName;
0 ignored issues
show
Bug introduced by
The property calenderName does not seem to exist. Did you mean calendarName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
152
        $calendarArray["EnableNavigation"] = self::$enable_navigation;
153
        $calendarArray["ColSpan"] = 5;
154
        $calendarArray["PageLink"] = $this->pageLink;
155
        $calendarArray["MonthName"] = $this->getMonthName();
156
        if (self::$show_year_in_calendar) {
157
            $calendarArray["YearName"] = $this->year;
158
        }
159
        $calendarArray["CellSpacing"] = self::$table_cell_spacing;
160
        $calendarArray["PreviousMonthNavText"] = self::$previous_month_nav_text;
161
        $calendarArray["NextMonthNavText"] = self::$next_month_nav_text;
162
        if (self::$enable_navigation) {
163
            $previousMonth = explode('-', date('n-Y', strtotime('-1 month', $this->timeStamp)));
164
            $calendarArray["PreviousMonthMonthNumber"] = $previousMonth[0];
165
            $calendarArray["PreviousMonthYearNumber"] = $previousMonth[1];
166
            $nextYear = explode('-', date('n-Y', strtotime('+1 month', $this->timeStamp)));
167
            $calendarArray["NextMonthMonthNumber"] = $nextYear[0];
168
            $calendarArray["NextMonthYearNumber"] = $nextYear[1];
169
            $calendarArray["ColSpan"] = 7;
170
        }
171
172
        $doSetDayNames = new DataObjectSet();
173
        //day name
174
        foreach (self::$day_names as $name) {
175
            $doSetDayNames->push(new ArrayData(array("DayName" => $name)));
0 ignored issues
show
Documentation introduced by
new \ArrayData(array('DayName' => $name)) is of type object<ArrayData>, but the function expects a object<DataObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
        }
177
        $calendarArray["DayNames"] = $doSetDayNames;
178
179
        $extraDaysToBeAddedFromPreviousMonth = date('N', $this->timeStamp) + self::$start_day_of_the_week - 1;
180
        $position = 0;
181
182
        $doSetDates = new DataObjectSet();
183
        // previous month
184
        for ($e = 1 ; $e <= $extraDaysToBeAddedFromPreviousMonth ; $e++) {
185
            $position++;
186
            $timeStamp = $this->makeTimeStamp("-" . intval($extraDaysToBeAddedFromPreviousMonth -$e) . " days");
187
            $doSetDates->push($this->getDateDataObject($position, $timeStamp, false));
0 ignored issues
show
Documentation introduced by
$this->getDateDataObject...ion, $timeStamp, false) is of type object<ArrayData>, but the function expects a object<DataObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
188
        }
189
190
        // current Month
191
        for ($i = 1 ; $i <= $this->daysInMonth ; $i++) {
192
            $position++;
193
            $timeStamp = $this->makeTimeStamp("+".($i-1)." days");
194
            $doSetDates->push($this->getDateDataObject($position, $timeStamp));
0 ignored issues
show
Documentation introduced by
$this->getDateDataObject($position, $timeStamp) is of type object<ArrayData>, but the function expects a object<DataObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
195
        }
196
197
        // next month
198
        for ($e2 = 1 ; $e2 < (7 - (($e + $this->daysInMonth -1) % 7)) ; $e2++) {
199
            $position++;
200
            $timeStamp = $this->makeTimeStamp("+$e2 days");
201
            $doSetDates->push($this->getDateDataObject($position, $timeStamp, false));
0 ignored issues
show
Documentation introduced by
$this->getDateDataObject...ion, $timeStamp, false) is of type object<ArrayData>, but the function expects a object<DataObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
202
        }
203
        $calendarArray["Days"] = $doSetDates;
204
        return new ArrayData($calendarArray);
205
    }
206
207
    private function getDateDataObject($position, $timeStamp, $currentMonth = true)
208
    {
209
        $array = array();
210
        $array["Day"] = date(self::$calendar_day_format, $timeStamp);
211
        if ($currentMonth) {
212
            $array["OutsideCurrentMonth"] = true;
213
        } else {
214
            $array["OutsideCurrentMonth"] = false;
215
        }
216
        $eventsDoSet = new DataObjectSet();
217
        if (isset($this->events[$timeStamp])) {
218
            if (is_array($this->events[$timeStamp])) {
219
                foreach ($this->events[$timeStamp] as $event) {
220
                    $eventsDoSet->push(new ArrayData($event));
0 ignored issues
show
Documentation introduced by
new \ArrayData($event) is of type object<ArrayData>, but the function expects a object<DataObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
221
                }
222
            }
223
        }
224
        $array["Events"] = $eventsDoSet;
225
        if (($position % 7) == 0 /*&& $position < */) {
226
            $array["LastDayOfTheWeek"] = true;
227
        }
228
        if ($timeStamp == mktime(1, 1, 1, date('n'), date('j'), date('Y'))) {
229
            $array["IsCurrentDay"] = true;
230
        } else {
231
            $array["IsCurrentDay"] = false;
232
        }
233
        if ($position % 2) {
234
            $array["EvenCol"] = true;
235
        }
236
        if (round($position / 7) % 2) {
237
            $array["EvenRow"] = false;
238
        }
239
        return new ArrayData($array);
240
    }
241
242
    private function cleanEventTitle($title)
243
    {
244
        $title = eregi_replace("[^[:alnum:]]", " ", $title);
245
        $title = trim(eregi_replace(" +", "", $title)); //removes excess spaces
246
        return $title;
247
    }
248
249
    private function makeTimeStamp($offset)
250
    {
251
        return strtotime($offset, $this->timeStamp);
252
    }
253
254
    private function getMonthName()
255
    {
256
        return ucwords(self::$month_names[$this->month-1]);
257
    }
258
259
    public function forTemplate()
260
    {
261
        return $this->renderWith('ManyMonthsCalendar');
262
    }
263
}
264