Passed
Pull Request — master (#2)
by tsms
02:11
created

Calendar_Factory::create()   C

Complexity

Conditions 13
Paths 30

Size

Total Lines 48
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 48
rs 6.6166
c 0
b 0
f 0
cc 13
nc 30
nop 7

1 Method

Rating   Name   Duplication   Size   Complexity  
C Factory::create() 0 39 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4
/**
5
 * Contains the Calendar_Factory 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
/**
42
 * Allows Calendar include path to be redefined
43
 * @ignore
44
 */
45
if (!defined('CALENDAR_ROOT')) {
46
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
47
}
48
49
/**
50
 * Load Calendar base class
51
 */
52
require_once CALENDAR_ROOT.'Calendar.php';
53
54
/**
55
 * Contains a factory method to return a Singleton instance of a class
56
 * implementing the Calendar_Engine_Interface.<br>
57
 * For Month objects, to control type of month returned, use CALENDAR_MONTH_STATE
58
 * constact e.g.;
59
 * <code>
60
 * require_once 'Calendar/Factory.php';
61
 * define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKDAYS); // Use Calendar_Month_Weekdays
62
 * // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKS); // Use Calendar_Month_Weeks
63
 * // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH); // Use Calendar_Month
64
 * </code>
65
 * It defaults to building Calendar_Month objects.<br>
66
 * Use the constract CALENDAR_FIRST_DAY_OF_WEEK to control the first day of the week
67
 * for Month or Week objects (e.g. 0 = Sunday, 6 = Saturday)
68
 *
69
 * @category  Date and Time
70
 * @package   Calendar
71
 * @author    Harry Fuecks <[email protected]>
72
 * @author    Lorenzo Alberton <[email protected]>
73
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
74
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
75
 * @link      http://pear.php.net/package/Calendar
76
 * @access protected
77
 */
78
class Factory
79
{
80
    /**
81
     * Creates a calendar object given the type and units
82
     *
83
     * @param string $type class of calendar object to create
84
     * @param int    $y    year
85
     * @param int    $m    month
86
     * @param int    $d    day
87
     * @param int    $h    hour
88
     * @param int    $i    minute
89
     * @param int    $s    second
90
     *
91
     * @return object subclass of Calendar
92
     * @access public
93
     * @static
94
     */
95
    function create($type, $y = 2000, $m = 1, $d = 1, $h = 0, $i = 0, $s = 0)
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...
96
    {
97
        $firstDay = defined('CALENDAR_FIRST_DAY_OF_WEEK') ? CALENDAR_FIRST_DAY_OF_WEEK : 1;
98
        switch ($type) {
99
        case 'Day':
100
            return new Day($y, $m, $d);
101
        case 'Month':
102
            // Set default state for which month type to build
103
            if (!defined('CALENDAR_MONTH_STATE')) {
104
                define('CALENDAR_MONTH_STATE', CALENDAR_USE_MONTH);
105
            }
106
            switch (CALENDAR_MONTH_STATE) {
107
            case CALENDAR_USE_MONTH_WEEKDAYS:
108
                $class = 'Calendar_Month_Weekdays';
109
                break;
110
            case CALENDAR_USE_MONTH_WEEKS:
111
                $class = 'Calendar_Month_Weeks';
112
                break;
113
            case CALENDAR_USE_MONTH:
114
            default:
115
                $class = 'Pear\Calendar\Month';
116
                break;
117
            }
118
            return new $class($y, $m, $firstDay);
119
        case 'Week':
120
            return new Week($y, $m, $d, $firstDay);
121
        case 'Hour':
122
            return new Hour($y, $m, $d, $h);
123
        case 'Minute':
124
            return new Minute($y, $m, $d, $h, $i);
125
        case 'Second':
126
            return new Second($y, $m, $d, $h, $i, $s);
127
        case 'Year':
128
            return new Year($y);
129
        default:
130
            include_once 'PEAR.php';
131
            PEAR::raiseError('Calendar_Factory::create() unrecognised type: '.$type,
0 ignored issues
show
Bug introduced by
The type Pear\Calendar\PEAR 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...
132
                null, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Factory::create()');
0 ignored issues
show
Bug introduced by
The constant Pear\Calendar\PEAR_ERROR_TRIGGER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
133
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type object.
Loading history...
134
        }
135
    }
136
137
    /**
138
     * Creates an instance of a calendar object, given a type and timestamp
139
     *
140
     * @param string $type  type of object to create
141
     * @param mixed  $stamp timestamp (depending on Calendar engine being used)
142
     *
143
     * @return object subclass of Calendar
144
     * @access public
145
     * @static
146
     */
147
    function & createByTimestamp($type, $stamp)
148
    {
149
        $cE  = & Calendar_Engine_Factory::getEngine();
150
        $y   = $cE->stampToYear($stamp);
151
        $m   = $cE->stampToMonth($stamp);
152
        $d   = $cE->stampToDay($stamp);
153
        $h   = $cE->stampToHour($stamp);
154
        $i   = $cE->stampToMinute($stamp);
155
        $s   = $cE->stampToSecond($stamp);
156
        $cal = Factory::create($type, $y, $m, $d, $h, $i, $s);
0 ignored issues
show
Bug Best Practice introduced by
The method Pear\Calendar\Factory::create() 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

156
        /** @scrutinizer ignore-call */ 
157
        $cal = Factory::create($type, $y, $m, $d, $h, $i, $s);
Loading history...
157
        return $cal;
158
    }
159
}
160