ProfileCertain   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRessourceName() 0 3 1
A getMandatoryFields() 0 4 1
A getProfileByEmail() 0 18 4
A getProfileCertainReturnByEmail() 0 4 1
A getProfiles() 0 9 2
1
<?php
2
namespace Wabel\CertainAPI\Ressources;
3
4
use Wabel\CertainAPI\Interfaces\CertainRessourceInterface;
5
use Wabel\CertainAPI\CertainRessourceAbstract;
6
use Wabel\CertainAPI\Exceptions\RessourceException;
7
/**
8
 * ProfileCertain about the Profile entity
9
 *
10
 * @author rbergina
11
 */
12
class ProfileCertain extends CertainRessourceAbstract implements CertainRessourceInterface
13
{
14
    public function getRessourceName(){
15
        return 'Profile';
16
    }
17
    public function getMandatoryFields()
18
    {
19
        return array();
20
    }
21
22
    /**
23
     * Return the Profile object
24
     * @param string $email
25
     * @return ProfileObj
26
     * @throws RessourceException
27
     */
28
    public function getProfileByEmail($email)
29
    {
30
        $resultCertain =  $this->getProfileCertainReturnByEmail($email);
31
        if($resultCertain->isSuccessFul()){
32
            $resultCertainProfile = $resultCertain->getResults();
33
            if($resultCertainProfile->size == 1){
34
                return $resultCertainProfile->profiles[0];
35
36
            } elseif($resultCertainProfile->size > 1){
37
                throw new RessourceException('Duplicate entries');
38
            } else{
39
                    return $resultCertain;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $resultCertain; (Wabel\CertainAPI\Ressources\ProfileCertain) is incompatible with the return type documented by Wabel\CertainAPI\Ressour...tain::getProfileByEmail of type Wabel\CertainAPI\Ressources\ProfileObj.

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...
40
            }
41
        } else{
42
            return null;
43
        }
44
45
    }
46
47
48
    /**
49
     * Return with all the result from certain.
50
     * @param string $email
51
     * @return ProfileCertain
52
     */
53
    public function getProfileCertainReturnByEmail($email)
54
    {
55
        return $this->get(null, ['email'=> $email]);
56
    }
57
58
59
    /**
60
     * Return with all the result from certain.
61
     * @return ProfileCertain[]
62
     */
63
    public function getProfiles($params = [])
64
    {
65
        $request=  $this->get(null,$params);
66
        if($request->isSuccessFul()){
67
            $profileCertainResults = $request->getResults();
68
            return $profileCertainResults->profiles;
69
        }
70
        return null;
71
    }
72
73
}