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 ( 08bdaa...11681a )
by Carsten
09:28 queued 11s
created

EmailExceptionMiddleware   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 186
Duplicated Lines 24.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 55.77%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 46
loc 186
ccs 29
cts 52
cp 0.5577
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getMailer() 0 10 2
A getMessage() 0 10 2
A __construct() 0 10 1
A process() 18 18 3
A __invoke() 21 21 3
A handleThrowable() 0 24 1
A render() 0 5 1

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
namespace Germania\Middleware;
4
5
use Germania\Middleware\Exceptions\FactoryException;
6
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
13
class EmailExceptionMiddleware implements MiddlewareInterface
14
{
15
    /**
16
     * @var callable
17
     */
18
    public $mailer_factory;
19
20
21
    /**
22
     * @var callable
23
     */
24
    public $message_factory;
25
26
27
    /**
28
     * @var string
29
     */
30
    public $app_name;
31
32
33
    /**
34
     * @var string
35
     */
36
    public $include_file;
37
38
39
    /**
40
     * @param string   $app_name        Name of application (used in email subject)
41
     * @param callable $mailer_factory  Factory that returns Swift_Mailer instance
42
     * @param callable $message_factory Factory that returns Swift_Message instance
43
     */
44 12
    public function __construct($app_name, callable $mailer_factory, callable $message_factory)
45
    {
46 12
        $this->app_name = $app_name;
47
48 12
        $this->mailer_factory = $mailer_factory;
49 12
        $this->message_factory = $message_factory;
50
51 12
        $include_path = realpath(__DIR__.'/../includes');
52 12
        $this->include_file = $include_path.'/exception.php';
53 12
    }
54
55
56
57
    
58
    /**
59
     * PSR-15 Single Pass
60
     *
61
     * @param  ServerRequestInterface  $request Server reuest instance
62
     * @param  RequestHandlerInterface $handler Request handler
63
     * @return ResponseInterface
64
     */
65 4 View Code Duplication
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
67
        try {
68 4
            $response = $handler->handle($request);
69 4
            return $response;
70
        }
71
        // Executed only in PHP 7, will not match in PHP 5.x
72
        catch (\Throwable $e) {
73
            $this->handleThrowable($e);
74
            throw $e;
75
        }
76
77
        // Executed only in PHP 5.x, will not be reached in PHP 7
78
        catch (\Exception $e) {
79
            $this->handleThrowable($e);
80
            throw $e;
81
        }
82
    }
83
84
    
85
86
    /**
87
     * PSR-7 Double Pass
88
     *
89
     * Wrap $next callable in a try-catch block.
90
     * When an exception is caught, an email will be sent, and the execption will be re-thrown.
91
     *
92
     * @param RequestInterface   $request
93
     * @param ResponseInterface  $response
94
     * @param callable           $next
95
     *
96
     * @return ResponseInterface
97
     */
98 4 View Code Duplication
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
99
    {
100
        try {
101
            // Try to do business as usual...
102 4
            return $next($request, $response);
103
        }
104
105
        // Executed only in PHP 7, will not match in PHP 5.x
106 4
        catch (\Throwable $e) {
107 4
            $this->handleThrowable($e);
108
            throw $e;
109
        }
110
111
        // Executed only in PHP 5.x, will not be reached in PHP 7
112
        catch (\Exception $e) {
113
            $this->handleThrowable($e);
114
            throw $e;
115
        }
116
117
        // Anything else NOT caught here will bubble up.
118
    }
119
120
121
    /**
122
     * @param  \Exception|\Throwable $e
123
     */
124 4
    public function handleThrowable($e)
125
    {
126
        // Render email body, prepare some things
127 4
        $text    = $this->render($e, [
0 ignored issues
show
Documentation introduced by
$e is of type object<Throwable>, but the function expects a object<Germania\Middleware\Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128 3
            'package' => array(
129 1
                'title'       => 'Germania KG · Middleware',
130
                'name'       => 'germania-kg/middleware',
131
                'packagist'  => 'https://packagist.org/packages/germania-kg/middleware',
132
                'middleware' => 'EmailExceptionMiddleware'
133
            )
134
        ]);
135 4
        $format  = 'text/html';
136 4
        $subject = sprintf("[%s] Exception %s", $this->app_name, get_class($e));
137
138
        // Create email message instance
139 4
        $message = $this->getMessage();
140
        $message->setContentType($format)
141
                ->setSubject($subject)
142
                ->setBody($text);
143
144
        // Create emailer instance + send
145
        $mailer = $this->getMailer();
146
        $mailer->send($message);
147
    }
148
149
    /**
150
     * Creates the email body.
151
     *
152
     * In this class, an include file creates a basic information table.
153
     * Override this method to use your own method.
154
     *
155
     * @param Exception $e
156
     *
157
     * @return string Exception explanation
158
     */
159 4
    public function render($e, array $context = array())
160
    {
161 4
        extract($context);
162 4
        return require $this->include_file;
163
    }
164
165
166
    /**
167
     * @return Swift_Mailer
168
     *
169
     * @throws FactoryException
170
     */
171
    public function getMailer()
172
    {
173
        $mailer_factory = $this->mailer_factory;
174
        $mailer = $mailer_factory();
175
        if (!$mailer instanceof \Swift_Mailer) {
0 ignored issues
show
Bug introduced by
The class Swift_Mailer does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
176
            throw new FactoryException('Mailer factory must return Swift_Mailer instance.');
177
        }
178
179
        return $mailer;
180
    }
181
182
183
    /**
184
     * @return Swift_Message
185
     *
186
     * @throws FactoryException
187
     */
188 4
    public function getMessage()
189
    {
190 4
        $message_factory = $this->message_factory;
191 4
        $message = $message_factory();
192 4
        if (!$message instanceof \Swift_Message) {
0 ignored issues
show
Bug introduced by
The class Swift_Message does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
193 4
            throw new FactoryException('Message factory must return Swift_Message instance.');
194
        }
195
196
        return $message;
197
    }
198
}
199