Conditions | 5 |
Paths | 8 |
Total Lines | 120 |
Code Lines | 84 |
Lines | 23 |
Ratio | 19.17 % |
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 |
||
22 | public function home() |
||
23 | { |
||
24 | if (Configure::read('Analytics.enabled') === true) { |
||
25 | $httpAdapter = new CurlHttpAdapter(); |
||
26 | $client = new Client(Configure::read('Analytics.client_id'), Configure::read('Analytics.private_key'), $httpAdapter); |
||
27 | $service = new Service($client); |
||
28 | |||
29 | $statistics = Cache::remember('statistics', function () use ($service) { |
||
30 | $statistics = new Query(Configure::read('Analytics.profile_id')); |
||
31 | $statistics |
||
32 | ->setStartDate(new \DateTime(Configure::read('Analytics.start_date'))) |
||
33 | ->setEndDate(new \DateTime()) |
||
34 | ->setMetrics([ |
||
35 | 'ga:visits', 'ga:visitors', 'ga:pageviews', 'ga:pageviewsPerVisit', |
||
36 | 'ga:avgtimeOnSite', 'ga:visitBounceRate', 'ga:percentNewVisits' |
||
37 | ]); |
||
38 | |||
39 | return $service->query($statistics); |
||
40 | }, 'analytics'); |
||
41 | |||
42 | View Code Duplication | $browsers = Cache::remember('browsers', function () use ($service) { |
|
|
|||
43 | $browsers = new Query(Configure::read('Analytics.profile_id')); |
||
44 | $browsers |
||
45 | ->setStartDate(new \DateTime(Configure::read('Analytics.start_date'))) |
||
46 | ->setEndDate(new \DateTime()) |
||
47 | ->setDimensions(['ga:browser']) |
||
48 | ->setMetrics(['ga:pageviews']) |
||
49 | ->setSorts(['ga:pageviews']) |
||
50 | ->setFilters(['ga:browser==Chrome,ga:browser==Firefox,ga:browser==Internet Explorer,ga:browser==Safari,ga:browser==Opera']); |
||
51 | |||
52 | return $service->query($browsers); |
||
53 | }, 'analytics'); |
||
54 | |||
55 | $continents = Cache::remember('continents', function () use ($service) { |
||
56 | $continentsRows = new Query(Configure::read('Analytics.profile_id')); |
||
57 | $continentsRows |
||
58 | ->setStartDate(new \DateTime(Configure::read('Analytics.start_date'))) |
||
59 | ->setEndDate(new \DateTime()) |
||
60 | ->setDimensions(['ga:continent']) |
||
61 | ->setMetrics(['ga:visitors']) |
||
62 | ->setSorts(['ga:visitors']) |
||
63 | ->setFilters(['ga:continent==Africa,ga:continent==Americas,ga:continent==Asia,ga:continent==Europe,ga:continent==Oceania']); |
||
64 | |||
65 | $continentsRows = $service->query($continentsRows); |
||
66 | |||
67 | $color = new Color("1abc9c"); |
||
68 | $light = 1; |
||
69 | |||
70 | $continents = []; |
||
71 | |||
72 | foreach (array_reverse($continentsRows->getRows()) as $continentRow) { |
||
73 | $continent = []; |
||
74 | $continent['label'] = $continentRow[0]; |
||
75 | $continent['data'] = $continentRow[1]; |
||
76 | $continent['color'] = '#' . $color->lighten($light); |
||
77 | |||
78 | array_push($continents, $continent); |
||
79 | $light += 10; |
||
80 | } |
||
81 | |||
82 | return $continents; |
||
83 | }, 'analytics'); |
||
84 | |||
85 | View Code Duplication | $graphVisitors = Cache::remember('graphVisitors', function () use ($service) { |
|
86 | $graphVisitors = new Query(Configure::read('Analytics.profile_id')); |
||
87 | $graphVisitors |
||
88 | ->setStartDate(new \DateTime('-7 days')) |
||
89 | ->setEndDate(new \DateTime()) |
||
90 | ->setDimensions(['ga:date']) |
||
91 | ->setMetrics(['ga:visits', 'ga:pageviews']) |
||
92 | ->setSorts(['ga:date']); |
||
93 | |||
94 | return $service->query($graphVisitors); |
||
95 | }, 'analytics'); |
||
96 | |||
97 | $this->set(compact('statistics', 'browsers', 'continents', 'graphVisitors')); |
||
98 | } |
||
99 | |||
100 | $this->loadModel('Users'); |
||
101 | //UsersGraph |
||
102 | $usersGraphCount = $this->Users |
||
103 | ->find('all') |
||
104 | ->select([ |
||
105 | 'date' => 'DATE_FORMAT(created,\'%d-%m-%Y\')', |
||
106 | 'count' => 'COUNT(id)' |
||
107 | ]) |
||
108 | ->group('DATE(created)') |
||
109 | ->order([ |
||
110 | 'date' => 'desc' |
||
111 | ]) |
||
112 | ->where([ |
||
113 | 'UNIX_TIMESTAMP(DATE(created)) >' => (new \DateTime('-8 days'))->getTimestamp() |
||
114 | ]) |
||
115 | ->toArray(); |
||
116 | |||
117 | $usersGraph = []; |
||
118 | |||
119 | //Fill the new array with the date of the 8 past days and give them the value 0. |
||
120 | for ($i = 0; $i < 8; $i++) { |
||
121 | $date = new \DateTime("$i days ago"); |
||
122 | |||
123 | $usersGraph[$date->format('d-m-Y')] = 0; |
||
124 | } |
||
125 | |||
126 | //Foreach value that we got in the database, parse the array by the key date, |
||
127 | //and if the key exist, attribute the new value. |
||
128 | foreach ($usersGraphCount as $user) { |
||
129 | $usersGraph[$user->date] = intval($user->count); |
||
130 | } |
||
131 | $usersGraph = array_reverse($usersGraph); |
||
132 | |||
133 | $stats = $this->_buildStats([ |
||
134 | 'Users' => 'Model.Users.register', |
||
135 | 'Articles' => 'Model.BlogArticles.new', |
||
136 | 'ArticlesLikes' => 'Model.BlogArticlesLikes.new', |
||
137 | 'ArticlesComments' => 'Model.BlogArticlesComments.new' |
||
138 | ]); |
||
139 | |||
140 | $this->set(compact('stats', 'usersGraph')); |
||
141 | } |
||
142 | |||
167 |
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.