NotFoundException   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 7.48 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 8
loc 107
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A notFound() 8 8 2
A nameOfMessage() 0 4 1
A nameOfCommand() 0 4 1
A handlerMethodNameForObject() 0 4 1
A nameOfQuery() 0 4 1
A handlerFor() 0 7 1
A middlewareOfType() 0 7 1
A methodForObject() 0 4 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
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Core\Bus\Exception;
12
13
use RuntimeException;
14
use Exception;
15
16
/**
17
 * NotFoundException class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class NotFoundException extends RuntimeException
22
{
23
    /**
24
     * Creates an exception for a not found message name, method name or handler for a given message.
25
     *
26
     * @param string         $type
27
     * @param mixed          $message
28
     * @param int            $code
29
     * @param Exception|null $cause
30
     *
31
     * @return NotFoundException
32
     */
33 View Code Duplication
    protected static function notFound($type, $message, $code = 0, Exception $cause = null)
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...
34
    {
35
        return new static(sprintf(
36
            'Not found a %s for a given object of type %s',
37
            $type,
38
            is_object($message) ? get_class($message) : gettype($message)
39
        ), $code, $cause);
40
    }
41
42
    /**
43
     * @param mixed          $object
44
     * @param Exception|null $cause
45
     *
46
     * @return NotFoundException
47
     */
48
    public static function nameOfMessage($object, Exception $cause = null)
49
    {
50
        return self::notFound('name of message', $object, 1, $cause);
51
    }
52
53
    /**
54
     * @param mixed          $object
55
     * @param Exception|null $cause
56
     *
57
     * @return NotFoundException
58
     */
59
    public static function nameOfCommand($object, Exception $cause = null)
60
    {
61
        return self::notFound('name of command', $object, 2, $cause);
62
    }
63
64
    /**
65
     * @param mixed          $object
66
     * @param Exception|null $cause
67
     *
68
     * @return NotFoundException
69
     */
70
    public static function handlerMethodNameForObject($object, Exception $cause = null)
71
    {
72
        return self::notFound('handler method name', $object, 3, $cause);
73
    }
74
75
    /**
76
     * @param mixed          $object
77
     * @param Exception|null $cause
78
     *
79
     * @return NotFoundException
80
     */
81
    public static function nameOfQuery($object, Exception $cause = null)
82
    {
83
        return self::notFound('name of query', $object, 4, $cause);
84
    }
85
86
    /**
87
     * @param string         $messageName
88
     * @param Exception|null $cause
89
     *
90
     * @return NotFoundException
91
     */
92
    public static function handlerFor($messageName, Exception $cause = null)
93
    {
94
        return new static(sprintf(
95
            'Not found a handler for a given message named %s',
96
            $messageName
97
        ), 5, $cause);
98
    }
99
100
    /**
101
     * Creates an exception for a not found middleware.
102
     *
103
     * @param string         $type
104
     * @param Exception|null $cause
105
     *
106
     * @return NotFoundException
107
     */
108
    public static function middlewareOfType($type, Exception $cause = null)
109
    {
110
        return new static(sprintf(
111
            'Not found a middleware of type %s',
112
            $type
113
        ), 6, $cause);
114
    }
115
116
    /**
117
     * @param mixed          $object
118
     * @param string         $method
119
     * @param Exception|null $cause
120
     *
121
     * @return NotFoundException
122
     */
123
    public static function methodForObject($object, $method, Exception $cause = null)
124
    {
125
        return self::notFound('method with name `'.$method.'`', $object, 7, $cause);
126
    }
127
}
128