IntegerMessage   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 72
loc 72
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 2
A getMessageId() 4 4 1
A getMessage() 4 4 1
A __toString() 4 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
/**
4
 * AppserverIo\Messaging\IntegerMessage
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/messaging
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Messaging;
22
23
use Rhumsaa\Uuid\Uuid;
24
25
/**
26
 * The implementation for sending a message containing data encapsulated in a Integer.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/messaging
32
 * @link      http://www.appserver.io
33
 */
34 View Code Duplication
class IntegerMessage extends AbstractMessage
0 ignored issues
show
Duplication introduced by
This class 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...
35
{
36
37
    /**
38
     * The message id as hash value.
39
     *
40
     * @var string
41
     */
42
    protected $messageId = null;
43
44
    /**
45
     * The message itself.
46
     *
47
     * @var integer
48
     */
49
    protected $message = null;
50
51
    /**
52
     * Initializes the message with the Integer
53
     * to send to the queue.
54
     *
55
     * @param integer $message The integer with the data to send
56
     *
57
     * @throws \Exception Is thrown if the passed message is not a valid integer value
58
     */
59
    public function __construct($message)
60
    {
61
62
        // call parent constructor
63
        parent::__construct();
64
65
        // check if we've an integer passed
66
        if (is_integer($message) === false) {
67
            throw new \Exception(sprintf('Message \'%s\' is not a valid integer', $message));
68
        }
69
70
        // initialize the integer sent with the message
71
        $this->message = $message;
72
        // initialize the message id
73
        $this->messageId = Uuid::uuid4()->__toString();
74
    }
75
76
    /**
77
     * Returns the unique message-ID.
78
     *
79
     * @return string The unique message-ID
80
     */
81
    public function getMessageId()
82
    {
83
        return $this->messageId;
84
    }
85
86
    /**
87
     * The message itself.
88
     *
89
     * @return integer The message itself
90
     */
91
    public function getMessage()
92
    {
93
        return $this->message;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->message; (integer) is incompatible with the return type declared by the interface AppserverIo\Psr\Pms\MessageInterface::getMessage of type object.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
94
    }
95
96
    /**
97
     * Returns the message as string.
98
     *
99
     * @return string The message as string
100
     */
101
    public function __toString()
102
    {
103
        return "'" . $this->message . "'";
104
    }
105
}
106