Completed
Branch dev (a6be27)
by Marwan
07:52 queued 05:50
created

AmqpAgentException::rethrow()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 11
c 1
b 0
f 0
nc 32
nop 3
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 7
rs 8.8333
1
<?php
2
/**
3
 * @author Marwan Al-Soltany <[email protected]>
4
 * @copyright Marwan Al-Soltany 2020
5
 * For the full copyright and license information, please view
6
 * the LICENSE file that was distributed with this source code.
7
 */
8
9
namespace MAKS\AmqpAgent\Exception;
10
11
use Exception as CoreException;
12
use MAKS\AmqpAgent\Helper\Utility;
13
14
/**
15
 * AMQP Agent base exception class.
16
 * @since 1.0.0
17
 */
18
class AmqpAgentException extends CoreException
19
{
20
    /**
21
     * Redefine the exception so message is not an optional parameter.
22
     * @param string $message
23
     * @param int $code
24
     * @param CoreException $previous
25
     */
26 22
    public function __construct(string $message, int $code = 0, CoreException $previous = null)
27
    {
28 22
        parent::__construct($message, $code, $previous);
29 22
    }
30
31
    /**
32
     * String representation of the object.
33
     * @return string
34
     */
35 1
    public function __toString()
36
    {
37 1
        return static::class . ": [{$this->code}]: {$this->message}\n{$this->getTraceAsString()}\n";
38
    }
39
40
    /**
41
     * Rethrows an exception with an additional message.
42
     * @param CoreException $exception The exception to rethrow.
43
     * @param string|null $message An additional message to add to the wrapping exception before the message of the passed exception.
44
     * @param string|bool $wrap Wether to throw the exception using the passed class (FQN), in the same exception type (true), or wrap it with the class this method was called on (false). Any other value will be translated to false.
45
     * @return void
46
     */
47 5
    public static function rethrow(CoreException $exception, ?string $message = null, $wrap = false): void
48
    {
49 5
        if (null === $message) {
50 4
            $trace = Utility::backtrace(['file', 'line', 'class', 'function']);
51 4
            $prefix = (isset($trace['class']) ? "{$trace['class']}::" : "{$trace['file']}({$trace['line']}): ");
52 4
            $suffix = "{$trace['function']}() failed!";
53 4
            $message = 'Rethrown Exception: ' . $prefix . $suffix . ' ';
54
        } else {
55 1
            $message = strlen($message) ? $message . ' ' : $message;
56
        }
57
58 5
        $error = $wrap === true
59 1
            ? get_class($exception)
60 5
            : (class_exists($wrap) && is_subclass_of($wrap, 'Exception') ? $wrap : static::class);
0 ignored issues
show
Bug introduced by
It seems like $wrap can also be of type false; however, parameter $class_name of class_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            : (class_exists(/** @scrutinizer ignore-type */ $wrap) && is_subclass_of($wrap, 'Exception') ? $wrap : static::class);
Loading history...
61
62 5
        throw new $error($message . (string)$exception->getMessage(), (int)$exception->getCode(), $exception);
63
    }
64
65
    /**
66
     * Rethrows an exception with an additional message.
67
     * @deprecated 1.2.0 Use `self::rethrow()` instead.
68
     * @param CoreException $exception The exception to rethrow.
69
     * @param string|null $message An additional message to add to the wrapping exception before the message of the passed exception.
70
     * @param string|bool $wrap Wether to throw the exception using the passed class (FQN), in the same exception type (true), or wrap it with the class this method was called on (false). Any other value will be translated to false.
71
     * @return void
72
     */
73 1
    public static function rethrowException(): void
74
    {
75 1
        static::rethrow(...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $exception of MAKS\AmqpAgent\Exception...entException::rethrow() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        static::rethrow(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
76
    }
77
}
78