BlogTopics   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 8.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 9 1
A search() 0 10 1
A getById() 0 6 1
A create() 10 10 1
A update() 0 8 1
A delete() 0 6 1
A merge() 0 11 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
namespace SevenShores\Hubspot\Resources;
4
5
class BlogTopics extends Resource
6
{
7
8
    /**
9
     * Get all the blog topcis
10
     *
11
     * @param  array $params Optional parameters ['name','slug','limit','offset']
12
     * @return \SevenShores\Hubspot\Http\Response
13
     */
14
    function all($params = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16
17
        $endpoint = 'https://api.hubapi.com/blogs/v3/topics';
18
19
        $queryString = build_query_string($params);
20
21
        return $this->client->request('get', $endpoint, [], $queryString);
22
    }
23
24
    /**
25
     * Search a topic by the query. $query will match name and slug partially
26
     *
27
     * @see http://developers.hubspot.com/docs/methods/blog/v3/search-blog-topics
28
     *
29
     * @param string $query  Search query
30
     * @param array $params Array of optional parameters ['name','slug','limit', 'offset', 'active', 'blog']
31
     * @return \SevenShores\Hubspot\Http\Response
32
     */
33
    function search($query, $params = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35
        $endpoint = 'https://api.hubapi.com/blogs/v3/topics/search';
36
37
        $params['q'] = $query;
38
39
        $queryString = build_query_string($params);
40
41
        return $this->client->request('get', $endpoint, [], $queryString);
42
    }
43
44
    /**
45
     * @param int $id
46
     * @return \SevenShores\Hubspot\Http\Response
47
     */
48
    function getById($id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
    {
50
        $endpoint = "https://api.hubapi.com/blogs/v3/topics/{$id}";
51
52
        return $this->client->request('get', $endpoint);
53
    }
54
55
    /**
56
     * Create a new blog topic.
57
     *
58
     * @param string $name Name of the topic
59
     * @param  array $params Optional Parameters.
60
     * @return \SevenShores\Hubspot\Http\Response
61
     */
62 View Code Duplication
    function create($name, $params = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
63
    {
64
        $endpoint = 'https://api.hubapi.com/blogs/v3/topics';
65
66
        $params['name'] = $name;
67
68
        $options['json'] = $params;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
69
70
        return $this->client->request('post', $endpoint, $options);
71
    }
72
73
    /**
74
     * Update a blog topic.
75
     *
76
     * @param  int   $id     The blog topic id.
77
     * @param  array $params The blog topic fields to update.
78
     * @return \SevenShores\Hubspot\Http\Response
79
     */
80
    function update($id, $params = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
81
    {
82
        $endpoint = "https://api.hubapi.com/blogs/v3/topics/{$id}";
83
84
        $options['json'] = $params;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
85
86
        return $this->client->request('put', $endpoint, $options);
87
    }
88
89
    /**
90
     * Delete a blog topic.
91
     *
92
     * @param  int $id
93
     * @return \SevenShores\Hubspot\Http\Response
94
     */
95
    function delete($id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
96
    {
97
        $endpoint = "https://api.hubapi.com/blogs/v3/topics/{$id}";
98
99
        return $this->client->request('delete', $endpoint);
100
    }
101
102
    /**
103
     * Group blog topics
104
     *
105
     * @param array $topicIds Array of topic ids
106
     * @param string $groupedTopicName New name of the group
107
     * @return \SevenShores\Hubspot\Http\Response
108
     */
109
    function merge($topicIds, $groupedTopicName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
    {
111
        $endpoint = "https://api.hubapi.com/blogs/v3/topics/group-topics";
112
113
        $options['json'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
114
            'topicIds' => $topicIds,
115
            'groupedTopicName' => $groupedTopicName
116
        ];
117
118
        return $this->client->request('post', $endpoint, $options);
119
    }
120
}
121