Completed
Push — master ( 0022f4...ad9fa0 )
by Michal
04:27
created

IncidentsController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 129
Duplicated Lines 4.65 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.42%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
lcom 1
cbo 6
dl 6
loc 129
ccs 61
cts 66
cp 0.9242
rs 10
c 2
b 0
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 39 4
A json() 0 22 3
A view() 0 18 3
A _sendNotificationMail() 0 19 1
A _getSimilarFields() 6 20 2

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
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
/**
5
 * Incidents controller handling incident creation and rendering.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace App\Controller;
21
22
use Cake\Core\Configure;
23
use Cake\Network\Exception\NotFoundException;
24
use Cake\ORM\TableRegistry;
25
26
/**
27
 * Incidents controller handling incident creation and rendering.
28
 */
29
class IncidentsController extends AppController
30
{
31
    public $uses = array('Incident', 'Notification');
32
33
    public $components = array('Mailer');
34
35 1
    public function create()
36
    {
37
        // Only allow POST requests
38 1
        $this->request->allowMethod(['post']);
39
40 1
        $bugReport = $this->request->input('json_decode', true);
41 1
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
42
43 1
        if (count($result['incidents']) > 0
44 1
            && !in_array(false, $result['incidents'])
45
        ) {
46
            $response = array(
47 1
                'success' => true,
48 1
                'message' => 'Thank you for your submission',
49 1
                'incident_id' => $result['incidents'],        // Return a list of incident ids.
50
            );
51
        } else {
52
            $response = array(
53
                'success' => false,
54
                'message' => 'There was a problem with your submission.',
55
            );
56
        }
57 1
        $this->autoRender = false;
58 1
        $this->response->header(array(
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\Response::header() has been deprecated with message: 3.4.0 Use `withHeader()`, `getHeaderLine()` and `getHeaders()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59 1
            'Content-Type' => 'application/json',
60
            'X-Content-Type-Options' => 'nosniff',
61
        ));
62 1
        $this->response->body(
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\Response::body() has been deprecated with message: 3.4.0 Mutable response methods are deprecated. Use `withBody()` and `getBody()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
63 1
            json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
64
        );
65
66
        // For all the newly added reports,
67
        // send notification emails
68 1
        foreach ($result['reports'] as $report_id) {
69 1
            $this->_sendNotificationMail($report_id);
70
        }
71
72 1
        return $this->response;
73
    }
74
75 1
    public function json($id)
76
    {
77 1
        if (!$id) {
78
            throw new NotFoundException(__('Invalid Incident'));
79
        }
80
81 1
        $this->Incidents->recursive = -1;
82 1
        $incident = $this->Incidents->findById($id)->all()->first();
83 1
        if (!$incident) {
84
            throw new NotFoundException(__('Invalid Incident'));
85
        }
86
87 1
        $incident['full_report'] =
88 1
            json_decode($incident['full_report'], true);
89 1
        $incident['stacktrace'] =
90 1
            json_decode($incident['stacktrace'], true);
91
92 1
        $this->autoRender = false;
93 1
        $this->response->body(json_encode($incident, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\Response::body() has been deprecated with message: 3.4.0 Mutable response methods are deprecated. Use `withBody()` and `getBody()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
95 1
        return $this->response;
96
    }
97
98 1
    public function view($incidentId)
99
    {
100 1
        if (!$incidentId) {
101
            throw new NotFoundException(__('Invalid Incident'));
102
        }
103
104 1
        $incident = $this->Incidents->findById($incidentId)->all()->first();
105 1
        if (!$incident) {
106
            throw new NotFoundException(__('Invalid Incident'));
107
        }
108
109 1
        $incident['full_report'] =
110 1
            json_decode($incident['full_report'], true);
111 1
        $incident['stacktrace'] =
112 1
            json_decode($incident['stacktrace'], true);
113
114 1
        $this->set('incident', $incident);
115 1
    }
116
117 1
    private function _sendNotificationMail($reportId)
118
    {
119 1
        $this->Reports = TableRegistry::get('Reports');
0 ignored issues
show
Documentation introduced by
The property Reports does not exist on object<App\Controller\IncidentsController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120 1
        $report = $this->Reports->findById($reportId)->all()->first()->toArray();
121 1
        $this->Reports->id = $reportId;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Cake\ORM\Table.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
122
123
        $viewVars = array(
124 1
            'report' => $report,
125 1
            'project_name' => Configure::read('GithubRepoPath'),
126 1
            'incidents' => $this->Reports->getIncidents()->toArray(),
127 1
            'incidents_with_description' => $this->Reports->getIncidentsWithDescription(),
128 1
            'incidents_with_stacktrace' => $this->Reports->getIncidentsWithDifferentStacktrace(),
129 1
            'related_reports' => $this->Reports->getRelatedReports(),
130 1
            'status' => $this->Reports->status
131
        );
132 1
        $viewVars = array_merge($viewVars, $this->_getSimilarFields($reportId));
133
134 1
        $this->Mailer->sendReportMail($viewVars);
135 1
    }
136
137 1
    protected function _getSimilarFields($id)
138
    {
139 1
        $this->Reports->id = $id;
140
141
        $viewVars = array(
142 1
            'columns' => TableRegistry::get('Incidents')->summarizableFields
143
        );
144 1
        $relatedEntries = array();
145
146 1 View Code Duplication
        foreach (TableRegistry::get('Incidents')->summarizableFields as $field) {
147
            list($entriesWithCount, $totalEntries) =
148 1
                    $this->Reports->getRelatedByField($field, 25, true);
149 1
            $relatedEntries[$field] = $entriesWithCount;
150 1
            $viewVars["${field}_distinct_count"] = $totalEntries;
151
        }
152
153
        $viewVars['related_entries'] = $relatedEntries;
154
155
        return $viewVars;
156
    }
157
}
158