Conditions | 7 |
Paths | 15 |
Total Lines | 94 |
Code Lines | 66 |
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 |
||
84 | protected function loadCalendars() |
||
85 | { |
||
86 | $days = $this->getDatePeriod(); |
||
87 | $events = []; |
||
88 | foreach ($days as $day) { |
||
89 | /** @var \DateTime $day */ |
||
90 | if (!$this->isWeekend($day)) { |
||
91 | //work day |
||
92 | $event = new CalendarEvent(); |
||
93 | $event->setTitle('Work Reminder'); |
||
94 | $day->setTime(8, 0, 0); |
||
95 | $event->setStart(clone $day); |
||
96 | $day->setTime(18, 0, 0); |
||
97 | $event->setEnd(clone $day); |
||
98 | $event->setAllDay(true); |
||
99 | |||
100 | $events['workday'][] = $event; |
||
101 | |||
102 | //call |
||
103 | $event = new CalendarEvent(); |
||
104 | $event->setTitle('Client Call'); |
||
105 | $day->setTime(11, 0, 0); |
||
106 | $event->setStart(clone $day); |
||
107 | $day->setTime(12, 0, 0); |
||
108 | $event->setEnd(clone $day); |
||
109 | $event->setAllDay(false); |
||
110 | |||
111 | $events['call'][] = $event; |
||
112 | |||
113 | //meeting |
||
114 | $event = new CalendarEvent(); |
||
115 | $event->setTitle('Meeting'); |
||
116 | $day->setTime(16, 0, 0); |
||
117 | $event->setStart(clone $day); |
||
118 | $day->setTime(18, 0, 0); |
||
119 | $event->setEnd(clone $day); |
||
120 | $event->setAllDay(false); |
||
121 | |||
122 | $events['meeting'][] = $event; |
||
123 | |||
124 | //lunch |
||
125 | $event = new CalendarEvent(); |
||
126 | $event->setTitle('Lunch'); |
||
127 | $day->setTime(12, 0, 0); |
||
128 | $event->setStart(clone $day); |
||
129 | $day->setTime(12, 30, 0); |
||
130 | $event->setEnd(clone $day); |
||
131 | $event->setAllDay(false); |
||
132 | |||
133 | $events['lunch'][] = $event; |
||
134 | |||
135 | //business trip |
||
136 | $event = new CalendarEvent(); |
||
137 | $event->setTitle('Business trip'); |
||
138 | $day->setTime(0, 0, 0); |
||
139 | $event->setStart(clone $day); |
||
140 | $day->setTime(0, 0, 0); |
||
141 | $day->add(\DateInterval::createFromDateString('+3 days')); |
||
142 | $event->setEnd(clone $day); |
||
143 | $event->setAllDay(true); |
||
144 | |||
145 | $events['b_trip'][] = $event; |
||
146 | } else { |
||
147 | $event = new CalendarEvent(); |
||
148 | $event->setTitle('Weekend'); |
||
149 | $day->setTime(8, 0, 0); |
||
150 | $event->setStart(clone $day); |
||
151 | $day->setTime(18, 0, 0); |
||
152 | $event->setEnd(clone $day); |
||
153 | $event->setAllDay(true); |
||
154 | |||
155 | $events['weekend'][] = $event; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | foreach ($this->users as $user) { |
||
160 | //get default calendar, each user has default calendar after creation |
||
161 | $calendar = $this->calendar->findDefaultCalendar($user->getId(), $this->organization->getId()); |
||
162 | $this->setSecurityContext($calendar->getOwner()); |
||
163 | foreach ($events as $typeEvents) { |
||
164 | if (mt_rand(0, 1)) { |
||
165 | foreach ($typeEvents as $typeEvent) { |
||
166 | $calendar->addEvent(clone $typeEvent); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | $this->em->persist($calendar); |
||
172 | } |
||
173 | |||
174 | $this->em->flush(); |
||
175 | $this->em->clear('Oro\Bundle\CalendarBundle\Entity\CalendarEvent'); |
||
176 | $this->em->clear('Oro\Bundle\CalendarBundle\Entity\Calendar'); |
||
177 | } |
||
178 | |||
317 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: