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

AbstractSmartIdQuery   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 19.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 12
loc 61
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getFields() 0 9 1
A getValidationConstraints() 12 12 1
A getMethod() 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
namespace Isign\Login;
3
4
use Isign\QueryInterface;
5
use Isign\Validator\Constraints\Code;
6
use Isign\Validator\Constraints\Phone;
7
use Symfony\Component\Validator\Constraints as Assert;
8
9
/**
10
 * Absctract smartId query.
11
 */
12
abstract class AbstractSmartIdQuery implements QueryInterface
13
{
14
    /** @var string user personal code */
15
    private $code;
16
17
    /** @var string Country related to personal code. Example EE */
18
    private $country;
19
20
    /**
21
     * @param string $code
22
     * @param string $country
23
     * @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...
24
     */
25 5
    public function __construct(
26
        $code,
27
        $country
28
    ) {
29 5
        $this->code = $code;
30 5
        $this->country = $country;
31 5
    }
32
33
    /**
34
     * Field and values association used in query
35
     * @return array
36
     */
37 1
    public function getFields()
38
    {
39
        $return = [
40 1
            'code' => $this->code,
41 1
            'country' => $this->country,
42 1
        ];
43
44 1
        return $return;
45
    }
46
47
    /**
48
     * Validation constraints for request data validation
49
     * @return Assert\Collection
50
     */
51 1 View Code Duplication
    public function getValidationConstraints()
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...
52
    {
53 1
        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...
54 1
            'code' => new Assert\Required([
55 1
                new Assert\NotBlank(),
56 1
                new Code()
57 1
            ]),
58 1
            'country' => new Assert\Required([
59 1
                new Assert\NotBlank(),
60 1
            ]),
61 1
        ]);
62
    }
63
64
    /**
65
     * HTTP method to use
66
     * @return string
67
     */
68 1
    public function getMethod()
69
    {
70 1
        return QueryInterface::POST;
71
    }
72
}
73