Conditions | 3 |
Paths | 4 |
Total Lines | 69 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
57 | public function testScheduledExecutionInterval() |
||
58 | { |
||
59 | $test = new TestScheduledDataObject; |
||
60 | |||
61 | $test->Title = 'Test execute at custom interval sizes'; |
||
62 | $test->write(); |
||
63 | |||
64 | $test->FirstExecution = '1980-09-22 09:15:00'; |
||
65 | $test->ExecuteEvery = 'Minute'; |
||
66 | |||
67 | $test->write(); |
||
68 | |||
69 | // should now have a job |
||
70 | $this->assertTrue($test->ScheduledJobID > 0, 'Scheduled job has not been created'); |
||
71 | // should default the ExecuteInterval |
||
72 | $this->assertEquals(1, $test->ExecuteInterval, 'ExecuteInterval did not default to 1'); |
||
73 | |||
74 | // should check the interval in code also |
||
75 | $test->ExecuteInterval = 0; |
||
76 | $test->write(); |
||
77 | |||
78 | $jobId = $test->ScheduledJobID; |
||
79 | |||
80 | // execute said job |
||
81 | $job = $test->ScheduledJob(); |
||
82 | $job->execute(); |
||
83 | |||
84 | // reload the test object and make sure its job has now changed |
||
85 | $test = DataObject::get_by_id(TestScheduledDataObject::class, $test->ID); |
||
86 | |||
87 | $this->assertNotEquals($test->ScheduledJobID, $jobId); |
||
88 | $this->assertEquals('EXECUTED', $test->Message); |
||
89 | |||
90 | $job = $test->ScheduledJob(); |
||
91 | |||
92 | // should reschedule in 1 minute time |
||
93 | $expectedMinutes = date('i', time()); |
||
94 | $expectedMinutes = intval($expectedMinutes, 10); |
||
95 | if ($expectedMinutes + 1 > 59) { // Wrap around the hour |
||
96 | $expectedMinutes = $expectedMinutes - 59; |
||
97 | } |
||
98 | $scheduledMinutes = substr($job->StartAfter, 14, 2); |
||
99 | $scheduledMinutes = intval($scheduledMinutes, 10); |
||
100 | |||
101 | $this->assertEquals($expectedMinutes + 1, $scheduledMinutes, 'Did not reschedule 1 minute later'); |
||
102 | |||
103 | // test a custom interval of 3 minutes |
||
104 | |||
105 | $test->ExecuteInterval = 3; |
||
106 | $test->write(); |
||
107 | |||
108 | $job = $test->ScheduledJob(); |
||
109 | $job->execute(); |
||
110 | |||
111 | $test = DataObject::get_by_id(TestScheduledDataObject::class, $test->ID); |
||
112 | |||
113 | $job = $test->ScheduledJob(); |
||
114 | |||
115 | // should reschedule in 3 minutes time |
||
116 | $expectedMinutes = date('i', time()); |
||
117 | $expectedMinutes = intval($expectedMinutes, 10); |
||
118 | if ($expectedMinutes + 3 > 59) { |
||
119 | $expectedMinutes = $expectedMinutes - 59; |
||
120 | } |
||
121 | $scheduledMinutes = substr($job->StartAfter, 14, 2); |
||
122 | $scheduledMinutes = intval($scheduledMinutes, 10); |
||
123 | |||
124 | $this->assertEquals($expectedMinutes + 3, $scheduledMinutes, 'Did not reschedule 3 minutes later'); |
||
125 | } |
||
126 | } |
||
127 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.