DeviceEntityRequest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
lcom 0
cbo 4
dl 20
loc 20
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getJsonProperties() 9 9 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
 * Created by PhpStorm.
4
 * User: famoser
5
 * Date: 04.11.2016
6
 * Time: 19:17
7
 */
8
9
namespace Famoser\SyncApi\Models\Communication\Request;
10
11
use Famoser\SyncApi\Framework\Json\Models\ArrayProperty;
12
use Famoser\SyncApi\Framework\Json\Models\Base\AbstractJsonProperty;
13
use Famoser\SyncApi\Framework\Json\Models\Base\AbstractJsonValueProperty;
14
use Famoser\SyncApi\Framework\Json\Models\ObjectProperty;
15
use Famoser\SyncApi\Models\Communication\Entities\DeviceCommunicationEntity;
16
use Famoser\SyncApi\Models\Communication\Entities\SyncCommunicationEntity;
17
use Famoser\SyncApi\Models\Communication\Request\Base\BaseRequest;
18
19
/**
20
 * the entities to be synced
21
 * @package Famoser\SyncApi\Models\Communication\Request
22
 */
23 View Code Duplication
class DeviceEntityRequest extends BaseRequest
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...
24
{
25
    /* @var DeviceCommunicationEntity[] $DeviceEntities */
26
    public $DeviceEntities;
0 ignored issues
show
Coding Style introduced by
$DeviceEntities does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
28
    /**
29
     * gets the json properties needed to deserialize
30
     *
31
     * @return AbstractJsonValueProperty[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,\Famoser\Sy...Property|ArrayProperty>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
     */
33 3
    public function getJsonProperties()
34
    {
35 3
        $arr = parent::getJsonProperties();
36 3
        $arr['DeviceEntities'] = new ArrayProperty(
37 3
            'DeviceEntities',
38 3
            new ObjectProperty('DeviceEntities', new DeviceCommunicationEntity())
39
        );
40 3
        return $arr;
0 ignored issues
show
Best Practice introduced by
The expression return $arr; seems to be an array, but some of its elements' types (Famoser\SyncApi\Framewor...on\Models\ArrayProperty) are incompatible with the return type of the parent method Famoser\SyncApi\Models\C...uest::getJsonProperties of type array<string,Famoser\Syn...on\Models\TextProperty>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
41
    }
42
}
43