GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f67dca...fa525c )
by
unknown
10:06 queued 03:14
created

JsonStore   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 161
Duplicated Lines 19.88 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 22
c 1
b 0
f 1
lcom 1
cbo 2
dl 32
loc 161
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 15 3
D getEventData() 22 49 16
B storeEvent() 0 40 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @file
5
 * Class JsonStore
6
 */
7
8
namespace Roomify\Bat\Store;
9
10
use Roomify\Bat\Event\Event;
11
use Roomify\Bat\Store\Store;
12
13
/**
14
 * This is a JSON implementation of the Store.
15
 *
16
 */
17
class JsonStore extends Store {
18
19
  // There are two types of stores - for event ids and status
20
  const BAT_EVENT = 'event';
21
  const BAT_STATE = 'state';
22
23
  /**
24
   * The file that holds day data.
25
   * @var
26
   */
27
  public $day_file;
28
29
  /**
30
   * The file that holds hour data.
31
   * @var
32
   */
33
  public $hour_file;
34
35
  /**
36
   * The file that holds minute data.
37
   * @var
38
   */
39
  public $minute_file;
40
41
  /**
42
   * The event type we are dealing with.
43
   * @var
44
   */
45
  public $event_type;
46
47
48
  /**
49
   * JsonStore constructor.
50
   *
51
   * Provided with the event type it will determine the appropriate file names to
52
   * store data in. This assumes standard behaviour from Bat_Event
53
   * @param $event_type
54
   * @param string $event_data
55
   */
56
  public function __construct($event_type, $event_data = 'state') {
57
    $this->event_type = $event_type;
58
59 View Code Duplication
    if ($event_data == JsonStore::BAT_STATE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
      $this->day_file = 'build/' . $event_type . '_day_' . JsonStore::BAT_STATE . '.json';
61
      $this->hour_file = 'build/' . $event_type . '_hour_' . JsonStore::BAT_STATE . '.json';
62
      $this->minute_file = 'build/' . $event_type . '_minute_' . JsonStore::BAT_STATE . '.json';
63
    }
64
65 View Code Duplication
    if ($event_data == JsonStore::BAT_EVENT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
      $this->day_file = 'build/' . $event_type . '_day_' . JsonStore::BAT_EVENT . '.json';
67
      $this->hour_file = 'build/' . $event_type . '_hour_' . JsonStore::BAT_EVENT . '.json';
68
      $this->minute_file = 'build/' . $event_type . '_minute_' . JsonStore::BAT_EVENT . '.json';
69
    }
70
  }
71
72
  /**
73
   *
74
   * @param \DateTime $start_date
75
   * @param \DateTime $end_date
76
   * @param $unit_ids
77
   *
78
   * @return array
79
   */
80
  public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) {
81
82
    $dayfile = (array)json_decode(file_get_contents($this->day_file));
83
    $hourfile = (array)json_decode(file_get_contents($this->hour_file));
84
    $minutefile = (array)json_decode(file_get_contents($this->minute_file));
0 ignored issues
show
Unused Code introduced by
$minutefile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
86
    $events = array();
87
88
    // Cycle through day results and setup an event array
89
    foreach ($dayfile as $unit_id => $row) {
90
      foreach ($row as $year => $row2) {
91
        foreach ($row2 as $month => $row3) {
92
          foreach ($row3 as $day => $value) {
93
            $events[$unit_id][Event::BAT_DAY][$year][$month][$day] = $value;
94
          }
95
        }
96
      }
97
    }
98
99
    // With the day events taken care off let's cycle through hours
100 View Code Duplication
    foreach ($hourfile as $unit_id => $row) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
101
      foreach ($row as $year => $row2) {
102
        foreach ($row2 as $month => $row3) {
103
          foreach ($row3 as $day => $row4) {
104
            foreach ($row4 as $hour => $value) {
105
              $events[$unit_id][Event::BAT_HOUR][$year][$month][$day][$hour] = $value;
106
            }
107
          }
108
        }
109
      }
110
    }
111
112
    // With the hour events taken care off let's cycle through minutes
113
    foreach ($hourfile as $unit_id => $row) {
114 View Code Duplication
      foreach ($row as $year => $row2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
115
        foreach ($row2 as $month => $row3) {
116
          foreach ($row3 as $day => $row4) {
117
            foreach ($row4 as $hour => $row5) {
118
              foreach ((array)$row5 as $min => $value) {
119
                $events[$unit_id][Event::BAT_MINUTE][$year][$month][$day][$hour][$min] = $value;
120
              }
121
            }
122
          }
123
        }
124
      }
125
    }
126
127
    return $events;
128
  }
129
130
  /**
131
   * @param \Roomify\Bat\Event\Event $event
132
   * @param $granularity
133
   *
134
   * @return bool
135
   */
136
  public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) {
137
    $stored = TRUE;
138
139
    $dayfile_content = (array)json_decode(file_get_contents($this->day_file));
140
    $hourfile_content = (array)json_decode(file_get_contents($this->hour_file));
141
    $minutefile_content = (array)json_decode(file_get_contents($this->minute_file));
142
143
    try {
144
      // Itemize an event so we can save it
145
      $itemized = $event->itemizeEvent($granularity);
146
147
      $dayfile_content[$event->unit_id] = $itemized[Event::BAT_DAY];
0 ignored issues
show
Bug introduced by
The property unit_id cannot be accessed from this context as it is declared protected in class Roomify\Bat\Event\AbstractEvent.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
148
149
      // Write days
150
      $dayfile = fopen($this->day_file, 'w');
151
      fwrite($dayfile, json_encode($dayfile_content));
152
      fclose($dayfile);
153
154
      if ($granularity == Event::BAT_HOURLY) {
155
        $hourfile_content[$event->unit_id] = $itemized[Event::BAT_HOUR];
0 ignored issues
show
Bug introduced by
The property unit_id cannot be accessed from this context as it is declared protected in class Roomify\Bat\Event\AbstractEvent.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
156
157
        // Write Hours
158
        $hourfile = fopen($this->hour_file, 'w');
159
        fwrite($hourfile, json_encode($hourfile_content));
160
        fclose($hourfile);
161
162
        $minutefile_content[$event->unit_id] = $itemized[Event::BAT_MINUTE];
0 ignored issues
show
Bug introduced by
The property unit_id cannot be accessed from this context as it is declared protected in class Roomify\Bat\Event\AbstractEvent.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
163
164
        // Write minutes
165
        $minutefile = fopen($this->minute_file, 'w');
166
        fwrite($minutefile, json_encode($minutefile_content));
167
        fclose($minutefile);
168
      }
169
    }
170
    catch (\Exception $e) {
171
      $saved = FALSE;
0 ignored issues
show
Unused Code introduced by
$saved is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
172
    }
173
174
    return $stored;
175
  }
176
177
}
178