Completed
Pull Request — master (#83)
by
unknown
07:15
created

ContactsAPI   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 56.59 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.02%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 4
dl 73
loc 129
ccs 44
cts 53
cp 0.8302
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A pickContact() 13 13 4
A getFolderId() 0 8 2
A setFolderId() 0 4 1
A getContacts() 22 22 2
A getContact() 0 4 1
A createContacts() 18 18 2
A updateContactItem() 20 20 2

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
namespace garethp\ews;
4
5
use garethp\ews\API\Type;
6
7
class ContactsAPI extends API
8
{
9
    /**
10
     * @var Type\FolderIdType
11
     */
12
    protected $folderId;
13
14
    /**
15
     * Pick a Contacts based on it's folder name
16
     *
17
     * @param string|null $displayName
18
     * @return $this
19
     */
20 View Code Duplication
    public function pickContact($displayName = null)
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...
21
    {
22
        if ($displayName == 'default.contacts' || $displayName == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $displayName of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
23
            $folder = $this->getFolderByDistinguishedId('contacts');
24
        } else {
25
            $folder = $this->getFolderByDisplayName($displayName, 'contacts');
26
        }
27
        if (!$folder) {
28
            return false;   
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by garethp\ews\ContactsAPI::pickContact of type garethp\ews\ContactsAPI.

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...
29
        }
30
        $this->folderId = $folder->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<garethp\ews\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
31
        return $this;
32
    }
33
    
34
    /**
35
     * @return Type\FolderIdType
36
     */
37 5
    public function getFolderId()
38
    {
39 5
        if (!$this->folderId) {
40 5
            $this->folderId = $this->getFolderByDistinguishedId('contacts')->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<garethp\ews\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41 5
        }
42
43 5
        return $this->folderId;
44
    }
45
46
    /**
47
     * @param Type\FolderIdType $folderId
48
     */
49 5
    public function setFolderId($folderId)
50
    {
51 5
        $this->folderId = $folderId;
52 5
    }
53
54
    /**
55
     * @param Type\FolderIdType $folderId
56
     * @param array $options
57
     * @return Type\ContactItemType[]
58
     */
59 2 View Code Duplication
    public function getContacts($folderId = null, $options = array())
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...
60
    {
61 2
        if (!$folderId) {
62 2
            $folderId = $this->getFolderId();
63 2
        }
64
65
        $request = array(
66 2
            'Traversal' => 'Shallow',
67
            'ItemShape' => array(
68
                'BaseShape' => 'AllProperties'
69 2
            ),
70
            'ParentFolderIds' => array(
71 2
                'FolderId' => $folderId->toXmlObject()
72 2
            )
73 2
        );
74
75 2
        $request = array_replace_recursive($request, $options);
76
77 2
        $request = Type::buildFromArray($request);
78
79 2
        return $this->getClient()->FindItem($request);
80
    }
81
82
    /**
83
     * @param Type\ItemIdType $itemId
84
     * @return Type\ContactItemType
85
     */
86 3
    public function getContact($itemId)
87
    {
88 3
        return $this->getItem($itemId);
89
    }
90
91
    /**
92
     * @param $contacts
93
     * @param array $options
94
     * @return Type\ItemIdType[]
95
     */
96 5 View Code Duplication
    public function createContacts($contacts, $options = array())
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...
97
    {
98 5
        $request = array('Contact' => $contacts);
99
100
        $defaultOptions = array(
101 5
            'MessageDisposition' => 'SaveOnly',
102 5
            'SavedItemFolderId' => array('FolderId' => $this->getFolderId()->toArray())
103 5
        );
104 5
        $options = array_replace_recursive($defaultOptions, $options);
105
106 5
        $result = $this->createItems($request, $options);
107
108 5
        if (!is_array($result)) {
109 4
            $result = array($result);
110 4
        }
111
112 5
        return $result;
113
    }
114
115 2 View Code Duplication
    public function updateContactItem(Type\ItemIdType $itemId, $changes)
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...
116
    {
117
        //Create the request
118
        $request = array(
119
            'ItemChange' => array(
120 2
                'ItemId' => $itemId->toArray(),
121 2
                'Updates' => API\ItemUpdateBuilder::buildUpdateItemChanges('Contact', 'contacts', $changes)
122 2
            )
123 2
        );
124
125 2
        $options = array();
126
127 2
        $items = $this->updateItems($request, $options);
128
129 2
        if (!is_array($items)) {
130 2
            $items = array($items);
131 2
        }
132
133 2
        return $items;
134
    }
135
}
136