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

MonthDecorator::prevMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

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 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Description: demonstrates a decorator to provide simple output formatting
4
 * on the month while still allowing the days to be accessed via the decorator
5
 * In practice you _wouldn't_ do this - each decorator comes with a performance
6
 * hit for extra method calls. For this example some simple functions could help
7
 * format the month while the days are accessed via the normal Month object.
8
 */
9
if (!@include 'Calendar/Calendar.php') {
10
    define('CALENDAR_ROOT', '../../');
11
}
12
require_once CALENDAR_ROOT . 'Month/Weekdays.php';
13
require_once CALENDAR_ROOT . 'Decorator.php';
14
15
// Decorate a Month with methods to improve formatting
16
17
/**
18
 * Class MonthDecorator.
19
 */
20
class MonthDecorator extends 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...
21
{
22
    /**
23
     * @param object $Month
24
     *
25
     * @internal param $Calendar_Month
26
     */
27
    public function __construct(&$Month)
28
    {
29
        parent::__construct($Month);
30
    }
31
32
    /**
33
     * Override the prevMonth method to format the output.
34
     */
35
    public function prevMonth()
36
    {
37
        $prevStamp = parent::prevMonth(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $format of Calendar_Decorator::prevMonth(). ( Ignorable by Annotation )

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

37
        $prevStamp = parent::prevMonth(/** @scrutinizer ignore-type */ true);
Loading history...
38
39
        // Build the URL for the previous month
40
        return $_SERVER['PHP_SELF'] . '?y=' . date('Y', $prevStamp) . '&m=' . date('n', $prevStamp) . '&d=' . date('j', $prevStamp);
41
    }
42
43
    /**
44
     * Override the thisMonth method to format the output.
45
     */
46
    public function thisMonth()
47
    {
48
        $thisStamp = parent::thisMonth(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $format of Calendar_Decorator::thisMonth(). ( Ignorable by Annotation )

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

48
        $thisStamp = parent::thisMonth(/** @scrutinizer ignore-type */ true);
Loading history...
49
50
        // A human readable string from this month
51
        return date('F Y', $thisStamp);
52
    }
53
54
    /**
55
     * Override the nextMonth method to format the output.
56
     */
57
    public function nextMonth()
58
    {
59
        $nextStamp = parent::nextMonth(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $format of Calendar_Decorator::nextMonth(). ( Ignorable by Annotation )

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

59
        $nextStamp = parent::nextMonth(/** @scrutinizer ignore-type */ true);
Loading history...
60
61
        // Build the URL for next month
62
        return $_SERVER['PHP_SELF'] . '?y=' . date('Y', $nextStamp) . '&m=' . date('n', $nextStamp) . '&d=' . date('j', $nextStamp);
63
    }
64
}
65
66
if (!isset($_GET['y'])) {
67
    $_GET['y'] = date('Y');
68
}
69
if (!isset($_GET['m'])) {
70
    $_GET['m'] = date('n');
71
}
72
73
// Creata a month as usual
74
$Month = new Calendar_Month_Weekdays($_GET['y'], $_GET['m']);
75
76
// Pass it to the decorator and use the decorator from now on...
77
$MonthDecorator = new MonthDecorator($Month);
78
$MonthDecorator->build();
79
?>
80
81
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
82
<html>
83
<head>
84
    <title> A Simple Decorator </title>
85
</head>
86
<body>
87
<h1>A Simple Decorator</h1>
88
<table>
89
    <caption><?php echo $MonthDecorator->thisMonth(); ?></caption>
90
    <?php
91
    while ($Day = $MonthDecorator->fetch()) {
92
        if ($Day->isFirst()) {
93
            echo "\n<tr>\n";
94
        }
95
        if ($Day->isEmpty()) {
96
            echo '<td>&nbsp;</td>';
97
        } else {
98
            echo '<td>' . $Day->thisDay() . '</td>';
99
        }
100
        if ($Day->isLast()) {
101
            echo "\n</tr>\n";
102
        }
103
    }
104
    ?>
105
    <tr>
106
        <td><a href="<?php echo $MonthDecorator->prevMonth(); ?>">Prev</a></td>
107
        <td colspan="5">&nbsp;</td>
108
        <td><a href="<?php echo $MonthDecorator->nextMonth(); ?>">Next</a></td>
109
    </tr>
110
</table>
111
</body>
112
</html>
113