Completed
Pull Request — develop (#9)
by
unknown
03:41
created

AbstractStatus   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 56
loc 56
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getToken() 4 4 1
A getFields() 6 6 1
A getValidationConstraints() 8 8 1
A getMethod() 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
namespace Isign\Login;
3
4
use Isign\QueryInterface;
5
use Isign\TokenizedQueryInterface;
6
use Symfony\Component\Validator\Constraints as Assert;
7
8
/**
9
 * Login status for MobileId, SmartId, etc.
10
 */
11 View Code Duplication
abstract class AbstractStatus implements TokenizedQueryInterface
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...
12
{
13
    /** @var string unique resource identifier, received from mobile login request */
14
    protected $token;
15
16
    /**
17
     * @param string $token unique resource identifier, received from mobile login request
18
     * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
19
     */
20 28
    public function __construct($token)
21
    {
22 28
        $this->token = $token;
23 28
    }
24
25
    /**
26
     * Get token
27
     * @return string
28
     */
29 4
    public function getToken()
30
    {
31 4
        return $this->token;
32
    }
33
34
    /**
35
     * Get fields
36
     * @return array
37
     */
38 4
    public function getFields()
39
    {
40
        return [
41 4
            'token' => $this->token,
42 4
        ];
43
    }
44
45
    /**
46
     * Validation constraints for request data validation
47
     * @return Assert\Collection
48
     */
49 4
    public function getValidationConstraints()
50
    {
51 4
        return new Assert\Collection([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...traints\NotBlank())))); (Symfony\Component\Validator\Constraints\Collection) is incompatible with the return type declared by the interface Isign\QueryInterface::getValidationConstraints of type Isign\Symfony\Component\...\Constraints\Collection.

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...
52 4
            'token' => new Assert\Required(
53 4
                [new Assert\NotBlank()]
54 4
            )
55 4
        ]);
56
    }
57
58
    /**
59
     * HTTP method to use
60
     * @return string
61
     */
62 4
    public function getMethod()
63
    {
64 4
        return QueryInterface::GET;
65
    }
66
}
67